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

[v0.10] Fix namespace target customization with no defaults #3085

Closed
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
@@ -0,0 +1,10 @@
kind: GitRepo
apiVersion: fleet.cattle.io/v1alpha1
metadata:
name: test
namespace: fleet-local
spec:
repo: https://github.com/rancher/fleet-test-data
branch: master
paths:
- target-customization-namespace-labels/without-default-values
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ spec:
repo: https://github.com/rancher/fleet-test-data
branch: master
paths:
- target-customization-namespace-labels
- target-customization-namespace-labels/with-default-values
101 changes: 0 additions & 101 deletions e2e/single-cluster/targetCustomization_test.go

This file was deleted.

166 changes: 166 additions & 0 deletions e2e/single-cluster/target_customization_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package singlecluster_test

import (
"strings"

"github.com/rancher/fleet/e2e/testenv"
"github.com/rancher/fleet/e2e/testenv/kubectl"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Helm deploy options", func() {
var (
asset string
k kubectl.Command
)
BeforeEach(func() {
k = env.Kubectl.Namespace(env.Namespace)
})

JustBeforeEach(func() {
out, err := k.Apply("-f", testenv.AssetPath(asset))
Expect(err).ToNot(HaveOccurred(), out)
})

AfterEach(func() {
out, err := k.Delete("-f", testenv.AssetPath(asset))
Expect(err).ToNot(HaveOccurred(), out)
})

Describe("namespace labels and annotations in target customizations", func() {
When("namespaceLabels and namespaceAnnotations override existing root configuration", func() {
BeforeEach(func() {
asset = "single-cluster/ns-labels-target-customization.yaml"
})

It("deploys the bundledeployment with the merged namespaceLabels and namespaceAnnotations", func() {
By("setting the namespaces and annotations on the bundle deployment")
out, err := k.Get("cluster", "local", "-o", "jsonpath={.status.namespace}")
Expect(err).ToNot(HaveOccurred(), out)

clusterNS := strings.TrimSpace(out)
clusterK := k.Namespace(clusterNS)

var bundleDeploymentName string

Eventually(func(g Gomega) {
bundleDeploymentNames, _ := clusterK.Get(
"bundledeployments",
"-o",
"jsonpath={.items[*].metadata.name}",
)

for _, bdName := range strings.Split(bundleDeploymentNames, " ") {
if strings.HasPrefix(bdName, "test-target-customization-namespace-labels") {
bundleDeploymentName = bdName
break
}
}

g.Expect(bundleDeploymentName).ToNot(BeEmpty())
}).Should(Succeed())

Eventually(func(g Gomega) {
labels, err := clusterK.Get(
"bundledeployments",
bundleDeploymentName,
"-o",
"jsonpath={.spec.options.namespaceLabels}",
)

g.Expect(err).ToNot(HaveOccurred())
g.Expect(labels).To(Equal(`{"foo":"bar","this.is/a":"test"}`))

annot, err := clusterK.Get(
"bundledeployments",
bundleDeploymentName,
"-o",
"jsonpath={.spec.options.namespaceAnnotations}",
)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(annot).To(Equal(`{"foo":"bar","this.is/another":"test"}`))
}).Should(Succeed())

By("setting the namespaces and annotations on the created namespace")
Eventually(func(g Gomega) {
labels, err := k.Get("ns", "ns-1", "-o", "jsonpath={.metadata.labels}")
g.Expect(err).ToNot(HaveOccurred())
g.Expect(labels).To(Equal(`{"foo":"bar","kubernetes.io/metadata.name":"ns-1","this.is/a":"test"}`))

ann, err := k.Get("ns", "ns-1", "-o", "jsonpath={.metadata.annotations}")
g.Expect(err).ToNot(HaveOccurred())
g.Expect(ann).To(Equal(`{"foo":"bar","this.is/another":"test"}`))
}).Should(Succeed())
})
})

When("namespaceLabels and namespaceAnnotations override empty root configuration", func() {
BeforeEach(func() {
asset = "single-cluster/ns-labels-target-customization-no-defaults.yaml"
})

It("deploys the bundledeployment with the merged namespaceLabels and namespaceAnnotations", func() {
By("setting the namespaces and annotations on the bundle deployment")
out, err := k.Get("cluster", "local", "-o", "jsonpath={.status.namespace}")
Expect(err).ToNot(HaveOccurred(), out)

clusterNS := strings.TrimSpace(out)
clusterK := k.Namespace(clusterNS)

var bundleDeploymentName string

Eventually(func(g Gomega) {
bundleDeploymentNames, _ := clusterK.Get(
"bundledeployments",
"-o",
"jsonpath={.items[*].metadata.name}",
)

for _, bdName := range strings.Split(bundleDeploymentNames, " ") {
if strings.HasPrefix(bdName, "test-target-customization-namespace-labels") {
bundleDeploymentName = bdName
break
}
}

g.Expect(bundleDeploymentName).ToNot(BeEmpty())
}).Should(Succeed())

Eventually(func(g Gomega) {
labels, err := clusterK.Get(
"bundledeployments",
bundleDeploymentName,
"-o",
"jsonpath={.spec.options.namespaceLabels}",
)

g.Expect(err).ToNot(HaveOccurred())
g.Expect(labels).To(Equal(`{"this.is/a":"test"}`))

annot, err := clusterK.Get(
"bundledeployments",
bundleDeploymentName,
"-o",
"jsonpath={.spec.options.namespaceAnnotations}",
)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(annot).To(Equal(`{"this.is/another":"test"}`))
}).Should(Succeed())

By("setting the namespaces and annotations on the created namespace")
Eventually(func(g Gomega) {
labels, err := k.Get("ns", "no-defaults-ns-1", "-o", "jsonpath={.metadata.labels}")
g.Expect(err).ToNot(HaveOccurred())
g.Expect(labels).To(Equal(`{"kubernetes.io/metadata.name":"no-defaults-ns-1","this.is/a":"test"}`))

ann, err := k.Get("ns", "no-defaults-ns-1", "-o", "jsonpath={.metadata.annotations}")
g.Expect(err).ToNot(HaveOccurred())
g.Expect(ann).To(Equal(`{"this.is/another":"test"}`))
}).Should(Succeed())
})
})
})

})
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func (i *importHandler) importCluster(cluster *fleet.Cluster, status fleet.Clust
TTL: &metav1.Duration{Duration: durations.ClusterImportTokenTTL},
},
})
logrus.Debugf("Failed to create ClusterRegistrationToken for cluster %s/%s: %v (requeueing)", cluster.Namespace, cluster.Name, err)
logrus.Debugf("Failed to create ClusterRegistrationToken for cluster %s/%s: %v (requeuing)", cluster.Namespace, cluster.Name, err)
i.clusters.EnqueueAfter(cluster.Namespace, cluster.Name, durations.TokenClusterEnqueueDelay)
return status, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (r *BundleDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Req

err = r.Status().Update(ctx, bd)
if err != nil {
logger.V(1).Error(err, "Reconcile failed update to bundle deployment status, requeueing", "status", bd.Status)
logger.V(1).Error(err, "Reconcile failed update to bundle deployment status, requeuing", "status", bd.Status)
return ctrl.Result{}, err
}

Expand Down
23 changes: 23 additions & 0 deletions internal/cmd/controller/target/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
}
bd.Spec.Paused = t.IsPaused()

