Skip to content

Commit

Permalink
extract skip label funx
Browse files Browse the repository at this point in the history
  • Loading branch information
lindnerby committed Jul 11, 2024
1 parent 2c7ac4e commit 1e70102
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 34 deletions.
33 changes: 0 additions & 33 deletions internal/declarative/v2/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@ package v2
import (
"context"
"os"
"strconv"
"time"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8slabels "k8s.io/apimachinery/pkg/labels"
"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 (
Expand All @@ -33,7 +28,6 @@ func DefaultOptions() *Options {
),
WithSingletonClientCache(NewMemoryClientCache()),
WithManifestCache(os.TempDir()),
WithSkipReconcileOn(SkipReconcileOnDefaultLabelPresentAndTrue),
WithManifestParser(NewInMemoryCachedManifestParser(DefaultInMemoryParseTTL)),
WithModuleCRDeletionCheck(NewDefaultDeletionCheck()),
)
Expand Down Expand Up @@ -62,8 +56,6 @@ type Options struct {
DeletionCheck ModuleCRDeletionCheck

DeletePrerequisites bool

ShouldSkip SkipReconcile
}

type Option interface {
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 12 additions & 1 deletion internal/declarative/v2/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/kyma-project/lifecycle-manager/internal"

Check failure on line 7 in internal/declarative/v2/reconciler.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/kyma-project/lifecycle-manager) -s blank -s dot --custom-order (gci)
"strconv"
"time"

Check failure on line 9 in internal/declarative/v2/reconciler.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed (gofumpt)

Check failure on line 10 in internal/declarative/v2/reconciler.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
Expand Down Expand Up @@ -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()
Expand All @@ -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
}

Expand Down

0 comments on commit 1e70102

Please sign in to comment.