Repository: rancher/fleet
Pull request #3531 (2025-04-09 14:07:08)

Adds extra labels and annotations in charts
Adds the `extraLabels` and `extraAnnotations` values to the helm charts. Those values are propagated to the `fleet-controller`, `gitjob` and `helmops` deployments.

It also introduces helm charts unittests testing those 2 new values.


Refers to https://github.com/rancher/fleet/issues/3295

### Checklist

- [ ] <!-- If applicable,--> I have updated the documentation via a pull request in the
[fleet-docs](https://github.com/rancher/fleet-docs) repository.



Patch:
diff --git a/.github/scripts/release-against-charts.sh b/.github/scripts/release-against-charts.sh
index e871b75b..f4c4bc6a 100755
--- a/.github/scripts/release-against-charts.sh
+++ b/.github/scripts/release-against-charts.sh
@@ -19,7 +19,7 @@ if [ ! -e ~/.gitconfig ]; then
     git config --global user.email fleet@suse.de
 fi
 
-if [[ "$(git rev-parse --abbrev-ref HEAD)" =~ dev-v2\.1[0-9]+ ]]; then
+if [ -f packages/fleet/package.yaml ];  then
     # Use new auto bump scripting until the Github action CI works as expected
     # no parameters besides the target branch are needed in theory, but the pr
     # creation still needs the new Chart and Fleet version
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 9b49e754..e82bbf92 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -28,6 +28,11 @@ jobs:
       -
         name: Run chart-testing (lint)
         run: ct lint --all --validate-maintainers=false charts/
+      -
+        name: helm-unittest
+        run: |
+          helm plugin install https://github.com/helm-unittest/helm-unittest.git
+          helm unittest ./charts/fleet
       -
         uses: actions/setup-go@0aaccfd150d50ccaeb58ebd88d36e91967a5f35b # v5
         with:
diff --git a/charts/fleet/templates/deployment.yaml b/charts/fleet/templates/deployment.yaml
index 2951a844..cef1dfd7 100644
--- a/charts/fleet/templates/deployment.yaml
+++ b/charts/fleet/templates/deployment.yaml
@@ -26,6 +26,13 @@ spec:
         {{- if empty $shard.id }}
         fleet.cattle.io/shard-default: "true"
         {{- end }}
+{{- if $.Values.extraLabels }}
+{{ toYaml $.Values.extraLabels | indent 8 }}
+{{- end }}
+{{- if $.Values.extraAnnotations }}
+      annotations:
+{{ toYaml $.Values.extraAnnotations | indent 8 }}
+{{- end }}
     spec:
       containers:
       - env:
diff --git a/charts/fleet/templates/deployment_gitjob.yaml b/charts/fleet/templates/deployment_gitjob.yaml
index baa95c2b..8302f277 100644
--- a/charts/fleet/templates/deployment_gitjob.yaml
+++ b/charts/fleet/templates/deployment_gitjob.yaml
@@ -27,6 +27,13 @@ spec:
         {{- if empty $shard.id }}
         fleet.cattle.io/shard-default: "true"
         {{- end }}
+{{- if $.Values.extraLabels }}
+{{ toYaml $.Values.extraLabels | indent 8 }}
+{{- end }}
+{{- if $.Values.extraAnnotations }}
+      annotations:
+{{ toYaml $.Values.extraAnnotations | indent 8 }}
+{{- end }}
     spec:
       serviceAccountName: gitjob
       containers:
diff --git a/charts/fleet/templates/deployment_helmops.yaml b/charts/fleet/templates/deployment_helmops.yaml
index 47a71089..2ea710f0 100644
--- a/charts/fleet/templates/deployment_helmops.yaml
+++ b/charts/fleet/templates/deployment_helmops.yaml
@@ -27,6 +27,13 @@ spec:
         {{- if empty $shard.id }}
         fleet.cattle.io/shard-default: "true"
         {{- end }}
+{{- if $.Values.extraLabels }}
+{{ toYaml $.Values.extraLabels | indent 8 }}
+{{- end }}
+{{- if $.Values.extraAnnotations }}
+      annotations:
+{{ toYaml $.Values.extraAnnotations | indent 8 }}
+{{- end }}
     spec:
       serviceAccountName: helmops
       containers:
diff --git a/charts/fleet/values.yaml b/charts/fleet/values.yaml
index b325214b..944228d3 100644
--- a/charts/fleet/values.yaml
+++ b/charts/fleet/values.yaml
@@ -140,3 +140,13 @@ agent:
 #   - id: shard2
 #     nodeSelector:
 #       kubernetes.io/hostname: k3d-upstream-server-2
+
+# Extra labels passed to the fleet pods.
+# extraLabels:
+#   new-label: "new-label-value"
+#   new-label-2: "new-label-value-2"
+
+# Extra annotations passed to the fleet pods.
+# extraAnnotations:
+#   new-annotation: "new-annotation-value"
+#   new-annotation-2: "new-annotation-value-2"
diff --git a/pkg/git/fetch.go b/pkg/git/fetch.go
index 6273618b..60ac185e 100644
--- a/pkg/git/fetch.go
+++ b/pkg/git/fetch.go
@@ -70,7 +70,7 @@ func (f *Fetch) LatestCommit(ctx context.Context, gitrepo *v1alpha1.GitRepo, cli
 		knownHosts = kh
 	}
 
-	if f.KnownHosts != nil && !f.KnownHosts.IsStrict() {
+	if secret.Data != nil && f.KnownHosts != nil && !f.KnownHosts.IsStrict() {
 		// This prevents errors about keys being mismatch or not found when host key checks are disabled.
 		secret.Data["known_hosts"] = nil
 	}


Test patch:
diff --git a/charts/fleet/tests/extraAnnotations_test.yaml b/charts/fleet/tests/extraAnnotations_test.yaml
new file mode 100644
index 00000000..b5a15496
--- /dev/null
+++ b/charts/fleet/tests/extraAnnotations_test.yaml
@@ -0,0 +1,75 @@
+suite: extraAnnotations tests
+templates:
+  - deployment.yaml
+  - deployment_gitjob.yaml
+  - deployment_helmops.yaml
+tests:
+  - it: should set extraAnnotations variables in fleet-controller deployment
+    set:
+      extraAnnotations:
+        test-annotation-1: testvalue1
+        test-annotation-2: testvalue2
+    template: deployment.yaml
+    asserts:
+      - hasDocuments:
+          count: 1
+      - isKind:
+          of: Deployment
+      - equal:
+          path: spec.template.metadata.annotations.test-annotation-1
+          value: testvalue1
+      - equal:
+          path: spec.template.metadata.annotations.test-annotation-2
+          value: testvalue2
+  - it: should set extraAnnotations variables in gitjob deployment
+    set:
+      extraAnnotations:
+        test-annotation-1: testvalue1
+        test-annotation-2: testvalue2
+      gitops:
+        enabled: true
+    template: deployment_gitjob.yaml
+    asserts:
+      - hasDocuments:
+          count: 1
+      - isKind:
+          of: Deployment
+      - equal:
+          path: spec.template.metadata.annotations.test-annotation-1
+          value: testvalue1
+      - equal:
+          path: spec.template.metadata.annotations.test-annotation-2
+          value: testvalue2
+  - it: should set extraAnnotations variables in helmops deployment
+    set:
+      extraAnnotations:
+        test-annotation-1: testvalue1
+        test-annotation-2: testvalue2
+      extraEnv:
+        - name: EXPERIMENTAL_HELM_OPS
+          value: "true"
+    template: deployment_helmops.yaml
+    asserts:
+      - hasDocuments:
+          count: 1
+      - isKind:
+          of: Deployment
+      - equal:
+          path: spec.template.metadata.annotations.test-annotation-1
+          value: testvalue1
+      - equal:
+          path: spec.template.metadata.annotations.test-annotation-2
+          value: testvalue2
+  - it: should not set extraAnnotations variables when empty
+    set:
+      extraAnnotations: {}
+      extraEnv:
+        - name: EXPERIMENTAL_HELM_OPS
+          value: "true"
+    asserts:
+      - hasDocuments:
+          count: 1
+      - isKind: 
+          of: Deployment
+      - notExists:
+          path: spec.template.metadata.annotations
diff --git a/charts/fleet/tests/extraLabels_test.yaml b/charts/fleet/tests/extraLabels_test.yaml
new file mode 100644
index 00000000..1f7c8517
--- /dev/null
+++ b/charts/fleet/tests/extraLabels_test.yaml
@@ -0,0 +1,120 @@
+suite: extraLabels tests
+tests:
+  - it: should set extraLabels variables in fleet-controller deployment
+    set:
+      extraLabels:
+        test-label-1: testvalue1
+        test-label-2: testvalue2
+    template: deployment.yaml
+    asserts:
+      - hasDocuments:
+          count: 1
+      - isKind:
+          of: Deployment
+      - equal:
+          path: spec.template.metadata.labels
+          value: 
+            app: fleet-controller
+            fleet.cattle.io/shard-default: "true" 
+            fleet.cattle.io/shard-id: ""
+            test-label-1: testvalue1
+            test-label-2: testvalue2
+  - it: should set extraLabels variables in gitjob deployment
+    set:
+      extraLabels:
+        test-label-1: testvalue1
+        test-label-2: testvalue2
+      gitops:
+        enabled: true
+    template: deployment_gitjob.yaml
+    asserts:
+      - hasDocuments:
+          count: 1
+      - isKind:
+          of: Deployment
+      - equal:
+          path: spec.template.metadata.labels
+          value: 
+            app: gitjob
+            fleet.cattle.io/shard-default: "true" 
+            fleet.cattle.io/shard-id: ""
+            test-label-1: testvalue1
+            test-label-2: testvalue2
+  - it: should set extraLabels variables in helmops deployment
+    set:
+      extraLabels:
+        test-label-1: testvalue1
+        test-label-2: testvalue2
+      extraEnv:
+        - name: EXPERIMENTAL_HELM_OPS
+          value: "true"
+    template: deployment_helmops.yaml
+    asserts:
+      - hasDocuments:
+          count: 1
+      - isKind:
+          of: Deployment
+      - equal:
+          path: spec.template.metadata.labels
+          value: 
+            app: helmops
+            fleet.cattle.io/shard-default: "true" 
+            fleet.cattle.io/shard-id: ""
+            test-label-1: testvalue1
+            test-label-2: testvalue2
+  - it: should not set more labels in fleet-controller deployment when extraLabels is empty
+    set:
+      extraLabels: {}
+      extraEnv:
+        - name: EXPERIMENTAL_HELM_OPS
+          value: "true"
+    template: deployment.yaml
+    asserts:
+      - hasDocuments:
+          count: 1
+      - isKind: 
+          of: Deployment
+      - equal:
+          path: spec.template.metadata.labels
+          value: 
+            app: fleet-controller
+            fleet.cattle.io/shard-default: "true" 
+            fleet.cattle.io/shard-id: ""
+
+  - it: should not set more labels in gitjob deployment when extraLabels is empty
+    set:
+      extraLabels: {}
+      extraEnv:
+        - name: EXPERIMENTAL_HELM_OPS
+          value: "true"
+    template: deployment_gitjob.yaml
+    asserts:
+      - hasDocuments:
+          count: 1
+      - isKind: 
+          of: Deployment
+      - equal:
+          path: spec.template.metadata.labels
+          value: 
+            app: gitjob
+            fleet.cattle.io/shard-default: "true" 
+            fleet.cattle.io/shard-id: ""
+
+  - it: should not set more labels in helmops deployment when extraLabels is empty
+    set:
+      extraLabels: {}
+      extraEnv:
+        - name: EXPERIMENTAL_HELM_OPS
+          value: "true"
+    template: deployment_helmops.yaml
+    asserts:
+      - hasDocuments:
+          count: 1
+      - isKind: 
+          of: Deployment
+      - equal:
+          path: spec.template.metadata.labels
+          value: 
+            app: helmops
+            fleet.cattle.io/shard-default: "true" 
+            fleet.cattle.io/shard-id: ""
diff --git a/pkg/git/fetch_test.go b/pkg/git/fetch_test.go
index 882bf015..a667a61a 100644
--- a/pkg/git/fetch_test.go
+++ b/pkg/git/fetch_test.go
@@ -164,6 +164,32 @@ var _ = Describe("git fetch's LatestCommit tests", func() {
 		Expect(commit).To(Equal("2ada7cca738877df8459b3a34839a15e5683edaa"))
 	})
 
+	It("returns the commit for the expected branch with no secret", func() {
+		gr := &fleetv1.GitRepo{
+			ObjectMeta: metav1.ObjectMeta{
+				Name:      "test-gitrepo",
+				Namespace: "test-ns",
+			},
+			Spec: fleetv1.GitRepoSpec{
+				ClientSecretName: "test-secret",
+				Repo:             fakeGithub.URL,
+				Branch:           "master",
+			},
+			Status: fleetv1.GitRepoStatus{
+				Commit: "",
+			},
+		}
+		c := newTestClient()
+		f := git.Fetch{
+			KnownHosts: mockKnownHostsGetter{
+				data: "foo",
+			},
+		}
+		commit, err := f.LatestCommit(context.Background(), gr, c)
+		Expect(err).ToNot(HaveOccurred())
+		Expect(commit).To(Equal("2ada7cca738877df8459b3a34839a15e5683edaa"))
+	})
+
 	It("returns an error when secret's type is not expected", func() {
 		secret := &corev1.Secret{
 			ObjectMeta: metav1.ObjectMeta{