Skip to content

Commit 312fbd0

Browse files
authored
Merge pull request #6435 from jabellard/cluster-ctrl-logs
JSON logging for cluster controllers
2 parents a0178b7 + 80c4221 commit 312fbd0

File tree

2 files changed

+33
-35
lines changed

2 files changed

+33
-35
lines changed

pkg/controllers/cluster/cluster_controller.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func (n *clusterHealthData) deepCopy() *clusterHealthData {
154154
// The Controller will requeue the Request to be processed again if an error is non-nil or
155155
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
156156
func (c *Controller) Reconcile(ctx context.Context, req controllerruntime.Request) (controllerruntime.Result, error) {
157-
klog.V(4).Infof("Reconciling cluster %s", req.NamespacedName.Name)
157+
klog.V(4).InfoS("Reconciling cluster", "cluster", req.NamespacedName.Name)
158158

159159
cluster := &clusterv1alpha1.Cluster{}
160160
if err := c.Client.Get(ctx, req.NamespacedName, cluster); err != nil {
@@ -175,13 +175,13 @@ func (c *Controller) Reconcile(ctx context.Context, req controllerruntime.Reques
175175

176176
// Start starts an asynchronous loop that monitors the status of cluster.
177177
func (c *Controller) Start(ctx context.Context) error {
178-
klog.Infof("Starting cluster health monitor")
179-
defer klog.Infof("Shutting cluster health monitor")
178+
klog.InfoS("Starting cluster health monitor")
179+
defer klog.InfoS("Shutting cluster health monitor")
180180

181181
// Incorporate the results of cluster health signal pushed from cluster-status-controller to master.
182182
go wait.UntilWithContext(ctx, func(ctx context.Context) {
183183
if err := c.monitorClusterHealth(ctx); err != nil {
184-
klog.Errorf("Error monitoring cluster health: %v", err)
184+
klog.ErrorS(err, "Error monitoring cluster health")
185185
}
186186
}, c.ClusterMonitorPeriod)
187187
<-ctx.Done()
@@ -219,18 +219,18 @@ func (c *Controller) syncCluster(ctx context.Context, cluster *clusterv1alpha1.C
219219

220220
func (c *Controller) removeCluster(ctx context.Context, cluster *clusterv1alpha1.Cluster) (controllerruntime.Result, error) {
221221
if err := c.removeExecutionSpace(ctx, cluster); err != nil {
222-
klog.Errorf("Failed to remove execution space %s: %v", cluster.Name, err)
222+
klog.ErrorS(err, "Failed to remove execution space", "cluster", cluster.Name)
223223
c.EventRecorder.Event(cluster, corev1.EventTypeWarning, events.EventReasonRemoveExecutionSpaceFailed, err.Error())
224224
return controllerruntime.Result{}, err
225225
}
226226
msg := fmt.Sprintf("Removed execution space for cluster(%s).", cluster.Name)
227227
c.EventRecorder.Event(cluster, corev1.EventTypeNormal, events.EventReasonRemoveExecutionSpaceSucceed, msg)
228228

229229
if exist, err := c.ExecutionSpaceExistForCluster(ctx, cluster.Name); err != nil {
230-
klog.Errorf("Failed to check weather the execution space exist in the given member cluster or not, error is: %v", err)
230+
klog.ErrorS(err, "Failed to check execution space existence", "cluster", cluster.Name)
231231
return controllerruntime.Result{}, err
232232
} else if exist {
233-
klog.Infof("Requeuing operation until the cluster(%s) execution space deleted", cluster.Name)
233+
klog.InfoS("Requeuing operation until execution space deleted", "cluster", cluster.Name)
234234
return controllerruntime.Result{RequeueAfter: c.CleanupCheckInterval}, nil
235235
}
236236

@@ -287,12 +287,12 @@ func (c *Controller) removeExecutionSpace(ctx context.Context, cluster *clusterv
287287
// delete finalizers of work objects when the sync-mode is pull and cluster status is notready or unknown
288288
if cluster.Spec.SyncMode == clusterv1alpha1.Pull && !util.IsClusterReady(&cluster.Status) {
289289
if err := c.deleteFinalizerForWorks(ctx, executionSpaceObj); err != nil {
290-
klog.Errorf("Error while deleting finalizers of work which in %s: %s", executionSpaceName, err)
290+
klog.ErrorS(err, "Error while deleting finalizers of work", "namespace", executionSpaceName)
291291
return err
292292
}
293293
}
294294
if err := c.Client.Delete(ctx, executionSpaceObj); err != nil && !apierrors.IsNotFound(err) {
295-
klog.Errorf("Error while deleting namespace %s: %s", executionSpaceName, err)
295+
klog.ErrorS(err, "Error while deleting namespace", "namespace", executionSpaceName)
296296
return err
297297
}
298298

@@ -306,11 +306,11 @@ func (c *Controller) ExecutionSpaceExistForCluster(ctx context.Context, clusterN
306306
executionSpaceObj := &corev1.Namespace{}
307307
err := c.Client.Get(ctx, types.NamespacedName{Name: executionSpaceName}, executionSpaceObj)
308308
if apierrors.IsNotFound(err) {
309-
klog.V(2).Infof("Execution space(%s) no longer exists", executionSpaceName)
309+
klog.V(2).InfoS("Execution space no longer exists", "namespace", executionSpaceName)
310310
return false, nil
311311
}
312312
if err != nil {
313-
klog.Errorf("Failed to get execution space %v, err is %v ", executionSpaceName, err)
313+
klog.ErrorS(err, "Failed to get execution space", "namespace", executionSpaceName)
314314
return false, err
315315
}
316316
return true, nil
@@ -323,7 +323,7 @@ func (c *Controller) deleteFinalizerForWorks(ctx context.Context, workSpace *cor
323323
Namespace: workSpace.Name,
324324
})
325325
if err != nil {
326-
klog.Errorf("Failed to list works in %s: %s", workSpace.Name, err)
326+
klog.ErrorS(err, "Failed to list works in namespace", "namespace", workSpace.Name)
327327
return err
328328
}
329329

@@ -386,7 +386,7 @@ func (c *Controller) createExecutionSpace(ctx context.Context, cluster *clusterv
386386
err := c.Client.Get(ctx, types.NamespacedName{Name: executionSpaceName}, executionSpaceObj)
387387
if err != nil {
388388
if !apierrors.IsNotFound(err) {
389-
klog.Errorf("Failed to get namespace(%s): %v", executionSpaceName, err)
389+
klog.ErrorS(err, "Failed to get namespace", "namespace", executionSpaceName)
390390
return err
391391
}
392392

@@ -401,11 +401,11 @@ func (c *Controller) createExecutionSpace(ctx context.Context, cluster *clusterv
401401
}
402402
err = c.Client.Create(ctx, executionSpace)
403403
if err != nil {
404-
klog.Errorf("Failed to create execution space for cluster(%s): %v", cluster.Name, err)
404+
klog.ErrorS(err, "Failed to create execution space", "cluster", cluster.Name)
405405
return err
406406
}
407407
msg := fmt.Sprintf("Created execution space(%s) for cluster(%s).", executionSpaceName, cluster.Name)
408-
klog.V(2).Info(msg)
408+
klog.V(2).InfoS(msg)
409409
c.EventRecorder.Event(cluster, corev1.EventTypeNormal, events.EventReasonCreateExecutionSpaceSucceed, msg)
410410
}
411411

@@ -438,7 +438,7 @@ func (c *Controller) monitorClusterHealth(ctx context.Context) (err error) {
438438
}
439439
return false, nil
440440
}); err != nil {
441-
klog.Errorf("Update health of Cluster '%v' from Controller error: %v. Skipping.", cluster.Name, err)
441+
klog.ErrorS(err, "Failed to update health, skipping", "cluster", cluster.Name)
442442
continue
443443
}
444444
}
@@ -531,8 +531,7 @@ func (c *Controller) tryUpdateClusterHealth(ctx context.Context, cluster *cluste
531531
for _, clusterConditionType := range clusterConditionTypes {
532532
currentCondition := meta.FindStatusCondition(cluster.Status.Conditions, clusterConditionType)
533533
if currentCondition == nil {
534-
klog.V(2).Infof("Condition %v of cluster %v was never updated by cluster-status-controller",
535-
clusterConditionType, cluster.Name)
534+
klog.V(2).InfoS("Condition was never updated by cluster-status-controller", "condition", clusterConditionType, "cluster", cluster.Name)
536535
cluster.Status.Conditions = append(cluster.Status.Conditions, metav1.Condition{
537536
Type: clusterConditionType,
538537
Status: metav1.ConditionUnknown,
@@ -541,8 +540,9 @@ func (c *Controller) tryUpdateClusterHealth(ctx context.Context, cluster *cluste
541540
LastTransitionTime: nowTimestamp,
542541
})
543542
} else {
544-
klog.V(2).Infof("Cluster %v hasn't been updated for %+v. Last %v is: %+v",
543+
message := fmt.Sprintf("Cluster %v hasn't been updated for %+v. Last %v is: %+v",
545544
cluster.Name, metav1.Now().Time.Sub(clusterHealth.probeTimestamp.Time), clusterConditionType, currentCondition)
545+
klog.V(2).InfoS(message, "cluster", cluster.Name)
546546
if currentCondition.Status != metav1.ConditionUnknown {
547547
currentCondition.Status = metav1.ConditionUnknown
548548
currentCondition.Reason = "ClusterStatusUnknown"
@@ -556,7 +556,7 @@ func (c *Controller) tryUpdateClusterHealth(ctx context.Context, cluster *cluste
556556

557557
if !equality.Semantic.DeepEqual(currentReadyCondition, observedReadyCondition) {
558558
if err := c.Status().Update(ctx, cluster); err != nil {
559-
klog.Errorf("Error updating cluster %s: %v", cluster.Name, err)
559+
klog.ErrorS(err, "Error updating cluster status", "cluster", cluster.Name)
560560
return err
561561
}
562562
clusterHealth = &clusterHealthData{

pkg/controllers/cluster/taint_manager.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ type NoExecuteTaintManager struct {
6767
// The Controller will requeue the Request to be processed again if an error is non-nil or
6868
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
6969
func (tc *NoExecuteTaintManager) Reconcile(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
70-
klog.V(4).Infof("Reconciling cluster %s for taint manager", req.NamespacedName.Name)
70+
klog.V(4).InfoS("Reconciling cluster for taint manager", "cluster", req.NamespacedName.Name)
7171

7272
cluster := &clusterv1alpha1.Cluster{}
7373
if err := tc.Client.Get(ctx, req.NamespacedName, cluster); err != nil {
@@ -98,7 +98,7 @@ func (tc *NoExecuteTaintManager) syncCluster(ctx context.Context, cluster *clust
9898
for i := range rbList.Items {
9999
key, err := keys.FederatedKeyFunc(cluster.Name, &rbList.Items[i])
100100
if err != nil {
101-
klog.Warningf("Failed to generate key for obj: %s", rbList.Items[i].GetObjectKind().GroupVersionKind())
101+
klog.ErrorS(err, "Failed to generate federated key for ResourceBinding", "gvk", rbList.Items[i].GetObjectKind().GroupVersionKind())
102102
continue
103103
}
104104
tc.bindingEvictionWorker.Add(key)
@@ -115,7 +115,7 @@ func (tc *NoExecuteTaintManager) syncCluster(ctx context.Context, cluster *clust
115115
for i := range crbList.Items {
116116
key, err := keys.FederatedKeyFunc(cluster.Name, &crbList.Items[i])
117117
if err != nil {
118-
klog.Warningf("Failed to generate key for obj: %s", crbList.Items[i].GetObjectKind().GroupVersionKind())
118+
klog.ErrorS(err, "Failed to generate federated key for ClusterResourceBinding", "gvk", crbList.Items[i].GetObjectKind().GroupVersionKind())
119119
continue
120120
}
121121
tc.clusterBindingEvictionWorker.Add(key)
@@ -148,12 +148,12 @@ func (tc *NoExecuteTaintManager) Start(ctx context.Context) error {
148148
func (tc *NoExecuteTaintManager) syncBindingEviction(key util.QueueKey) error {
149149
fedKey, ok := key.(keys.FederatedKey)
150150
if !ok {
151-
klog.Errorf("Failed to sync binding eviction as invalid key: %v", key)
152-
return fmt.Errorf("invalid key")
151+
err := fmt.Errorf("invalid key type %T", key)
152+
klog.ErrorS(err, "Failed to sync binding eviction due to invalid key", "key", key)
153+
return err
153154
}
154155
cluster := fedKey.Cluster
155-
klog.V(4).Infof("Begin to sync ResourceBinding(%s) with taintManager for Cluster(%s)",
156-
fedKey.ClusterWideKey.NamespaceKey(), cluster)
156+
klog.V(4).InfoS("Begin syncing ResourceBinding for taint eviction", "binding", fedKey.ClusterWideKey.NamespaceKey(), "cluster", cluster)
157157

158158
binding := &workv1alpha2.ResourceBinding{}
159159
if err := tc.Client.Get(context.TODO(), types.NamespacedName{Namespace: fedKey.Namespace, Name: fedKey.Name}, binding); err != nil {
@@ -205,8 +205,7 @@ func (tc *NoExecuteTaintManager) syncBindingEviction(key util.QueueKey) error {
205205
klog.ErrorS(err, "Failed to update binding", "binding", klog.KObj(binding))
206206
return err
207207
}
208-
klog.V(2).Infof("Success to evict Cluster(%s) from ResourceBinding(%s) schedule result",
209-
fedKey.Cluster, fedKey.ClusterWideKey.NamespaceKey())
208+
klog.V(2).InfoS("Evicted cluster from ResourceBinding", "cluster", fedKey.Cluster, "binding", fedKey.ClusterWideKey.NamespaceKey())
210209
} else if tolerationTime > 0 {
211210
tc.bindingEvictionWorker.AddAfter(fedKey, tolerationTime)
212211
}
@@ -217,12 +216,12 @@ func (tc *NoExecuteTaintManager) syncBindingEviction(key util.QueueKey) error {
217216
func (tc *NoExecuteTaintManager) syncClusterBindingEviction(key util.QueueKey) error {
218217
fedKey, ok := key.(keys.FederatedKey)
219218
if !ok {
220-
klog.Errorf("Failed to sync cluster binding eviction as invalid key: %v", key)
221-
return fmt.Errorf("invalid key")
219+
err := fmt.Errorf("invalid key type %T", key)
220+
klog.ErrorS(err, "Failed to sync cluster binding eviction due to invalid key", "key", key)
221+
return err
222222
}
223223
cluster := fedKey.Cluster
224-
klog.V(4).Infof("Begin to sync ClusterResourceBinding(%s) with taintManager for Cluster(%s)",
225-
fedKey.ClusterWideKey.NamespaceKey(), cluster)
224+
klog.V(4).InfoS("Begin syncing ClusterResourceBinding with taintManager", "binding", fedKey.ClusterWideKey.NamespaceKey(), "cluster", cluster)
226225

227226
binding := &workv1alpha2.ClusterResourceBinding{}
228227
if err := tc.Client.Get(context.TODO(), types.NamespacedName{Name: fedKey.Name}, binding); err != nil {
@@ -264,8 +263,7 @@ func (tc *NoExecuteTaintManager) syncClusterBindingEviction(key util.QueueKey) e
264263
klog.ErrorS(err, "Failed to update cluster binding", "binding", binding.Name)
265264
return err
266265
}
267-
klog.V(2).Infof("Success to evict Cluster(%s) from ClusterResourceBinding(%s) schedule result",
268-
fedKey.ClusterWideKey.NamespaceKey(), fedKey.Cluster)
266+
klog.V(2).InfoS("Evicted cluster from ClusterResourceBinding", "cluster", fedKey.Cluster, "binding", fedKey.ClusterWideKey.NamespaceKey())
269267
} else if tolerationTime > 0 {
270268
tc.clusterBindingEvictionWorker.AddAfter(fedKey, tolerationTime)
271269
return nil

0 commit comments

Comments
 (0)