Skip to content

Commit

Permalink
Migrate restorationHash to customFields in IpAddress and Prefix API
Browse files Browse the repository at this point in the history
  • Loading branch information
jstudler authored and henrybear327 committed Aug 30, 2024
1 parent c854f9f commit 7da5efc
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 42 deletions.
3 changes: 0 additions & 3 deletions api/v1/ipaddress_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ type IpAddressSpec struct {
Description string `json:"description,omitempty"`

PreserveInNetbox bool `json:"preserveInNetbox,omitempty"`

//+kubebuilder:validation:XValidation:rule="self == oldSelf",message="Field 'restorationHash' is immutable"
RestorationHash string `json:"restorationHash,omitempty"`
}

// IpAddressStatus defines the observed state of IpAddress
Expand Down
3 changes: 0 additions & 3 deletions api/v1/prefix_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ type PrefixSpec struct {
Comments string `json:"comments,omitempty"`

PreserveInNetbox bool `json:"preserveInNetbox,omitempty"`

//+kubebuilder:validation:XValidation:rule="self == oldSelf",message="Field 'restorationHash' is immutable"
RestorationHash string `json:"restorationHash,omitempty"`
}

// PrefixStatus defines the observed state of Prefix
Expand Down
5 changes: 0 additions & 5 deletions config/crd/bases/netbox.dev_ipaddresses.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ spec:
rule: self == oldSelf
preserveInNetbox:
type: boolean
restorationHash:
type: string
x-kubernetes-validations:
- message: Field 'restorationHash' is immutable
rule: self == oldSelf
tenant:
type: string
x-kubernetes-validations:
Expand Down
5 changes: 0 additions & 5 deletions config/crd/bases/netbox.dev_prefixes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ spec:
rule: self == oldSelf
preserveInNetbox:
type: boolean
restorationHash:
type: string
x-kubernetes-validations:
- message: Field 'restorationHash' is immutable
rule: self == oldSelf
site:
type: string
tenant:
Expand Down
6 changes: 2 additions & 4 deletions internal/controller/ipaddress_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (r *IpAddressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
annotations = make(map[string]string)
}

