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

New Reconciler Extensions #157

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
74 changes: 74 additions & 0 deletions pkg/extension/extension.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package extension

import (
"context"

"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/release"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/client-go/rest"
)

// ReconcilerExtension defines an extension for the reconciler.
// It consists of several sub-interfaces.
type ReconcilerExtension interface {
Name() string
BeginReconciliationExtension
EndReconciliationExtension
}

// BeginReconciliationExtension defines the extension point to execute at the beginning of a reconciliation flow.
type BeginReconciliationExtension interface {
BeginReconcile(ctx context.Context, reconciliationContext *Context, obj *unstructured.Unstructured) error
}

// EndReconciliationExtension defiens the extension point to execute at the end of a reconciliation flow.
type EndReconciliationExtension interface {
EndReconcile(ctx context.Context, reconciliationContext *Context, obj *unstructured.Unstructured) error
}

// NoOpReconcilerExtension implements all extension methods as no-ops and can be used for convenience
// when only a subset of the available methods needs to be implemented.
type NoOpReconcilerExtension struct{}
Copy link
Member

Choose a reason for hiding this comment

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

Do we need NoOpReconcilerExtension? Instead can we can let the users handle this, since as we keep on adding extension points, we would have to keep updating this. In an ideal scenario, we would not want breaking changes when we add extension points, but in case we still end up having predefined interfaces, would this be necessary?


func (e NoOpReconcilerExtension) BeginReconcile(ctx context.Context, reconciliationContext *Context, obj *unstructured.Unstructured) error {
return nil
}

func (e NoOpReconcilerExtension) EndReconcile(ctx context.Context, reconciliationContext *Context, obj *unstructured.Unstructured) error {
return nil
}

// Context can be used for providing extension methods will more context about the reconciliation flow.
// This contains data objects which can be useful for specific extensions but might not be required for all.
type Context struct {
KubernetesConfig *rest.Config
Copy link
Member

Choose a reason for hiding this comment

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

Do the users need to know the rest.Config entirely? Wouldn't it be sufficient if we pass just the client instead?

HelmRelease *release.Release
HelmValues chartutil.Values
}

// GetHelmRelease is a nil-safe getter retrieving the Helm release information from a reconciliation context, if available.
// Returns an empty Helm release if the release information is not available at the time the extension point is called.
func (c *Context) GetHelmRelease() release.Release {
if c == nil || c.HelmRelease == nil {
return release.Release{}
}
return *c.HelmRelease
}

// GetHelmValues is a nil-safe getter retrieving the Helm values information from a reconciliation context, if available.
// Returns nil if the Helm value are not available at the time the extension point is called.
func (c *Context) GetHelmValues() chartutil.Values {
if c == nil {
return nil
}
return c.HelmValues
}

// GetKubernetesConfig is a nil-safe getter for retrieving the Kubernetes config from a reconciliation context, if available.
func (c *Context) GetKubernetesConfig() *rest.Config {
if c == nil {
return nil
}
return c.KubernetesConfig
}
61 changes: 51 additions & 10 deletions pkg/hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,69 @@ limitations under the License.
package hook

