Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance handling prune for CR #214

Merged
merged 1 commit into from
Apr 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion pkg/patterns/declarative/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some explaination for this interface?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An explanation of the interface has been appended.

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
// <group>/<version>/<kind> (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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And, []string has specific format for gvk?
If so, please wirte that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perfect! 😄

PruneWhiteList() []string
}

type ErrorResult struct {
Result reconcile.Result
Err error
Expand Down Expand Up @@ -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 := ""
Expand Down