Skip to content

Commit db76ef0

Browse files
authored
Merge pull request #6565 from jennryaz/erya-slog2
Use structured logging for cluster status controller
2 parents 5f4bd5e + a479580 commit db76ef0

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

pkg/controllers/status/cluster_status_controller.go

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ type ClusterStatusController struct {
125125
// The Controller will requeue the Request to be processed again if an error is non-nil or
126126
// Result.Requeue is true, otherwise upon completion it will requeue the reconcile key after the duration.
127127
func (c *ClusterStatusController) Reconcile(ctx context.Context, req controllerruntime.Request) (controllerruntime.Result, error) {
128-
klog.V(4).Infof("Syncing cluster status: %s", req.NamespacedName.Name)
128+
klog.V(4).InfoS("Syncing cluster status", "cluster", req.NamespacedName.Name)
129129

130130
cluster := &clusterv1alpha1.Cluster{}
131131
if err := c.Client.Get(ctx, req.NamespacedName, cluster); err != nil {
@@ -151,9 +151,9 @@ func (c *ClusterStatusController) Reconcile(ctx context.Context, req controllerr
151151
}
152152

153153
// start syncing status only when the finalizer is present on the given Cluster to
154-
// avoid conflict with cluster controller.
154+
// avoid conflict with the cluster controller.
155155
if !controllerutil.ContainsFinalizer(cluster, util.ClusterControllerFinalizer) {
156-
klog.V(2).Infof("Waiting finalizer present for member cluster: %s", cluster.Name)
156+
klog.V(2).InfoS("Waiting finalizer present for member cluster", "cluster", cluster.Name)
157157
return controllerruntime.Result{Requeue: true}, nil
158158
}
159159

@@ -190,7 +190,7 @@ func (c *ClusterStatusController) syncClusterStatus(ctx context.Context, cluster
190190
// create a ClusterClient for the given member cluster
191191
clusterClient, err := c.ClusterClientSetFunc(cluster.Name, c.Client, c.ClusterClientOption)
192192
if err != nil {
193-
klog.Errorf("Failed to create a ClusterClient for the given member cluster: %v, err is : %v", cluster.Name, err)
193+
klog.ErrorS(err, "Failed to create a ClusterClient for the given member cluster", "cluster", cluster.Name)
194194
return setStatusCollectionFailedCondition(ctx, c.Client, cluster, fmt.Sprintf("failed to create a ClusterClient: %v", err))
195195
}
196196

@@ -200,8 +200,8 @@ func (c *ClusterStatusController) syncClusterStatus(ctx context.Context, cluster
200200

201201
// cluster is offline after retry timeout, update cluster status immediately and return.
202202
if !online && readyCondition.Status != metav1.ConditionTrue {
203-
klog.V(2).Infof("Cluster(%s) still offline after %s, ensuring offline is set.",
204-
cluster.Name, c.ClusterFailureThreshold.Duration)
203+
klog.V(2).InfoS("Cluster still offline after ensuring offline is set",
204+
"cluster", cluster.Name, "duration", c.ClusterFailureThreshold.Duration)
205205
return updateStatusCondition(ctx, c.Client, cluster, *readyCondition)
206206
}
207207

@@ -235,7 +235,7 @@ func (c *ClusterStatusController) setCurrentClusterStatus(clusterClient *util.Cl
235235
var conditions []metav1.Condition
236236
clusterVersion, err := getKubernetesVersion(clusterClient)
237237
if err != nil {
238-
klog.Errorf("Failed to get Kubernetes version for Cluster %s. Error: %v.", cluster.GetName(), err)
238+
klog.ErrorS(err, "Failed to get Kubernetes version for Cluster", "cluster", cluster.GetName())
239239
}
240240
currentClusterStatus.KubernetesVersion = clusterVersion
241241

@@ -245,11 +245,11 @@ func (c *ClusterStatusController) setCurrentClusterStatus(clusterClient *util.Cl
245245
if len(apiEnables) == 0 {
246246
apiEnablementCondition = util.NewCondition(clusterv1alpha1.ClusterConditionCompleteAPIEnablements,
247247
apiEnablementEmptyAPIEnablements, "collected empty APIEnablements from the cluster", metav1.ConditionFalse)
248-
klog.Errorf("Failed to get any APIs installed in Cluster %s. Error: %v.", cluster.GetName(), err)
248+
klog.ErrorS(err, "Failed to get any APIs installed in Cluster", "cluster", cluster.GetName())
249249
} else if err != nil {
250250
apiEnablementCondition = util.NewCondition(clusterv1alpha1.ClusterConditionCompleteAPIEnablements,
251251
apiEnablementPartialAPIEnablements, fmt.Sprintf("might collect partial APIEnablements(%d) from the cluster", len(apiEnables)), metav1.ConditionFalse)
252-
klog.Warningf("Maybe get partial(%d) APIs installed in Cluster %s. Error: %v.", len(apiEnables), cluster.GetName(), err)
252+
klog.ErrorS(err, "Collected partial number of APIs installed in Cluster", "numApiEnablements", len(apiEnables), "cluster", cluster.GetName())
253253
} else {
254254
apiEnablementCondition = util.NewCondition(clusterv1alpha1.ClusterConditionCompleteAPIEnablements,
255255
apiEnablementsComplete, "collected complete APIEnablements from the cluster", metav1.ConditionTrue)
@@ -261,19 +261,19 @@ func (c *ClusterStatusController) setCurrentClusterStatus(clusterClient *util.Cl
261261
// get or create informer for pods and nodes in member cluster
262262
clusterInformerManager, err := c.buildInformerForCluster(clusterClient)
263263
if err != nil {
264-
klog.Errorf("Failed to get or create informer for Cluster %s. Error: %v.", cluster.GetName(), err)
264+
klog.ErrorS(err, "Failed to get or create informer for Cluster", "cluster", cluster.GetName())
265265
// in large-scale clusters, the timeout may occur.
266266
// if clusterInformerManager fails to be built, should be returned, otherwise, it may cause a nil pointer
267267
return nil, err
268268
}
269269
nodes, err := listNodes(clusterInformerManager)
270270
if err != nil {
271-
klog.Errorf("Failed to list nodes for Cluster %s. Error: %v.", cluster.GetName(), err)
271+
klog.ErrorS(err, "Failed to list nodes for Cluster", "cluster", cluster.GetName())
272272
}
273273

274274
pods, err := listPods(clusterInformerManager)
275275
if err != nil {
276-
klog.Errorf("Failed to list pods for Cluster %s. Error: %v.", cluster.GetName(), err)
276+
klog.ErrorS(err, "Failed to list pods for Cluster", "cluster", cluster.GetName())
277277
}
278278
currentClusterStatus.NodeSummary = getNodeSummary(nodes)
279279
currentClusterStatus.ResourceSummary = getResourceSummary(nodes, pods)
@@ -296,7 +296,7 @@ func (c *ClusterStatusController) updateStatusIfNeeded(ctx context.Context, clus
296296
meta.SetStatusCondition(&currentClusterStatus.Conditions, condition)
297297
}
298298
if !equality.Semantic.DeepEqual(cluster.Status, currentClusterStatus) {
299-
klog.V(4).Infof("Start to update cluster status: %s", cluster.Name)
299+
klog.V(4).InfoS("Start to update cluster status", "cluster", cluster.Name)
300300
err := retry.RetryOnConflict(retry.DefaultRetry, func() (err error) {
301301
_, err = helper.UpdateStatus(ctx, c.Client, cluster, func() error {
302302
cluster.Status.KubernetesVersion = currentClusterStatus.KubernetesVersion
@@ -311,7 +311,7 @@ func (c *ClusterStatusController) updateStatusIfNeeded(ctx context.Context, clus
311311
return err
312312
})
313313
if err != nil {
314-
klog.Errorf("Failed to update health status of the member cluster: %v, err is : %v", cluster.Name, err)
314+
klog.ErrorS(err, "Failed to update health status of the member cluster", "cluster", cluster.Name)
315315
return err
316316
}
317317
}
@@ -320,7 +320,7 @@ func (c *ClusterStatusController) updateStatusIfNeeded(ctx context.Context, clus
320320
}
321321

322322
func updateStatusCondition(ctx context.Context, c client.Client, cluster *clusterv1alpha1.Cluster, conditions ...metav1.Condition) error {
323-
klog.V(4).Infof("Start to update cluster(%s) status condition", cluster.Name)
323+
klog.V(4).InfoS("Start to update cluster status condition", "cluster", cluster.Name)
324324
err := retry.RetryOnConflict(retry.DefaultRetry, func() (err error) {
325325
_, err = helper.UpdateStatus(ctx, c, cluster, func() error {
326326
for _, condition := range conditions {
@@ -331,7 +331,7 @@ func updateStatusCondition(ctx context.Context, c client.Client, cluster *cluste
331331
return err
332332
})
333333
if err != nil {
334-
klog.Errorf("Failed to update status condition of the member cluster: %v, err is : %v", cluster.Name, err)
334+
klog.ErrorS(err, "Failed to update status condition of the member cluster", "cluster", cluster.Name)
335335
return err
336336
}
337337
return nil
@@ -344,7 +344,7 @@ func (c *ClusterStatusController) initializeGenericInformerManagerForCluster(clu
344344

345345
dynamicClient, err := c.ClusterDynamicClientSetFunc(clusterClient.ClusterName, c.Client, c.ClusterClientOption)
346346
if err != nil {
347-
klog.Errorf("Failed to build dynamic cluster client for cluster %s.", clusterClient.ClusterName)
347+
klog.ErrorS(err, "Failed to build dynamic cluster client", "cluster", clusterClient.ClusterName)
348348
return
349349
}
350350
c.GenericInformerManager.ForCluster(clusterClient.ClusterName, dynamicClient.DynamicClientSet, 0)
@@ -366,7 +366,7 @@ func (c *ClusterStatusController) buildInformerForCluster(clusterClient *util.Cl
366366
if !singleClusterInformerManager.IsInformerSynced(gvr) {
367367
allSynced = false
368368
if _, err := singleClusterInformerManager.Lister(gvr); err != nil {
369-
klog.Errorf("Failed to get the lister for gvr %s: %v", gvr.String(), err)
369+
klog.ErrorS(err, "Failed to get the lister for gvr", "gvr", gvr.String())
370370
}
371371
}
372372
}
@@ -389,7 +389,7 @@ func (c *ClusterStatusController) buildInformerForCluster(clusterClient *util.Cl
389389
}
390390
return nil
391391
}(); err != nil {
392-
klog.Errorf("Failed to sync cache for cluster: %s, error: %v", clusterClient.ClusterName, err)
392+
klog.ErrorS(err, "Failed to sync cache for cluster", "cluster", clusterClient.ClusterName)
393393
c.TypedInformerManager.Stop(clusterClient.ClusterName)
394394
return nil, err
395395
}
@@ -422,12 +422,12 @@ func (c *ClusterStatusController) initLeaseController(cluster *clusterv1alpha1.C
422422

423423
// start syncing lease
424424
go func() {
425-
klog.Infof("Starting syncing lease for cluster: %s", cluster.Name)
425+
klog.InfoS("Starting syncing lease for cluster", "cluster", cluster.Name)
426426

427427
// lease controller will keep running until the stop channel is closed(context is canceled)
428428
clusterLeaseController.Run(ctx)
429429

430-
klog.Infof("Stop syncing lease for cluster: %s", cluster.Name)
430+
klog.InfoS("Stop syncing lease for cluster", "cluster", cluster.Name)
431431
c.ClusterLeaseControllers.Delete(cluster.Name) // ensure the cache is clean
432432
}()
433433
}
@@ -440,12 +440,12 @@ func getClusterHealthStatus(clusterClient *util.ClusterClient) (online, healthy
440440
}
441441

442442
if err != nil {
443-
klog.Errorf("Failed to do cluster health check for cluster %v, err is : %v ", clusterClient.ClusterName, err)
443+
klog.ErrorS(err, "Failed to do cluster health check for cluster", "cluster", clusterClient.ClusterName)
444444
return false, false
445445
}
446446

447447
if healthStatus != http.StatusOK {
448-
klog.Infof("Member cluster %v isn't healthy", clusterClient.ClusterName)
448+
klog.InfoS("Member cluster isn't healthy", "cluster", clusterClient.ClusterName)
449449
return true, false
450450
}
451451

@@ -627,7 +627,8 @@ func getNodeAvailable(allocatable corev1.ResourceList, podResources *util.Resour
627627
// When too many pods have been created, scheduling will fail so that the allocating pods number may be huge.
628628
// If allowedPodNumber is less than or equal to 0, we don't allow more pods to be created.
629629
if allowedPodNumber <= 0 {
630-
klog.Warningf("The number of schedulable Pods on the node is less than or equal to 0, we won't add the node to cluster resource models.")
630+
klog.InfoS("The number of schedulable Pods on the node is less than or equal to 0, " +
631+
"we won't add the node to cluster resource models.")
631632
return nil
632633
}
633634

@@ -647,7 +648,7 @@ func getAllocatableModelings(cluster *clusterv1alpha1.Cluster, nodes []*corev1.N
647648
}
648649
modelingSummary, err := modeling.InitSummary(cluster.Spec.ResourceModels)
649650
if err != nil {
650-
klog.Errorf("Failed to init cluster summary from cluster resource models for Cluster %s. Error: %v.", cluster.GetName(), err)
651+
klog.ErrorS(err, "Failed to init cluster summary from cluster resource models for Cluster", "cluster", cluster.GetName())
651652
return nil
652653
}
653654

0 commit comments

Comments
 (0)