Skip to content

Commit

Permalink
move the warning for ignored manually defined restoration hash to cla…
Browse files Browse the repository at this point in the history
…im controller
  • Loading branch information
bruelea committed Sep 6, 2024
1 parent b54683c commit 8fa4571
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 21 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ docker-build: ## Build docker image with the manager.
$(CONTAINER_TOOL) build -t ${IMG} .

.PHONY: docker-build-local
docker-build-local: SSH_AGENT_PUBLIC_KEY=
docker-build-local: ## Build docker image with the manager.
DOCKER_BUILDKIT=1 $(CONTAINER_TOOL) build -t ${LOCAL_IMG} -f Dockerfile .

Expand Down
5 changes: 0 additions & 5 deletions internal/controller/ipaddress_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,6 @@ func (r *IpAddressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
}

// 2. reserve or update ip address in netbox
_, ok := o.Spec.CustomFields[config.GetOperatorConfig().NetboxRestorationHashFieldName]
if ok {
logger.Info(fmt.Sprintf("Warning: restoration hash is calculated from spec, custom field with key %s will be ignored", config.GetOperatorConfig().NetboxRestorationHashFieldName))
}

accessor := apismeta.NewAccessor()
annotations, err := accessor.Annotations(o)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/ipaddressclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (r *IpAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque
}

// 6.a create the IPAddress object
ipAddressResource := generateIpAddressFromIpAddressClaim(o, ipAddressModel.IpAddress)
ipAddressResource := generateIpAddressFromIpAddressClaim(o, ipAddressModel.IpAddress, logger)
err = controllerutil.SetControllerReference(o, ipAddressResource, r.Scheme)
if err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -175,7 +175,7 @@ func (r *IpAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque
return ctrl.Result{}, err
}

