@@ -149,9 +149,15 @@ func (r *BpfmanConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request
149
149
}
150
150
151
151
func (r * BpfmanConfigReconciler ) reconcileCM (ctx context.Context , bpfmanConfig * v1alpha1.Config ) error {
152
- cm := & corev1.ConfigMap {ObjectMeta : metav1.ObjectMeta {
153
- Name : internal .BpfmanCmName ,
154
- Namespace : bpfmanConfig .Spec .Namespace },
152
+ cm := & corev1.ConfigMap {
153
+ TypeMeta : metav1.TypeMeta {
154
+ Kind : "ConfigMap" ,
155
+ APIVersion : "v1" ,
156
+ },
157
+ ObjectMeta : metav1.ObjectMeta {
158
+ Name : internal .BpfmanCmName ,
159
+ Namespace : bpfmanConfig .Spec .Namespace ,
160
+ },
155
161
Data : map [string ]string {
156
162
internal .BpfmanTOML : bpfmanConfig .Spec .Configuration ,
157
163
internal .BpfmanAgentLogLevel : bpfmanConfig .Spec .Agent .LogLevel ,
@@ -164,7 +170,13 @@ func (r *BpfmanConfigReconciler) reconcileCM(ctx context.Context, bpfmanConfig *
164
170
}
165
171
166
172
func (r * BpfmanConfigReconciler ) reconcileCSIDriver (ctx context.Context , bpfmanConfig * v1alpha1.Config ) error {
167
- csiDriver := & storagev1.CSIDriver {ObjectMeta : metav1.ObjectMeta {Name : internal .BpfmanCsiDriverName }}
173
+ csiDriver := & storagev1.CSIDriver {
174
+ TypeMeta : metav1.TypeMeta {
175
+ Kind : "CSIDriver" ,
176
+ APIVersion : "storage.k8s.io/v1" ,
177
+ },
178
+ ObjectMeta : metav1.ObjectMeta {Name : internal .BpfmanCsiDriverName },
179
+ }
168
180
r .Logger .Info ("Loading object" , "object" , csiDriver .Name , "path" , r .CsiDriverDS )
169
181
csiDriver , err := load (csiDriver , r .CsiDriverDS , csiDriver .Name )
170
182
if err != nil {
@@ -178,6 +190,10 @@ func (r *BpfmanConfigReconciler) reconcileCSIDriver(ctx context.Context, bpfmanC
178
190
func (r * BpfmanConfigReconciler ) reconcileSCC (ctx context.Context , bpfmanConfig * v1alpha1.Config ) error {
179
191
if r .IsOpenshift {
180
192
bpfmanRestrictedSCC := & osv1.SecurityContextConstraints {
193
+ TypeMeta : metav1.TypeMeta {
194
+ Kind : "SecurityContextConstraints" ,
195
+ APIVersion : "security.openshift.io/v1" ,
196
+ },
181
197
ObjectMeta : metav1.ObjectMeta {
182
198
Name : internal .BpfmanRestrictedSccName ,
183
199
},
@@ -190,16 +206,22 @@ func (r *BpfmanConfigReconciler) reconcileSCC(ctx context.Context, bpfmanConfig
190
206
return assureResource (ctx , r , bpfmanConfig , bpfmanRestrictedSCC , func (existing , desired * osv1.SecurityContextConstraints ) bool {
191
207
existingCopy := existing .DeepCopy ()
192
208
desiredCopy := desired .DeepCopy ()
193
- existingCopy .ObjectMeta = metav1. ObjectMeta {}
194
- desiredCopy .ObjectMeta = metav1 .ObjectMeta {}
209
+ existingCopy .TypeMeta = desired . TypeMeta
210
+ existingCopy .ObjectMeta = desired .ObjectMeta
195
211
return ! equality .Semantic .DeepEqual (existingCopy , desiredCopy )
196
212
})
197
213
}
198
214
return nil
199
215
}
200
216
201
217
func (r * BpfmanConfigReconciler ) reconcileStandardDS (ctx context.Context , bpfmanConfig * v1alpha1.Config ) error {
202
- bpfmanDS := & appsv1.DaemonSet {ObjectMeta : metav1.ObjectMeta {Name : internal .BpfmanDsName }}
218
+ bpfmanDS := & appsv1.DaemonSet {
219
+ TypeMeta : metav1.TypeMeta {
220
+ Kind : "DaemonSet" ,
221
+ APIVersion : "apps/v1" ,
222
+ },
223
+ ObjectMeta : metav1.ObjectMeta {Name : internal .BpfmanDsName },
224
+ }
203
225
r .Logger .Info ("Loading object" , "object" , bpfmanDS .Name , "path" , r .BpfmanStandardDS )
204
226
bpfmanDS , err := load (bpfmanDS , r .BpfmanStandardDS , bpfmanDS .Name )
205
227
if err != nil {
@@ -213,7 +235,13 @@ func (r *BpfmanConfigReconciler) reconcileStandardDS(ctx context.Context, bpfman
213
235
214
236
func (r * BpfmanConfigReconciler ) reconcileMetricsProxyDS (ctx context.Context , bpfmanConfig * v1alpha1.Config ) error {
215
237
// Reconcile metrics-proxy daemonset
216
- metricsProxyDS := & appsv1.DaemonSet {ObjectMeta : metav1.ObjectMeta {Name : internal .BpfmanMetricsProxyDsName }}
238
+ metricsProxyDS := & appsv1.DaemonSet {
239
+ TypeMeta : metav1.TypeMeta {
240
+ Kind : "DaemonSet" ,
241
+ APIVersion : "apps/v1" ,
242
+ },
243
+ ObjectMeta : metav1.ObjectMeta {Name : internal .BpfmanMetricsProxyDsName },
244
+ }
217
245
r .Logger .Info ("Loading object" , "object" , metricsProxyDS .Name , "path" , r .BpfmanMetricsProxyDS )
218
246
metricsProxyDS , err := load (metricsProxyDS , r .BpfmanMetricsProxyDS , metricsProxyDS .Name )
219
247
if err != nil {
@@ -370,39 +398,32 @@ func load[T client.Object](t T, path, name string) (T, error) {
370
398
}
371
399
372
400
// assureResource ensures a Kubernetes resource exists and is up to date.
373
- // Creates the resource if it doesn't exist, otherwise updates it to match the desired state.
401
+ // SSA patch creates the resource if it doesn't exist, otherwise updates it to match the desired state.
374
402
func assureResource [T client.Object ](ctx context.Context , r * BpfmanConfigReconciler ,
375
403
bpfmanConfig * v1alpha1.Config , resource T , needsUpdate func (existing T , desired T ) bool ) error {
376
404
if err := ctrl .SetControllerReference (bpfmanConfig , resource , r .Scheme ); err != nil {
377
405
return err
378
406
}
379
407
380
- objectKey := types.NamespacedName {Namespace : resource .GetNamespace (), Name : resource .GetName ()}
381
408
r .Logger .Info ("Getting object" ,
382
409
"type" , resource .GetObjectKind (), "namespace" , resource .GetNamespace (), "name" , resource .GetName ())
410
+ objectKey := types.NamespacedName {Namespace : resource .GetNamespace (), Name : resource .GetName ()}
383
411
existingResource := resource .DeepCopyObject ().(T )
412
+ found := true
384
413
if err := r .Get (ctx , objectKey , existingResource ); err != nil {
385
- if errors .IsNotFound (err ) {
386
- r .Logger .Info ( "Creating object" ,
414
+ if ! errors .IsNotFound (err ) {
415
+ r .Logger .Error ( err , "Failed to get object" ,
387
416
"type" , resource .GetObjectKind (), "namespace" , resource .GetNamespace (), "name" , resource .GetName ())
388
- if err := r .Create (ctx , resource ); err != nil {
389
- r .Logger .Error (err , "Failed to create object" ,
390
- "type" , resource .GetObjectKind (), "namespace" , resource .GetNamespace (), "name" , resource .GetName ())
391
- return err
392
- }
393
- return nil
417
+ return err
394
418
}
395
- r .Logger .Error (err , "Failed to get object" ,
396
- "type" , resource .GetObjectKind (), "namespace" , resource .GetNamespace (), "name" , resource .GetName ())
397
- return err
419
+ found = false
398
420
}
399
421
400
- if needsUpdate (existingResource , resource ) {
401
- r .Logger .Info ("Updating object" ,
422
+ if ! found || needsUpdate (existingResource , resource ) {
423
+ r .Logger .Info ("Patching object" ,
402
424
"type" , resource .GetObjectKind (), "namespace" , resource .GetNamespace (), "name" , resource .GetName ())
403
- resource .SetResourceVersion (existingResource .GetResourceVersion ())
404
- if err := r .Update (ctx , resource ); err != nil {
405
- r .Logger .Error (err , "Failed updating object" ,
425
+ if err := r .Patch (ctx , resource , client .Apply , client .ForceOwnership , client .FieldOwner (bpfmanConfig .Name )); err != nil {
426
+ r .Logger .Error (err , "Failed patching object" ,
406
427
"type" , resource .GetObjectKind (), "namespace" , resource .GetNamespace (), "name" , resource .GetName ())
407
428
return err
408
429
}
0 commit comments