@@ -82,7 +82,7 @@ type WorkStatusController struct {
82
82
// The Controller will requeue the Request to be processed again if an error is non-nil or
83
83
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
84
84
func (c * WorkStatusController ) Reconcile (ctx context.Context , req controllerruntime.Request ) (controllerruntime.Result , error ) {
85
- klog .V (4 ).Infof ("Reconciling status of Work %s." , req .NamespacedName . String () )
85
+ klog .V (4 ).InfoS ("Reconciling status of Work." , "namespace" , req . Namespace , "name" , req .Name )
86
86
87
87
work := & workv1alpha1.Work {}
88
88
if err := c .Client .Get (ctx , req .NamespacedName , work ); err != nil {
@@ -104,19 +104,20 @@ func (c *WorkStatusController) Reconcile(ctx context.Context, req controllerrunt
104
104
105
105
clusterName , err := names .GetClusterName (work .GetNamespace ())
106
106
if err != nil {
107
- klog .Errorf ( "Failed to get member cluster name by %s. Error: %v." , work .GetNamespace (), err )
107
+ klog .ErrorS ( err , "Failed to get member cluster name from Work." , "namespace" , work .GetNamespace ())
108
108
return controllerruntime.Result {}, err
109
109
}
110
110
111
111
cluster , err := util .GetCluster (c .Client , clusterName )
112
112
if err != nil {
113
- klog .Errorf ( "Failed to the get given member cluster %s " , clusterName )
113
+ klog .ErrorS ( err , "Failed to get the given member cluster" , "cluster " , clusterName )
114
114
return controllerruntime.Result {}, err
115
115
}
116
116
117
117
if ! util .IsClusterReady (& cluster .Status ) {
118
- klog .Errorf ("Stop syncing the Work(%s/%s) to the cluster(%s) as not ready." , work .Namespace , work .Name , cluster .Name )
119
- return controllerruntime.Result {}, fmt .Errorf ("cluster(%s) not ready" , cluster .Name )
118
+ err := fmt .Errorf ("cluster(%s) not ready" , cluster .Name )
119
+ klog .ErrorS (err , "Stop syncing the Work to the cluster as not ready." , "namespace" , work .Namespace , "name" , work .Name , "cluster" , cluster .Name )
120
+ return controllerruntime.Result {}, err
120
121
}
121
122
122
123
return c .buildResourceInformers (cluster , work )
@@ -127,7 +128,7 @@ func (c *WorkStatusController) Reconcile(ctx context.Context, req controllerrunt
127
128
func (c * WorkStatusController ) buildResourceInformers (cluster * clusterv1alpha1.Cluster , work * workv1alpha1.Work ) (controllerruntime.Result , error ) {
128
129
err := c .registerInformersAndStart (cluster , work )
129
130
if err != nil {
130
- klog .Errorf ( "Failed to register informer for Work %s/%s. Error: %v." , work .GetNamespace (), work .GetName (), err )
131
+ klog .ErrorS ( err , "Failed to register informer for Work." , "namespace" , work .GetNamespace (), "name" , work .GetName ())
131
132
return controllerruntime.Result {}, err
132
133
}
133
134
return controllerruntime.Result {}, nil
@@ -171,13 +172,13 @@ func generateKey(obj interface{}) (util.QueueKey, error) {
171
172
func getClusterNameFromAnnotation (resource * unstructured.Unstructured ) (string , error ) {
172
173
workNamespace , exist := resource .GetAnnotations ()[workv1alpha2 .WorkNamespaceAnnotation ]
173
174
if ! exist {
174
- klog .V (5 ).Infof ("Ignore resource(kind=%s, %s/%s) which is not managed by Karmada." , resource .GetKind (), resource .GetNamespace (), resource .GetName ())
175
+ klog .V (5 ).InfoS ("Ignore resource which is not managed by Karmada." , "kind" , resource .GetKind (), "namespace" , resource .GetNamespace (), "name" , resource .GetName ())
175
176
return "" , nil
176
177
}
177
178
178
179
cluster , err := names .GetClusterName (workNamespace )
179
180
if err != nil {
180
- klog .Errorf ( "Failed to get cluster name from work namespace: %s, error: %v ." , workNamespace , err )
181
+ klog .ErrorS ( err , "Failed to get cluster name from Work ." , "namespace" , workNamespace )
181
182
return "" , err
182
183
}
183
184
return cluster , nil
@@ -188,8 +189,9 @@ func (c *WorkStatusController) syncWorkStatus(key util.QueueKey) error {
188
189
ctx := context .Background ()
189
190
fedKey , ok := key .(keys.FederatedKey )
190
191
if ! ok {
191
- klog .Errorf ("Failed to sync status as invalid key: %v" , key )
192
- return fmt .Errorf ("invalid key" )
192
+ err := fmt .Errorf ("invalid key" )
193
+ klog .ErrorS (err , "Failed to sync status" , "key" , key )
194
+ return err
193
195
}
194
196
195
197
observedObj , err := helper .GetObjectFromCache (c .RESTMapper , c .InformerManager , fedKey )
@@ -204,7 +206,7 @@ func (c *WorkStatusController) syncWorkStatus(key util.QueueKey) error {
204
206
workNamespace , nsExist := observedAnnotations [workv1alpha2 .WorkNamespaceAnnotation ]
205
207
workName , nameExist := observedAnnotations [workv1alpha2 .WorkNameAnnotation ]
206
208
if ! nsExist || ! nameExist {
207
- klog .Infof ( "Ignore object(%s) which not managed by Karmada." , fedKey .String ())
209
+ klog .InfoS ( "Ignoring object which is not managed by Karmada." , "object " , fedKey .String ())
208
210
return nil
209
211
}
210
212
@@ -215,7 +217,7 @@ func (c *WorkStatusController) syncWorkStatus(key util.QueueKey) error {
215
217
return nil
216
218
}
217
219
218
- klog .Errorf ( "Failed to get Work(%s/%s) from cache: %v" , workNamespace , workName , err )
220
+ klog .ErrorS ( err , "Failed to get Work from cache" , "namespace" , workNamespace , "name" , workName )
219
221
return err
220
222
}
221
223
@@ -228,7 +230,7 @@ func (c *WorkStatusController) syncWorkStatus(key util.QueueKey) error {
228
230
return err
229
231
}
230
232
231
- klog .Infof ("Reflecting the resource(kind=%s, %s/%s) status to the Work(%s/%s) ." , observedObj .GetKind (), observedObj .GetNamespace (), observedObj .GetName (), workNamespace , workName )
233
+ klog .InfoS ("Reflecting resource status to Work." , "kind" , observedObj .GetKind (), "resource" , observedObj .GetNamespace ()+ "/" + observedObj .GetName (), "namespace" , workNamespace , "name" , workName )
232
234
return c .reflectStatus (ctx , workObject , observedObj )
233
235
}
234
236
@@ -244,7 +246,7 @@ func (c *WorkStatusController) updateResource(ctx context.Context, observedObj *
244
246
245
247
clusterName , err := names .GetClusterName (workObject .Namespace )
246
248
if err != nil {
247
- klog .Errorf ( "Failed to get member cluster name: %v" , err )
249
+ klog .ErrorS ( err , "Failed to get member cluster name" , "cluster" , workObject . Namespace )
248
250
return err
249
251
}
250
252
@@ -255,7 +257,7 @@ func (c *WorkStatusController) updateResource(ctx context.Context, observedObj *
255
257
operationResult , updateErr := c .ObjectWatcher .Update (ctx , clusterName , desiredObj , observedObj )
256
258
metrics .CountUpdateResourceToCluster (updateErr , desiredObj .GetAPIVersion (), desiredObj .GetKind (), clusterName , string (operationResult ))
257
259
if updateErr != nil {
258
- klog .Errorf ( "Updating %s failed: %v" , fedKey .String (), updateErr )
260
+ klog .ErrorS ( updateErr , "Updating resource failed" , "resource" , fedKey .String ())
259
261
return updateErr
260
262
}
261
263
// We can't return even after a success updates, because that might lose the chance to collect status.
@@ -282,7 +284,7 @@ func (c *WorkStatusController) handleDeleteEvent(ctx context.Context, key keys.F
282
284
return nil
283
285
}
284
286
285
- klog .Errorf ( "Failed to get Work from cache: %v" , err )
287
+ klog .ErrorS ( err , "Failed to get Work from cache" )
286
288
return err
287
289
}
288
290
@@ -316,7 +318,7 @@ func (c *WorkStatusController) recreateResourceIfNeeded(ctx context.Context, wor
316
318
if reflect .DeepEqual (desiredGVK , workloadKey .GroupVersionKind ()) &&
317
319
manifest .GetNamespace () == workloadKey .Namespace &&
318
320
manifest .GetName () == workloadKey .Name {
319
- klog .Infof ("Recreating resource(%s). " , workloadKey .String ())
321
+ klog .InfoS ("Recreating resource." , "resource " , workloadKey .String ())
320
322
err := c .ObjectWatcher .Create (ctx , workloadKey .Cluster , manifest )
321
323
metrics .CountCreateResourceToCluster (err , workloadKey .GroupVersion ().String (), workloadKey .Kind , workloadKey .Cluster , true )
322
324
if err != nil {
@@ -349,16 +351,15 @@ func (c *WorkStatusController) updateAppliedCondition(ctx context.Context, work
349
351
})
350
352
351
353
if err != nil {
352
- klog .Errorf ( "Failed to update condition of work %s/%s: %s" , work .Namespace , work . Name , err . Error () )
354
+ klog .ErrorS ( err , "Failed to update condition of work." , "namespace" , work .Namespace , "name" , work . Name )
353
355
}
354
356
}
355
357
356
358
// reflectStatus grabs cluster object's running status then updates to its owner object(Work).
357
359
func (c * WorkStatusController ) reflectStatus (ctx context.Context , work * workv1alpha1.Work , clusterObj * unstructured.Unstructured ) error {
358
360
statusRaw , err := c .ResourceInterpreter .ReflectStatus (clusterObj )
359
361
if err != nil {
360
- klog .Errorf ("Failed to reflect status for object(%s/%s/%s) with resourceInterpreter, err: %v" ,
361
- clusterObj .GetKind (), clusterObj .GetNamespace (), clusterObj .GetName (), err )
362
+ klog .ErrorS (err , "Failed to reflect status for object with resourceInterpreter" , "kind" , clusterObj .GetKind (), "resource" , clusterObj .GetNamespace ()+ "/" + clusterObj .GetName ())
362
363
c .EventRecorder .Eventf (work , corev1 .EventTypeWarning , events .EventReasonReflectStatusFailed , "Reflect status for object(%s/%s/%s) failed, err: %s." , clusterObj .GetKind (), clusterObj .GetNamespace (), clusterObj .GetName (), err .Error ())
363
364
return err
364
365
}
@@ -389,7 +390,7 @@ func (c *WorkStatusController) reflectStatus(ctx context.Context, work *workv1al
389
390
func (c * WorkStatusController ) interpretHealth (clusterObj * unstructured.Unstructured , work * workv1alpha1.Work ) workv1alpha1.ResourceHealth {
390
391
// For kind that doesn't have health check, we treat it as healthy.
391
392
if ! c .ResourceInterpreter .HookEnabled (clusterObj .GroupVersionKind (), configv1alpha1 .InterpreterOperationInterpretHealth ) {
392
- klog .V (5 ).Infof ("skipping health assessment for object: %v %s/%s as missing customization and will treat it as healthy." , clusterObj .GroupVersionKind (), clusterObj .GetNamespace (), clusterObj .GetName ())
393
+ klog .V (5 ).InfoS ("skipping health assessment for object as customization missing; will treat it as healthy." , "kind" , clusterObj .GroupVersionKind (), "resource" , clusterObj .GetNamespace ()+ "/" + clusterObj .GetName ())
393
394
return workv1alpha1 .ResourceHealthy
394
395
}
395
396
@@ -496,7 +497,7 @@ func (c *WorkStatusController) registerInformersAndStart(cluster *clusterv1alpha
496
497
}
497
498
return nil
498
499
}(); err != nil {
499
- klog .Errorf ( "Failed to sync cache for cluster: %s, error: %v" , cluster .Name , err )
500
+ klog .ErrorS ( err , "Failed to sync cache for cluster" , "cluster" , cluster .Name )
500
501
c .InformerManager .Stop (cluster .Name )
501
502
return err
502
503
}
@@ -511,12 +512,12 @@ func (c *WorkStatusController) getGVRsFromWork(work *workv1alpha1.Work) (map[sch
511
512
workload := & unstructured.Unstructured {}
512
513
err := workload .UnmarshalJSON (manifest .Raw )
513
514
if err != nil {
514
- klog .Errorf ( "Failed to unmarshal workload. Error: %v." , err )
515
+ klog .ErrorS ( err , "Failed to unmarshal workload." )
515
516
return nil , err
516
517
}
517
518
gvr , err := restmapper .GetGroupVersionResource (c .RESTMapper , workload .GroupVersionKind ())
518
519
if err != nil {
519
- klog .Errorf ( "Failed to get GVR from GVK for resource %s/%s. Error: %v." , workload .GetNamespace (), workload .GetName (), err )
520
+ klog .ErrorS ( err , "Failed to get GVR from GVK for resource." , "namespace" , workload .GetNamespace (), "name" , workload .GetName ())
520
521
return nil , err
521
522
}
522
523
gvrTargets [gvr ] = true
@@ -533,7 +534,7 @@ func (c *WorkStatusController) getSingleClusterManager(cluster *clusterv1alpha1.
533
534
if singleClusterInformerManager == nil {
534
535
dynamicClusterClient , err := c .ClusterDynamicClientSetFunc (cluster .Name , c .Client , c .ClusterClientOption )
535
536
if err != nil {
536
- klog .Errorf ( "Failed to build dynamic cluster client for cluster %s. " , cluster .Name )
537
+ klog .ErrorS ( err , "Failed to build dynamic cluster client for cluster." , "cluster " , cluster .Name )
537
538
return nil , err
538
539
}
539
540
singleClusterInformerManager = c .InformerManager .ForCluster (dynamicClusterClient .ClusterName , dynamicClusterClient .DynamicClientSet , 0 )
@@ -560,7 +561,7 @@ func (c *WorkStatusController) SetupWithManager(mgr controllerruntime.Manager) e
560
561
func (c * WorkStatusController ) eventf (object * unstructured.Unstructured , eventType , reason , messageFmt string , args ... interface {}) {
561
562
ref , err := util .GenEventRef (object )
562
563
if err != nil {
563
- klog .Errorf ( "Ignore event(%s) as failing to build event reference for: kind=%s, %s due to %v" , reason , object .GetKind (), klog .KObj (object ), err )
564
+ klog .ErrorS ( err , "Ignoring event. Failed to build event reference." , "reason" , reason , "kind" , object .GetKind (), "reference" , klog .KObj (object ))
564
565
return
565
566
}
566
567
c .EventRecorder .Eventf (ref , eventType , reason , messageFmt , args ... )
0 commit comments