From 1e7010210b3d0b3ba6ddfb53248fd38ef7acce93 Mon Sep 17 00:00:00 2001 From: Benjamin Lindner Date: Thu, 11 Jul 2024 08:23:30 +0200 Subject: [PATCH] extract skip label funx --- internal/declarative/v2/options.go | 33 --------------------------- internal/declarative/v2/reconciler.go | 13 ++++++++++- 2 files changed, 12 insertions(+), 34 deletions(-) diff --git a/internal/declarative/v2/options.go b/internal/declarative/v2/options.go index bf7bb1de5c..fd6e83c81d 100644 --- a/internal/declarative/v2/options.go +++ b/internal/declarative/v2/options.go @@ -3,7 +3,6 @@ package v2 import ( "context" "os" - "strconv" "time" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -11,11 +10,7 @@ import ( "k8s.io/client-go/rest" "k8s.io/client-go/tools/record" "sigs.k8s.io/controller-runtime/pkg/client" - logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/manager" - - "github.com/kyma-project/lifecycle-manager/api/shared" - "github.com/kyma-project/lifecycle-manager/internal" ) const ( @@ -33,7 +28,6 @@ func DefaultOptions() *Options { ), WithSingletonClientCache(NewMemoryClientCache()), WithManifestCache(os.TempDir()), - WithSkipReconcileOn(SkipReconcileOnDefaultLabelPresentAndTrue), WithManifestParser(NewInMemoryCachedManifestParser(DefaultInMemoryParseTTL)), WithModuleCRDeletionCheck(NewDefaultDeletionCheck()), ) @@ -62,8 +56,6 @@ type Options struct { DeletionCheck ModuleCRDeletionCheck DeletePrerequisites bool - - ShouldSkip SkipReconcile } type Option interface { @@ -236,31 +228,6 @@ func (o WithRemoteTargetClusterOption) Apply(options *Options) { options.TargetCluster = o.ClusterFn } -func WithSkipReconcileOn(skipReconcile SkipReconcile) WithSkipReconcileOnOption { - return WithSkipReconcileOnOption{skipReconcile: skipReconcile} -} - -type SkipReconcile func(context.Context, Object) (skip bool) - -// SkipReconcileOnDefaultLabelPresentAndTrue determines SkipReconcile by checking if DefaultSkipReconcileLabel is true. -func SkipReconcileOnDefaultLabelPresentAndTrue(ctx context.Context, object Object) bool { - if object.GetLabels() != nil && object.GetLabels()[shared.SkipReconcileLabel] == strconv.FormatBool(true) { - logf.FromContext(ctx, "skip-label", shared.SkipReconcileLabel). - V(internal.DebugLogLevel).Info("resource gets skipped because of label") - return true - } - - return false -} - -type WithSkipReconcileOnOption struct { - skipReconcile SkipReconcile -} - -func (o WithSkipReconcileOnOption) Apply(options *Options) { - options.ShouldSkip = o.skipReconcile -} - type ClientCacheKeyFn func(ctx context.Context, obj Object) (string, bool) type WithClientCacheKeyOption struct { diff --git a/internal/declarative/v2/reconciler.go b/internal/declarative/v2/reconciler.go index 8c34a6f930..ad4cd04d76 100644 --- a/internal/declarative/v2/reconciler.go +++ b/internal/declarative/v2/reconciler.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "github.com/kyma-project/lifecycle-manager/internal" "strconv" "time" @@ -98,6 +99,16 @@ func newResourcesCondition(obj Object) apimetav1.Condition { } } +func hasSkipReconcileLabel(ctx context.Context, object Object) bool { + if object.GetLabels() != nil && object.GetLabels()[shared.SkipReconcileLabel] == strconv.FormatBool(true) { + logf.FromContext(ctx, "skip-label", shared.SkipReconcileLabel). + V(internal.DebugLogLevel).Info("resource gets skipped because of label") + return true + } + + return false +} + //nolint:funlen,cyclop,gocognit // Declarative pkg will be removed soon func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { startTime := time.Now() @@ -117,7 +128,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu } currentObjStatus := obj.GetStatus() - if r.ShouldSkip(ctx, obj) { + if hasSkipReconcileLabel(ctx, obj) { return ctrl.Result{RequeueAfter: r.Success}, nil }