initialiseOptionsMaps(bd)

for _, bundleTarget := range t.Bundle.Spec.Targets {
if bundleTarget.NamespaceLabels != nil {
for key, value := range *bundleTarget.NamespaceLabels {
Expand Down Expand Up @@ -140,3 +142,24 @@
func (t *Target) message() string {
return summary.MessageFromDeployment(t.Deployment)
}

// initialiseOptionsMaps initialises options and staged options maps of namespace labels and annotations on bd, if
// needed.
// Assumes that bd is not nil.
func initialiseOptionsMaps(bd *fleet.BundleDeployment) {
if bd.Spec.Options.NamespaceLabels == nil {
bd.Spec.Options.NamespaceLabels = map[string]string{}

Check failure on line 151 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.24.17-k3s1, default)

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 151 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.24.17-k3s1, sharding, [{"id":"shard0"},{"id":"shard1"},{"id":"shard2"}])

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 151 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.30.2-k3s2, infra-setup)

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 151 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.24.17-k3s1, infra-setup)

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 151 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-mc-test

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 151 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.30.2-k3s2, sharding, [{"id":"shard0"},{"id":"shard1"},{"id":"shard2"}])

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 151 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.30.2-k3s2, default)

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 151 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / fleet-upgrade-test (v1.30.2-k3s2)

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 151 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / golangci-lint

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 151 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / golangci-lint

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 151 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / unit-test

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment
}

