|
| 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 | + "fmt" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/fluxcd/pkg/apis/meta" |
| 28 | + "github.com/fluxcd/pkg/testserver" |
| 29 | + sourcev1 "github.com/fluxcd/source-controller/api/v1" |
| 30 | + . "github.com/onsi/gomega" |
| 31 | + "github.com/opencontainers/go-digest" |
| 32 | + apimeta "k8s.io/apimachinery/pkg/api/meta" |
| 33 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 34 | + "k8s.io/apimachinery/pkg/types" |
| 35 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 36 | + |
| 37 | + kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1" |
| 38 | +) |
| 39 | + |
| 40 | +func TestKustomizationReconciler_ExternalArtifact(t *testing.T) { |
| 41 | + g := NewWithT(t) |
| 42 | + id := "ea-" + randStringRunes(5) |
| 43 | + revision := "v1.0.0" |
| 44 | + |
| 45 | + err := createNamespace(id) |
| 46 | + g.Expect(err).NotTo(HaveOccurred(), "failed to create test namespace") |
| 47 | + |
| 48 | + err = createKubeConfigSecret(id) |
| 49 | + g.Expect(err).NotTo(HaveOccurred(), "failed to create kubeconfig secret") |
| 50 | + |
| 51 | + manifests := func(name string, data string) []testserver.File { |
| 52 | + return []testserver.File{ |
| 53 | + { |
| 54 | + Name: "secret.yaml", |
| 55 | + Body: fmt.Sprintf(`--- |
| 56 | +apiVersion: v1 |
| 57 | +kind: Secret |
| 58 | +metadata: |
| 59 | + name: %[1]s |
| 60 | +stringData: |
| 61 | + key: "%[2]s" |
| 62 | +`, name, data), |
| 63 | + }, |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + artifact, err := testServer.ArtifactFromFiles(manifests(id, randStringRunes(5))) |
| 68 | + g.Expect(err).NotTo(HaveOccurred(), "failed to create artifact from files") |
| 69 | + |
| 70 | + eaName := types.NamespacedName{ |
| 71 | + Name: randStringRunes(5), |
| 72 | + Namespace: id, |
| 73 | + } |
| 74 | + |
| 75 | + err = applyExternalArtifact(eaName, artifact, revision) |
| 76 | + g.Expect(err).NotTo(HaveOccurred()) |
| 77 | + |
| 78 | + kustomizationKey := types.NamespacedName{ |
| 79 | + Name: fmt.Sprintf("ea-%s", randStringRunes(5)), |
| 80 | + Namespace: id, |
| 81 | + } |
| 82 | + kustomization := &kustomizev1.Kustomization{ |
| 83 | + ObjectMeta: metav1.ObjectMeta{ |
| 84 | + Name: kustomizationKey.Name, |
| 85 | + Namespace: kustomizationKey.Namespace, |
| 86 | + }, |
| 87 | + Spec: kustomizev1.KustomizationSpec{ |
| 88 | + Interval: metav1.Duration{Duration: time.Hour}, |
| 89 | + Path: "./", |
| 90 | + KubeConfig: &meta.KubeConfigReference{ |
| 91 | + SecretRef: &meta.SecretKeyReference{ |
| 92 | + Name: "kubeconfig", |
| 93 | + }, |
| 94 | + }, |
| 95 | + SourceRef: kustomizev1.CrossNamespaceSourceReference{ |
| 96 | + Name: eaName.Name, |
| 97 | + Namespace: eaName.Namespace, |
| 98 | + Kind: sourcev1.ExternalArtifactKind, |
| 99 | + }, |
| 100 | + TargetNamespace: id, |
| 101 | + Wait: true, |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + g.Expect(k8sClient.Create(context.Background(), kustomization)).To(Succeed()) |
| 106 | + |
| 107 | + resultK := &kustomizev1.Kustomization{} |
| 108 | + readyCondition := &metav1.Condition{} |
| 109 | + |
| 110 | + t.Run("reconciles from external artifact source", func(t *testing.T) { |
| 111 | + g.Eventually(func() bool { |
| 112 | + _ = k8sClient.Get(context.Background(), client.ObjectKeyFromObject(kustomization), resultK) |
| 113 | + readyCondition = apimeta.FindStatusCondition(resultK.Status.Conditions, meta.ReadyCondition) |
| 114 | + return resultK.Status.LastAppliedRevision == revision |
| 115 | + }, timeout, time.Second).Should(BeTrue()) |
| 116 | + |
| 117 | + g.Expect(readyCondition.Reason).To(Equal(meta.ReconciliationSucceededReason)) |
| 118 | + g.Expect(resultK.Status.LastAppliedRevision).To(Equal(revision)) |
| 119 | + |
| 120 | + events := getEvents(resultK.GetName(), map[string]string{"kustomize.toolkit.fluxcd.io/revision": revision}) |
| 121 | + g.Expect(len(events) > 2).To(BeTrue()) |
| 122 | + g.Expect(events[0].Reason).To(BeIdenticalTo(meta.ProgressingReason)) |
| 123 | + g.Expect(events[0].Message).To(ContainSubstring("created")) |
| 124 | + g.Expect(events[1].Reason).To(BeIdenticalTo(meta.ProgressingReason)) |
| 125 | + g.Expect(events[1].Message).To(ContainSubstring("check passed")) |
| 126 | + g.Expect(events[2].Reason).To(BeIdenticalTo(meta.ReconciliationSucceededReason)) |
| 127 | + g.Expect(events[2].Message).To(ContainSubstring("finished")) |
| 128 | + }) |
| 129 | + |
| 130 | + t.Run("watches for external artifact revision change", func(t *testing.T) { |
| 131 | + newRev := "v2.0.0" |
| 132 | + err = applyExternalArtifact(eaName, artifact, newRev) |
| 133 | + g.Expect(err).NotTo(HaveOccurred()) |
| 134 | + |
| 135 | + g.Eventually(func() bool { |
| 136 | + _ = k8sClient.Get(context.Background(), client.ObjectKeyFromObject(kustomization), resultK) |
| 137 | + return resultK.Status.LastAppliedRevision == newRev |
| 138 | + }, timeout, time.Second).Should(BeTrue()) |
| 139 | + |
| 140 | + g.Expect(resultK.Status.History).To(HaveLen(1)) |
| 141 | + g.Expect(resultK.Status.History[0].TotalReconciliations).To(BeEquivalentTo(2)) |
| 142 | + g.Expect(resultK.Status.History[0].LastReconciledStatus).To(Equal(meta.ReconciliationSucceededReason)) |
| 143 | + g.Expect(resultK.Status.History[0].Metadata).To(ContainElements(newRev)) |
| 144 | + }) |
| 145 | +} |
| 146 | + |
| 147 | +func applyExternalArtifact(objKey client.ObjectKey, artifactName string, revision string) error { |
| 148 | + ea := &sourcev1.ExternalArtifact{ |
| 149 | + TypeMeta: metav1.TypeMeta{ |
| 150 | + Kind: sourcev1.ExternalArtifactKind, |
| 151 | + APIVersion: sourcev1.GroupVersion.String(), |
| 152 | + }, |
| 153 | + ObjectMeta: metav1.ObjectMeta{ |
| 154 | + Name: objKey.Name, |
| 155 | + Namespace: objKey.Namespace, |
| 156 | + }, |
| 157 | + } |
| 158 | + |
| 159 | + b, _ := os.ReadFile(filepath.Join(testServer.Root(), artifactName)) |
| 160 | + dig := digest.SHA256.FromBytes(b) |
| 161 | + |
| 162 | + url := fmt.Sprintf("%s/%s", testServer.URL(), artifactName) |
| 163 | + |
| 164 | + status := sourcev1.ExternalArtifactStatus{ |
| 165 | + Conditions: []metav1.Condition{ |
| 166 | + { |
| 167 | + Type: meta.ReadyCondition, |
| 168 | + Status: metav1.ConditionTrue, |
| 169 | + LastTransitionTime: metav1.Now(), |
| 170 | + Reason: meta.SucceededReason, |
| 171 | + }, |
| 172 | + }, |
| 173 | + Artifact: &meta.Artifact{ |
| 174 | + Path: url, |
| 175 | + URL: url, |
| 176 | + Revision: revision, |
| 177 | + Digest: dig.String(), |
| 178 | + LastUpdateTime: metav1.Now(), |
| 179 | + }, |
| 180 | + } |
| 181 | + |
| 182 | + patchOpts := []client.PatchOption{ |
| 183 | + client.ForceOwnership, |
| 184 | + client.FieldOwner("kustomize-controller"), |
| 185 | + } |
| 186 | + |
| 187 | + if err := k8sClient.Patch(context.Background(), ea, client.Apply, patchOpts...); err != nil { |
| 188 | + return err |
| 189 | + } |
| 190 | + |
| 191 | + ea.ManagedFields = nil |
| 192 | + ea.Status = status |
| 193 | + |
| 194 | + statusOpts := &client.SubResourcePatchOptions{ |
| 195 | + PatchOptions: client.PatchOptions{ |
| 196 | + FieldManager: "source-controller", |
| 197 | + }, |
| 198 | + } |
| 199 | + |
| 200 | + if err := k8sClient.Status().Patch(context.Background(), ea, client.Apply, statusOpts); err != nil { |
| 201 | + return err |
| 202 | + } |
| 203 | + return nil |
| 204 | +} |
0 commit comments