|
92 | 92 | Interval: metav1.Duration{Duration: time.Minute},
|
93 | 93 | Path: "./",
|
94 | 94 | KubeConfig: &kustomizev1.KubeConfig{
|
95 |
| - SecretRef: kustomizev1.SecretRef{ |
| 95 | + SecretRef: meta.SecretKeyReference{ |
96 | 96 | Name: "kubeconfig",
|
97 | 97 | },
|
98 | 98 | },
|
@@ -208,3 +208,113 @@ data:
|
208 | 208 | g.Expect(apierrors.IsNotFound(err)).To(BeTrue())
|
209 | 209 | })
|
210 | 210 | }
|
| 211 | + |
| 212 | +func TestKustomizationReconciler_KubeConfig(t *testing.T) { |
| 213 | + g := NewWithT(t) |
| 214 | + id := "kc-" + randStringRunes(5) |
| 215 | + revision := "v1.0.0" |
| 216 | + |
| 217 | + err := createNamespace(id) |
| 218 | + g.Expect(err).NotTo(HaveOccurred(), "failed to create test namespace") |
| 219 | + |
| 220 | + manifests := func(name string, data string) []testserver.File { |
| 221 | + return []testserver.File{ |
| 222 | + { |
| 223 | + Name: "config.yaml", |
| 224 | + Body: fmt.Sprintf(`--- |
| 225 | +apiVersion: v1 |
| 226 | +kind: ConfigMap |
| 227 | +metadata: |
| 228 | + name: %[1]s |
| 229 | +data: |
| 230 | + key: "%[2]s" |
| 231 | +`, name, data), |
| 232 | + }, |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + artifact, err := testServer.ArtifactFromFiles(manifests(id, randStringRunes(5))) |
| 237 | + g.Expect(err).NotTo(HaveOccurred(), "failed to create artifact from files") |
| 238 | + |
| 239 | + repositoryName := types.NamespacedName{ |
| 240 | + Name: randStringRunes(5), |
| 241 | + Namespace: id, |
| 242 | + } |
| 243 | + |
| 244 | + err = applyGitRepository(repositoryName, artifact, revision) |
| 245 | + g.Expect(err).NotTo(HaveOccurred()) |
| 246 | + |
| 247 | + const ( |
| 248 | + secretName = "user-defined-name" |
| 249 | + secretKey = "user-defined-key" |
| 250 | + ) |
| 251 | + kustomizationKey := types.NamespacedName{ |
| 252 | + Name: randStringRunes(5), |
| 253 | + Namespace: id, |
| 254 | + } |
| 255 | + kustomization := &kustomizev1.Kustomization{ |
| 256 | + ObjectMeta: metav1.ObjectMeta{ |
| 257 | + Name: kustomizationKey.Name, |
| 258 | + Namespace: kustomizationKey.Namespace, |
| 259 | + }, |
| 260 | + Spec: kustomizev1.KustomizationSpec{ |
| 261 | + Interval: metav1.Duration{Duration: time.Minute}, |
| 262 | + Path: "./", |
| 263 | + KubeConfig: &kustomizev1.KubeConfig{ |
| 264 | + SecretRef: meta.SecretKeyReference{ |
| 265 | + Name: secretName, |
| 266 | + Key: secretKey, |
| 267 | + }, |
| 268 | + }, |
| 269 | + SourceRef: kustomizev1.CrossNamespaceSourceReference{ |
| 270 | + Name: repositoryName.Name, |
| 271 | + Namespace: repositoryName.Namespace, |
| 272 | + Kind: sourcev1.GitRepositoryKind, |
| 273 | + }, |
| 274 | + TargetNamespace: id, |
| 275 | + Prune: true, |
| 276 | + }, |
| 277 | + } |
| 278 | + |
| 279 | + g.Expect(k8sClient.Create(context.Background(), kustomization)).To(Succeed()) |
| 280 | + |
| 281 | + resultK := &kustomizev1.Kustomization{} |
| 282 | + readyCondition := &metav1.Condition{} |
| 283 | + |
| 284 | + t.Run("fails to reconcile with missing kubeconfig secret", func(t *testing.T) { |
| 285 | + g.Eventually(func() bool { |
| 286 | + _ = k8sClient.Get(context.Background(), client.ObjectKeyFromObject(kustomization), resultK) |
| 287 | + readyCondition = apimeta.FindStatusCondition(resultK.Status.Conditions, meta.ReadyCondition) |
| 288 | + return apimeta.IsStatusConditionFalse(resultK.Status.Conditions, meta.ReadyCondition) |
| 289 | + }, timeout, time.Second).Should(BeTrue()) |
| 290 | + |
| 291 | + g.Expect(readyCondition.Reason).To(Equal(kustomizev1.ReconciliationFailedReason)) |
| 292 | + g.Expect(readyCondition.Message).To(ContainSubstring(`Secret "%s" not found`, secretName)) |
| 293 | + }) |
| 294 | + |
| 295 | + secret := &corev1.Secret{ |
| 296 | + ObjectMeta: metav1.ObjectMeta{ |
| 297 | + Name: secretName, |
| 298 | + Namespace: id, |
| 299 | + }, |
| 300 | + Data: map[string][]byte{ |
| 301 | + secretKey: kubeConfig, |
| 302 | + }, |
| 303 | + } |
| 304 | + g.Expect(k8sClient.Create(context.Background(), secret)).NotTo(HaveOccurred(), "failed to create kubeconfig secret") |
| 305 | + |
| 306 | + t.Run("reconciles successfully after secret is created", func(t *testing.T) { |
| 307 | + revision = "v2.0.0" |
| 308 | + err = applyGitRepository(repositoryName, artifact, revision) |
| 309 | + g.Expect(err).NotTo(HaveOccurred()) |
| 310 | + |
| 311 | + g.Eventually(func() bool { |
| 312 | + _ = k8sClient.Get(context.Background(), client.ObjectKeyFromObject(kustomization), resultK) |
| 313 | + readyCondition = apimeta.FindStatusCondition(resultK.Status.Conditions, meta.ReadyCondition) |
| 314 | + return resultK.Status.LastAppliedRevision == revision |
| 315 | + }, timeout, time.Second).Should(BeTrue()) |
| 316 | + |
| 317 | + g.Expect(readyCondition.Reason).To(Equal(kustomizev1.ReconciliationSucceededReason)) |
| 318 | + }) |
| 319 | + |
| 320 | +} |
0 commit comments