Skip to content

Commit

Permalink
update deletion for finalizers and move finalizer creation up
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoanganh.Mai authored and henrybear327 committed Sep 30, 2024
1 parent c48bd3c commit b6cc9db
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
11 changes: 10 additions & 1 deletion internal/controller/ipaddress_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/netbox-community/netbox-operator/pkg/config"
"github.com/netbox-community/netbox-operator/pkg/netbox/api"
"github.com/netbox-community/netbox-operator/pkg/netbox/models"
"github.com/netbox-community/netbox-operator/pkg/netbox/utils"
"github.com/swisscom/leaselocker"
corev1 "k8s.io/api/core/v1"
apismeta "k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -78,9 +79,17 @@ func (r *IpAddressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
// if being deleted
if !o.ObjectMeta.DeletionTimestamp.IsZero() {
if controllerutil.ContainsFinalizer(o, IpAddressFinalizerName) {
if o.Status.IpAddressId != 0 {
if !o.Spec.PreserveInNetbox {
err := r.NetboxClient.DeleteIpAddress(o.Status.IpAddressId)
if err != nil {
if errors.Is(err, utils.ErrNotFound) {
setConditionErr := r.SetConditionAndCreateEvent(ctx, o, netboxv1.ConditionIpaddressReadyFalse, corev1.EventTypeWarning, err.Error())
if setConditionErr != nil {
return ctrl.Result{}, fmt.Errorf("error updating status: %w, when deleting IPAddress failed: %w", setConditionErr, err)
}

return ctrl.Result{Requeue: true}, nil
}
return ctrl.Result{}, err
}
}
Expand Down
11 changes: 10 additions & 1 deletion internal/controller/prefix_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"github.com/netbox-community/netbox-operator/pkg/config"
"github.com/netbox-community/netbox-operator/pkg/netbox/models"
"github.com/netbox-community/netbox-operator/pkg/netbox/utils"
corev1 "k8s.io/api/core/v1"
apismeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -79,8 +80,16 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
// if being deleted
if !prefix.ObjectMeta.DeletionTimestamp.IsZero() {
if controllerutil.ContainsFinalizer(prefix, PrefixFinalizerName) {
if prefix.Status.PrefixId != 0 {
if !prefix.Spec.PreserveInNetbox {
if err := r.NetboxClient.DeletePrefix(prefix.Status.PrefixId); err != nil {
if errors.Is(err, utils.ErrNotFound) {
setConditionErr := r.SetConditionAndCreateEvent(ctx, prefix, netboxv1.ConditionPrefixReadyFalse, corev1.EventTypeWarning, err.Error())
if setConditionErr != nil {
return ctrl.Result{}, fmt.Errorf("error updating status: %w, when deleting Prefix failed: %w", setConditionErr, err)
}

return ctrl.Result{Requeue: true}, nil
}
return ctrl.Result{}, err
}
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/netbox/api/ip_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package api

import (
"strings"
"unicode/utf8"

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

func (r *NetboxClient) DeleteIpAddress(ipAddressId int64) error {
// assuming id starts from 1 when IPAdress is created so 0 means that IPAddress doesn't exist
if ipAddressId == 0 {
return nil
}
requestDeleteIp := ipam.NewIpamIPAddressesDeleteParams().WithID(ipAddressId)
_, err := r.Ipam.IpamIPAddressesDelete(requestDeleteIp, nil)
if err != nil {
if strings.Contains(err.Error(), "No IPAddress matches the given query.") {
return utils.NetboxNotFoundError("IPAddress")
}
return utils.NetboxError("Failed to delete IP Address from Netbox", err)
}
return nil
Expand Down
10 changes: 10 additions & 0 deletions pkg/netbox/api/prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package api

import (
"strings"

"github.com/netbox-community/go-netbox/v3/netbox/client/ipam"
netboxModels "github.com/netbox-community/go-netbox/v3/netbox/models"

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

func (r *NetboxClient) DeletePrefix(prefixId int64) error {
// assuming id starts from 1 when Prefix is created so 0 means that Prefix doesn't exist
if prefixId == 0 {
return nil
}

requestDeletePrefix := ipam.NewIpamPrefixesDeleteParams().WithID(prefixId)
_, err := r.Ipam.IpamPrefixesDelete(requestDeletePrefix, nil)
if err != nil {
if strings.Contains(err.Error(), "No Prefix matches the given query.") {
return utils.NetboxNotFoundError("Prefix")
}
return utils.NetboxError("failed to delete Prefix", err)
}
return nil
Expand Down

0 comments on commit b6cc9db

Please sign in to comment.