Skip to content

Commit cf45b57

Browse files
authored
Merge pull request #6540 from zhangsquared/log-taint-policy-controller
Use structured logging for cluster taint policy controller
2 parents c60a2e0 + d2694e5 commit cf45b57

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

pkg/controllers/taint/clustertaintpolicy_controller.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,18 @@ type ClusterTaintPolicyController struct {
5858
// The Controller will requeue the Request to be processed again if an error is non-nil or
5959
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
6060
func (c *ClusterTaintPolicyController) Reconcile(ctx context.Context, req controllerruntime.Request) (controllerruntime.Result, error) {
61-
klog.V(4).Infof("Reconciling Cluster %s.", req.Name)
61+
klog.V(4).InfoS("Reconciling Cluster", "cluster", req.Name)
6262

6363
clusterObj := &clusterv1alpha1.Cluster{}
6464
if err := c.Client.Get(ctx, req.NamespacedName, clusterObj); err != nil {
65-
klog.Errorf("Failed to get Cluster(%s), error: %v", req.Name, err)
65+
klog.ErrorS(err, "Failed to get Cluster", "cluster", req.Name)
6666
return controllerruntime.Result{}, client.IgnoreNotFound(err)
6767
}
6868

6969
clusterTaintPolicyList := &policyv1alpha1.ClusterTaintPolicyList{}
7070
listOption := &client.ListOptions{UnsafeDisableDeepCopy: ptr.To(true)}
7171
if err := c.Client.List(ctx, clusterTaintPolicyList, listOption); err != nil {
72-
klog.Errorf("Failed to list ClusterTaintPolicy, error: %v", err)
72+
klog.ErrorS(err, "Failed to list ClusterTaintPolicy")
7373
return controllerruntime.Result{}, err
7474
}
7575

@@ -99,18 +99,18 @@ func (c *ClusterTaintPolicyController) Reconcile(ctx context.Context, req contro
9999
objPatch := client.MergeFrom(clusterObj)
100100
err := c.Client.Patch(ctx, clusterCopyObj, objPatch)
101101
if err != nil {
102-
klog.Errorf("Failed to patch Cluster(%s), error: %v", req.Name, err)
102+
klog.ErrorS(err, "Failed to patch Cluster", "cluster", req.Name)
103103
c.EventRecorder.Event(clusterCopyObj, corev1.EventTypeWarning, events.EventReasonTaintClusterFailed, err.Error())
104104
return controllerruntime.Result{}, err
105105
}
106106

107107
msg := fmt.Sprintf("Update Cluster(%s) with taints(%v) succeed", req.Name,
108108
utilhelper.GenerateTaintsMessage(clusterCopyObj.Spec.Taints))
109-
klog.Info(msg)
109+
klog.InfoS("Successfully updated cluster taints", "cluster", req.Name, "taints", clusterCopyObj.Spec.Taints)
110110
c.EventRecorder.Event(clusterCopyObj, corev1.EventTypeNormal, events.EventReasonTaintClusterSucceed, msg)
111111
return controllerruntime.Result{}, nil
112112
}
113-
klog.V(4).Infof("Cluster(%s) taints are up to date.", req.Name)
113+
klog.V(4).InfoS("Cluster taints are up to date.", "cluster", req.Name)
114114
return controllerruntime.Result{}, nil
115115
}
116116

@@ -147,7 +147,8 @@ func conditionMatches(conditions []metav1.Condition, matchConditions []policyv1a
147147
}
148148
}
149149
default:
150-
klog.Errorf("Unsupported MatchCondition operator: %s", matchCondition.Operator)
150+
err := fmt.Errorf("unsupported MatchCondition operator")
151+
klog.ErrorS(err, "operator", matchCondition.Operator)
151152
return false
152153
}
153154
}
@@ -235,7 +236,7 @@ func (c *ClusterTaintPolicyController) SetupWithManager(mgr controllerruntime.Ma
235236
clusterList := &clusterv1alpha1.ClusterList{}
236237
listOption := &client.ListOptions{UnsafeDisableDeepCopy: ptr.To(true)}
237238
if err := c.Client.List(ctx, clusterList, listOption); err != nil {
238-
klog.Errorf("Failed to list Cluster, error: %v", err)
239+
klog.ErrorS(err, "Failed to list Cluster")
239240
return nil
240241
}
241242

0 commit comments

Comments
 (0)