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

Adding namespaceLabels and namespaceAnnotations as TargetCustomization #2583

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/Tommy12789/fleet-examples-tommy
branch: test
paths:
- namespaceLabels_targetCustomization
78 changes: 78 additions & 0 deletions e2e/single-cluster/targetCustomization_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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("namespaceLabels TargetCustomization", func() {
BeforeEach(func() {
asset = "single-cluster/namespaceLabels_targetCustomization.yaml"
})
When("namespaceLabels set as targetCustomization ", func() {
It("deploy the bundledeployment with the merged namespaceLabels", func() {
Eventually(func() string {
bundleDeploymentNames, _ := k.Namespace("cluster-fleet-local-local-1a3d67d0a899").Get("bundledeployments", "-o", "jsonpath={.items[*].metadata.name}")

var bundleDeploymentName string
for _, podName := range strings.Split(bundleDeploymentNames, " ") {
if strings.HasPrefix(podName, "test-namespacelabels-targetcustomization") {
bundleDeploymentName = podName
break
}
}
if bundleDeploymentName == "" {
return "nil"
}

bundleDeploymentNamespacesLabels, _ := k.Namespace("cluster-fleet-local-local-1a3d67d0a899").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}")

var bundleDeploymentName string
for _, podName := range strings.Split(bundleDeploymentNames, " ") {
if strings.HasPrefix(podName, "test-namespacelabels-targetcustomization") {
bundleDeploymentName = podName
break
}
}
if bundleDeploymentName == "" {
return "nil"
}

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

})
})
})

})
16 changes: 16 additions & 0 deletions internal/cmd/controller/target/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ 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
manno marked this conversation as resolved.
Show resolved Hide resolved
(*bd.Spec.StagedOptions.NamespaceLabels)[key] = value
}
}
}
for _, bundleTargets := range t.Bundle.Spec.Targets {
if bundleTargets.NamespaceAnnotations != nil {
for key, value := range *bundleTargets.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
6 changes: 6 additions & 0 deletions pkg/apis/fleet.cattle.io/v1alpha1/bundle_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ type BundleTarget struct {
ClusterGroupSelector *metav1.LabelSelector `json:"clusterGroupSelector,omitempty"`
// DoNotDeploy if set to true, will not deploy to this target.
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"`
Copy link
Member

Choose a reason for hiding this comment

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

I see this is already a pointer to a map in the bundledeployment type. Is that really necessary? Looking at corev1 in k8s, it seems +optional and omitempty is sufficient, unless we really need to differentiate between a null value and an empty map.

Oh, it seems we do:

if bd.Spec.Options.NamespaceAnnotations != nil {

That code deletes all annotations if it's an empty map, but not if it's nil. 👍

Copy link
Member

Choose a reason for hiding this comment

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

However that would still work with a map, as a map is effectively a pointer.

// NamespaceAnnotations are annotations that will be appended to the namespace created by Fleet.
// +nullable
NamespaceAnnotations *map[string]string `json:"namespaceAnnotations,omitempty"`
}

// BundleSummary contains the number of bundle deployments in each state and a
Expand Down
22 changes: 22 additions & 0 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