Skip to content

Commit f69d55b

Browse files
author
sdutta133
committed
continue using klog with structured logging
Signed-off-by: sdutta133 <[email protected]> linter issue Signed-off-by: sdutta133 <[email protected]> fix logging pattern Signed-off-by: sdutta133 <[email protected]>
1 parent b20715e commit f69d55b

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

pkg/controllers/execution/execution_controller.go

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ type Controller struct {
8080
// The Controller will requeue the Request to be processed again if an error is non-nil or
8181
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
8282
func (c *Controller) Reconcile(ctx context.Context, req controllerruntime.Request) (controllerruntime.Result, error) {
83-
klog.V(4).Infof("Reconciling Work %s", req.NamespacedName.String())
83+
klog.V(4).InfoS("Reconciling Work", "work", req.NamespacedName.String())
8484

8585
work := &workv1alpha1.Work{}
8686
if err := c.Client.Get(ctx, req.NamespacedName, work); err != nil {
@@ -94,13 +94,13 @@ func (c *Controller) Reconcile(ctx context.Context, req controllerruntime.Reques
9494

9595
clusterName, err := names.GetClusterName(work.Namespace)
9696
if err != nil {
97-
klog.Errorf("Failed to get member cluster name for work %s/%s", work.Namespace, work.Name)
97+
klog.ErrorS(err, "Failed to get member cluster name for work", "namespace", work.Namespace, "name", work.Name)
9898
return controllerruntime.Result{}, err
9999
}
100100

101101
cluster, err := util.GetCluster(c.Client, clusterName)
102102
if err != nil {
103-
klog.Errorf("Failed to get the given member cluster %s", clusterName)
103+
klog.ErrorS(err, "Failed to get the given member cluster", "cluster", clusterName)
104104
return controllerruntime.Result{}, err
105105
}
106106

@@ -113,18 +113,19 @@ func (c *Controller) Reconcile(ctx context.Context, req controllerruntime.Reques
113113
}
114114

115115
if err := c.updateWorkDispatchingConditionIfNeeded(ctx, work); err != nil {
116-
klog.Errorf("Failed to update work condition type %s. err is %v", workv1alpha1.WorkDispatching, err)
116+
klog.ErrorS(err, "Failed to update work condition type", "type", workv1alpha1.WorkDispatching)
117117
return controllerruntime.Result{}, err
118118
}
119119

120120
if util.IsWorkSuspendDispatching(work) {
121-
klog.V(4).Infof("Skip syncing work(%s/%s) for cluster(%s) as work dispatch is suspended.", work.Namespace, work.Name, cluster.Name)
121+
klog.V(4).InfoS("Skip syncing work for cluster as work dispatch is suspended.", "namespace", work.Namespace, "name", work.Name, "cluster", cluster.Name)
122122
return controllerruntime.Result{}, nil
123123
}
124124

125125
if !util.IsClusterReady(&cluster.Status) {
126-
klog.Errorf("Stop syncing the work(%s/%s) for the cluster(%s) as cluster not ready.", work.Namespace, work.Name, cluster.Name)
127-
return controllerruntime.Result{}, fmt.Errorf("cluster(%s) not ready", cluster.Name)
126+
err := fmt.Errorf("cluster(%s) not ready", cluster.Name)
127+
klog.ErrorS(err, "Stop syncing the work for the cluster as cluster not ready.", "namespace", work.Namespace, "name", work.Name, "cluster", cluster.Name)
128+
return controllerruntime.Result{}, err
128129
}
129130

130131
return c.syncWork(ctx, clusterName, work)
@@ -161,18 +162,18 @@ func (c *Controller) syncWork(ctx context.Context, clusterName string, work *wor
161162
func (c *Controller) handleWorkDelete(ctx context.Context, work *workv1alpha1.Work, cluster *clusterv1alpha1.Cluster) error {
162163
if ptr.Deref(work.Spec.PreserveResourcesOnDeletion, false) {
163164
if err := c.cleanupPolicyClaimMetadata(ctx, work, cluster); err != nil {
164-
klog.Errorf("Failed to remove annotations and labels in on cluster(%s)", cluster.Name)
165+
klog.ErrorS(err, "Failed to remove annotations and labels", "cluster", cluster.Name)
165166
return err
166167
}
167-
klog.V(4).Infof("Preserving resource on deletion from work(%s/%s) on cluster(%s)", work.Namespace, work.Name, cluster.Name)
168+
klog.V(4).InfoS("Preserving resource on deletion from work on cluster", "namespace", work.Namespace, "name", work.Name, "cluster", cluster.Name)
168169
return nil
169170
}
170171

171172
// Abort deleting workload if cluster is unready when unjoining cluster, otherwise the unjoin process will be failed.
172173
if util.IsClusterReady(&cluster.Status) {
173174
err := c.tryDeleteWorkload(ctx, cluster.Name, work)
174175
if err != nil {
175-
klog.Errorf("Failed to delete work %v, namespace is %v, err is %v", work.Name, work.Namespace, err)
176+
klog.ErrorS(err, "Failed to delete work", "name", work.Name, "namespace", work.Namespace)
176177
return err
177178
}
178179
} else if cluster.DeletionTimestamp.IsZero() { // cluster is unready, but not terminating
@@ -186,21 +187,19 @@ func (c *Controller) cleanupPolicyClaimMetadata(ctx context.Context, work *workv
186187
for _, manifest := range work.Spec.Workload.Manifests {
187188
workload := &unstructured.Unstructured{}
188189
if err := workload.UnmarshalJSON(manifest.Raw); err != nil {
189-
klog.Errorf("Failed to unmarshal workload from work(%s/%s), error is: %v", err, work.GetNamespace(), work.GetName())
190+
klog.ErrorS(err, "Failed to unmarshal workload from work", "namespace", work.GetNamespace(), "name", work.GetName())
190191
return err
191192
}
192193

193194
fedKey, err := keys.FederatedKeyFunc(cluster.Name, workload)
194195
if err != nil {
195-
klog.Errorf("Failed to get the federated key resource(kind=%s, %s/%s) from member cluster(%s), err is %v ",
196-
workload.GetKind(), workload.GetNamespace(), workload.GetName(), cluster.Name, err)
196+
klog.ErrorS(err, "Failed to get the federated key resource from member cluster", "kind", workload.GetKind(), "namespace", workload.GetNamespace(), "name", workload.GetName(), "cluster", cluster.Name)
197197
return err
198198
}
199199

200200
clusterObj, err := helper.GetObjectFromCache(c.RESTMapper, c.InformerManager, fedKey)
201201
if err != nil {
202-
klog.Errorf("Failed to get the resource(kind=%s, %s/%s) from member cluster(%s) cache, err is %v ",
203-
workload.GetKind(), workload.GetNamespace(), workload.GetName(), cluster.Name, err)
202+
klog.ErrorS(err, "Failed to get the resource from member cluster cache", "kind", workload.GetKind(), "namespace", workload.GetNamespace(), "name", workload.GetName(), "cluster", cluster.Name)
204203
return err
205204
}
206205

@@ -215,7 +214,7 @@ func (c *Controller) cleanupPolicyClaimMetadata(ctx context.Context, work *workv
215214
operationResult, err := c.ObjectWatcher.Update(ctx, cluster.Name, workload, clusterObj)
216215
metrics.CountUpdateResourceToCluster(err, workload.GetAPIVersion(), workload.GetKind(), cluster.Name, string(operationResult))
217216
if err != nil {
218-
klog.Errorf("Failed to update metadata in the given member cluster %v, err is %v", cluster.Name, err)
217+
klog.ErrorS(err, "Failed to update metadata in the given member cluster", "cluster", cluster.Name)
219218
return err
220219
}
221220
}
@@ -229,14 +228,14 @@ func (c *Controller) tryDeleteWorkload(ctx context.Context, clusterName string,
229228
workload := &unstructured.Unstructured{}
230229
err := workload.UnmarshalJSON(manifest.Raw)
231230
if err != nil {
232-
klog.Errorf("Failed to unmarshal workload, error is: %v", err)
231+
klog.ErrorS(err, "Failed to unmarshal workload")
233232
return err
234233
}
235234

236235
err = c.ObjectWatcher.Delete(ctx, clusterName, workload)
237236
metrics.CountDeleteResourceFromCluster(err, workload.GetAPIVersion(), workload.GetKind(), clusterName)
238237
if err != nil {
239-
klog.Errorf("Failed to delete resource in the given member cluster %v, err is %v", clusterName, err)
238+
klog.ErrorS(err, "Failed to delete resource in the given member cluster", "cluster", clusterName)
240239
return err
241240
}
242241
}
@@ -266,13 +265,13 @@ func (c *Controller) syncToClusters(ctx context.Context, clusterName string, wor
266265
workload := &unstructured.Unstructured{}
267266
err := workload.UnmarshalJSON(manifest.Raw)
268267
if err != nil {
269-
klog.Errorf("Failed to unmarshal workload of the work(%s/%s), error is: %v", work.GetNamespace(), work.GetName(), err)
268+
klog.ErrorS(err, "Failed to unmarshal workload of the work", "namespace", work.GetNamespace(), "name", work.GetName())
270269
errs = append(errs, err)
271270
continue
272271
}
273272

274273
if err = c.tryCreateOrUpdateWorkload(ctx, clusterName, workload); err != nil {
275-
klog.Errorf("Failed to create or update resource(%v/%v) in the given member cluster %s, err is %v", workload.GetNamespace(), workload.GetName(), clusterName, err)
274+
klog.ErrorS(err, "Failed to create or update resource in the given member cluster", "namespace", workload.GetNamespace(), "name", workload.GetName(), "cluster", clusterName)
276275
c.eventf(workload, corev1.EventTypeWarning, events.EventReasonSyncWorkloadFailed, "Failed to create or update resource(%s) in member cluster(%s): %v", klog.KObj(workload), clusterName, err)
277276
errs = append(errs, err)
278277
continue
@@ -286,15 +285,15 @@ func (c *Controller) syncToClusters(ctx context.Context, clusterName string, wor
286285
message := fmt.Sprintf("Failed to apply all manifests (%d/%d): %s", syncSucceedNum, total, errors.NewAggregate(errs).Error())
287286
err := c.updateAppliedCondition(ctx, work, metav1.ConditionFalse, "AppliedFailed", message)
288287
if err != nil {
289-
klog.Errorf("Failed to update applied status for given work %v, namespace is %v, err is %v", work.Name, work.Namespace, err)
288+
klog.ErrorS(err, "Failed to update applied status for given work", "name", work.Name, "namespace", work.Namespace)
290289
errs = append(errs, err)
291290
}
292291
return errors.NewAggregate(errs)
293292
}
294293

295294
err := c.updateAppliedCondition(ctx, work, metav1.ConditionTrue, "AppliedSuccessful", "Manifest has been successfully applied")
296295
if err != nil {
297-
klog.Errorf("Failed to update applied status for given work %v, namespace is %v, err is %v", work.Name, work.Namespace, err)
296+
klog.ErrorS(err, "Failed to update applied status for given work", "name", work.Name, "namespace", work.Namespace)
298297
return err
299298
}
300299

@@ -304,14 +303,14 @@ func (c *Controller) syncToClusters(ctx context.Context, clusterName string, wor
304303
func (c *Controller) tryCreateOrUpdateWorkload(ctx context.Context, clusterName string, workload *unstructured.Unstructured) error {
305304
fedKey, err := keys.FederatedKeyFunc(clusterName, workload)
306305
if err != nil {
307-
klog.Errorf("Failed to get FederatedKey %s, error: %v", workload.GetName(), err)
306+
klog.ErrorS(err, "Failed to get FederatedKey", "name", workload.GetName())
308307
return err
309308
}
310309

311310
clusterObj, err := helper.GetObjectFromCache(c.RESTMapper, c.InformerManager, fedKey)
312311
if err != nil {
313312
if !apierrors.IsNotFound(err) {
314-
klog.Errorf("Failed to get the resource(kind=%s, %s/%s) from member cluster(%s), err is %v ", workload.GetKind(), workload.GetNamespace(), workload.GetName(), clusterName, err)
313+
klog.ErrorS(err, "Failed to get the resource from member cluster", "kind", workload.GetKind(), "namespace", workload.GetNamespace(), "name", workload.GetName(), "cluster", clusterName)
315314
return err
316315
}
317316
err = c.ObjectWatcher.Create(ctx, clusterName, workload)
@@ -389,7 +388,7 @@ func (c *Controller) setStatusCondition(ctx context.Context, work *workv1alpha1.
389388
func (c *Controller) eventf(object *unstructured.Unstructured, eventType, reason, messageFmt string, args ...interface{}) {
390389
ref, err := util.GenEventRef(object)
391390
if err != nil {
392-
klog.Errorf("Ignore event(%s) as failed to build event reference for: kind=%s, %s due to %v", reason, object.GetKind(), klog.KObj(object), err)
391+
klog.ErrorS(err, "Ignore event as failed to build event reference", "reason", reason, "kind", object.GetKind(), "object", klog.KObj(object))
393392
return
394393
}
395394
c.EventRecorder.Eventf(ref, eventType, reason, messageFmt, args...)

0 commit comments

Comments
 (0)