Skip to content

Commit f72a28a

Browse files
darkowlzzhiddeco
authored andcommitted
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 <[email protected]>
1 parent 07a539e commit f72a28a

File tree

6 files changed

+49
-24
lines changed

6 files changed

+49
-24
lines changed

controllers/bucket_controller.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ type BucketReconciler struct {
9595
kuberecorder.EventRecorder
9696
helper.Metrics
9797

98-
Storage *Storage
98+
Storage *Storage
99+
ControllerName string
99100
}
100101

101102
type BucketReconcilerOptions struct {
@@ -160,6 +161,7 @@ func (r *BucketReconciler) Reconcile(ctx context.Context, req ctrl.Request) (res
160161
summarize.RecordReconcileReq,
161162
),
162163
summarize.WithResultBuilder(sreconcile.AlwaysRequeueResultBuilder{RequeueAfter: obj.GetInterval().Duration}),
164+
summarize.WithPatchFieldOwner(r.ControllerName),
163165
}
164166
result, retErr = summarizeHelper.SummarizeAndPatch(ctx, obj, summarizeOpts...)
165167

controllers/gitrepository_controller.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ type GitRepositoryReconciler struct {
9595
kuberecorder.EventRecorder
9696
helper.Metrics
9797

98-
Storage *Storage
98+
Storage *Storage
99+
ControllerName string
99100

100101
requeueDependency time.Duration
101102
}
@@ -166,6 +167,7 @@ func (r *GitRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Reques
166167
summarize.RecordReconcileReq,
167168
),
168169
summarize.WithResultBuilder(sreconcile.AlwaysRequeueResultBuilder{RequeueAfter: obj.GetInterval().Duration}),
170+
summarize.WithPatchFieldOwner(r.ControllerName),
169171
}
170172
result, retErr = summarizeHelper.SummarizeAndPatch(ctx, obj, summarizeOpts...)
171173

controllers/helmchart_controller.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ type HelmChartReconciler struct {
102102
kuberecorder.EventRecorder
103103
helper.Metrics
104104

105-
Storage *Storage
106-
Getters helmgetter.Providers
105+
Storage *Storage
106+
Getters helmgetter.Providers
107+
ControllerName string
107108
}
108109

109110
func (r *HelmChartReconciler) SetupWithManager(mgr ctrl.Manager) error {
@@ -191,6 +192,7 @@ func (r *HelmChartReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
191192
summarize.RecordReconcileReq,
192193
),
193194
summarize.WithResultBuilder(sreconcile.AlwaysRequeueResultBuilder{RequeueAfter: obj.GetInterval().Duration}),
195+
summarize.WithPatchFieldOwner(r.ControllerName),
194196
}
195197
result, retErr = summarizeHelper.SummarizeAndPatch(ctx, obj, summarizeOpts...)
196198

controllers/helmrepository_controller.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ type HelmRepositoryReconciler struct {
8686
kuberecorder.EventRecorder
8787
helper.Metrics
8888

89-
Getters helmgetter.Providers
90-
Storage *Storage
89+
Getters helmgetter.Providers
90+
Storage *Storage
91+
ControllerName string
9192
}
9293

9394
type HelmRepositoryReconcilerOptions struct {
@@ -153,6 +154,7 @@ func (r *HelmRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Reque
153154
summarize.RecordReconcileReq,
154155
),
155156
summarize.WithResultBuilder(sreconcile.AlwaysRequeueResultBuilder{RequeueAfter: obj.GetInterval().Duration}),
157+
summarize.WithPatchFieldOwner(r.ControllerName),
156158
}
157159
result, retErr = summarizeHelper.SummarizeAndPatch(ctx, obj, summarizeOpts...)
158160

internal/reconcile/summarize/summary.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ type HelperOptions struct {
8585
ReconcileError error
8686
// ResultBuilder defines how the reconciliation result is computed.
8787
ResultBuilder reconcile.RuntimeResultBuilder
88+
// PatchFieldOwner defines the field owner configuration for the Kubernetes
89+
// patch operation.
90+
PatchFieldOwner string
8891
}
8992

