Skip to content

Commit

Permalink
Use maps instead of pointers for namespace labels and annotations
Browse files Browse the repository at this point in the history
This simplifies dealing with bundle deployment options, since an empty
map is semantically equivalent to a nil pointer.
  • Loading branch information
weyfonk authored and manno committed Aug 12, 2024
1 parent a62a0b5 commit 6ac70f9
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 30 deletions.
4 changes: 2 additions & 2 deletions internal/cmd/agent/deployer/deployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,13 @@ func (d *Deployer) setNamespaceLabelsAndAnnotations(ctx context.Context, bd *fle
}

if bd.Spec.Options.NamespaceLabels != nil {
addLabelsFromOptions(ns.Labels, *bd.Spec.Options.NamespaceLabels)
addLabelsFromOptions(ns.Labels, bd.Spec.Options.NamespaceLabels)
}
if bd.Spec.Options.NamespaceAnnotations != nil {
if ns.Annotations == nil {
ns.Annotations = map[string]string{}
}
addAnnotationsFromOptions(ns.Annotations, *bd.Spec.Options.NamespaceAnnotations)
addAnnotationsFromOptions(ns.Annotations, bd.Spec.Options.NamespaceAnnotations)
}
err = d.updateNamespace(ctx, ns)
if err != nil {
Expand Down
39 changes: 31 additions & 8 deletions internal/cmd/agent/deployer/deployer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,34 @@ func TestSetNamespaceLabelsAndAnnotations(t *testing.T) {
release string
expectedNs corev1.Namespace
}{
"Empty sets of NamespaceLabels and NamespaceAnnotations are supported": {
bd: &fleet.BundleDeployment{Spec: fleet.BundleDeploymentSpec{
Options: fleet.BundleDeploymentOptions{
NamespaceLabels: nil, // equivalent to map[string]string{}
NamespaceAnnotations: nil,
},
}},
ns: corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "namespace",
Labels: map[string]string{"kubernetes.io/metadata.name": "namespace"},
},
},
release: "namespace/foo/bar",
expectedNs: corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: "namespace",
Labels: map[string]string{"kubernetes.io/metadata.name": "namespace"},
Annotations: nil,
},
},
},

"NamespaceLabels and NamespaceAnnotations are appended": {
bd: &fleet.BundleDeployment{Spec: fleet.BundleDeploymentSpec{
Options: fleet.BundleDeploymentOptions{
NamespaceLabels: &map[string]string{"optLabel1": "optValue1", "optLabel2": "optValue2"},
NamespaceAnnotations: &map[string]string{"optAnn1": "optValue1"},
NamespaceLabels: map[string]string{"optLabel1": "optValue1", "optLabel2": "optValue2"},
NamespaceAnnotations: map[string]string{"optAnn1": "optValue1"},
},
}},
ns: corev1.Namespace{
Expand All @@ -49,8 +72,8 @@ func TestSetNamespaceLabelsAndAnnotations(t *testing.T) {
"NamespaceLabels and NamespaceAnnotations removes entries that are not in the options, except the name label": {
bd: &fleet.BundleDeployment{Spec: fleet.BundleDeploymentSpec{
Options: fleet.BundleDeploymentOptions{
NamespaceLabels: &map[string]string{"optLabel": "optValue"},
NamespaceAnnotations: &map[string]string{},
NamespaceLabels: map[string]string{"optLabel": "optValue"},
NamespaceAnnotations: map[string]string{},
},
}},
ns: corev1.Namespace{
Expand All @@ -73,8 +96,8 @@ func TestSetNamespaceLabelsAndAnnotations(t *testing.T) {
"NamespaceLabels and NamespaceAnnotations updates existing values": {
bd: &fleet.BundleDeployment{Spec: fleet.BundleDeploymentSpec{
Options: fleet.BundleDeploymentOptions{
NamespaceLabels: &map[string]string{"bdLabel": "labelUpdated"},
NamespaceAnnotations: &map[string]string{"bdAnn": "annUpdated"},
NamespaceLabels: map[string]string{"bdLabel": "labelUpdated"},
NamespaceAnnotations: map[string]string{"bdAnn": "annUpdated"},
},
}},
ns: corev1.Namespace{
Expand Down Expand Up @@ -130,8 +153,8 @@ func TestSetNamespaceLabelsAndAnnotations(t *testing.T) {
func TestSetNamespaceLabelsAndAnnotationsError(t *testing.T) {
bd := &fleet.BundleDeployment{Spec: fleet.BundleDeploymentSpec{
Options: fleet.BundleDeploymentOptions{
NamespaceLabels: &map[string]string{"optLabel1": "optValue1", "optLabel2": "optValue2"},
NamespaceAnnotations: &map[string]string{"optAnn1": "optValue1"},
NamespaceLabels: map[string]string{"optLabel1": "optValue1", "optLabel2": "optValue2"},
NamespaceAnnotations: map[string]string{"optAnn1": "optValue1"},
},
}}
release := "test/foo/bar"
Expand Down
8 changes: 4 additions & 4 deletions internal/cmd/controller/target/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ func (t *Target) BundleDeployment() *fleet.BundleDeployment {

for _, bundleTarget := range t.Bundle.Spec.Targets {
for key, value := range bundleTarget.NamespaceLabels {
(*bd.Spec.Options.NamespaceLabels)[key] = value
(*bd.Spec.StagedOptions.NamespaceLabels)[key] = value
bd.Spec.Options.NamespaceLabels[key] = value
bd.Spec.StagedOptions.NamespaceLabels[key] = value
}

for key, value := range bundleTarget.NamespaceAnnotations {
(*bd.Spec.Options.NamespaceAnnotations)[key] = value
(*bd.Spec.StagedOptions.NamespaceAnnotations)[key] = value
bd.Spec.Options.NamespaceAnnotations[key] = value
bd.Spec.StagedOptions.NamespaceAnnotations[key] = value
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/fleet.cattle.io/v1alpha1/bundledeployment_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ type BundleDeploymentOptions struct {

// 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"`

// DeleteCRDResources deletes CRDs. Warning! this will also delete all your Custom Resources.
DeleteCRDResources bool `json:"deleteCRDResources,omitempty"`
Expand Down
20 changes: 6 additions & 14 deletions pkg/apis/fleet.cattle.io/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6ac70f9

Please sign in to comment.