-
Notifications
You must be signed in to change notification settings - Fork 47
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
mtesseract
wants to merge
1
commit into
operator-framework:main
Choose a base branch
from
mtesseract:mc/reconciler-extensions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do the users need to know the |
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?