Skip to content

Commit dba1c70

Browse files
authored
Merge pull request #7534 from sbueringer/pr-improve-logging
🐛 logging: Avoid adding multiple objects to the same logger in for loops
2 parents ebbd333 + 968edf8 commit dba1c70

File tree

6 files changed

+14
-13
lines changed

6 files changed

+14
-13
lines changed

controlplane/kubeadm/internal/controllers/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ func (r *KubeadmControlPlaneReconciler) reconcileCertificateExpiries(ctx context
600600
}
601601

602602
for _, m := range machines {
603-
log = log.WithValues("Machine", klog.KObj(m))
603+
log := log.WithValues("Machine", klog.KObj(m))
604604

605605
kubeadmConfig, ok := controlPlane.GetKubeadmConfig(m.Name)
606606
if !ok {

internal/controllers/machine/machine_controller.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,8 @@ func patchMachine(ctx context.Context, patchHelper *patch.Helper, machine *clust
253253
}
254254

255255
func (r *Reconciler) reconcile(ctx context.Context, cluster *clusterv1.Cluster, m *clusterv1.Machine) (ctrl.Result, error) {
256-
log := ctrl.LoggerFrom(ctx)
257-
258256
if err := r.watchClusterNodes(ctx, cluster); err != nil {
259-
log.Error(err, "error watching nodes on target cluster")
260-
return ctrl.Result{}, err
257+
return ctrl.Result{}, errors.Wrapf(err, "error watching nodes on target cluster")
261258
}
262259

263260
// If the Machine belongs to a cluster, add an owner reference.

internal/controllers/machinedeployment/machinedeployment_rollout_ondelete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (r *Reconciler) reconcileOldMachineSetsOnDelete(ctx context.Context, oldMSs
8585
totalReplicas := mdutil.GetReplicaCountForMachineSets(allMSs)
8686
scaleDownAmount := totalReplicas - *deployment.Spec.Replicas
8787
for _, oldMS := range oldMSs {
88-
log = log.WithValues("MachineSet", klog.KObj(oldMS))
88+
log := log.WithValues("MachineSet", klog.KObj(oldMS))
8989
if oldMS.Spec.Replicas == nil || *oldMS.Spec.Replicas <= 0 {
9090
log.V(4).Info("fully scaled down")
9191
continue
@@ -138,7 +138,7 @@ func (r *Reconciler) reconcileOldMachineSetsOnDelete(ctx context.Context, oldMSs
138138
}
139139
log.V(4).Info("Finished reconcile of Old MachineSets to account for deleted machines. Now analyzing if there's more potential to scale down")
140140
for _, oldMS := range oldMSs {
141-
log = log.WithValues("MachineSet", klog.KObj(oldMS))
141+
log := log.WithValues("MachineSet", klog.KObj(oldMS))
142142
if scaleDownAmount <= 0 {
143143
break
144144
}

internal/controllers/machineset/machineset_controller.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func (r *Reconciler) reconcile(ctx context.Context, cluster *clusterv1.Cluster,
262262
filteredMachines := make([]*clusterv1.Machine, 0, len(allMachines.Items))
263263
for idx := range allMachines.Items {
264264
machine := &allMachines.Items[idx]
265-
log = log.WithValues("Machine", klog.KObj(machine))
265+
log := log.WithValues("Machine", klog.KObj(machine))
266266
if shouldExcludeMachine(machineSet, machine) {
267267
continue
268268
}
@@ -283,7 +283,7 @@ func (r *Reconciler) reconcile(ctx context.Context, cluster *clusterv1.Cluster,
283283

284284
var errs []error
285285
for _, machine := range filteredMachines {
286-
log = log.WithValues("Machine", klog.KObj(machine))
286+
log := log.WithValues("Machine", klog.KObj(machine))
287287
// filteredMachines contains machines in deleting status to calculate correct status.
288288
// skip remediation for those in deleting status.
289289
if !machine.DeletionTimestamp.IsZero() {
@@ -369,6 +369,8 @@ func (r *Reconciler) syncReplicas(ctx context.Context, ms *clusterv1.MachineSet,
369369
)
370370

371371
for i := 0; i < diff; i++ {
372+
// Create a new logger so the global logger is not modified.
373+
log := log
372374
machine := r.getNewMachine(ms)
373375

374376
// Clone and set the infrastructure and bootstrap references.
@@ -665,7 +667,7 @@ func (r *Reconciler) updateStatus(ctx context.Context, cluster *clusterv1.Cluste
665667
templateLabel := labels.Set(ms.Spec.Template.Labels).AsSelectorPreValidated()
666668

667669
for _, machine := range filteredMachines {
668-
log = log.WithValues("Machine", klog.KObj(machine))
670+
log := log.WithValues("Machine", klog.KObj(machine))
669671

670672
if templateLabel.Matches(labels.Set(machine.Labels)) {
671673
fullyLabeledReplicasCount++

internal/controllers/topology/cluster/patches/engine.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (e *engine) Apply(ctx context.Context, blueprint *scope.ClusterBlueprint, d
7878
// respecting the order in which they are defined.
7979
for i := range blueprint.ClusterClass.Spec.Patches {
8080
clusterClassPatch := blueprint.ClusterClass.Spec.Patches[i]
81-
ctx, log = log.WithValues("patch", clusterClassPatch.Name).Into(ctx)
81+
ctx, log := log.WithValues("patch", clusterClassPatch.Name).Into(ctx)
8282

8383
log.V(5).Infof("Applying patch to templates")
8484

@@ -115,7 +115,7 @@ func (e *engine) Apply(ctx context.Context, blueprint *scope.ClusterBlueprint, d
115115
continue
116116
}
117117

118-
ctx, log = log.WithValues("patch", clusterClassPatch.Name).Into(ctx)
118+
ctx, log := log.WithValues("patch", clusterClassPatch.Name).Into(ctx)
119119

120120
log.V(5).Infof("Validating topology")
121121

@@ -283,7 +283,7 @@ func applyPatchesToRequest(ctx context.Context, req *runtimehooksv1.GeneratePatc
283283
log := tlog.LoggerFrom(ctx)
284284

285285
for _, patch := range resp.Items {
286-
log = log.WithValues("uid", patch.UID)
286+
log := log.WithValues("uid", patch.UID)
287287

288288
// Get the request item the patch belongs to.
289289
requestItem := getRequestItemByUID(req, patch.UID)

test/extension/handlers/topologymutation/handler.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ func (h *ExtensionHandlers) GeneratePatches(ctx context.Context, req *runtimehoo
8181
// easier to read and less error prone than using unstructured or working with raw json/yaml.
8282
// IMPORTANT: by unit testing this func/nested func properly, it is possible to prevent unexpected rollouts when patches are modified.
8383
topologymutation.WalkTemplates(ctx, h.decoder, req, resp, func(ctx context.Context, obj runtime.Object, variables map[string]apiextensionsv1.JSON, holderRef runtimehooksv1.HolderReference) error {
84+
log := ctrl.LoggerFrom(ctx)
85+
8486
switch obj := obj.(type) {
8587
case *infrav1.DockerClusterTemplate:
8688
if err := patchDockerClusterTemplate(ctx, obj, variables); err != nil {

0 commit comments

Comments
 (0)