Skip to content

Commit f67dcc9

Browse files
authored
Merge pull request #6469 from zclyne/yifan/workloadrebalancer-json-logging
workloadbalancer controller now supports json format logging
2 parents 2d74aee + 4bf059f commit f67dcc9

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

pkg/controllers/workloadrebalancer/workloadrebalancer_controller.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ func (c *RebalancerController) SetupWithManager(mgr controllerruntime.Manager) e
7676
// The Controller will requeue the Request to be processed again if an error is non-nil or
7777
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
7878
func (c *RebalancerController) Reconcile(ctx context.Context, req controllerruntime.Request) (controllerruntime.Result, error) {
79-
klog.V(4).Infof("Reconciling for WorkloadRebalancer %s", req.Name)
79+
klog.V(4).InfoS("Reconciling for WorkloadRebalancer %s", req.Name)
8080

8181
// 1. get latest WorkloadRebalancer
8282
rebalancer := &appsv1alpha1.WorkloadRebalancer{}
8383
if err := c.Client.Get(ctx, req.NamespacedName, rebalancer); err != nil {
8484
if apierrors.IsNotFound(err) {
85-
klog.Infof("no need to reconcile WorkloadRebalancer for it not found")
85+
klog.InfoS("no need to reconcile WorkloadRebalancer for it not found")
8686
return controllerruntime.Result{}, nil
8787
}
8888
return controllerruntime.Result{}, err
@@ -203,7 +203,7 @@ func (c *RebalancerController) triggerReschedule(ctx context.Context, metadata m
203203
if resource.Workload.Namespace != "" {
204204
binding := &workv1alpha2.ResourceBinding{}
205205
if err := c.Client.Get(ctx, client.ObjectKey{Namespace: resource.Workload.Namespace, Name: bindingName}, binding); err != nil {
206-
klog.Errorf("get binding for resource %+v failed: %+v", resource.Workload, err)
206+
klog.ErrorS(err, "get binding for resource failed", "resource", resource.Workload)
207207
c.recordAndCountRebalancerFailed(&newStatus.ObservedWorkloads[i], &retryNum, err)
208208
continue
209209
}
@@ -212,7 +212,7 @@ func (c *RebalancerController) triggerReschedule(ctx context.Context, metadata m
212212
binding.Spec.RescheduleTriggeredAt = &metadata.CreationTimestamp
213213

214214
if err := c.Client.Update(ctx, binding); err != nil {
215-
klog.Errorf("update binding for resource %+v failed: %+v", resource.Workload, err)
215+
klog.ErrorS(err, "update binding for resource failed", "resource", resource.Workload)
216216
c.recordAndCountRebalancerFailed(&newStatus.ObservedWorkloads[i], &retryNum, err)
217217
continue
218218
}
@@ -221,7 +221,7 @@ func (c *RebalancerController) triggerReschedule(ctx context.Context, metadata m
221221
} else {
222222
clusterbinding := &workv1alpha2.ClusterResourceBinding{}
223223
if err := c.Client.Get(ctx, client.ObjectKey{Name: bindingName}, clusterbinding); err != nil {
224-
klog.Errorf("get cluster binding for resource %+v failed: %+v", resource.Workload, err)
224+
klog.ErrorS(err, "get cluster binding for resource failed", "resource", resource.Workload)
225225
c.recordAndCountRebalancerFailed(&newStatus.ObservedWorkloads[i], &retryNum, err)
226226
continue
227227
}
@@ -230,7 +230,7 @@ func (c *RebalancerController) triggerReschedule(ctx context.Context, metadata m
230230
clusterbinding.Spec.RescheduleTriggeredAt = &metadata.CreationTimestamp
231231

232232
if err := c.Client.Update(ctx, clusterbinding); err != nil {
233-
klog.Errorf("update cluster binding for resource %+v failed: %+v", resource.Workload, err)
233+
klog.ErrorS(err, "update cluster binding for resource failed", "resource", resource.Workload)
234234
c.recordAndCountRebalancerFailed(&newStatus.ObservedWorkloads[i], &retryNum, err)
235235
continue
236236
}
@@ -239,8 +239,8 @@ func (c *RebalancerController) triggerReschedule(ctx context.Context, metadata m
239239
}
240240
}
241241

242-
klog.V(4).Infof("Finish handling WorkloadRebalancer (%s), %d/%d resource success in all, while %d resource need retry",
243-
metadata.Name, successNum, len(newStatus.ObservedWorkloads), retryNum)
242+
klog.V(4).InfoS(fmt.Sprintf("Finish handling WorkloadRebalancer, %d/%d resource success in all, while %d resource need retry",
243+
successNum, len(newStatus.ObservedWorkloads), retryNum), "workloadRebalancer", metadata.Name, "successNum", successNum, "totalNum", len(newStatus.ObservedWorkloads), "retryNum", retryNum)
244244
return newStatus, retryNum
245245
}
246246

@@ -269,33 +269,33 @@ func (c *RebalancerController) updateWorkloadRebalancerStatus(ctx context.Contex
269269
modifiedRebalancer.Status = *newStatus
270270

271271
return retry.RetryOnConflict(retry.DefaultRetry, func() (err error) {
272-
klog.V(4).Infof("Start to patch WorkloadRebalancer(%s) status", rebalancer.Name)
272+
klog.V(4).InfoS("Start to patch WorkloadRebalancer status", "workloadRebalancer", rebalancer.Name)
273273
if err = c.Client.Status().Patch(ctx, modifiedRebalancer, client.MergeFrom(rebalancer)); err != nil {
274-
klog.Errorf("Failed to patch WorkloadRebalancer (%s) status, err: %+v", rebalancer.Name, err)
274+
klog.ErrorS(err, "Failed to patch WorkloadRebalancer status", "workloadRebalancer", rebalancer.Name)
275275
return err
276276
}
277-
klog.V(4).Infof("Patch WorkloadRebalancer(%s) successful", rebalancer.Name)
277+
klog.V(4).InfoS("Patch WorkloadRebalancer successful", "workloadRebalancer", rebalancer.Name)
278278
return nil
279279
})
280280
}
281281

282282
func (c *RebalancerController) deleteWorkloadRebalancer(ctx context.Context, rebalancer *appsv1alpha1.WorkloadRebalancer) error {
283-
klog.V(4).Infof("Start to clean up WorkloadRebalancer(%s)", rebalancer.Name)
283+
klog.V(4).InfoS("Start to clean up WorkloadRebalancer", "workloadRebalancer", rebalancer.Name)
284284

285285
options := &client.DeleteOptions{Preconditions: &metav1.Preconditions{ResourceVersion: &rebalancer.ResourceVersion}}
286286
if err := c.Client.Delete(ctx, rebalancer, options); err != nil {
287-
klog.Errorf("Cleaning up WorkloadRebalancer(%s) failed: %+v.", rebalancer.Name, err)
287+
klog.ErrorS(err, "Cleaning up WorkloadRebalancer failed", "workloadRebalancer", rebalancer.Name)
288288
return err
289289
}
290290

291-
klog.V(4).Infof("Cleaning up WorkloadRebalancer(%s) successful", rebalancer.Name)
291+
klog.V(4).InfoS("Cleaning up WorkloadRebalancer successful", "workloadRebalancer", rebalancer.Name)
292292
return nil
293293
}
294294

295295
func timeLeft(r *appsv1alpha1.WorkloadRebalancer) time.Duration {
296296
expireAt := r.Status.FinishTime.Add(time.Duration(*r.Spec.TTLSecondsAfterFinished) * time.Second)
297297
remainingTTL := time.Until(expireAt)
298298

299-
klog.V(4).Infof("Found Rebalancer(%s) finished at: %+v, remainingTTL: %+v", r.Name, r.Status.FinishTime.UTC(), remainingTTL)
299+
klog.V(4).InfoS("Check remaining TTL", "workloadRebalancer", r.Name, "FinishTime", r.Status.FinishTime.UTC(), "remainingTTL", remainingTTL)
300300
return remainingTTL
301301
}

0 commit comments

Comments
 (0)