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

View File

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

View File

@ -102,8 +102,9 @@ type HelmChartReconciler struct {
kuberecorder.EventRecorder kuberecorder.EventRecorder
helper.Metrics helper.Metrics
Storage *Storage Storage *Storage
Getters helmgetter.Providers Getters helmgetter.Providers
ControllerName string
} }
func (r *HelmChartReconciler) SetupWithManager(mgr ctrl.Manager) error { 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.RecordReconcileReq,
), ),
summarize.WithResultBuilder(sreconcile.AlwaysRequeueResultBuilder{RequeueAfter: obj.GetInterval().Duration}), summarize.WithResultBuilder(sreconcile.AlwaysRequeueResultBuilder{RequeueAfter: obj.GetInterval().Duration}),
summarize.WithPatchFieldOwner(r.ControllerName),
} }
result, retErr = summarizeHelper.SummarizeAndPatch(ctx, obj, summarizeOpts...) result, retErr = summarizeHelper.SummarizeAndPatch(ctx, obj, summarizeOpts...)

View File

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

View File

@ -85,6 +85,9 @@ type HelperOptions struct {
ReconcileError error ReconcileError error
// ResultBuilder defines how the reconciliation result is computed. // ResultBuilder defines how the reconciliation result is computed.
ResultBuilder reconcile.RuntimeResultBuilder ResultBuilder reconcile.RuntimeResultBuilder
// PatchFieldOwner defines the field owner configuration for the Kubernetes
// patch operation.
PatchFieldOwner string
} }
// Option is configuration that modifies SummarizeAndPatch. // 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. // SummarizeAndPatch summarizes and patches the result to the target object.
// When used at the very end of a reconciliation, the result builder must be // When used at the very end of a reconciliation, the result builder must be
// specified using the Option WithResultBuilder(). The returned result and error // 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, Conditions: ownedConditions,
}, },
} }
if opts.PatchFieldOwner != "" {
patchOpts = append(patchOpts, patch.WithFieldOwner(opts.PatchFieldOwner))
}
// Process the results of reconciliation. // Process the results of reconciliation.
for _, processor := range opts.Processors { for _, processor := range opts.Processors {

40
main.go
View File

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