@@ -112,7 +112,7 @@ var _ = Describe("KustomizationReconciler", func() {
112112 Expect (k8sClient .Delete (context .Background (), namespace )).To (Succeed ())
113113 })
114114
115- It ("garbage collects deleted manifests" , func () {
115+ It ("collects deleted manifests" , func () {
116116 configMapManifest := func (name string ) string {
117117 return fmt .Sprintf (`---
118118apiVersion: v1
@@ -176,6 +176,163 @@ data:
176176 Expect (apierrors .IsNotFound (err )).To (BeTrue ())
177177 })
178178
179+ It ("skips objects with blockOwnerDeletion=true" , func () {
180+ configMapManifest := func (name string ) string {
181+ return fmt .Sprintf (`---
182+ apiVersion: v1
183+ kind: ConfigMap
184+ metadata:
185+ name: %[1]s
186+ data:
187+ value: %[1]s
188+ ` , name )
189+ }
190+ manifest := testserver.File {Name : "configmap.yaml" , Body : configMapManifest ("first" )}
191+ artifact , err := artifactServer .ArtifactFromFiles ([]testserver.File {manifest })
192+ Expect (err ).ToNot (HaveOccurred ())
193+ artifactURL , err := artifactServer .URLForFile (artifact )
194+ Expect (err ).ToNot (HaveOccurred ())
195+
196+ gitRepo .Status .Artifact .URL = artifactURL
197+ gitRepo .Status .Artifact .Revision = "first"
198+
199+ Expect (k8sClient .Create (context .Background (), gitRepo )).To (Succeed ())
200+ Expect (k8sClient .Status ().Update (context .Background (), gitRepo )).To (Succeed ())
201+ Expect (k8sClient .Create (context .Background (), kustomization )).To (Succeed ())
202+
203+ var got kustomizev1.Kustomization
204+ Eventually (func () bool {
205+ _ = k8sClient .Get (context .Background (), client .ObjectKeyFromObject (kustomization ), & got )
206+ c := apimeta .FindStatusCondition (got .Status .Conditions , meta .ReadyCondition )
207+ return c != nil && c .Reason == meta .ReconciliationSucceededReason
208+ }, timeout , time .Second ).Should (BeTrue ())
209+
210+ var configMap corev1.ConfigMap
211+ Expect (k8sClient .Get (context .Background (), client.ObjectKey {Name : "first" , Namespace : namespace .Name }, & configMap )).To (Succeed ())
212+
213+ owner := & corev1.ServiceAccount {
214+ ObjectMeta : metav1.ObjectMeta {
215+ Name : "test" ,
216+ Namespace : namespace .Name ,
217+ },
218+ }
219+ Expect (k8sClient .Create (context .Background (), owner )).To (Succeed ())
220+
221+ sa := & corev1.ServiceAccount {}
222+ objName := types.NamespacedName {Name : "test" , Namespace : namespace .Name }
223+ Expect (k8sClient .Get (context .Background (), objName , sa )).To (Succeed ())
224+
225+ blockOwnerDeletion := true
226+ owned := & corev1.ConfigMap {
227+ TypeMeta : metav1.TypeMeta {},
228+ ObjectMeta : metav1.ObjectMeta {
229+ Name : "test" ,
230+ Namespace : namespace .Name ,
231+ Labels : configMap .GetLabels (),
232+ Annotations : configMap .GetAnnotations (),
233+ OwnerReferences : []metav1.OwnerReference {
234+ {
235+ APIVersion : "v1" ,
236+ Kind : "ServiceAccount" ,
237+ Name : sa .Name ,
238+ UID : sa .UID ,
239+ Controller : & blockOwnerDeletion ,
240+ BlockOwnerDeletion : & blockOwnerDeletion ,
241+ },
242+ },
243+ },
244+ }
245+ Expect (k8sClient .Create (context .Background (), owned )).To (Succeed ())
246+
247+ Expect (k8sClient .Delete (context .Background (), kustomization )).To (Succeed ())
248+ Eventually (func () bool {
249+ err = k8sClient .Get (context .Background (), client.ObjectKey {Name : kustomization .Name , Namespace : namespace .Name }, kustomization )
250+ return apierrors .IsNotFound (err )
251+ }, timeout , time .Second ).Should (BeTrue ())
252+
253+ cf := & corev1.ConfigMap {}
254+ Expect (k8sClient .Get (context .Background (), objName , cf )).To (Succeed ())
255+ })
256+
257+ It ("deletes objects with blockOwnerDeletion=false" , func () {
258+ configMapManifest := func (name string ) string {
259+ return fmt .Sprintf (`---
260+ apiVersion: v1
261+ kind: ConfigMap
262+ metadata:
263+ name: %[1]s
264+ data:
265+ value: %[1]s
266+ ` , name )
267+ }
268+ manifest := testserver.File {Name : "configmap.yaml" , Body : configMapManifest ("first" )}
269+ artifact , err := artifactServer .ArtifactFromFiles ([]testserver.File {manifest })
270+ Expect (err ).ToNot (HaveOccurred ())
271+ artifactURL , err := artifactServer .URLForFile (artifact )
272+ Expect (err ).ToNot (HaveOccurred ())
273+
274+ gitRepo .Status .Artifact .URL = artifactURL
275+ gitRepo .Status .Artifact .Revision = "first"
276+
277+ Expect (k8sClient .Create (context .Background (), gitRepo )).To (Succeed ())
278+ Expect (k8sClient .Status ().Update (context .Background (), gitRepo )).To (Succeed ())
279+ Expect (k8sClient .Create (context .Background (), kustomization )).To (Succeed ())
280+
281+ var got kustomizev1.Kustomization
282+ Eventually (func () bool {
283+ _ = k8sClient .Get (context .Background (), client .ObjectKeyFromObject (kustomization ), & got )
284+ c := apimeta .FindStatusCondition (got .Status .Conditions , meta .ReadyCondition )
285+ return c != nil && c .Reason == meta .ReconciliationSucceededReason
286+ }, timeout , time .Second ).Should (BeTrue ())
287+
288+ var configMap corev1.ConfigMap
289+ Expect (k8sClient .Get (context .Background (), client.ObjectKey {Name : "first" , Namespace : namespace .Name }, & configMap )).To (Succeed ())
290+
291+ owner := & corev1.ServiceAccount {
292+ ObjectMeta : metav1.ObjectMeta {
293+ Name : "test" ,
294+ Namespace : namespace .Name ,
295+ },
296+ }
297+ Expect (k8sClient .Create (context .Background (), owner )).To (Succeed ())
298+
299+ sa := & corev1.ServiceAccount {}
300+ objName := types.NamespacedName {Name : "test" , Namespace : namespace .Name }
301+ Expect (k8sClient .Get (context .Background (), objName , sa )).To (Succeed ())
302+
303+ blockOwnerDeletion := false
304+ owned := & corev1.ConfigMap {
305+ TypeMeta : metav1.TypeMeta {},
306+ ObjectMeta : metav1.ObjectMeta {
307+ Name : "test" ,
308+ Namespace : namespace .Name ,
309+ Labels : configMap .GetLabels (),
310+ Annotations : configMap .GetAnnotations (),
311+ OwnerReferences : []metav1.OwnerReference {
312+ {
313+ APIVersion : "v1" ,
314+ Kind : "ServiceAccount" ,
315+ Name : sa .Name ,
316+ UID : sa .UID ,
317+ Controller : & blockOwnerDeletion ,
318+ BlockOwnerDeletion : & blockOwnerDeletion ,
319+ },
320+ },
321+ },
322+ }
323+ Expect (k8sClient .Create (context .Background (), owned )).To (Succeed ())
324+
325+ Expect (k8sClient .Delete (context .Background (), kustomization )).To (Succeed ())
326+ Eventually (func () bool {
327+ err = k8sClient .Get (context .Background (), client.ObjectKey {Name : kustomization .Name , Namespace : namespace .Name }, kustomization )
328+ return apierrors .IsNotFound (err )
329+ }, timeout , time .Second ).Should (BeTrue ())
330+
331+ cf := & corev1.ConfigMap {}
332+ err = k8sClient .Get (context .Background (), objName , cf )
333+ Expect (apierrors .IsNotFound (err )).To (BeTrue ())
334+ })
335+
179336 It ("skips deleted manifests labeled with prune disabled" , func () {
180337 configMapManifest := func (name string , skip string ) string {
181338 return fmt .Sprintf (`---
0 commit comments