import (
"context"

"github.com/go-logr/logr"
"github.com/operator-framework/helm-operator-plugins/pkg/extension"
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/release"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

type PreHook interface {
Exec(*unstructured.Unstructured, chartutil.Values, logr.Logger) error
type PreHookFunc func(context.Context, *unstructured.Unstructured, logr.Logger) error

func WrapPreHookFunc(f PreHookFunc) PreHookFunc {
wrappedF := func(ctx context.Context, obj *unstructured.Unstructured, log logr.Logger) error {
err := f(ctx, obj, log)
if err != nil {
log.Error(err, "pre-release hook failed")
}
return nil
}

return PreHookFunc(wrappedF)
}

type PreHookFunc func(*unstructured.Unstructured, chartutil.Values, logr.Logger) error
func WrapPostHookFunc(f PostHookFunc) PostHookFunc {
wrappedF := func(ctx context.Context, obj *unstructured.Unstructured, rel release.Release, vals chartutil.Values, log logr.Logger) error {
err := f(ctx, obj, rel, vals, log)
if err != nil {
log.Error(err, "post-release hook failed", "name", rel.Name, "version", rel.Version)
}
return nil
}

func (f PreHookFunc) Exec(obj *unstructured.Unstructured, vals chartutil.Values, log logr.Logger) error {
return f(obj, vals, log)
return PostHookFunc(wrappedF)
}

type PostHook interface {
Exec(*unstructured.Unstructured, release.Release, logr.Logger) error
func (h PreHookFunc) Name() string {
return "pre-hook"
}

type PostHookFunc func(*unstructured.Unstructured, release.Release, logr.Logger) error
func (h PreHookFunc) BeginReconcile(ctx context.Context, reconciliationContext *extension.Context, obj *unstructured.Unstructured) error {
log := logr.FromContextOrDiscard(ctx)
return h(ctx, obj, log)
}

func (f PostHookFunc) Exec(obj *unstructured.Unstructured, rel release.Release, log logr.Logger) error {
return f(obj, rel, log)
func (h PreHookFunc) EndReconcile(ctx context.Context, reconciliationContext *extension.Context, obj *unstructured.Unstructured) error {
return nil
}

var _ extension.ReconcilerExtension = (PreHookFunc)(nil)

type PostHookFunc func(context.Context, *unstructured.Unstructured, release.Release, chartutil.Values, logr.Logger) error

func (h PostHookFunc) Name() string {
return "post-hook"
}

func (f PostHookFunc) BeginReconcile(ctx context.Context, reconciliationContext *extension.Context, obj *unstructured.Unstructured) error {
return nil
}

func (f PostHookFunc) EndReconcile(ctx context.Context, reconciliationContext *extension.Context, obj *unstructured.Unstructured) error {
log := logr.FromContextOrDiscard(ctx)
return f(ctx, obj, reconciliationContext.GetHelmRelease(), reconciliationContext.GetHelmValues(), log)
}

var _ extension.ReconcilerExtension = (*PostHookFunc)(nil)
27 changes: 16 additions & 11 deletions pkg/hook/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,41 @@ limitations under the License.
package hook_test

import (
"context"

"github.com/go-logr/logr"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"helm.sh/helm/v3/pkg/chartutil"
"helm.sh/helm/v3/pkg/release"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

. "github.com/operator-framework/helm-operator-plugins/pkg/hook"
"github.com/operator-framework/helm-operator-plugins/pkg/extension"
"github.com/operator-framework/helm-operator-plugins/pkg/hook"
)

var _ = Describe("Hook", func() {
var _ = Describe("PreHookFunc", func() {
It("should implement the PreHook interface", func() {
called := false
var h PreHook = PreHookFunc(func(*unstructured.Unstructured, chartutil.Values, logr.Logger) error {
called = true
return nil
})
Expect(h.Exec(nil, nil, logr.Discard())).To(Succeed())
var h extension.ReconcilerExtension = hook.PreHookFunc(
func(context.Context, *unstructured.Unstructured, logr.Logger) error {
called = true
return nil
})
Expect(h.BeginReconcile(context.TODO(), nil, nil)).To(Succeed())
Expect(called).To(BeTrue())
})
})
var _ = Describe("PostHookFunc", func() {
It("should implement the PostHook interface", func() {
called := false
var h PostHook = PostHookFunc(func(*unstructured.Unstructured, release.Release, logr.Logger) error {
called = true
return nil
})
Expect(h.Exec(nil, release.Release{}, logr.Discard())).To(Succeed())
var h extension.ReconcilerExtension = hook.PostHookFunc(
func(context.Context, *unstructured.Unstructured, release.Release, chartutil.Values, logr.Logger) error {
called = true
return nil
})
Expect(h.EndReconcile(context.TODO(), nil, nil)).To(Succeed())
Expect(called).To(BeTrue())
})
})
Expand Down
51 changes: 51 additions & 0 deletions pkg/reconciler/extensions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package reconciler

import (
"context"
"fmt"

"github.com/operator-framework/helm-operator-plugins/pkg/extension"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

type extensions []extension.ReconcilerExtension

func (es extensions) forEach(f func(e extension.ReconcilerExtension) error) error {
var err error
for _, e := range es {
err = f(e)
if err != nil {
return err
}
}
return err
}

func (r *Reconciler) extBeginReconcile(ctx context.Context, reconciliationContext *extension.Context, obj *unstructured.Unstructured) error {
return r.extensions.forEach(func(ext extension.ReconcilerExtension) error {
e, ok := ext.(extension.BeginReconciliationExtension)
if !ok {
return nil
}
err := e.BeginReconcile(ctx, reconciliationContext, obj)
if err != nil {
return fmt.Errorf("extension %s failed during begin-reconcile phase: %v", ext.Name(), err)
}
return nil
})
}

func (r *Reconciler) extEndReconcile(ctx context.Context, reconciliationContext *extension.Context, obj *unstructured.Unstructured) error {
return r.extensions.forEach(func(ext extension.ReconcilerExtension) error {
e, ok := ext.(extension.EndReconciliationExtension)
if !ok {
return nil
}

err := e.EndReconcile(ctx, reconciliationContext, obj)
if err != nil {
return fmt.Errorf("extension %s failed during end-reconcile phase: %v", ext.Name(), err)
}
return nil
})
}
19 changes: 15 additions & 4 deletions pkg/reconciler/internal/hook/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ limitations under the License.
package hook

import (
"context"
"sync"

"github.com/go-logr/logr"
sdkhandler "github.com/operator-framework/operator-lib/handler"
"helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/releaseutil"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -33,12 +33,12 @@ import (
"sigs.k8s.io/yaml"

"github.com/operator-framework/helm-operator-plugins/internal/sdk/controllerutil"
"github.com/operator-framework/helm-operator-plugins/pkg/hook"
"github.com/operator-framework/helm-operator-plugins/pkg/extension"
"github.com/operator-framework/helm-operator-plugins/pkg/internal/predicate"
"github.com/operator-framework/helm-operator-plugins/pkg/manifestutil"
)

func NewDependentResourceWatcher(c controller.Controller, rm meta.RESTMapper) hook.PostHook {
func NewDependentResourceWatcher(c controller.Controller, rm meta.RESTMapper) extension.ReconcilerExtension {
return &dependentResourceWatcher{
controller: c,
restMapper: rm,
Expand All @@ -53,9 +53,20 @@ type dependentResourceWatcher struct {

m sync.Mutex
watches map[schema.GroupVersionKind]struct{}

extension.NoOpReconcilerExtension
}

func (d *dependentResourceWatcher) Exec(owner *unstructured.Unstructured, rel release.Release, log logr.Logger) error {
var _ extension.EndReconciliationExtension = (*dependentResourceWatcher)(nil)

func (d *dependentResourceWatcher) Name() string {
return "internal-dependent-resource-watcher"
}

func (d *dependentResourceWatcher) EndReconcile(ctx context.Context, reconciliationContext *extension.Context, owner *unstructured.Unstructured) error {
log := logr.FromContextOrDiscard(ctx)
rel := reconciliationContext.GetHelmRelease()

// using predefined functions for filtering events
dependentPredicate := predicate.DependentPredicateFuncs()

Expand Down
Loading