if o.Spec.CustomFields != nil && len(o.Spec.CustomFields) > 0 {
if len(o.Spec.CustomFields) > 0 {
lastIpAddressMetadata, err := json.Marshal(o.Spec.CustomFields)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to marshal lastIpAddressMetadata annotation: %w", err)
Expand Down Expand Up @@ -259,7 +259,7 @@ func generateNetboxIpAddressModelFromIpAddressSpec(spec *netboxv1.IpAddressSpec,
}

netboxCustomFields := make(map[string]string)
if spec.CustomFields != nil && len(spec.CustomFields) > 0 {
if len(spec.CustomFields) > 0 {
netboxCustomFields = maps.Clone(spec.CustomFields)
}

Expand All @@ -272,8 +272,6 @@ func generateNetboxIpAddressModelFromIpAddressSpec(spec *netboxv1.IpAddressSpec,
}
}

netboxCustomFields[config.GetOperatorConfig().NetboxRestorationHashFieldName] = spec.RestorationHash

return &models.IPAddress{
IpAddress: spec.IpAddress,
Metadata: &models.NetboxMetadata{
Expand Down
11 changes: 7 additions & 4 deletions internal/controller/ipaddressclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,14 @@ func (r *IpAddressClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reque
if err != nil {
return ctrl.Result{}, err
}

updatedIpAddressSpec := generateIpAddressSpec(o, ipAddress.Spec.IpAddress)
_, err = ctrl.CreateOrUpdate(ctx, r.Client, ipAddress, func() error {
ipAddress.Spec.CustomFields = o.Spec.CustomFields
ipAddress.Spec.Comments = o.Spec.Comments
ipAddress.Spec.Description = o.Spec.Description
ipAddress.Spec.PreserveInNetbox = o.Spec.PreserveInNetbox
// only add the mutable fields here
ipAddress.Spec.CustomFields = updatedIpAddressSpec.CustomFields
ipAddress.Spec.Comments = updatedIpAddressSpec.Comments
ipAddress.Spec.Description = updatedIpAddressSpec.Description
ipAddress.Spec.PreserveInNetbox = updatedIpAddressSpec.PreserveInNetbox
return nil
})
if err != nil {
Expand Down
10 changes: 8 additions & 2 deletions internal/controller/ipaddressclaim_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"

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"
)

Expand All @@ -36,11 +37,16 @@ func generateIpAddressFromIpAddressClaim(claim *netboxv1.IpAddressClaim, ip stri
}

func generateIpAddressSpec(claim *netboxv1.IpAddressClaim, ip string) netboxv1.IpAddressSpec {
// 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,
CustomFields: claim.Spec.CustomFields,
RestorationHash: generateIpAddressRestorationHash(claim),
CustomFields: customFields,
Description: claim.Spec.Description,
Comments: claim.Spec.Comments,
PreserveInNetbox: claim.Spec.PreserveInNetbox,
Expand Down
6 changes: 1 addition & 5 deletions internal/controller/netbox_testdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ var tenantId = int64(1)
var tenant = "test-tenant"
var tenantSlug = "test-tenant-slug"

var restorationHash = "6f6c67651f0b43b2969ba2ae35c74fc91815513b"

var netboxLabel = "Status"
var value = "active"

Expand All @@ -71,7 +69,6 @@ func defaultIpAddressCR(preserveInNetbox bool) *netboxv1.IpAddress {
IpAddress: ipAddress,
Tenant: tenant,
CustomFields: customFields,
RestorationHash: "",
Comments: comments,
Description: description,
PreserveInNetbox: preserveInNetbox,
Expand All @@ -89,7 +86,6 @@ func defaultIpAddressCreatedByClaim(preserveInNetbox bool) *netboxv1.IpAddress {
IpAddress: ipAddress,
Tenant: tenant,
CustomFields: customFields,
RestorationHash: restorationHash,
Comments: comments,
Description: description,
PreserveInNetbox: preserveInNetbox,
Expand Down Expand Up @@ -224,7 +220,7 @@ var expectedIpToUpdate = &netboxModels.WritableIPAddress{
Comments: comments + warningComment,
CustomFields: map[string]string{
"example_field": "example value",
config.GetOperatorConfig().NetboxRestorationHashFieldName: "",
config.GetOperatorConfig().NetboxRestorationHashFieldName: "a0601ac7e6d196a82c0e61f9be17313113c3043f",
},
Description: nsn + description + warningComment,
Status: "active",
Expand Down
6 changes: 2 additions & 4 deletions internal/controller/prefix_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (r *PrefixReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
annotations = make(map[string]string)
}

if prefix.Spec.CustomFields != nil && len(prefix.Spec.CustomFields) > 0 {
if len(prefix.Spec.CustomFields) > 0 {
lastPrefixMetadata, err := json.Marshal(prefix.Spec.CustomFields)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to marshal lastPrefixMetadata annotation: %w", err)
Expand Down Expand Up @@ -254,7 +254,7 @@ func generateNetboxPrefixModelFromPrefixSpec(spec *netboxv1.PrefixSpec, req ctrl
}

netboxCustomFields := make(map[string]string)
if spec.CustomFields != nil && len(spec.CustomFields) > 0 {
if len(spec.CustomFields) > 0 {
netboxCustomFields = maps.Clone(spec.CustomFields)
}

Expand All @@ -266,8 +266,6 @@ func generateNetboxPrefixModelFromPrefixSpec(spec *netboxv1.PrefixSpec, req ctrl
}
}

netboxCustomFields[config.GetOperatorConfig().NetboxRestorationHashFieldName] = spec.RestorationHash

return &models.Prefix{
Prefix: spec.Prefix,
Metadata: &models.NetboxMetadata{
Expand Down
11 changes: 6 additions & 5 deletions internal/controller/prefixclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,14 @@ func (r *PrefixClaimReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, err
}

updatedPrefixSpec := generatePrefixSpec(prefixClaim, prefix.Spec.Prefix)
if _, err = ctrl.CreateOrUpdate(ctx, r.Client, prefix, func() error {
// only add the mutable fields here
prefix.Spec.Site = prefixClaim.Spec.Site
prefix.Spec.CustomFields = prefixClaim.Spec.CustomFields
prefix.Spec.Description = prefixClaim.Spec.Description
prefix.Spec.Comments = prefixClaim.Spec.Comments
prefix.Spec.PreserveInNetbox = prefixClaim.Spec.PreserveInNetbox
prefix.Spec.Site = updatedPrefixSpec.Site
prefix.Spec.CustomFields = updatedPrefixSpec.CustomFields
prefix.Spec.Description = updatedPrefixSpec.Description
prefix.Spec.Comments = updatedPrefixSpec.Comments
prefix.Spec.PreserveInNetbox = updatedPrefixSpec.PreserveInNetbox
return nil
}); err != nil {
return ctrl.Result{}, err
Expand Down
10 changes: 8 additions & 2 deletions internal/controller/prefixclaim_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"

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"
)

Expand All @@ -35,11 +36,16 @@ func generatePrefixFromPrefixClaim(claim *netboxv1.PrefixClaim, prefix string) *
}

func generatePrefixSpec(claim *netboxv1.PrefixClaim, prefix string) netboxv1.PrefixSpec {
// 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,
CustomFields: claim.Spec.CustomFields,
RestorationHash: generatePrefixRestorationHash(claim),
CustomFields: customFields,
Description: claim.Spec.Description,
Comments: claim.Spec.Comments,
PreserveInNetbox: claim.Spec.PreserveInNetbox,
Expand Down

0 comments on commit 7da5efc

Please sign in to comment.