Use field owner in the patch helper

- Update summarize helper to have patch field owner.
- Updated the controllers to set the patch field owner.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
This commit is contained in:
Sunny 2022-02-21 15:38:28 +05:30 committed by Hidde Beydals
parent 07a539e3d6
commit f72a28a193
6 changed files with 49 additions and 24 deletions

View File

@ -95,7 +95,8 @@ type BucketReconciler struct {
kuberecorder.EventRecorder
helper.Metrics
Storage *Storage
Storage *Storage
ControllerName string
}
type BucketReconcilerOptions struct {
@ -160,6 +161,7 @@ func (r *BucketReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
summarize.RecordReconcileReq,
),
summarize.WithResultBuilder(sreconcile.AlwaysRequeueResultBuilder{RequeueAfter: obj.GetInterval().Duration}),
summarize.WithPatchFieldOwner(r.ControllerName),
}
result, retErr = summarizeHelper.SummarizeAndPatch(ctx, obj, summarizeOpts...)

View File

@ -95,7 +95,8 @@ type GitRepositoryReconciler struct {
kuberecorder.EventRecorder
helper.Metrics
Storage *Storage
Storage *Storage
ControllerName string
requeueDependency time.Duration
}
@ -166,6 +167,7 @@ func (r *GitRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Reques
summarize.RecordReconcileReq,
),
summarize.WithResultBuilder(sreconcile.AlwaysRequeueResultBuilder{RequeueAfter: obj.GetInterval().Duration}),
summarize.WithPatchFieldOwner(r.ControllerName),
}
result, retErr = summarizeHelper.SummarizeAndPatch(ctx, obj, summarizeOpts...)

View File

@ -102,8 +102,9 @@ type HelmChartReconciler struct {
kuberecorder.EventRecorder
helper.Metrics
Storage *Storage
Getters helmgetter.Providers
Storage *Storage
Getters helmgetter.Providers
ControllerName string
}
func (r *HelmChartReconciler) SetupWithManager(mgr ctrl.Manager) error {
@ -191,6 +192,7 @@ func (r *HelmChartReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
summarize.RecordReconcileReq,
),
summarize.WithResultBuilder(sreconcile.AlwaysRequeueResultBuilder{RequeueAfter: obj.GetInterval().Duration}),
summarize.WithPatchFieldOwner(r.ControllerName),
}
result, retErr = summarizeHelper.SummarizeAndPatch(ctx, obj, summarizeOpts...)

View File

@ -86,8 +86,9 @@ type HelmRepositoryReconciler struct {
kuberecorder.EventRecorder
helper.Metrics
Getters helmgetter.Providers
Storage *Storage
Getters helmgetter.Providers
Storage *Storage
ControllerName string
}
type HelmRepositoryReconcilerOptions struct {
@ -153,6 +154,7 @@ func (r *HelmRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Reque
summarize.RecordReconcileReq,
),
summarize.WithResultBuilder(sreconcile.AlwaysRequeueResultBuilder{RequeueAfter: obj.GetInterval().Duration}),
summarize.WithPatchFieldOwner(r.ControllerName),
}
result, retErr = summarizeHelper.SummarizeAndPatch(ctx, obj, summarizeOpts...)

View File

@ -85,6 +85,9 @@ type HelperOptions struct {
ReconcileError error
// ResultBuilder defines how the reconciliation result is computed.
ResultBuilder reconcile.RuntimeResultBuilder
// PatchFieldOwner defines the field owner configuration for the Kubernetes
// patch operation.
PatchFieldOwner string
}
// Option is configuration that modifies SummarizeAndPatch.
@ -137,6 +140,13 @@ func WithReconcileError(re error) Option {
}
}
// WithPatchFieldOwner sets the FieldOwner in the patch helper.
func WithPatchFieldOwner(fieldOwner string) Option {
return func(s *HelperOptions) {
s.PatchFieldOwner = fieldOwner
}
}
// SummarizeAndPatch summarizes and patches the result to the target object.
// When used at the very end of a reconciliation, the result builder must be
// specified using the Option WithResultBuilder(). The returned result and error
@ -161,6 +171,9 @@ func (h *Helper) SummarizeAndPatch(ctx context.Context, obj conditions.Setter, o
Conditions: ownedConditions,
},
}
if opts.PatchFieldOwner != "" {
patchOpts = append(patchOpts, patch.WithFieldOwner(opts.PatchFieldOwner))
}
// Process the results of reconciliation.
for _, processor := range opts.Processors {

40
main.go
View File

@ -165,10 +165,11 @@ func main() {
storage := mustInitStorage(storagePath, storageAdvAddr, setupLog)
if err = (&controllers.GitRepositoryReconciler{
Client: mgr.GetClient(),
EventRecorder: eventRecorder,
Metrics: metricsH,
Storage: storage,
Client: mgr.GetClient(),
EventRecorder: eventRecorder,
Metrics: metricsH,
Storage: storage,
ControllerName: controllerName,
}).SetupWithManagerAndOptions(mgr, controllers.GitRepositoryReconcilerOptions{
MaxConcurrentReconciles: concurrent,
DependencyRequeueInterval: requeueDependency,
@ -177,11 +178,12 @@ func main() {
os.Exit(1)
}
if err = (&controllers.HelmRepositoryReconciler{
Client: mgr.GetClient(),
EventRecorder: eventRecorder,
Metrics: metricsH,
Storage: storage,
Getters: getters,
Client: mgr.GetClient(),
EventRecorder: eventRecorder,
Metrics: metricsH,
Storage: storage,
Getters: getters,
ControllerName: controllerName,
}).SetupWithManagerAndOptions(mgr, controllers.HelmRepositoryReconcilerOptions{
MaxConcurrentReconciles: concurrent,
}); err != nil {
@ -189,11 +191,12 @@ func main() {
os.Exit(1)
}
if err = (&controllers.HelmChartReconciler{
Client: mgr.GetClient(),
Storage: storage,
Getters: getters,
EventRecorder: eventRecorder,
Metrics: metricsH,
Client: mgr.GetClient(),
Storage: storage,
Getters: getters,
EventRecorder: eventRecorder,
Metrics: metricsH,
ControllerName: controllerName,
}).SetupWithManagerAndOptions(mgr, controllers.HelmChartReconcilerOptions{
MaxConcurrentReconciles: concurrent,
}); err != nil {
@ -201,10 +204,11 @@ func main() {
os.Exit(1)
}
if err = (&controllers.BucketReconciler{
Client: mgr.GetClient(),
EventRecorder: eventRecorder,
Metrics: metricsH,
Storage: storage,
Client: mgr.GetClient(),
EventRecorder: eventRecorder,
Metrics: metricsH,
Storage: storage,
ControllerName: controllerName,
}).SetupWithManagerAndOptions(mgr, controllers.BucketReconcilerOptions{
MaxConcurrentReconciles: concurrent,
}); err != nil {