|
| 1 | +/* |
| 2 | +Copyright 2025 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 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/fluxcd/pkg/apis/kustomize" |
| 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 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 31 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 32 | + "k8s.io/apimachinery/pkg/types" |
| 33 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 34 | + |
| 35 | + kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1" |
| 36 | +) |
| 37 | + |
| 38 | +// TestKustomizationReconciler_MultiplePatchDelete tests the handling of multiple |
| 39 | +// $patch: delete directives in strategic merge patches. |
| 40 | +// This test ensures that the controller properly handles scenarios where multiple |
| 41 | +// resources are deleted using a single patch specification. |
| 42 | +func TestKustomizationReconciler_MultiplePatchDelete(t *testing.T) { |
| 43 | + g := NewWithT(t) |
| 44 | + id := "multi-patch-delete-" + randStringRunes(5) |
| 45 | + revision := "v1.0.0" |
| 46 | + |
| 47 | + err := createNamespace(id) |
| 48 | + g.Expect(err).NotTo(HaveOccurred(), "failed to create test namespace") |
| 49 | + |
| 50 | + err = createKubeConfigSecret(id) |
| 51 | + g.Expect(err).NotTo(HaveOccurred(), "failed to create kubeconfig secret") |
| 52 | + |
| 53 | + // Create test files with multiple ConfigMaps |
| 54 | + manifests := func(name string, data string) []testserver.File { |
| 55 | + return []testserver.File{ |
| 56 | + { |
| 57 | + Name: "configmaps.yaml", |
| 58 | + Body: `--- |
| 59 | +apiVersion: v1 |
| 60 | +kind: ConfigMap |
| 61 | +metadata: |
| 62 | + name: cm1 |
| 63 | + namespace: ` + name + ` |
| 64 | +data: |
| 65 | + key: ` + data + `1 |
| 66 | +--- |
| 67 | +apiVersion: v1 |
| 68 | +kind: ConfigMap |
| 69 | +metadata: |
| 70 | + name: cm2 |
| 71 | + namespace: ` + name + ` |
| 72 | +data: |
| 73 | + key: ` + data + `2 |
| 74 | +--- |
| 75 | +apiVersion: v1 |
| 76 | +kind: ConfigMap |
| 77 | +metadata: |
| 78 | + name: cm3 |
| 79 | + namespace: ` + name + ` |
| 80 | +data: |
| 81 | + key: ` + data + `3 |
| 82 | +`, |
| 83 | + }, |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + artifact, err := testServer.ArtifactFromFiles(manifests(id, randStringRunes(5))) |
| 88 | + g.Expect(err).NotTo(HaveOccurred()) |
| 89 | + |
| 90 | + repositoryName := types.NamespacedName{ |
| 91 | + Name: randStringRunes(5), |
| 92 | + Namespace: id, |
| 93 | + } |
| 94 | + |
| 95 | + err = applyGitRepository(repositoryName, artifact, revision) |
| 96 | + g.Expect(err).NotTo(HaveOccurred()) |
| 97 | + |
| 98 | + kustomizationKey := types.NamespacedName{ |
| 99 | + Name: "patch-delete-" + randStringRunes(5), |
| 100 | + Namespace: id, |
| 101 | + } |
| 102 | + |
| 103 | + t.Run("multiple patch delete in single patch should work", func(t *testing.T) { |
| 104 | + // This test verifies that multiple $patch: delete directives in a single patch work correctly |
| 105 | + // Ref: https://github.com/fluxcd/kustomize-controller/issues/1306 |
| 106 | + kustomization := &kustomizev1.Kustomization{ |
| 107 | + ObjectMeta: metav1.ObjectMeta{ |
| 108 | + Name: kustomizationKey.Name, |
| 109 | + Namespace: kustomizationKey.Namespace, |
| 110 | + }, |
| 111 | + Spec: kustomizev1.KustomizationSpec{ |
| 112 | + Interval: metav1.Duration{Duration: reconciliationInterval}, |
| 113 | + Path: "./", |
| 114 | + KubeConfig: &meta.KubeConfigReference{ |
| 115 | + SecretRef: meta.SecretKeyReference{ |
| 116 | + Name: "kubeconfig", |
| 117 | + }, |
| 118 | + }, |
| 119 | + SourceRef: kustomizev1.CrossNamespaceSourceReference{ |
| 120 | + Name: repositoryName.Name, |
| 121 | + Namespace: repositoryName.Namespace, |
| 122 | + Kind: sourcev1.GitRepositoryKind, |
| 123 | + }, |
| 124 | + Prune: true, |
| 125 | + Patches: []kustomize.Patch{ |
| 126 | + { |
| 127 | + // Multiple $patch: delete in a single patch |
| 128 | + Patch: `$patch: delete |
| 129 | +apiVersion: v1 |
| 130 | +kind: ConfigMap |
| 131 | +metadata: |
| 132 | + name: cm1 |
| 133 | + namespace: ` + id + ` |
| 134 | +--- |
| 135 | +$patch: delete |
| 136 | +apiVersion: v1 |
| 137 | +kind: ConfigMap |
| 138 | +metadata: |
| 139 | + name: cm2 |
| 140 | + namespace: ` + id + ``, |
| 141 | + }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + } |
| 145 | + |
| 146 | + g.Expect(k8sClient.Create(context.Background(), kustomization)).To(Succeed()) |
| 147 | + |
| 148 | + // Wait for reconciliation and check that it succeeds without panic |
| 149 | + g.Eventually(func() bool { |
| 150 | + var obj kustomizev1.Kustomization |
| 151 | + _ = k8sClient.Get(context.Background(), client.ObjectKeyFromObject(kustomization), &obj) |
| 152 | + return obj.Status.LastAppliedRevision == revision |
| 153 | + }, timeout, time.Second).Should(BeTrue()) |
| 154 | + |
| 155 | + // Verify that only cm3 ConfigMap exists (cm1 and cm2 should be deleted) |
| 156 | + var cm corev1.ConfigMap |
| 157 | + err := k8sClient.Get(context.Background(), client.ObjectKey{Name: "cm1", Namespace: id}, &cm) |
| 158 | + g.Expect(err).To(HaveOccurred(), "cm1 should have been deleted") |
| 159 | + |
| 160 | + err = k8sClient.Get(context.Background(), client.ObjectKey{Name: "cm2", Namespace: id}, &cm) |
| 161 | + g.Expect(err).To(HaveOccurred(), "cm2 should have been deleted") |
| 162 | + |
| 163 | + err = k8sClient.Get(context.Background(), client.ObjectKey{Name: "cm3", Namespace: id}, &cm) |
| 164 | + g.Expect(err).NotTo(HaveOccurred(), "cm3 should still exist") |
| 165 | + |
| 166 | + // Cleanup |
| 167 | + g.Expect(k8sClient.Delete(context.Background(), kustomization)).To(Succeed()) |
| 168 | + g.Eventually(func() bool { |
| 169 | + err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(kustomization), kustomization) |
| 170 | + return apierrors.IsNotFound(err) |
| 171 | + }, timeout, time.Second).Should(BeTrue()) |
| 172 | + }) |
| 173 | + |
| 174 | + t.Run("multiple patch delete in separate patches should work", func(t *testing.T) { |
| 175 | + // This test verifies that separate patches (which was previously a workaround) still work correctly |
| 176 | + kustomizationSeparate := &kustomizev1.Kustomization{ |
| 177 | + ObjectMeta: metav1.ObjectMeta{ |
| 178 | + Name: kustomizationKey.Name + "-separate", |
| 179 | + Namespace: kustomizationKey.Namespace, |
| 180 | + }, |
| 181 | + Spec: kustomizev1.KustomizationSpec{ |
| 182 | + Interval: metav1.Duration{Duration: reconciliationInterval}, |
| 183 | + Path: "./", |
| 184 | + KubeConfig: &meta.KubeConfigReference{ |
| 185 | + SecretRef: meta.SecretKeyReference{ |
| 186 | + Name: "kubeconfig", |
| 187 | + }, |
| 188 | + }, |
| 189 | + SourceRef: kustomizev1.CrossNamespaceSourceReference{ |
| 190 | + Name: repositoryName.Name, |
| 191 | + Namespace: repositoryName.Namespace, |
| 192 | + Kind: sourcev1.GitRepositoryKind, |
| 193 | + }, |
| 194 | + Prune: true, |
| 195 | + Patches: []kustomize.Patch{ |
| 196 | + { |
| 197 | + Patch: `$patch: delete |
| 198 | +apiVersion: v1 |
| 199 | +kind: ConfigMap |
| 200 | +metadata: |
| 201 | + name: cm1 |
| 202 | + namespace: ` + id + ``, |
| 203 | + }, |
| 204 | + { |
| 205 | + Patch: `$patch: delete |
| 206 | +apiVersion: v1 |
| 207 | +kind: ConfigMap |
| 208 | +metadata: |
| 209 | + name: cm2 |
| 210 | + namespace: ` + id + ``, |
| 211 | + }, |
| 212 | + }, |
| 213 | + }, |
| 214 | + } |
| 215 | + |
| 216 | + g.Expect(k8sClient.Create(context.Background(), kustomizationSeparate)).To(Succeed()) |
| 217 | + |
| 218 | + // Wait for successful reconciliation |
| 219 | + g.Eventually(func() bool { |
| 220 | + var obj kustomizev1.Kustomization |
| 221 | + _ = k8sClient.Get(context.Background(), client.ObjectKeyFromObject(kustomizationSeparate), &obj) |
| 222 | + return obj.Status.LastAppliedRevision == revision |
| 223 | + }, timeout, time.Second).Should(BeTrue()) |
| 224 | + |
| 225 | + // Verify that only cm3 ConfigMap exists |
| 226 | + var cm corev1.ConfigMap |
| 227 | + err := k8sClient.Get(context.Background(), client.ObjectKey{Name: "cm1", Namespace: id}, &cm) |
| 228 | + g.Expect(err).To(HaveOccurred(), "cm1 should have been deleted") |
| 229 | + |
| 230 | + err = k8sClient.Get(context.Background(), client.ObjectKey{Name: "cm2", Namespace: id}, &cm) |
| 231 | + g.Expect(err).To(HaveOccurred(), "cm2 should have been deleted") |
| 232 | + |
| 233 | + err = k8sClient.Get(context.Background(), client.ObjectKey{Name: "cm3", Namespace: id}, &cm) |
| 234 | + g.Expect(err).NotTo(HaveOccurred(), "cm3 should still exist") |
| 235 | + |
| 236 | + // Cleanup |
| 237 | + g.Expect(k8sClient.Delete(context.Background(), kustomizationSeparate)).To(Succeed()) |
| 238 | + g.Eventually(func() bool { |
| 239 | + err := k8sClient.Get(context.Background(), client.ObjectKeyFromObject(kustomizationSeparate), kustomizationSeparate) |
| 240 | + return apierrors.IsNotFound(err) |
| 241 | + }, timeout, time.Second).Should(BeTrue()) |
| 242 | + }) |
| 243 | +} |
0 commit comments