Skip to content

Commit

Permalink
Add GC owner reference tests
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
  • Loading branch information
stefanprodan committed Aug 19, 2021
1 parent a49bbf9 commit 59376db
Showing 1 changed file with 158 additions and 1 deletion.
159 changes: 158 additions & 1 deletion controllers/kustomization_controller_gc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ var _ = Describe("KustomizationReconciler", func() {
Expect(k8sClient.Delete(context.Background(), namespace)).To(Succeed())
})

It("garbage collects deleted manifests", func() {
It("collects deleted manifests", func() {
configMapManifest := func(name string) string {
return fmt.Sprintf(`---
apiVersion: v1
Expand Down Expand Up @@ -176,6 +176,163 @@ data:
Expect(apierrors.IsNotFound(err)).To(BeTrue())
})

It("skips objects with blockOwnerDeletion=true", func() {
configMapManifest := func(name string) string {
return fmt.Sprintf(`---
apiVersion: v1
kind: ConfigMap
metadata:
name: %[1]s
data:
value: %[1]s
`, name)
}
manifest := testserver.File{Name: "configmap.yaml", Body: configMapManifest("first")}
artifact, err := artifactServer.ArtifactFromFiles([]testserver.File{manifest})
Expect(err).ToNot(HaveOccurred())
artifactURL, err := artifactServer.URLForFile(artifact)
Expect(err).ToNot(HaveOccurred())

gitRepo.Status.Artifact.URL = artifactURL
gitRepo.Status.Artifact.Revision = "first"

Expect(k8sClient.Create(context.Background(), gitRepo)).To(Succeed())
Expect(k8sClient.Status().Update(context.Background(), gitRepo)).To(Succeed())
Expect(k8sClient.Create(context.Background(), kustomization)).To(Succeed())

var got kustomizev1.Kustomization
Eventually(func() bool {
_ = k8sClient.Get(context.Background(), client.ObjectKeyFromObject(kustomization), &got)
c := apimeta.FindStatusCondition(got.Status.Conditions, meta.ReadyCondition)
return c != nil && c.Reason == meta.ReconciliationSucceededReason
}, timeout, time.Second).Should(BeTrue())

var configMap corev1.ConfigMap
Expect(k8sClient.Get(context.Background(), client.ObjectKey{Name: "first", Namespace: namespace.Name}, &configMap)).To(Succeed())

owner := &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: namespace.Name,
},
}
Expect(k8sClient.Create(context.Background(), owner)).To(Succeed())

sa := &corev1.ServiceAccount{}
objName := types.NamespacedName{Name: "test", Namespace: namespace.Name}
Expect(k8sClient.Get(context.Background(), objName, sa)).To(Succeed())

blockOwnerDeletion := true
owned := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: namespace.Name,
Labels: configMap.GetLabels(),
Annotations: configMap.GetAnnotations(),
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "v1",
Kind: "ServiceAccount",
Name: sa.Name,
UID: sa.UID,
Controller: &blockOwnerDeletion,
BlockOwnerDeletion: &blockOwnerDeletion,
},
},
},
}
Expect(k8sClient.Create(context.Background(), owned)).To(Succeed())

Expect(k8sClient.Delete(context.Background(), kustomization)).To(Succeed())
Eventually(func() bool {
err = k8sClient.Get(context.Background(), client.ObjectKey{Name: kustomization.Name, Namespace: namespace.Name}, kustomization)
return apierrors.IsNotFound(err)
}, timeout, time.Second).Should(BeTrue())

cf := &corev1.ConfigMap{}
Expect(k8sClient.Get(context.Background(), objName, cf)).To(Succeed())
})

It("deletes objects with blockOwnerDeletion=false", func() {
configMapManifest := func(name string) string {
return fmt.Sprintf(`---
apiVersion: v1
kind: ConfigMap
metadata:
name: %[1]s
data:
value: %[1]s
`, name)
}
manifest := testserver.File{Name: "configmap.yaml", Body: configMapManifest("first")}
artifact, err := artifactServer.ArtifactFromFiles([]testserver.File{manifest})
Expect(err).ToNot(HaveOccurred())
artifactURL, err := artifactServer.URLForFile(artifact)
Expect(err).ToNot(HaveOccurred())

gitRepo.Status.Artifact.URL = artifactURL
gitRepo.Status.Artifact.Revision = "first"

Expect(k8sClient.Create(context.Background(), gitRepo)).To(Succeed())
Expect(k8sClient.Status().Update(context.Background(), gitRepo)).To(Succeed())
Expect(k8sClient.Create(context.Background(), kustomization)).To(Succeed())

var got kustomizev1.Kustomization
Eventually(func() bool {
_ = k8sClient.Get(context.Background(), client.ObjectKeyFromObject(kustomization), &got)
c := apimeta.FindStatusCondition(got.Status.Conditions, meta.ReadyCondition)
return c != nil && c.Reason == meta.ReconciliationSucceededReason
}, timeout, time.Second).Should(BeTrue())

var configMap corev1.ConfigMap
Expect(k8sClient.Get(context.Background(), client.ObjectKey{Name: "first", Namespace: namespace.Name}, &configMap)).To(Succeed())

owner := &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: namespace.Name,
},
}
Expect(k8sClient.Create(context.Background(), owner)).To(Succeed())

sa := &corev1.ServiceAccount{}
objName := types.NamespacedName{Name: "test", Namespace: namespace.Name}
Expect(k8sClient.Get(context.Background(), objName, sa)).To(Succeed())

blockOwnerDeletion := false
owned := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: namespace.Name,
Labels: configMap.GetLabels(),
Annotations: configMap.GetAnnotations(),
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "v1",
Kind: "ServiceAccount",
Name: sa.Name,
UID: sa.UID,
Controller: &blockOwnerDeletion,
BlockOwnerDeletion: &blockOwnerDeletion,
},
},
},
}
Expect(k8sClient.Create(context.Background(), owned)).To(Succeed())

Expect(k8sClient.Delete(context.Background(), kustomization)).To(Succeed())
Eventually(func() bool {
err = k8sClient.Get(context.Background(), client.ObjectKey{Name: kustomization.Name, Namespace: namespace.Name}, kustomization)
return apierrors.IsNotFound(err)
}, timeout, time.Second).Should(BeTrue())

cf := &corev1.ConfigMap{}
err = k8sClient.Get(context.Background(), objName, cf)
Expect(apierrors.IsNotFound(err)).To(BeTrue())
})

It("skips deleted manifests labeled with prune disabled", func() {
configMapManifest := func(name string, skip string) string {
return fmt.Sprintf(`---
Expand Down

0 comments on commit 59376db

Please sign in to comment.