Skip to content

Commit 295c69f

Browse files
authored
Merge pull request #6578 from cbaenziger/work_status_controller_structured_logging
Work status controller structured logging
2 parents f67dcc9 + de65bc7 commit 295c69f

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

pkg/controllers/status/work_status_controller.go

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ type WorkStatusController struct {
8282
// The Controller will requeue the Request to be processed again if an error is non-nil or
8383
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
8484
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)
8686

8787
work := &workv1alpha1.Work{}
8888
if err := c.Client.Get(ctx, req.NamespacedName, work); err != nil {
@@ -104,19 +104,20 @@ func (c *WorkStatusController) Reconcile(ctx context.Context, req controllerrunt
104104

105105
clusterName, err := names.GetClusterName(work.GetNamespace())
106106
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())
108108
return controllerruntime.Result{}, err
109109
}
110110

111111
cluster, err := util.GetCluster(c.Client, clusterName)
112112
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)
114114
return controllerruntime.Result{}, err
115115
}
116116

117117
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
120121
}
121122

122123
return c.buildResourceInformers(cluster, work)
@@ -127,7 +128,7 @@ func (c *WorkStatusController) Reconcile(ctx context.Context, req controllerrunt
127128
func (c *WorkStatusController) buildResourceInformers(cluster *clusterv1alpha1.Cluster, work *workv1alpha1.Work) (controllerruntime.Result, error) {
128129
err := c.registerInformersAndStart(cluster, work)
129130
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())
131132
return controllerruntime.Result{}, err
132133
}
133134
return controllerruntime.Result{}, nil
@@ -171,13 +172,13 @@ func generateKey(obj interface{}) (util.QueueKey, error) {
171172
func getClusterNameFromAnnotation(resource *unstructured.Unstructured) (string, error) {
172173
workNamespace, exist := resource.GetAnnotations()[workv1alpha2.WorkNamespaceAnnotation]
173174
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())
175176
return "", nil
176177
}
177178

178179
cluster, err := names.GetClusterName(workNamespace)
179180
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)
181182
return "", err
182183
}
183184
return cluster, nil
@@ -188,8 +189,9 @@ func (c *WorkStatusController) syncWorkStatus(key util.QueueKey) error {
188189
ctx := context.Background()
189190
fedKey, ok := key.(keys.FederatedKey)
190191
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
193195
}
194196

195197
observedObj, err := helper.GetObjectFromCache(c.RESTMapper, c.InformerManager, fedKey)
@@ -204,7 +206,7 @@ func (c *WorkStatusController) syncWorkStatus(key util.QueueKey) error {
204206
workNamespace, nsExist := observedAnnotations[workv1alpha2.WorkNamespaceAnnotation]
205207
workName, nameExist := observedAnnotations[workv1alpha2.WorkNameAnnotation]
206208
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())
208210
return nil
209211
}
210212

@@ -215,7 +217,7 @@ func (c *WorkStatusController) syncWorkStatus(key util.QueueKey) error {
215217
return nil
216218
}
217219

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)
219221
return err
220222
}
221223

@@ -228,7 +230,7 @@ func (c *WorkStatusController) syncWorkStatus(key util.QueueKey) error {
228230
return err
229231
}
230232

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)
232234
return c.reflectStatus(ctx, workObject, observedObj)
233235
}
234236

@@ -244,7 +246,7 @@ func (c *WorkStatusController) updateResource(ctx context.Context, observedObj *
244246

245247
clusterName, err := names.GetClusterName(workObject.Namespace)
246248
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)
248250
return err
249251
}
250252

@@ -255,7 +257,7 @@ func (c *WorkStatusController) updateResource(ctx context.Context, observedObj *
255257
operationResult, updateErr := c.ObjectWatcher.Update(ctx, clusterName, desiredObj, observedObj)
256258
metrics.CountUpdateResourceToCluster(updateErr, desiredObj.GetAPIVersion(), desiredObj.GetKind(), clusterName, string(operationResult))
257259
if updateErr != nil {
258-
klog.Errorf("Updating %s failed: %v", fedKey.String(), updateErr)
260+
klog.ErrorS(updateErr, "Updating resource failed", "resource", fedKey.String())
259261
return updateErr
260262
}
261263
// 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
282284
return nil
283285
}
284286

