8000 Fix namespace target customization support with no defaults by weyfonk · Pull Request #3052 · rancher/fleet · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix namespace target customization support with no defaults #3052

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

Merged
merged 3 commits into from
Nov 7, 2024
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
@@ -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
119 changes: 92 additions & 27 deletions e2e/single-cluster/targetCustomization_test.go
8000
Original file line number Diff line number Diff line change
Expand Up @@ -29,71 +29,136 @@ var _ = Describe("Helm deploy options", func() {
Expect(err).ToNot(HaveOccurred(), out)
})

Describe("namespaceLabels TargetCustomization", func() {
BeforeEach(func() {
asset = "single-cluster/namespaceLabels_targetCustomization.yaml"
})
When("namespaceLabels and namespaceAnnotations are set as targetCustomization ", func() {
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)
Eventually(func() string {

var bundleDeploymentName string

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

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

bundleDeploymentNamespacesLabels, _ := clusterK.Get(
g.Expect(bundleDeploymentName).ToNot(BeEmpty())
}).Should(Succeed())

Eventually(func(g Gomega) {
labels, err := clusterK.Get(
"bundledeployments",
bundleDeploymentName,
"-o",
"jsonpath={.spec.options.namespaceLabels}",
)
return bundleDeploymentNamespacesLabels
}).Should(Equal(`{"foo":"bar","this.is/a":"test"}`))

Eventually(func() string {
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}",
)

var bundleDeploymentName string
for _, podName := range strings.Split(bundleDeploymentNames, " ") {
if strings.HasPrefix(podName, "test-target-customization-namespace-labels") {
bundleDeploymentName = podName
for _, bdName := range strings.Split(bundleDeploymentNames, " ") {
if strings.HasPrefix(bdName, "test-target-customization-namespace-labels") {
bundleDeploymentName = bdName
break
}
}
if bundleDeploymentName == "" {
return "nil&q 8000 uot;
}

bundleDeploymentNamespacesLabels, _ := clusterK.Get(
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}",
)
return bundleDeploymentNamespacesLabels
}).Should(Equal(`{"foo":"bar","this.is/a":"test"}`))
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())
})
})
})
Expand Down
23 changes: 23 additions & 0 deletions
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func (t *Target) BundleDeployment() *fleet.BundleDeployment {
}
bd.Spec.Paused = t.IsPaused()

initialiseOptionsMaps(bd)

for _, bundleTarget := range t.Bundle.Spec.Targets {
for key, value := range bundleTarget.NamespaceLabels {
bd.Spec.Options.NamespaceLabels[key] = value
Expand Down Expand Up @@ -136,3 +138,24 @@ func (t *Target) state() fleet.BundleState {
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.
Copy link
Member
@manno manno Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: At this point we could just pass spec?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, indeed we could!

func initialiseOptionsMaps(bd *fleet.BundleDeployment) {
if bd.Spec.Options.NamespaceLabels == nil {
bd.Spec.Options.NamespaceLabels = map[string]string{}
}

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

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

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