From 04ba42c0f7792c02386c9f6f23839669062b87e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Corentin=20N=C3=A9au?= Date: Tue, 23 Jul 2024 11:14:02 +0200 Subject: [PATCH] Use maps for namespace labels and annotations Pointers to maps are superfluous, as iterating over an empty map will be a no-op, and we do not need to otherwise differentiate an empty map from an unset one. This still leaves original namespace labels and annotations as pointers to maps in bundle deployment options, for backward compatibility. --- internal/cmd/controller/target/target.go | 16 ++++++--------- .../fleet.cattle.io/v1alpha1/bundle_types.go | 4 ++-- .../v1alpha1/zz_generated.deepcopy.go | 20 ++++++------------- 3 files changed, 14 insertions(+), 26 deletions(-) diff --git a/internal/cmd/controller/target/target.go b/internal/cmd/controller/target/target.go index 510f23c938..d39e5227dd 100644 --- a/internal/cmd/controller/target/target.go +++ b/internal/cmd/controller/target/target.go @@ -60,18 +60,14 @@ func (t *Target) BundleDeployment() *fleet.BundleDeployment { bd.Spec.Paused = t.IsPaused() for _, bundleTarget := range t.Bundle.Spec.Targets { - if bundleTarget.NamespaceLabels != nil { - for key, value := range *bundleTarget.NamespaceLabels { - (*bd.Spec.Options.NamespaceLabels)[key] = value - (*bd.Spec.StagedOptions.NamespaceLabels)[key] = value - } + for key, value := range bundleTarget.NamespaceLabels { + (*bd.Spec.Options.NamespaceLabels)[key] = value + (*bd.Spec.StagedOptions.NamespaceLabels)[key] = value } - if bundleTarget.NamespaceAnnotations != nil { - for key, value := range *bundleTarget.NamespaceAnnotations { - (*bd.Spec.Options.NamespaceAnnotations)[key] = value - (*bd.Spec.StagedOptions.NamespaceAnnotations)[key] = value - } + for key, value := range bundleTarget.NamespaceAnnotations { + (*bd.Spec.Options.NamespaceAnnotations)[key] = value + (*bd.Spec.StagedOptions.NamespaceAnnotations)[key] = value } } diff --git a/pkg/apis/fleet.cattle.io/v1alpha1/bundle_types.go b/pkg/apis/fleet.cattle.io/v1alpha1/bundle_types.go index a3431fbd37..e010f29373 100644 --- a/pkg/apis/fleet.cattle.io/v1alpha1/bundle_types.go +++ b/pkg/apis/fleet.cattle.io/v1alpha1/bundle_types.go @@ -229,10 +229,10 @@ type BundleTarget struct { DoNotDeploy bool `json:"doNotDeploy,omitempty"` // NamespaceLabels are labels that will be appended to the namespace created by Fleet. // +nullable - NamespaceLabels *map[string]string `json:"namespaceLabels,omitempty"` + NamespaceLabels map[string]string `json:"namespaceLabels,omitempty"` // NamespaceAnnotations are annotations that will be appended to the namespace created by Fleet. // +nullable - NamespaceAnnotations *map[string]string `json:"namespaceAnnotations,omitempty"` + NamespaceAnnotations map[string]string `json:"namespaceAnnotations,omitempty"` } // BundleSummary contains the number of bundle deployments in each state and a diff --git a/pkg/apis/fleet.cattle.io/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/fleet.cattle.io/v1alpha1/zz_generated.deepcopy.go index e64aa50838..c21f9c91a0 100644 --- a/pkg/apis/fleet.cattle.io/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/fleet.cattle.io/v1alpha1/zz_generated.deepcopy.go @@ -581,24 +581,16 @@ func (in *BundleTarget) DeepCopyInto(out *BundleTarget) { } if in.NamespaceLabels != nil { in, out := &in.NamespaceLabels, &out.NamespaceLabels - *out = new(map[string]string) - if **in != nil { - in, out := *in, *out - *out = make(map[string]string, len(*in)) - for key, val := range *in { - (*out)[key] = val - } + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val } } if in.NamespaceAnnotations != nil { in, out := &in.NamespaceAnnotations, &out.NamespaceAnnotations - *out = new(map[string]string) - if **in != nil { - in, out := *in, *out - *out = make(map[string]string, len(*in)) - for key, val := range *in { - (*out)[key] = val - } + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val } } }