9093
// Option is configuration that modifies SummarizeAndPatch.
@@ -137,6 +140,13 @@ func WithReconcileError(re error) Option {
137140
}
138141
}
139142

143+
// WithPatchFieldOwner sets the FieldOwner in the patch helper.
144+
func WithPatchFieldOwner(fieldOwner string) Option {
145+
return func(s *HelperOptions) {
146+
s.PatchFieldOwner = fieldOwner
147+
}
148+
}
149+
140150
// SummarizeAndPatch summarizes and patches the result to the target object.
141151
// When used at the very end of a reconciliation, the result builder must be
142152
// 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
161171
Conditions: ownedConditions,
162172
},
163173
}
174+
if opts.PatchFieldOwner != "" {
175+
patchOpts = append(patchOpts, patch.WithFieldOwner(opts.PatchFieldOwner))
176+
}
164177

165178
// Process the results of reconciliation.
166179
for _, processor := range opts.Processors {

main.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,11 @@ func main() {
165165
storage := mustInitStorage(storagePath, storageAdvAddr, setupLog)
166166

167167
if err = (&controllers.GitRepositoryReconciler{
168-
Client: mgr.GetClient(),
169-
EventRecorder: eventRecorder,
170-
Metrics: metricsH,
171-
Storage: storage,
168+
Client: mgr.GetClient(),
169+
EventRecorder: eventRecorder,
170+
Metrics: metricsH,
171+
Storage: storage,
172+
ControllerName: controllerName,
172173
}).SetupWithManagerAndOptions(mgr, controllers.GitRepositoryReconcilerOptions{
173174
MaxConcurrentReconciles: concurrent,
174175
DependencyRequeueInterval: requeueDependency,
@@ -177,34 +178,37 @@ func main() {
177178
os.Exit(1)
178179
}
179180
if err = (&controllers.HelmRepositoryReconciler{
180-
Client: mgr.GetClient(),
181-
EventRecorder: eventRecorder,
182-
Metrics: metricsH,
183-
Storage: storage,
184-
Getters: getters,
181+
Client: mgr.GetClient(),
182+
EventRecorder: eventRecorder,
183+
Metrics: metricsH,
184+
Storage: storage,
185+
Getters: getters,
186+
ControllerName: controllerName,
185187
}).SetupWithManagerAndOptions(mgr, controllers.HelmRepositoryReconcilerOptions{
186188
MaxConcurrentReconciles: concurrent,
187189
}); err != nil {
188190
setupLog.Error(err, "unable to create controller", "controller", sourcev1.HelmRepositoryKind)
189191
os.Exit(1)
190192
}
191193
if err = (&controllers.HelmChartReconciler{
192-
Client: mgr.GetClient(),
193-
Storage: storage,
194-
Getters: getters,
195-
EventRecorder: eventRecorder,
196-
Metrics: metricsH,
194+
Client: mgr.GetClient(),
195+
Storage: storage,
196+
Getters: getters,
197+
EventRecorder: eventRecorder,
198+
Metrics: metricsH,
199+
ControllerName: controllerName,
197200
}).SetupWithManagerAndOptions(mgr, controllers.HelmChartReconcilerOptions{
198201
MaxConcurrentReconciles: concurrent,
199202
}); err != nil {
200203
setupLog.Error(err, "unable to create controller", "controller", sourcev1.HelmChartKind)
201204
os.Exit(1)
202205
}
203206
if err = (&controllers.BucketReconciler{
204-
Client: mgr.GetClient(),
205-
EventRecorder: eventRecorder,
206-
Metrics: metricsH,
207-
Storage: storage,
207+
Client: mgr.GetClient(),
208+
EventRecorder: eventRecorder,
209+
Metrics: metricsH,
210+
Storage: storage,
211+
ControllerName: controllerName,
208212
}).SetupWithManagerAndOptions(mgr, controllers.BucketReconcilerOptions{
209213
MaxConcurrentReconciles: concurrent,
210214
}); err != nil {

0 commit comments

Comments
 (0)