Skip to content

Commit b6cc9db

Browse files
Hoanganh.Maihenrybear327
authored andcommitted
update deletion for finalizers and move finalizer creation up
1 parent c48bd3c commit b6cc9db

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

internal/controller/ipaddress_controller.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/netbox-community/netbox-operator/pkg/config"
3131
"github.com/netbox-community/netbox-operator/pkg/netbox/api"
3232
"github.com/netbox-community/netbox-operator/pkg/netbox/models"
33+
"github.com/netbox-community/netbox-operator/pkg/netbox/utils"
3334
"github.com/swisscom/leaselocker"
3435
corev1 "k8s.io/api/core/v1"
3536
apismeta "k8s.io/apimachinery/pkg/api/meta"
@@ -78,9 +79,17 @@ func (r *IpAddressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
7879
// if being deleted
7980
if !o.ObjectMeta.DeletionTimestamp.IsZero() {
8081
if controllerutil.ContainsFinalizer(o, IpAddressFinalizerName) {
81-
if o.Status.IpAddressId != 0 {
82+
if !o.Spec.PreserveInNetbox {
8283
err := r.NetboxClient.DeleteIpAddress(o.Status.IpAddressId)
8384
if err != nil {
85+
if errors.Is(err, utils.ErrNotFound) {
86+
setConditionErr := r.SetConditionAndCreateEvent(ctx, o, netboxv1.ConditionIpaddressReadyFalse, corev1.EventTypeWarning, err.Error())
87+
if setConditionErr != nil {
88+
return ctrl.Result{}, fmt.Errorf("error updating status: %w, when deleting IPAddress failed: %w", setConditionErr, err)
89+
}
90+
91+
return ctrl.Result{Requeue: true}, nil
92+
}
8493
return ctrl.Result{}, err
8594
}
8695
}

internal/controller/prefix_controller.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828

2929
"github.com/netbox-community/netbox-operator/pkg/config"
3030
"github.com/netbox-community/netbox-operator/pkg/netbox/models"
31+
"github.com/netbox-community/netbox-operator/pkg/netbox/utils"
3132
corev1 "k8s.io/api/core/v1"
3233
apismeta "k8s.io/apimachinery/pkg/api/meta"
3334
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -79,8 +80,16 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
7980
// if being deleted
8081
if !prefix.ObjectMeta.DeletionTimestamp.IsZero() {
8182
if controllerutil.ContainsFinalizer(prefix, PrefixFinalizerName) {
82-
if prefix.Status.PrefixId != 0 {
83+
if !prefix.Spec.PreserveInNetbox {
8384
if err := r.NetboxClient.DeletePrefix(prefix.Status.PrefixId); err != nil {
85+
if errors.Is(err, utils.ErrNotFound) {
86+
setConditionErr := r.SetConditionAndCreateEvent(ctx, prefix, netboxv1.ConditionPrefixReadyFalse, corev1.EventTypeWarning, err.Error())
87+
if setConditionErr != nil {
88+
return ctrl.Result{}, fmt.Errorf("error updating status: %w, when deleting Prefix failed: %w", setConditionErr, err)
89+
}
90+
91+
return ctrl.Result{Requeue: true}, nil
92+
}
8493
return ctrl.Result{}, err
8594
}
8695
}

pkg/netbox/api/ip_address.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package api
1818

1919
import (
20+
"strings"
2021
"unicode/utf8"
2122

2223
"github.com/netbox-community/go-netbox/v3/netbox/client/ipam"
@@ -97,9 +98,16 @@ func (r *NetboxClient) UpdateIpAddress(ipAddressId int64, ipAddress *netboxModel
9798
}
9899

99100
func (r *NetboxClient) DeleteIpAddress(ipAddressId int64) error {
101+
// assuming id starts from 1 when IPAdress is created so 0 means that IPAddress doesn't exist
102+
if ipAddressId == 0 {
103+
return nil
104+
}
100105
requestDeleteIp := ipam.NewIpamIPAddressesDeleteParams().WithID(ipAddressId)
101106
_, err := r.Ipam.IpamIPAddressesDelete(requestDeleteIp, nil)
102107
if err != nil {
108+
if strings.Contains(err.Error(), "No IPAddress matches the given query.") {
109+
return utils.NetboxNotFoundError("IPAddress")
110+
}
103111
return utils.NetboxError("Failed to delete IP Address from Netbox", err)
104112
}
105113
return nil

pkg/netbox/api/prefix.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package api
1818

1919
import (
20+
"strings"
21+
2022
"github.com/netbox-community/go-netbox/v3/netbox/client/ipam"
2123
netboxModels "github.com/netbox-community/go-netbox/v3/netbox/models"
2224

@@ -98,9 +100,17 @@ func (r *NetboxClient) UpdatePrefix(prefixId int64, prefix *netboxModels.Writabl
98100
}
99101

100102
func (r *NetboxClient) DeletePrefix(prefixId int64) error {
103+
// assuming id starts from 1 when Prefix is created so 0 means that Prefix doesn't exist
104+
if prefixId == 0 {
105+
return nil
106+
}
107+
101108
requestDeletePrefix := ipam.NewIpamPrefixesDeleteParams().WithID(prefixId)
102109
_, err := r.Ipam.IpamPrefixesDelete(requestDeletePrefix, nil)
103110
if err != nil {
111+
if strings.Contains(err.Error(), "No Prefix matches the given query.") {
112+
return utils.NetboxNotFoundError("Prefix")
113+
}
104114
return utils.NetboxError("failed to delete Prefix", err)
105115
}
106116
return nil

0 commit comments

Comments
 (0)