diff --git a/pkg/patterns/declarative/reconciler.go b/pkg/patterns/declarative/reconciler.go index 7e3a2a96..be86b5ab 100644 --- a/pkg/patterns/declarative/reconciler.go +++ b/pkg/patterns/declarative/reconciler.go @@ -74,6 +74,21 @@ type DeclarativeObject interface { metav1.Object } +// Pruner is a trait for addon CRDs that determines whether pruning behavior should be enabled for the current CR. +// To enable this feature, it's necessary to enable WithApplyPrune. If WithApplyPrune is enabled but Pruner is not +// implemented, Prune behavior is assumed by default. +type Pruner interface { + Prune() bool +} + +// PruneWhiteLister is a trait for addon CRDs that determines which kind of resources should be pruned. It's useful +// when CR in installed by Addon and want to prune them automatically. The format of array item should be exactly like +// // (core group using 'core' indeed). For example: ["core/v1/ConfigMap", "batch/v1/Job"]. +// Notice: kubeadm has a built-in prune white list, and it will be ignored if this method is implemented. +type PruneWhiteLister interface { + PruneWhiteList() []string +} + type ErrorResult struct { Result reconcile.Result Err error @@ -248,13 +263,20 @@ func (r *Reconciler) reconcileExists(ctx context.Context, name types.NamespacedN extraArgs := []string{"--force"} - if r.options.prune { + // allow user disable prune in CR + if p, ok := instance.(Pruner); (!ok && r.options.prune) || (ok && r.options.prune && p.Prune()) { var labels []string for k, v := range r.options.labelMaker(ctx, instance) { labels = append(labels, fmt.Sprintf("%s=%s", k, v)) } extraArgs = append(extraArgs, "--prune", "--selector", strings.Join(labels, ",")) + + if lister, ok := instance.(PruneWhiteLister); ok { + for _, gvk := range lister.PruneWhiteList() { + extraArgs = append(extraArgs, "--prune-whitelist", gvk) + } + } } ns := ""