|
| 1 | +/* |
| 2 | +Copyright 2023 The Flux authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controller |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "testing" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/fluxcd/pkg/apis/meta" |
| 26 | + "github.com/fluxcd/pkg/testserver" |
| 27 | + sourcev1 "github.com/fluxcd/source-controller/api/v1" |
| 28 | + . "github.com/onsi/gomega" |
| 29 | + corev1 "k8s.io/api/core/v1" |
| 30 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 31 | + "k8s.io/apimachinery/pkg/types" |
| 32 | + ctrl "sigs.k8s.io/controller-runtime" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 34 | + |
| 35 | + kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1" |
| 36 | +) |
| 37 | + |
| 38 | +func TestKustomizationReconciler_DisallowedManagers(t *testing.T) { |
| 39 | + g := NewWithT(t) |
| 40 | + id := "disallowed-managers-" + randStringRunes(5) |
| 41 | + revision := "v1.0.0" |
| 42 | + |
| 43 | + err := createNamespace(id) |
| 44 | + g.Expect(err).NotTo(HaveOccurred(), "failed to create test namespace") |
| 45 | + |
| 46 | + err = createKubeConfigSecret(id) |
| 47 | + g.Expect(err).NotTo(HaveOccurred(), "failed to create kubeconfig secret") |
| 48 | + |
| 49 | + manifests := func(name string, data string) []testserver.File { |
| 50 | + return []testserver.File{ |
| 51 | + { |
| 52 | + Name: "configmap.yaml", |
| 53 | + Body: fmt.Sprintf(`--- |
| 54 | +apiVersion: v1 |
| 55 | +kind: ConfigMap |
| 56 | +metadata: |
| 57 | + name: %[1]s |
| 58 | +data: |
| 59 | + key: %[2]s |
| 60 | +`, name, data), |
| 61 | + }, |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + artifact, err := testServer.ArtifactFromFiles(manifests(id, randStringRunes(5))) |
| 66 | + g.Expect(err).NotTo(HaveOccurred(), "failed to create artifact from files") |
| 67 | + |
| 68 | + repositoryName := types.NamespacedName{ |
| 69 | + Name: fmt.Sprintf("disallowed-managers-%s", randStringRunes(5)), |
| 70 | + Namespace: id, |
| 71 | + } |
| 72 | + |
| 73 | + err = applyGitRepository(repositoryName, artifact, revision) |
| 74 | + g.Expect(err).NotTo(HaveOccurred()) |
| 75 | + |
| 76 | + kustomizationKey := types.NamespacedName{ |
| 77 | + Name: fmt.Sprintf("disallowed-managers-%s", randStringRunes(5)), |
| 78 | + Namespace: id, |
| 79 | + } |
| 80 | + kustomization := &kustomizev1.Kustomization{ |
| 81 | + ObjectMeta: metav1.ObjectMeta{ |
| 82 | + Name: kustomizationKey.Name, |
| 83 | + Namespace: kustomizationKey.Namespace, |
| 84 | + }, |
| 85 | + Spec: kustomizev1.KustomizationSpec{ |
| 86 | + Interval: metav1.Duration{Duration: reconciliationInterval}, |
| 87 | + Path: "./", |
| 88 | + KubeConfig: &meta.KubeConfigReference{ |
| 89 | + SecretRef: meta.SecretKeyReference{ |
| 90 | + Name: "kubeconfig", |
| 91 | + }, |
| 92 | + }, |
| 93 | + SourceRef: kustomizev1.CrossNamespaceSourceReference{ |
| 94 | + Name: repositoryName.Name, |
| 95 | + Namespace: repositoryName.Namespace, |
| 96 | + Kind: sourcev1.GitRepositoryKind, |
| 97 | + }, |
| 98 | + HealthChecks: []meta.NamespacedObjectKindReference{ |
| 99 | + { |
| 100 | + APIVersion: "v1", |
| 101 | + Kind: "ConfigMap", |
| 102 | + Name: id, |
| 103 | + Namespace: id, |
| 104 | + }, |
| 105 | + }, |
| 106 | + TargetNamespace: id, |
| 107 | + Force: false, |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + g.Expect(k8sClient.Create(context.Background(), kustomization)).To(Succeed()) |
| 112 | + |
| 113 | + resultK := &kustomizev1.Kustomization{} |
| 114 | + initialConfigMap := &corev1.ConfigMap{} |
| 115 | + badConfigMap := &corev1.ConfigMap{} |
| 116 | + fixedConfigMap := &corev1.ConfigMap{} |
| 117 | + |
| 118 | + t.Run("creates configmap", func(t *testing.T) { |
| 119 | + g.Eventually(func() bool { |
| 120 | + _ = k8sClient.Get(context.Background(), client.ObjectKeyFromObject(kustomization), resultK) |
| 121 | + return resultK.Status.LastAppliedRevision == revision |
| 122 | + }, timeout, time.Second).Should(BeTrue()) |
| 123 | + logStatus(t, resultK) |
| 124 | + |
| 125 | + kstatusCheck.CheckErr(ctx, resultK) |
| 126 | + g.Expect(k8sClient.Get(context.Background(), types.NamespacedName{Name: id, Namespace: id}, initialConfigMap)).Should(Succeed()) |
| 127 | + g.Expect(initialConfigMap.Data).Should(HaveKey("key")) |
| 128 | + }) |
| 129 | + |
| 130 | + t.Run("update configmap with new data", func(t *testing.T) { |
| 131 | + configMap := corev1.ConfigMap{ |
| 132 | + ObjectMeta: metav1.ObjectMeta{ |
| 133 | + Name: id, |
| 134 | + Namespace: id, |
| 135 | + }, |
| 136 | + } |
| 137 | + err = k8sClient.Patch(context.Background(), &configMap, client.RawPatch(types.MergePatchType, []byte(`{"data":{"bad-key":"overridden field manager"}}`)), &client.PatchOptions{FieldManager: overrideManagerName}) |
| 138 | + g.Expect(err).NotTo(HaveOccurred()) |
| 139 | + err = k8sClient.Patch(context.Background(), &configMap, client.RawPatch(types.MergePatchType, []byte(`{"data":{"key2":"not overridden field manager"}}`)), &client.PatchOptions{FieldManager: "good-name"}) |
| 140 | + g.Expect(err).NotTo(HaveOccurred()) |
| 141 | + err = k8sClient.Get(context.Background(), client.ObjectKeyFromObject(initialConfigMap), badConfigMap) |
| 142 | + g.Expect(err).NotTo(HaveOccurred()) |
| 143 | + g.Expect(badConfigMap.Data).Should(HaveKey("bad-key")) |
| 144 | + g.Expect(badConfigMap.Data).Should(HaveKey("key2")) |
| 145 | + }) |
| 146 | + |
| 147 | + t.Run("bad-key should be removed from the configmap", func(t *testing.T) { |
| 148 | + reconciler.Reconcile(context.Background(), ctrl.Request{ |
| 149 | + NamespacedName: kustomizationKey, |
| 150 | + }) |
| 151 | + g.Eventually(func() bool { |
| 152 | + _ = k8sClient.Get(context.Background(), client.ObjectKeyFromObject(initialConfigMap), fixedConfigMap) |
| 153 | + return g.Expect(fixedConfigMap.Data).ShouldNot(HaveKey("bad-key")) && g.Expect(fixedConfigMap.Data).Should(HaveKey("key2")) |
| 154 | + }, timeout, time.Second).Should(BeTrue()) |
| 155 | + }) |
| 156 | +} |
0 commit comments