diff --git a/controllers/apps/component/transformer_component_deletion.go b/controllers/apps/component/transformer_component_deletion.go index d2978c81ae8..af1191cb35c 100644 --- a/controllers/apps/component/transformer_component_deletion.go +++ b/controllers/apps/component/transformer_component_deletion.go @@ -23,8 +23,10 @@ import ( "fmt" "time" + "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" + "k8s.io/apimachinery/pkg/types" "k8s.io/klog/v2" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/apiutil" @@ -140,6 +142,9 @@ func (t *componentDeletionTransformer) deleteCompResources(transCtx *componentTr graphCli.Status(dag, comp, transCtx.Component) return intctrlutil.NewRequeueError(time.Second*1, "not all component sub-resources deleted") } else { + if err = notifyDependents4CompDeletion(transCtx, dag); err != nil { + return intctrlutil.NewRequeueError(appsutil.RequeueDuration, fmt.Sprintf("notify dependent components error: %s", err.Error())) + } graphCli.Delete(dag, comp) } @@ -211,6 +216,33 @@ func handleRBACResourceDeletion(obj client.Object, transCtx *componentTransformC } } +func notifyDependents4CompDeletion(transCtx *componentTransformContext, dag *graph.DAG) error { + var ( + ctx = transCtx.Context + cli = transCtx.Client + comp = transCtx.Component + ) + + compDef := &appsv1.ComponentDefinition{} + if err := transCtx.Client.Get(transCtx.Context, types.NamespacedName{Name: comp.Spec.CompDef}, compDef); err != nil { + return errors.Wrap(err, "failed to get the component definition for dependents notifier at deletion") + } + + synthesizedComp, err := component.BuildSynthesizedComponent(ctx, cli, compDef, comp) + if err != nil { + return errors.Wrap(err, "failed to build synthesized component for dependents notifier at deletion") + } + + bak := transCtx.SynthesizeComponent + defer func() { + transCtx.SynthesizeComponent = bak + }() + + transCtx.SynthesizeComponent = synthesizedComp + transformer := &componentNotifierTransformer{} + return transformer.Transform(transCtx, dag) +} + func compOwnedWorkloadKinds() []client.ObjectList { return []client.ObjectList{ &workloads.InstanceSetList{}, diff --git a/controllers/apps/component/transformer_component_notifier.go b/controllers/apps/component/transformer_component_notifier.go index c97df141ff7..b918b418dc6 100644 --- a/controllers/apps/component/transformer_component_notifier.go +++ b/controllers/apps/component/transformer_component_notifier.go @@ -39,7 +39,7 @@ var _ graph.Transformer = &componentNotifierTransformer{} func (t *componentNotifierTransformer) Transform(ctx graph.TransformContext, dag *graph.DAG) error { transCtx, _ := ctx.(*componentTransformContext) - if model.IsObjectDeleting(transCtx.ComponentOrig) || model.IsObjectStatusUpdating(transCtx.ComponentOrig) { + if !model.IsObjectUpdating(transCtx.ComponentOrig) { return nil }