Skip to content

Commit

Permalink
notify at deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
leon-inf committed Jan 27, 2025
1 parent f08841d commit 91a78a3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions controllers/apps/component/transformer_component_deletion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 91a78a3

Please sign in to comment.