Skip to content

Commit

Permalink
Adding NamespaceLabels and NamespaceAnnotations as TargetCustomization
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommy12789 authored and Tommy committed Jul 9, 2024
1 parent b0964d5 commit 57589c7
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
10 changes: 10 additions & 0 deletions e2e/assets/single-cluster/namespaceLabels_targetCustomization.yaml
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
(*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"`
// 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.

0 comments on commit 57589c7

Please sign in to comment.