From 9e1065d002aa2346ed865840c5568db650f8af90 Mon Sep 17 00:00:00 2001 From: pirosiki197 Date: Wed, 4 Feb 2026 00:32:40 +0900 Subject: [PATCH] fix: check if the resource exists when deleting it --- pkg/infrastructure/backend/k8simpl/helper.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/pkg/infrastructure/backend/k8simpl/helper.go b/pkg/infrastructure/backend/k8simpl/helper.go index 2c6a771c7..814e8baf2 100644 --- a/pkg/infrastructure/backend/k8simpl/helper.go +++ b/pkg/infrastructure/backend/k8simpl/helper.go @@ -11,6 +11,7 @@ import ( "github.com/friendsofgo/errors" "github.com/samber/lo" "github.com/sourcegraph/conc/pool" + apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/watch" @@ -129,7 +130,13 @@ func syncResources[T apiResource](ctx context.Context, cluster *discovery.Cluste return nil } -func syncResourcesWithReplace[T apiResource](ctx context.Context, cluster *discovery.Cluster, rcName string, existing []T, next []T, s syncer[T]) error { +func syncResourcesWithReplace[T apiResource]( + ctx context.Context, + cluster *discovery.Cluster, + rcName string, + existing []T, + next []T, + s syncer[T]) error { var patched int var replaced atomic.Int64 replacePool := pool.New().WithErrors().WithContext(ctx).WithMaxGoroutines(10) @@ -239,7 +246,11 @@ func replaceResource[T apiResource]( notifier *deletionNotifier, ) error { ch := notifier.add(rc.GetName()) - _ = s.Delete(ctx, rc.GetName(), metav1.DeleteOptions{PropagationPolicy: lo.ToPtr(metav1.DeletePropagationForeground)}) + err := s.Delete(ctx, rc.GetName(), metav1.DeleteOptions{PropagationPolicy: lo.ToPtr(metav1.DeletePropagationForeground)}) + if apierrors.IsNotFound(err) { + _, err = s.Patch(ctx, rc.GetName(), types.ApplyPatchType, data, metav1.PatchOptions{Force: lo.ToPtr(true), FieldManager: fieldManager}) + return err + } select { case <-ch: // Wait for the resource to be deleted @@ -252,6 +263,6 @@ func replaceResource[T apiResource]( return ctx.Err() } - _, err := s.Patch(ctx, rc.GetName(), types.ApplyPatchType, data, metav1.PatchOptions{Force: lo.ToPtr(true), FieldManager: fieldManager}) + _, err = s.Patch(ctx, rc.GetName(), types.ApplyPatchType, data, metav1.PatchOptions{Force: lo.ToPtr(true), FieldManager: fieldManager}) return err }