updatedIpAddressSpec := generateIpAddressSpec(o, ipAddress.Spec.IpAddress)
updatedIpAddressSpec := generateIpAddressSpec(o, ipAddress.Spec.IpAddress, logger)
_, err = ctrl.CreateOrUpdate(ctx, r.Client, ipAddress, func() error {
// only add the mutable fields here
ipAddress.Spec.CustomFields = updatedIpAddressSpec.CustomFields
Expand Down
15 changes: 12 additions & 3 deletions internal/controller/ipaddressclaim_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,38 @@ import (
"crypto/sha1"
"fmt"

"github.com/go-logr/logr"
netboxv1 "github.com/netbox-community/netbox-operator/api/v1"
"github.com/netbox-community/netbox-operator/pkg/config"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func generateIpAddressFromIpAddressClaim(claim *netboxv1.IpAddressClaim, ip string) *netboxv1.IpAddress {
func generateIpAddressFromIpAddressClaim(claim *netboxv1.IpAddressClaim, ip string, logger logr.Logger) *netboxv1.IpAddress {
ipAddressResource := &netboxv1.IpAddress{
ObjectMeta: metav1.ObjectMeta{
Name: claim.Name,
Namespace: claim.ObjectMeta.Namespace,
},
Spec: generateIpAddressSpec(claim, ip),
Spec: generateIpAddressSpec(claim, ip, logger),
}
return ipAddressResource
}

func generateIpAddressSpec(claim *netboxv1.IpAddressClaim, ip string) netboxv1.IpAddressSpec {
func generateIpAddressSpec(claim *netboxv1.IpAddressClaim, ip string, logger logr.Logger) netboxv1.IpAddressSpec {
// log a warning if the netboxOperatorRestorationHash name is a key in the customFields map of the IpAddressClaim
_, ok := claim.Spec.CustomFields[config.GetOperatorConfig().NetboxRestorationHashFieldName]
if ok {
logger.Info(fmt.Sprintf("Warning: restoration hash is calculated from spec, custom field with key %s will be ignored", config.GetOperatorConfig().NetboxRestorationHashFieldName))
}

// Copy customFields from claim and add restoration hash
customFields := make(map[string]string, len(claim.Spec.CustomFields)+1)
for k, v := range claim.Spec.CustomFields {
customFields[k] = v
}

customFields[config.GetOperatorConfig().NetboxRestorationHashFieldName] = generateIpAddressRestorationHash(claim)

return netboxv1.IpAddressSpec{
IpAddress: ip,
Tenant: claim.Spec.Tenant,
Expand Down
5 changes: 0 additions & 5 deletions internal/controller/prefix_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,6 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
}

/* 2. reserve or update Prefix in netbox */
_, ok := prefix.Spec.CustomFields[config.GetOperatorConfig().NetboxRestorationHashFieldName]
if ok {
logger.Info(fmt.Sprintf("Warning: restoration hash is calculated from spec, custom field with key %s will be ignored", config.GetOperatorConfig().NetboxRestorationHashFieldName))
}

accessor := apismeta.NewAccessor()
annotations, err := accessor.Annotations(prefix)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/prefixclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (r *PrefixClaimReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}

/* 6-1, create the Prefix object */
prefixResource := generatePrefixFromPrefixClaim(prefixClaim, prefixModel.Prefix)
prefixResource := generatePrefixFromPrefixClaim(prefixClaim, prefixModel.Prefix, logger)
err = controllerutil.SetControllerReference(prefixClaim, prefixResource, r.Scheme)
if err != nil {
return ctrl.Result{}, err
Expand All @@ -169,7 +169,7 @@ func (r *PrefixClaimReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, err
}

updatedPrefixSpec := generatePrefixSpec(prefixClaim, prefix.Spec.Prefix)
updatedPrefixSpec := generatePrefixSpec(prefixClaim, prefix.Spec.Prefix, logger)
if _, err = ctrl.CreateOrUpdate(ctx, r.Client, prefix, func() error {
// only add the mutable fields here
prefix.Spec.Site = updatedPrefixSpec.Site
Expand Down
15 changes: 12 additions & 3 deletions internal/controller/prefixclaim_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,37 @@ import (
"crypto/sha1"
"fmt"

"github.com/go-logr/logr"
netboxv1 "github.com/netbox-community/netbox-operator/api/v1"
"github.com/netbox-community/netbox-operator/pkg/config"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func generatePrefixFromPrefixClaim(claim *netboxv1.PrefixClaim, prefix string) *netboxv1.Prefix {
func generatePrefixFromPrefixClaim(claim *netboxv1.PrefixClaim, prefix string, logger logr.Logger) *netboxv1.Prefix {
return &netboxv1.Prefix{
ObjectMeta: metav1.ObjectMeta{
Name: claim.Name,
Namespace: claim.ObjectMeta.Namespace,
},
Spec: generatePrefixSpec(claim, prefix),
Spec: generatePrefixSpec(claim, prefix, logger),
}
}

func generatePrefixSpec(claim *netboxv1.PrefixClaim, prefix string) netboxv1.PrefixSpec {
func generatePrefixSpec(claim *netboxv1.PrefixClaim, prefix string, logger logr.Logger) netboxv1.PrefixSpec {
// log a warning if the netboxOperatorRestorationHash name is a key in the customFields map of the IpAddressClaim
_, ok := claim.Spec.CustomFields[config.GetOperatorConfig().NetboxRestorationHashFieldName]
if ok {
logger.Info(fmt.Sprintf("Warning: restoration hash is calculated from spec, custom field with key %s will be ignored", config.GetOperatorConfig().NetboxRestorationHashFieldName))
}

// Copy customFields from claim and add restoration hash
customFields := make(map[string]string, len(claim.Spec.CustomFields)+1)
for k, v := range claim.Spec.CustomFields {
customFields[k] = v
}

customFields[config.GetOperatorConfig().NetboxRestorationHashFieldName] = generatePrefixRestorationHash(claim)

return netboxv1.PrefixSpec{
Prefix: prefix,
Tenant: claim.Spec.Tenant,
Expand Down

0 comments on commit 8fa4571

Please sign in to comment.