285-
klog.Errorf("Failed to get Work from cache: %v", err)
287+
klog.ErrorS(err, "Failed to get Work from cache")
286288
return err
287289
}
288290

@@ -316,7 +318,7 @@ func (c *WorkStatusController) recreateResourceIfNeeded(ctx context.Context, wor
316318
if reflect.DeepEqual(desiredGVK, workloadKey.GroupVersionKind()) &&
317319
manifest.GetNamespace() == workloadKey.Namespace &&
318320
manifest.GetName() == workloadKey.Name {
319-
klog.Infof("Recreating resource(%s).", workloadKey.String())
321+
klog.InfoS("Recreating resource.", "resource", workloadKey.String())
320322
err := c.ObjectWatcher.Create(ctx, workloadKey.Cluster, manifest)
321323
metrics.CountCreateResourceToCluster(err, workloadKey.GroupVersion().String(), workloadKey.Kind, workloadKey.Cluster, true)
322324
if err != nil {
@@ -349,16 +351,15 @@ func (c *WorkStatusController) updateAppliedCondition(ctx context.Context, work
349351
})
350352

351353
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)
353355
}
354356
}
355357

356358
// reflectStatus grabs cluster object's running status then updates to its owner object(Work).
357359
func (c *WorkStatusController) reflectStatus(ctx context.Context, work *workv1alpha1.Work, clusterObj *unstructured.Unstructured) error {
358360
statusRaw, err := c.ResourceInterpreter.ReflectStatus(clusterObj)
359361
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())
362363
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())
363364
return err
364365
}
@@ -389,7 +390,7 @@ func (c *WorkStatusController) reflectStatus(ctx context.Context, work *workv1al
389390
func (c *WorkStatusController) interpretHealth(clusterObj *unstructured.Unstructured, work *workv1alpha1.Work) workv1alpha1.ResourceHealth {
390391
// For kind that doesn't have health check, we treat it as healthy.
391392
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())
393394
return workv1alpha1.ResourceHealthy
394395
}
395396

@@ -496,7 +497,7 @@ func (c *WorkStatusController) registerInformersAndStart(cluster *clusterv1alpha
496497
}
497498
return nil
498499
}(); 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)
500501
c.InformerManager.Stop(cluster.Name)
501502
return err
502503
}
@@ -511,12 +512,12 @@ func (c *WorkStatusController) getGVRsFromWork(work *workv1alpha1.Work) (map[sch
511512
workload := &unstructured.Unstructured{}
512513
err := workload.UnmarshalJSON(manifest.Raw)
513514
if err != nil {
514-
klog.Errorf("Failed to unmarshal workload. Error: %v.", err)
515+
klog.ErrorS(err, "Failed to unmarshal workload.")
515516
return nil, err
516517
}
517518
gvr, err := restmapper.GetGroupVersionResource(c.RESTMapper, workload.GroupVersionKind())
518519
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())
520521
return nil, err
521522
}
522523
gvrTargets[gvr] = true
@@ -533,7 +534,7 @@ func (c *WorkStatusController) getSingleClusterManager(cluster *clusterv1alpha1.
533534
if singleClusterInformerManager == nil {
534535
dynamicClusterClient, err := c.ClusterDynamicClientSetFunc(cluster.Name, c.Client, c.ClusterClientOption)
535536
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)
537538
return nil, err
538539
}
539540
singleClusterInformerManager = c.InformerManager.ForCluster(dynamicClusterClient.ClusterName, dynamicClusterClient.DynamicClientSet, 0)
@@ -560,7 +561,7 @@ func (c *WorkStatusController) SetupWithManager(mgr controllerruntime.Manager) e
560561
func (c *WorkStatusController) eventf(object *unstructured.Unstructured, eventType, reason, messageFmt string, args ...interface{}) {
561562
ref, err := util.GenEventRef(object)
562563
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))
564565
return
565566
}
566567
c.EventRecorder.Eventf(ref, eventType, reason, messageFmt, args...)

0 commit comments

Comments
 (0)