Skip to content

Commit c60a2e0

Browse files
zzklachlanKai Zhang
andauthored
Update logging format for clusterResourceBindingController (#6526)
Signed-off-by: Kai Zhang <[email protected]> Co-authored-by: Kai Zhang <[email protected]>
1 parent 74df2b4 commit c60a2e0

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

pkg/controllers/binding/cluster_resource_binding_controller.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ type ClusterResourceBindingController struct {
6969
// The Controller will requeue the Request to be processed again if an error is non-nil or
7070
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
7171
func (c *ClusterResourceBindingController) Reconcile(ctx context.Context, req controllerruntime.Request) (controllerruntime.Result, error) {
72-
klog.V(4).Infof("Reconciling ClusterResourceBinding %s.", req.NamespacedName.String())
72+
klog.V(4).InfoS("Reconciling ClusterResourceBinding.", "ClusterResourceBinding", req.NamespacedName.Name)
7373

7474
clusterResourceBinding := &workv1alpha2.ClusterResourceBinding{}
7575
if err := c.Client.Get(ctx, req.NamespacedName, clusterResourceBinding); err != nil {
@@ -82,9 +82,9 @@ func (c *ClusterResourceBindingController) Reconcile(ctx context.Context, req co
8282
}
8383

8484
if !clusterResourceBinding.DeletionTimestamp.IsZero() {
85-
klog.V(4).Infof("Begin to delete works owned by binding(%s).", req.NamespacedName.String())
85+
klog.V(4).InfoS("Begin to delete works owned by ClusterResourceBinding.", "ClusterResourceBinding", clusterResourceBinding.Name)
8686
if err := helper.DeleteWorks(ctx, c.Client, "", req.Name, clusterResourceBinding.Labels[workv1alpha2.ClusterResourceBindingPermanentIDLabel]); err != nil {
87-
klog.Errorf("Failed to delete works related to %s: %v", clusterResourceBinding.GetName(), err)
87+
klog.ErrorS(err, "Failed to delete works related to ClusterResourceBinding.", "ClusterResourceBinding", clusterResourceBinding.Name)
8888
return controllerruntime.Result{}, err
8989
}
9090
return c.removeFinalizer(ctx, clusterResourceBinding)
@@ -121,22 +121,22 @@ func (c *ClusterResourceBindingController) syncBinding(ctx context.Context, bind
121121
// So, just return without retry(requeue) would save unnecessary loop.
122122
return controllerruntime.Result{}, nil
123123
}
124-
klog.Errorf("Failed to fetch workload for clusterResourceBinding(%s). Error: %v.", binding.GetName(), err)
124+
klog.ErrorS(err, "Failed to fetch workload for ClusterResourceBinding.", "ClusterResourceBinding", binding.Name)
125125
return controllerruntime.Result{}, err
126126
}
127127

128128
start := time.Now()
129129
err = ensureWork(ctx, c.Client, c.ResourceInterpreter, workload, c.OverrideManager, binding, apiextensionsv1.ClusterScoped)
130130
metrics.ObserveSyncWorkLatency(err, start)
131131
if err != nil {
132-
klog.Errorf("Failed to transform clusterResourceBinding(%s) to works. Error: %v.", binding.GetName(), err)
132+
klog.ErrorS(err, "Failed to transform ClusterResourceBinding to works.", "ClusterResourceBinding", binding.Name)
133133
c.EventRecorder.Event(binding, corev1.EventTypeWarning, events.EventReasonSyncWorkFailed, err.Error())
134134
c.EventRecorder.Event(workload, corev1.EventTypeWarning, events.EventReasonSyncWorkFailed, err.Error())
135135
return controllerruntime.Result{}, err
136136
}
137137

138-
msg := fmt.Sprintf("Sync work of clusterResourceBinding(%s) successful.", binding.GetName())
139-
klog.V(4).Info(msg)
138+
msg := fmt.Sprintf("Sync work of ClusterResourceBinding(%s) successful.", binding.Name)
139+
klog.V(4).InfoS("Sync work of ClusterResourceBinding successful.", "ClusterResourceBinding", binding.Name)
140140
c.EventRecorder.Event(binding, corev1.EventTypeNormal, events.EventReasonSyncWorkSucceed, msg)
141141
c.EventRecorder.Event(workload, corev1.EventTypeNormal, events.EventReasonSyncWorkSucceed, msg)
142142
return controllerruntime.Result{}, nil
@@ -145,14 +145,14 @@ func (c *ClusterResourceBindingController) syncBinding(ctx context.Context, bind
145145
func (c *ClusterResourceBindingController) removeOrphanWorks(ctx context.Context, binding *workv1alpha2.ClusterResourceBinding) error {
146146
works, err := helper.FindOrphanWorks(ctx, c.Client, "", binding.Name, binding.Labels[workv1alpha2.ClusterResourceBindingPermanentIDLabel], helper.ObtainBindingSpecExistingClusters(binding.Spec))
147147
if err != nil {
148-
klog.Errorf("Failed to find orphan works by ClusterResourceBinding(%s). Error: %v.", binding.GetName(), err)
148+
klog.ErrorS(err, "Failed to find orphan works of ClusterResourceBinding.", "ClusterResourceBinding", binding.Name)
149149
c.EventRecorder.Event(binding, corev1.EventTypeWarning, events.EventReasonCleanupWorkFailed, err.Error())
150150
return err
151151
}
152152

153153
err = helper.RemoveOrphanWorks(ctx, c.Client, works)
154154
if err != nil {
155-
klog.Errorf("Failed to remove orphan works by clusterResourceBinding(%s). Error: %v.", binding.GetName(), err)
155+
klog.ErrorS(err, "Failed to remove orphan works of ClusterResourceBinding.", "ClusterResourceBinding", binding.Name)
156156
c.EventRecorder.Event(binding, corev1.EventTypeWarning, events.EventReasonCleanupWorkFailed, err.Error())
157157
return err
158158
}
@@ -183,15 +183,15 @@ func (c *ClusterResourceBindingController) newOverridePolicyFunc() handler.MapFu
183183

184184
readonlyBindingList := &workv1alpha2.ClusterResourceBindingList{}
185185
if err := c.Client.List(ctx, readonlyBindingList, &client.ListOptions{UnsafeDisableDeepCopy: ptr.To(true)}); err != nil {
186-
klog.Errorf("Failed to list clusterResourceBindings, error: %v", err)
186+
klog.ErrorS(err, "Failed to list ClusterResourceBindings")
187187
return nil
188188
}
189189

190190
var requests []reconcile.Request
191191
for _, binding := range readonlyBindingList.Items {
192192
// Nil resourceSelectors means matching all resources.
193193
if len(overrideRS) == 0 {
194-
klog.V(2).Infof("Enqueue ClusterResourceBinding(%s) as cluster override policy(%s) changes.", binding.Name, a.GetName())
194+
klog.V(2).InfoS("Enqueue ClusterResourceBinding as cluster override policy changes.", "ClusterResourceBinding", binding.Name, "ClusterOverridePolicy", a.GetName())
195195
requests = append(requests, reconcile.Request{NamespacedName: types.NamespacedName{Name: binding.Name}})
196196
continue
197197
}
@@ -200,13 +200,13 @@ func (c *ClusterResourceBindingController) newOverridePolicyFunc() handler.MapFu
200200
if err != nil {
201201
// If we cannot fetch resource template from binding, this may be due to the fact that the resource template has been deleted.
202202
// Just skip it so that it will not affect other bindings.
203-
klog.Errorf("Failed to fetch workload for clusterResourceBinding(%s). Error: %v.", binding.Name, err)
203+
klog.ErrorS(err, "Failed to fetch workload for ClusterResourceBinding.", "ClusterResourceBinding", binding.Name)
204204
continue
205205
}
206206

207207
for _, rs := range overrideRS {
208208
if util.ResourceMatches(workload, rs) {
209-
klog.V(2).Infof("Enqueue ClusterResourceBinding(%s) as cluster override policy(%s) changes.", binding.Name, a.GetName())
209+
klog.V(2).InfoS("Enqueue ClusterResourceBinding as cluster override policy changes.", "ClusterResourceBinding", binding.Name, "ClusterOverridePolicy", a.GetName())
210210
requests = append(requests, reconcile.Request{NamespacedName: types.NamespacedName{Name: binding.Name}})
211211
break
212212
}

pkg/controllers/binding/common.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ func ensureWork(
7575
if resourceInterpreter.HookEnabled(clonedWorkload.GroupVersionKind(), configv1alpha1.InterpreterOperationReviseReplica) {
7676
clonedWorkload, err = resourceInterpreter.ReviseReplica(clonedWorkload, int64(targetCluster.Replicas))
7777
if err != nil {
78-
klog.Errorf("Failed to revise replica for %s/%s/%s in cluster %s, err is: %v",
79-
workload.GetKind(), workload.GetNamespace(), workload.GetName(), targetCluster.Name, err)
78+
klog.ErrorS(err, "Failed to revise replica for workload in cluster.", "workloadKind", workload.GetKind(),
79+
"workloadNamespace", workload.GetNamespace(), "workloadName", workload.GetName(), "cluster", targetCluster.Name)
8080
errs = append(errs, err)
8181
continue
8282
}
@@ -93,8 +93,9 @@ func ensureWork(
9393
// setting this field as well.
9494
// Refer to: https://kubernetes.io/docs/concepts/workloads/controllers/job/#parallel-jobs.
9595
if err = helper.ApplyReplica(clonedWorkload, int64(jobCompletions[i].Replicas), util.CompletionsField); err != nil {
96-
klog.Errorf("Failed to apply Completions for %s/%s/%s in cluster %s, err is: %v",
97-
clonedWorkload.GetKind(), clonedWorkload.GetNamespace(), clonedWorkload.GetName(), targetCluster.Name, err)
96+
klog.ErrorS(err, "Failed to apply Completions for workload in cluster.",
97+
"workloadKind", clonedWorkload.GetKind(), "workloadNamespace", clonedWorkload.GetNamespace(),
98+
"workloadName", clonedWorkload.GetName(), "cluster", targetCluster.Name)
9899
errs = append(errs, err)
99100
continue
100101
}
@@ -103,8 +104,9 @@ func ensureWork(
103104
// We should call ApplyOverridePolicies last, as override rules have the highest priority
104105
cops, ops, err := overrideManager.ApplyOverridePolicies(clonedWorkload, targetCluster.Name)
105106
if err != nil {
106-
klog.Errorf("Failed to apply overrides for %s/%s/%s in cluster %s, err is: %v",
107-
clonedWorkload.GetKind(), clonedWorkload.GetNamespace(), clonedWorkload.GetName(), targetCluster.Name, err)
107+
klog.ErrorS(err, "Failed to apply overrides for workload in cluster.",
108+
"workloadKind", clonedWorkload.GetKind(), "workloadNamespace", clonedWorkload.GetNamespace(),
109+
"workloadName", clonedWorkload.GetName(), "cluster", targetCluster.Name)
108110
errs = append(errs, err)
109111
continue
110112
}
@@ -114,7 +116,7 @@ func ensureWork(
114116
annotations = mergeConflictResolution(clonedWorkload, bindingSpec.ConflictResolution, annotations)
115117
annotations, err = RecordAppliedOverrides(cops, ops, annotations)
116118
if err != nil {
117-
klog.Errorf("Failed to record appliedOverrides in cluster %s, Error: %v", targetCluster.Name, err)
119+
klog.ErrorS(err, "Failed to record appliedOverrides in cluster.", "cluster", targetCluster.Name)
118120
errs = append(errs, err)
119121
continue
120122
}

0 commit comments

Comments
 (0)