Skip to content

Commit

Permalink
refactor: Simplify declarative reconciler (#1676)
Browse files Browse the repository at this point in the history
* extract default finalizer from opts

* extract field owner and namespace

* extract skip label funx

* replace obj with manifest for manifest reconcile loop

* add skip reconcile check as method. internalize cr deletion check

* simplify SpecResolver

* fix integration test setup

* linting

* linting

* remove generace cache key
  • Loading branch information
lindnerby authored Jul 11, 2024
1 parent 13621dd commit 46d1e4b
Show file tree
Hide file tree
Showing 12 changed files with 228 additions and 480 deletions.
6 changes: 6 additions & 0 deletions api/v1beta2/manifest_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.
package v1beta2

import (
"strconv"

apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
machineryruntime "k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -130,3 +132,7 @@ type ManifestList struct {
func init() {
SchemeBuilder.Register(&Manifest{}, &ManifestList{})
}

func (manifest *Manifest) SkipReconciliation() bool {
return manifest.GetLabels() != nil && manifest.GetLabels()[shared.SkipReconcileLabel] == strconv.FormatBool(true)
}
8 changes: 2 additions & 6 deletions internal/controller/manifest/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package manifest
import (
"sigs.k8s.io/controller-runtime/pkg/manager"

"github.com/kyma-project/lifecycle-manager/api/v1beta2"
declarativev2 "github.com/kyma-project/lifecycle-manager/internal/declarative/v2"
"github.com/kyma-project/lifecycle-manager/internal/manifest"
"github.com/kyma-project/lifecycle-manager/internal/pkg/metrics"
Expand All @@ -22,15 +21,12 @@ func NewReconciler(mgr manager.Manager,
extractor := manifest.NewPathExtractor(nil)
lookup := &manifest.RemoteClusterLookup{KCP: kcp}
return declarativev2.NewFromManager(
mgr, &v1beta2.Manifest{}, requeueIntervals, manifestMetrics, mandatoryModulesMetrics,
declarativev2.WithSpecResolver(
manifest.NewSpecResolver(kcp, extractor),
),
mgr, requeueIntervals, manifestMetrics, mandatoryModulesMetrics,
manifest.NewSpecResolver(kcp.Client, extractor),
declarativev2.WithCustomReadyCheck(manifest.NewDeploymentReadyCheck()),
declarativev2.WithRemoteTargetCluster(lookup.ConfigResolver),
manifest.WithClientCacheKey(),
declarativev2.WithPostRun{manifest.PostRunCreateCR},
declarativev2.WithPreDelete{manifest.PreDeleteDeleteCR},
declarativev2.WithModuleCRDeletionCheck(manifest.NewModuleCRDeletionCheck()),
)
}
3 changes: 1 addition & 2 deletions internal/declarative/v2/inmemory_rendered.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,5 @@ func (c *InMemoryManifestCache) Parse(spec *Spec,
}

func generateCacheKey(spec *Spec) string {
file := filepath.Join(ManifestFilePrefix, spec.Path, spec.ManifestName)
return fmt.Sprintf("%s-%s", file, spec.Mode)
return filepath.Join(ManifestFilePrefix, spec.Path, spec.ManifestName)
}
22 changes: 0 additions & 22 deletions internal/declarative/v2/moduleCR_deletion_check.go

This file was deleted.

106 changes: 0 additions & 106 deletions internal/declarative/v2/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,23 @@ package v2
import (
"context"
"os"
"strconv"
"time"

apimetav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"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 (
FinalizerDefault = "declarative.kyma-project.io/finalizer"
FieldOwnerDefault = "declarative.kyma-project.io/applier"
EventRecorderDefault = "declarative.kyma-project.io/events"
DefaultInMemoryParseTTL = 24 * time.Hour
)

func DefaultOptions() *Options {
return (&Options{}).Apply(
WithNamespace(apimetav1.NamespaceDefault, false),
WithFinalizer(FinalizerDefault),
WithFieldOwner(FieldOwnerDefault),
WithPostRenderTransform(
ManagedByDeclarativeV2,
watchedByOwnedBy,
Expand All @@ -39,9 +28,7 @@ func DefaultOptions() *Options {
),
WithSingletonClientCache(NewMemoryClientCache()),
WithManifestCache(os.TempDir()),
WithSkipReconcileOn(SkipReconcileOnDefaultLabelPresentAndTrue),
WithManifestParser(NewInMemoryCachedManifestParser(DefaultInMemoryParseTTL)),
WithModuleCRDeletionCheck(NewDefaultDeletionCheck()),
)
}

Expand All @@ -51,31 +38,16 @@ type Options struct {
client.Client
TargetCluster ClusterFn

SpecResolver
ClientCache
ClientCacheKeyFn
ManifestParser
ManifestCache
CustomReadyCheck ReadyCheck

Namespace string
CreateNamespace bool

Finalizer string

ServerSideApply bool
FieldOwner client.FieldOwner

PostRenderTransforms []ObjectTransform

PostRuns []PostRun
PreDeletes []PreDelete

DeletionCheck ModuleCRDeletionCheck

DeletePrerequisites bool

ShouldSkip SkipReconcile
}

type Option interface {
Expand All @@ -89,35 +61,6 @@ func (o *Options) Apply(options ...Option) *Options {
return o
}

type WithNamespaceOption struct {
name string
createIfMissing bool
}

func WithNamespace(name string, createIfMissing bool) WithNamespaceOption {
return WithNamespaceOption{
name: name,
createIfMissing: createIfMissing,
}
}

func (o WithNamespaceOption) Apply(options *Options) {
options.Namespace = o.name
options.CreateNamespace = o.createIfMissing
}

type WithFieldOwner client.FieldOwner

func (o WithFieldOwner) Apply(options *Options) {
options.FieldOwner = client.FieldOwner(o)
}

type WithFinalizer string

func (o WithFinalizer) Apply(options *Options) {
options.Finalizer = string(o)
}

type WithManagerOption struct {
manager.Manager
}
Expand Down Expand Up @@ -151,18 +94,6 @@ func (o WithCustomResourceLabels) Apply(options *Options) {
options.PostRenderTransforms = append(options.PostRenderTransforms, labelTransform)
}

func WithSpecResolver(resolver SpecResolver) SpecResolverOption {
return SpecResolverOption{resolver}
}

type SpecResolverOption struct {
SpecResolver
}

func (o SpecResolverOption) Apply(options *Options) {
options.SpecResolver = o
}

type ObjectTransform = func(context.Context, Object, []*unstructured.Unstructured) error

func WithPostRenderTransform(transforms ...ObjectTransform) PostRenderTransformOption {
Expand Down Expand Up @@ -207,18 +138,6 @@ func (o WithPreDelete) Apply(options *Options) {
options.PreDeletes = append(options.PreDeletes, o...)
}

func WithModuleCRDeletionCheck(deletionCheckFn ModuleCRDeletionCheck) WithModuleCRDeletionCheckOption {
return WithModuleCRDeletionCheckOption{ModuleCRDeletionCheck: deletionCheckFn}
}

type WithModuleCRDeletionCheckOption struct {
ModuleCRDeletionCheck
}

func (o WithModuleCRDeletionCheckOption) Apply(options *Options) {
options.DeletionCheck = o
}

type WithSingletonClientCacheOption struct {
ClientCache
}
Expand Down Expand Up @@ -277,31 +196,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
Loading

0 comments on commit 46d1e4b

Please sign in to comment.