Skip to content

Commit bb4ec94

Browse files
authored
Merge pull request #6570 from NUMBKV/struct-log-for-deploy-replicas-syncer-controller
JSON logging for deployment replicas syncer controller
2 parents 3ff577c + 88b8fdb commit bb4ec94

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

pkg/controllers/deploymentreplicassyncer/deployment_replicas_syncer_controller.go

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package deploymentreplicassyncer
1919
import (
2020
"context"
2121
"encoding/json"
22+
"fmt"
2223
"time"
2324

2425
appsv1 "k8s.io/api/apps/v1"
@@ -71,7 +72,10 @@ var predicateFunc = predicate.Funcs{
7172
}
7273

7374
if oldObj.Spec.Replicas == nil || newObj.Spec.Replicas == nil {
74-
klog.Errorf("spec.replicas field unexpectedly become nil: %+v, %+v", oldObj.Spec.Replicas, newObj.Spec.Replicas)
75+
klog.ErrorS(fmt.Errorf("spec.replicas is nil in either old or new deployment object"),
76+
"Unexpected nil spec.replicas",
77+
"oldReplicas", oldObj.Spec.Replicas, "newReplicas", newObj.Spec.Replicas,
78+
"namespace", oldObj.Namespace, "name", oldObj.Name)
7579
return false
7680
}
7781

@@ -99,7 +103,7 @@ func (r *DeploymentReplicasSyncer) SetupWithManager(mgr controllerruntime.Manage
99103
// The Controller will requeue the Request to be processed again if an error is non-nil or
100104
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
101105
func (r *DeploymentReplicasSyncer) Reconcile(ctx context.Context, req controllerruntime.Request) (controllerruntime.Result, error) {
102-
klog.V(4).Infof("Reconciling for Deployment %s/%s", req.Namespace, req.Name)
106+
klog.V(4).InfoS("Reconciling for Deployment", "namespace", req.Namespace, "name", req.Name)
103107

104108
deployment := &appsv1.Deployment{}
105109
binding := &workv1alpha2.ResourceBinding{}
@@ -108,7 +112,8 @@ func (r *DeploymentReplicasSyncer) Reconcile(ctx context.Context, req controller
108112
// 1. get latest binding
109113
if err := r.Client.Get(ctx, client.ObjectKey{Namespace: req.Namespace, Name: bindingName}, binding); err != nil {
110114
if apierrors.IsNotFound(err) {
111-
klog.Infof("no need to update deployment replicas for binding not found")
115+
klog.InfoS("no need to update deployment replicas as binding was not found",
116+
"namespace", req.Namespace, "name", req.Name)
112117
return controllerruntime.Result{}, nil
113118
}
114119
return controllerruntime.Result{}, err
@@ -122,15 +127,18 @@ func (r *DeploymentReplicasSyncer) Reconcile(ctx context.Context, req controller
122127
// 3. get latest deployment
123128
if err := r.Client.Get(ctx, req.NamespacedName, deployment); err != nil {
124129
if apierrors.IsNotFound(err) {
125-
klog.Infof("no need to update deployment replicas for deployment not found")
130+
klog.InfoS("no need to update deployment replicas as deployment was not found",
131+
"namespace", req.Namespace, "name", req.Name)
126132
return controllerruntime.Result{}, nil
127133
}
128134
return controllerruntime.Result{}, err
129135
}
130136

131137
// 4. if replicas in spec already the same as in status, no need to update replicas
132138
if deployment.Spec.Replicas != nil && *deployment.Spec.Replicas == deployment.Status.Replicas {
133-
klog.Infof("replicas in spec field (%d) already equal to in status field (%d)", *deployment.Spec.Replicas, deployment.Status.Replicas)
139+
klog.InfoS("replicas in spec field are already equal to those in status field",
140+
"specReplicas", *deployment.Spec.Replicas, "statusReplicas", deployment.Status.Replicas,
141+
"namespace", deployment.Namespace, "name", deployment.Name)
134142
return controllerruntime.Result{}, nil
135143
}
136144

@@ -143,55 +151,64 @@ func (r *DeploymentReplicasSyncer) Reconcile(ctx context.Context, req controller
143151
oldReplicas := *deployment.Spec.Replicas
144152
deployment.Spec.Replicas = &deployment.Status.Replicas
145153
if err := r.Client.Update(ctx, deployment); err != nil {
146-
klog.Errorf("failed to update deployment (%s/%s) replicas: %+v", deployment.Namespace, deployment.Name, err)
154+
klog.ErrorS(err, "failed to update deployment replicas", "namespace", deployment.Namespace, "name", deployment.Name)
147155
return controllerruntime.Result{}, err
148156
}
149157

150-
klog.Infof("successfully update deployment (%s/%s) replicas from %d to %d", deployment.Namespace,
151-
deployment.Name, oldReplicas, deployment.Status.Replicas)
158+
klog.InfoS("successfully update deployment replicas",
159+
"namespace", deployment.Namespace, "name", deployment.Name,
160+
"oldReplicas", oldReplicas, "newReplicas", deployment.Status.Replicas)
152161

153162
return controllerruntime.Result{}, nil
154163
}
155164

156165
// isDeploymentStatusCollected judge whether deployment modification in spec has taken effect and its status has been collected.
157166
func isDeploymentStatusCollected(deployment *appsv1.Deployment, binding *workv1alpha2.ResourceBinding) bool {
158167
// make sure the replicas change in deployment.spec can sync to binding.spec, otherwise retry
159-
if deployment.Spec.Replicas == nil || *deployment.Spec.Replicas != binding.Spec.Replicas {
160-
klog.V(4).Infof("wait until replicas of binding (%d) equal to replicas of deployment (%d)",
161-
binding.Spec.Replicas, *deployment.Spec.Replicas)
168+
if deployment.Spec.Replicas == nil {
169+
// should never happen, as karmada-apiserver defaults `deployment.spec.Replicas` to 1 if it is not set.
170+
klog.ErrorS(nil, "deployment replicas is nil", "namespace", deployment.Namespace, "name", deployment.Name)
171+
return false
172+
}
173+
if *deployment.Spec.Replicas != binding.Spec.Replicas {
174+
klog.V(4).InfoS("wait until binding replicas are equal to deployment replicas",
175+
"bindingReplicas", binding.Spec.Replicas, "deploymentReplicas", *deployment.Spec.Replicas,
176+
"namespace", deployment.Namespace, "deploymentName", deployment.Name, "bindingName", binding.Name)
162177
return false
163178
}
164179

165180
// make sure the scheduler observed generation equal to generation in binding, otherwise retry
166181
if binding.Generation != binding.Status.SchedulerObservedGeneration {
167-
klog.V(4).Infof("wait until scheduler observed generation (%d) equal to generation in binding (%d)",
168-
binding.Status.SchedulerObservedGeneration, binding.Generation)
182+
klog.V(4).InfoS("wait until scheduler observed generation is equal to generation in binding",
183+
"schedulerObservedGeneration", binding.Status.SchedulerObservedGeneration, "generation", binding.Generation,
184+
"namespace", binding.Namespace, "name", binding.Name)
169185
return false
170186
}
171187

172188
// make sure the number of aggregated status in binding is as expected.
173189
if len(binding.Status.AggregatedStatus) != len(binding.Spec.Clusters) {
174-
klog.V(4).Infof("wait until all clusters status collected, got: %d, expected: %d",
175-
len(binding.Status.AggregatedStatus), len(binding.Spec.Clusters))
190+
klog.V(4).InfoS("wait until all clusters' statuses are collected",
191+
"got", len(binding.Status.AggregatedStatus), "expected", len(binding.Spec.Clusters),
192+
"namespace", binding.Namespace, "name", binding.Name)
176193
return false
177194
}
178195

179196
// make sure the status.replicas in binding has been collected from work to binding.
180197
bindingStatusSumReplicas := int32(0)
181198
for _, status := range binding.Status.AggregatedStatus {
182199
if status.Status == nil {
183-
klog.V(4).Infof("wait until aggregated status of cluster %s collected", status.ClusterName)
200+
klog.V(4).InfoS("wait until aggregated status of cluster is collected", "cluster", status.ClusterName)
184201
return false
185202
}
186203
itemStatus := &appsv1.DeploymentStatus{}
187204
if err := json.Unmarshal(status.Status.Raw, itemStatus); err != nil {
188-
klog.Errorf("unmarshal status.raw of cluster %s in binding failed: %+v", status.ClusterName, err)
205+
klog.ErrorS(err, "Failed to unmarshal aggregated status", "cluster", status.ClusterName)
189206
return false
190207
}
191208
// if member cluster deployment is controlled by hpa, its status.replicas must > 0 (since hpa.minReplicas > 0),
192209
// so, if its status replicas is 0, means the status haven't been collected from member cluster, and needs waiting.
193210
if itemStatus.Replicas <= 0 {
194-
klog.V(4).Infof("wait until aggregated status replicas of cluster %s collected", status.ClusterName)
211+
klog.V(4).InfoS("wait until aggregated status replicas of cluster are collected", "cluster", status.ClusterName)
195212
return false
196213
}
197214
bindingStatusSumReplicas += itemStatus.Replicas
@@ -201,8 +218,9 @@ func isDeploymentStatusCollected(deployment *appsv1.Deployment, binding *workv1a
201218
// e.g: user set deployment.spec.replicas = 2, then sum replicas in binding.status becomes 2, however, if now
202219
// deployment.status.replicas is still 1, we shouldn't update spec.replicas to 1 until status.replicas becomes 2.
203220
if deployment.Status.Replicas != bindingStatusSumReplicas {
204-
klog.V(4).Infof("wait until deployment status replicas (%d) equal to binding status replicas (%d)",
205-
deployment.Status.Replicas, bindingStatusSumReplicas)
221+
klog.V(4).InfoS("wait until deployment status replicas are equal to binding status replicas",
222+
"deploymentStatusReplicas", deployment.Status.Replicas, "bindingStatusReplicas", bindingStatusSumReplicas,
223+
"namespace", binding.Namespace, "bindingName", binding.Name, "deploymentName", deployment.Name)
206224
return false
207225
}
208226

0 commit comments

Comments
 (0)