Skip to content

Commit 1d6a5b2

Browse files
Do not reconcile images and builds being deleted When an image is being deleted, k8s does not delete the object immediately, instead it sets the deletion timestamp awaiting finalization. See [k8s docs](https://github.com/kubernetes/apimachinery/blob/45d29dc4d66fc2ac83e736e79752ad81a9c6195f/pkg/apis/meta/v1/types.go#L190-L209) for details If the kpack image (or its builds) has a finalizer being added by an external component (such as [Korifi](https://github.com/cloudfoundry/korifi/blob/17557eb68fed830f3f57abd651882a712fc25f5f/kpack-image-builder/controllers/webhooks/finalizer/finalizer_webhook.go#L25)), then when the client request the object to be deleted, the image/build reconcilers keep reconciling the image/build causing new build pods to be created as the image is being deleted. Even though eventually the image gets deleted, it takes significant amount of time. Signed-off-by: Danail Branekov <danailster@gmail.com> Co-authored-by: Georgi Sabev <georgethebeatle@gmail.com>
1 parent 7de12b1 commit 1d6a5b2

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

pkg/reconciler/build/build.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ func (c *Reconciler) Reconcile(ctx context.Context, key string) error {
136136
return err
137137
}
138138

139+
if build.DeletionTimestamp != nil {
140+
return nil
141+
}
142+
139143
build = build.DeepCopy()
140144
build.SetDefaults(ctx)
141145

pkg/reconciler/build/build_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,23 @@ func testBuildReconciler(t *testing.T, when spec.G, it spec.S) {
258258
})
259259
})
260260

261+
it("does not reconcile a build being deleted", func() {
262+
bld.Status.ObservedGeneration = 1
263+
264+
bld.Generation = 2
265+
deletionTimetamp := metav1.NewTime(time.Now())
266+
bld.DeletionTimestamp = &deletionTimetamp
267+
268+
rt.Test(rtesting.TableRow{
269+
Key: key,
270+
Objects: []runtime.Object{
271+
bld,
272+
},
273+
WantErr: false,
274+
WantStatusUpdates: []clientgotesting.UpdateActionImpl{},
275+
})
276+
})
277+
261278
it("does not update status if there is no update", func() {
262279
buildPod, err := podGenerator.Generate(ctx, bld)
263280
require.NoError(t, err)

pkg/reconciler/image/image.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ func (c *Reconciler) Reconcile(ctx context.Context, key string) error {
121121
return err
122122
}
123123

124+
if image.DeletionTimestamp != nil {
125+
return nil
126+
}
127+
124128
image = image.DeepCopy()
125129
image.SetDefaults(ctx)
126130

pkg/reconciler/image/image_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,27 @@ imageWithBuilder := &buildapi.Image{
252252
})
253253
})
254254

255+
it("does not reconcile images being deleted", func() {
256+
const observedGeneration int64 = 1
257+
imageWithBuilder.Status.ObservedGeneration = observedGeneration
258+
259+
imageWithBuilder.Generation = 2
260+
deletionTimetamp := metav1.NewTime(time.Now())
261+
imageWithBuilder.DeletionTimestamp = &deletionTimetamp
262+
263+
rt.Test(rtesting.TableRow{
264+
Key: key,
265+
Objects: []runtime.Object{
266+
imageWithBuilder,
267+
builder,
268+
clusterBuilder,
269+
unresolvedSourceResolver(imageWithBuilder),
270+
},
271+
WantErr: false,
272+
WantStatusUpdates: []clientgotesting.UpdateActionImpl{},
273+
})
274+
})
275+
255276
it("does not update status if there is no status update", func() {
256277
rt.Test(rtesting.TableRow{
257278
Key: key,

0 commit comments

Comments
 (0)