Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify target customisations for namespace labels and annotations #2664

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ metadata:
name: test
namespace: fleet-local
spec:
repo: https://github.com/Tommy12789/fleet-examples-tommy
branch: test
repo: https://github.com/rancher/fleet-test-data
branch: master
paths:
- namespaceLabels_targetCustomization
- target-customization-namespace-labels
39 changes: 31 additions & 8 deletions e2e/single-cluster/targetCustomization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,23 @@ var _ = Describe("Helm deploy options", func() {
BeforeEach(func() {
asset = "single-cluster/namespaceLabels_targetCustomization.yaml"
})
When("namespaceLabels set as targetCustomization ", func() {
It("deploy the bundledeployment with the merged namespaceLabels", func() {
When("namespaceLabels and namespaceAnnotations are set as targetCustomization ", func() {
It("deploys the bundledeployment with the merged namespaceLabels and namespaceAnnotations", func() {
out, err := k.Get("cluster", "local", "-o", "jsonpath={.status.namespace}")
Expect(err).ToNot(HaveOccurred(), out)

clusterNS := strings.TrimSpace(out)
clusterK := k.Namespace(clusterNS)
Eventually(func() string {
bundleDeploymentNames, _ := k.Namespace("cluster-fleet-local-local-1a3d67d0a899").Get("bundledeployments", "-o", "jsonpath={.items[*].metadata.name}")
bundleDeploymentNames, _ := clusterK.Get(
"bundledeployments",
"-o",
"jsonpath={.items[*].metadata.name}",
)

var bundleDeploymentName string
for _, podName := range strings.Split(bundleDeploymentNames, " ") {
if strings.HasPrefix(podName, "test-namespacelabels-targetcustomization") {
if strings.HasPrefix(podName, "test-target-customization-namespace-labels") {
bundleDeploymentName = podName
break
}
Expand All @@ -49,16 +58,25 @@ var _ = Describe("Helm deploy options", func() {
return "nil"
}

bundleDeploymentNamespacesLabels, _ := k.Namespace("cluster-fleet-local-local-1a3d67d0a899").Get("bundledeployments", bundleDeploymentName, "-o", "jsonpath={.spec.options.namespaceLabels}")
bundleDeploymentNamespacesLabels, _ := clusterK.Get(
"bundledeployments",
bundleDeploymentName,
"-o",
"jsonpath={.spec.options.namespaceLabels}",
)
return bundleDeploymentNamespacesLabels
}).Should(Equal(`{"foo":"bar","this.is/a":"test"}`))

Eventually(func() string {
bundleDeploymentNames, _ := k.Namespace("cluster-fleet-local-local-1a3d67d0a899").Get("bundledeployments", "-o", "jsonpath={.items[*].metadata.name}")
bundleDeploymentNames, _ := clusterK.Get(
"bundledeployments",
"-o",
"jsonpath={.items[*].metadata.name}",
)

var bundleDeploymentName string
for _, podName := range strings.Split(bundleDeploymentNames, " ") {
if strings.HasPrefix(podName, "test-namespacelabels-targetcustomization") {
if strings.HasPrefix(podName, "test-target-customization-namespace-labels") {
bundleDeploymentName = podName
break
}
Expand All @@ -67,7 +85,12 @@ var _ = Describe("Helm deploy options", func() {
return "nil"
}

bundleDeploymentNamespacesLabels, _ := k.Namespace("cluster-fleet-local-local-1a3d67d0a899").Get("bundledeployments", bundleDeploymentName, "-o", "jsonpath={.spec.options.namespaceAnnotations}")
bundleDeploymentNamespacesLabels, _ := clusterK.Get(
"bundledeployments",
bundleDeploymentName,
"-o",
"jsonpath={.spec.options.namespaceAnnotations}",
)
return bundleDeploymentNamespacesLabels
}).Should(Equal(`{"foo":"bar","this.is/a":"test"}`))

Expand Down
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
21 changes: 9 additions & 12 deletions internal/cmd/controller/target/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,19 @@ func (t *Target) BundleDeployment() *fleet.BundleDeployment {
Spec: t.Deployment.Spec,
}
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
}
}
for _, bundleTargets := range t.Bundle.Spec.Targets {
weyfonk marked this conversation as resolved.
Show resolved Hide resolved
if bundleTargets.NamespaceAnnotations != nil {
for key, value := range *bundleTargets.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
}
}

bd.Spec.DependsOn = t.Bundle.Spec.DependsOn
bd.Spec.CorrectDrift = t.Options.CorrectDrift
return bd
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/fleet.cattle.io/v1alpha1/bundle_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
40 changes: 12 additions & 28 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.

Loading