if bd.Spec.Options.NamespaceAnnotations == nil {
bd.Spec.Options.NamespaceAnnotations = map[string]string{}

Check failure on line 155 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.24.17-k3s1, default)

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 155 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.24.17-k3s1, sharding, [{"id":"shard0"},{"id":"shard1"},{"id":"shard2"}])

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 155 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.30.2-k3s2, infra-setup)

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 155 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.24.17-k3s1, infra-setup)

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 155 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-mc-test

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 155 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.30.2-k3s2, sharding, [{"id":"shard0"},{"id":"shard1"},{"id":"shard2"}])

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 155 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.30.2-k3s2, default)

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 155 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / fleet-upgrade-test (v1.30.2-k3s2)

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 155 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / golangci-lint

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 155 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / golangci-lint

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 155 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / unit-test

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment
}

if bd.Spec.StagedOptions.NamespaceLabels == nil {
bd.Spec.StagedOptions.NamespaceLabels = map[string]string{}

Check failure on line 159 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.24.17-k3s1, default)

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 159 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.24.17-k3s1, sharding, [{"id":"shard0"},{"id":"shard1"},{"id":"shard2"}])

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 159 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.30.2-k3s2, infra-setup)

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 159 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.24.17-k3s1, infra-setup)

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 159 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-mc-test

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 159 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.30.2-k3s2, sharding, [{"id":"shard0"},{"id":"shard1"},{"id":"shard2"}])

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 159 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.30.2-k3s2, default)

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 159 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / fleet-upgrade-test (v1.30.2-k3s2)

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 159 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / golangci-lint

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 159 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / golangci-lint

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 159 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / unit-test

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment
}

if bd.Spec.StagedOptions.NamespaceAnnotations == nil {
bd.Spec.StagedOptions.NamespaceAnnotations = map[string]string{}

Check failure on line 163 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.24.17-k3s1, default)

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 163 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.24.17-k3s1, sharding, [{"id":"shard0"},{"id":"shard1"},{"id":"shard2"}])

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 163 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.30.2-k3s2, infra-setup)

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 163 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.24.17-k3s1, infra-setup)

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 163 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-mc-test

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 163 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.30.2-k3s2, sharding, [{"id":"shard0"},{"id":"shard1"},{"id":"shard2"}])

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 163 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / e2e-fleet-test (v1.30.2-k3s2, default)

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 163 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / fleet-upgrade-test (v1.30.2-k3s2)

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment

Check failure on line 163 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / golangci-lint

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment) (typecheck)

Check failure on line 163 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / golangci-lint

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment) (typecheck)

Check failure on line 163 in internal/cmd/controller/target/target.go

View workflow job for this annotation

GitHub Actions / unit-test

cannot use map[string]string{} (value of type map[string]string) as *map[string]string value in assignment
}
}
Loading