Skip to content

Commit dcdb6bf

Browse files
authored
Merge pull request #7075 from sbueringer/pr-cr-log-keys-ii
✨ Improve key value pairs consistency in logging (II)
2 parents f3a67c1 + bbe7c2f commit dcdb6bf

File tree

35 files changed

+96
-124
lines changed

35 files changed

+96
-124
lines changed

bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,10 @@ func (r *KubeadmConfigReconciler) Reconcile(ctx context.Context, req ctrl.Reques
173173
if configOwner == nil {
174174
return ctrl.Result{}, nil
175175
}
176-
log = log.WithValues(configOwner.LowerCamelCaseKind(), klog.KRef(configOwner.GetNamespace(), configOwner.GetName()), "resourceVersion", configOwner.GetResourceVersion())
176+
log = log.WithValues(configOwner.GetKind(), klog.KRef(configOwner.GetNamespace(), configOwner.GetName()), "resourceVersion", configOwner.GetResourceVersion())
177+
178+
log = log.WithValues("Cluster", klog.KRef(configOwner.GetNamespace(), configOwner.ClusterName()))
179+
ctx = ctrl.LoggerInto(ctx, log)
177180

178181
// Lookup the cluster the config owner is associated with
179182
cluster, err := util.GetClusterByName(ctx, r.Client, configOwner.GetNamespace(), configOwner.ClusterName())
@@ -191,8 +194,6 @@ func (r *KubeadmConfigReconciler) Reconcile(ctx context.Context, req ctrl.Reques
191194
return ctrl.Result{}, err
192195
}
193196

194-
ctx = ctrl.LoggerInto(ctx, log.WithValues("cluster", klog.KObj(cluster)))
195-
196197
if annotations.IsPaused(cluster, config) {
197198
log.Info("Reconciliation is paused for this object")
198199
return ctrl.Result{}, nil
@@ -1001,7 +1002,7 @@ func (r *KubeadmConfigReconciler) storeBootstrapData(ctx context.Context, scope
10011002
if !apierrors.IsAlreadyExists(err) {
10021003
return errors.Wrapf(err, "failed to create bootstrap data secret for KubeadmConfig %s/%s", scope.Config.Namespace, scope.Config.Name)
10031004
}
1004-
log.Info("bootstrap data secret for KubeadmConfig already exists, updating", "secret", klog.KObj(secret))
1005+
log.Info("bootstrap data secret for KubeadmConfig already exists, updating", "Secret", klog.KObj(secret))
10051006
if err := r.Client.Update(ctx, secret); err != nil {
10061007
return errors.Wrapf(err, "failed to update bootstrap data secret for KubeadmConfig %s/%s", scope.Config.Namespace, scope.Config.Name)
10071008
}

bootstrap/kubeadm/internal/locking/control_plane_init_mutex.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func NewControlPlaneInitMutex(client client.Client) *ControlPlaneInitMutex {
5151
func (c *ControlPlaneInitMutex) Lock(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine) bool {
5252
sema := newSemaphore()
5353
cmName := configMapName(cluster.Name)
54-
log := ctrl.LoggerFrom(ctx, "configMap", klog.KRef(cluster.Namespace, cmName))
54+
log := ctrl.LoggerFrom(ctx, "ConfigMap", klog.KRef(cluster.Namespace, cmName))
5555
err := c.client.Get(ctx, client.ObjectKey{
5656
Namespace: cluster.Namespace,
5757
Name: cmName,
@@ -113,7 +113,7 @@ func (c *ControlPlaneInitMutex) Lock(ctx context.Context, cluster *clusterv1.Clu
113113
func (c *ControlPlaneInitMutex) Unlock(ctx context.Context, cluster *clusterv1.Cluster) bool {
114114
sema := newSemaphore()
115115
cmName := configMapName(cluster.Name)
116-
log := ctrl.LoggerFrom(ctx, "configMap", klog.KRef(cluster.Namespace, cmName))
116+
log := ctrl.LoggerFrom(ctx, "ConfigMap", klog.KRef(cluster.Namespace, cmName))
117117
err := c.client.Get(ctx, client.ObjectKey{
118118
Namespace: cluster.Namespace,
119119
Name: cmName,

bootstrap/util/configowner.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"sigs.k8s.io/cluster-api/controllers/external"
3232
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
3333
"sigs.k8s.io/cluster-api/feature"
34-
"sigs.k8s.io/cluster-api/util"
3534
)
3635

3736
// ConfigOwner provides a data interface for different config owner types.
@@ -124,12 +123,6 @@ func (co ConfigOwner) KubernetesVersion() string {
124123
return version
125124
}
126125

127-
// LowerCamelCaseKind mirrors how controller runtime formats the object's kind when used as a logging key
128-
// for the object being reconciled.
129-
func (co ConfigOwner) LowerCamelCaseKind() string {
130-
return util.LowerCamelCaseKind(co.Unstructured)
131-
}
132-
133126
// GetConfigOwner returns the Unstructured object owning the current resource.
134127
func GetConfigOwner(ctx context.Context, c client.Client, obj metav1.Object) (*ConfigOwner, error) {
135128
allowedGKs := []schema.GroupKind{

cmd/clusterctl/client/cluster/mover.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
kerrors "k8s.io/apimachinery/pkg/util/errors"
3131
"k8s.io/apimachinery/pkg/util/sets"
3232
"k8s.io/apimachinery/pkg/util/version"
33+
"k8s.io/klog/v2"
3334
"sigs.k8s.io/controller-runtime/pkg/client"
3435

3536
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
@@ -540,7 +541,7 @@ func setClusterPause(proxy Proxy, clusters []*node, value bool, dryRun bool) err
540541
setClusterPauseBackoff := newWriteBackoff()
541542
for i := range clusters {
542543
cluster := clusters[i]
543-
log.V(5).Info("Set Cluster.Spec.Paused", "paused", value, "cluster", cluster.identity.Name, "namespace", cluster.identity.Namespace)
544+
log.V(5).Info("Set Cluster.Spec.Paused", "paused", value, "Cluster", klog.KRef(cluster.identity.Namespace, cluster.identity.Name))
544545

545546
// Nb. The operation is wrapped in a retry loop to make setClusterPause more resilient to unexpected conditions.
546547
if err := retryWithExponentialBackoff(setClusterPauseBackoff, func() error {

controllers/remote/cluster_cache_tracker.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func (t *ClusterCacheTracker) deleteAccessor(_ context.Context, cluster client.O
345345
return
346346
}
347347

348-
log := t.log.WithValues("cluster", klog.KRef(cluster.Namespace, cluster.Name))
348+
log := t.log.WithValues("Cluster", klog.KRef(cluster.Namespace, cluster.Name))
349349
log.V(2).Info("Deleting clusterAccessor")
350350
log.V(4).Info("Stopping cache")
351351
a.cache.Stop()
@@ -396,7 +396,7 @@ func (t *ClusterCacheTracker) Watch(ctx context.Context, input WatchInput) error
396396
}
397397

398398
if a.watches.Has(input.Name) {
399-
t.log.V(6).Info("Watch already exists", "namespace", klog.KRef(input.Cluster.Namespace, ""), "cluster", klog.KRef(input.Cluster.Namespace, input.Cluster.Name), "name", input.Name)
399+
t.log.V(6).Info("Watch already exists", "Cluster", klog.KRef(input.Cluster.Namespace, input.Cluster.Name), "name", input.Name)
400400
return nil
401401
}
402402

@@ -501,7 +501,7 @@ func (t *ClusterCacheTracker) healthCheckCluster(ctx context.Context, in *health
501501
// NB. we are ignoring ErrWaitTimeout because this error happens when the channel is close, that in this case
502502
// happens when the cache is explicitly stopped.F
503503
if err != nil && err != wait.ErrWaitTimeout {
504-
t.log.Error(err, "Error health checking cluster", "cluster", klog.KRef(in.cluster.Namespace, in.cluster.Name))
504+
t.log.Error(err, "Error health checking cluster", "Cluster", klog.KRef(in.cluster.Namespace, in.cluster.Name))
505505
t.deleteAccessor(ctx, in.cluster)
506506
}
507507
}

controlplane/kubeadm/internal/controllers/controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (r *KubeadmControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.
143143
log.Info("Cluster Controller has not yet set OwnerRef")
144144
return ctrl.Result{}, nil
145145
}
146-
log = log.WithValues("cluster", klog.KObj(cluster))
146+
log = log.WithValues("Cluster", klog.KObj(cluster))
147147
ctx = ctrl.LoggerInto(ctx, log)
148148

149149
if annotations.IsPaused(cluster, kcp) {
@@ -463,7 +463,7 @@ func (r *KubeadmControlPlaneReconciler) reconcileDelete(ctx context.Context, clu
463463
var errs []error
464464
for i := range machinesToDelete {
465465
m := machinesToDelete[i]
466-
logger := log.WithValues("machine", klog.KObj(m))
466+
logger := log.WithValues("Machine", klog.KObj(m))
467467
if err := r.Client.Delete(ctx, machinesToDelete[i]); err != nil && !apierrors.IsNotFound(err) {
468468
logger.Error(err, "Failed to cleanup owned machine")
469469
errs = append(errs, err)

controlplane/kubeadm/internal/controllers/helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (r *KubeadmControlPlaneReconciler) reconcileKubeconfig(ctx context.Context,
104104

105105
func (r *KubeadmControlPlaneReconciler) adoptKubeconfigSecret(ctx context.Context, cluster *clusterv1.Cluster, configSecret *corev1.Secret, controllerOwnerRef metav1.OwnerReference) error {
106106
log := ctrl.LoggerFrom(ctx)
107-
log.Info("Adopting KubeConfig secret created by v1alpha2 controllers", "secret", klog.KObj(configSecret))
107+
log.Info("Adopting KubeConfig secret created by v1alpha2 controllers", "Secret", klog.KObj(configSecret))
108108

109109
patch, err := patch.NewHelper(configSecret, r.Client)
110110
if err != nil {

controlplane/kubeadm/internal/controllers/scale.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (r *KubeadmControlPlaneReconciler) scaleDownControlPlane(
141141
return ctrl.Result{}, err
142142
}
143143

144-
logger = logger.WithValues("machine", klog.KObj(machineToDelete))
144+
logger = logger.WithValues("Machine", klog.KObj(machineToDelete))
145145
if err := r.Client.Delete(ctx, machineToDelete); err != nil && !apierrors.IsNotFound(err) {
146146
logger.Error(err, "Failed to delete control plane machine")
147147
r.recorder.Eventf(kcp, corev1.EventTypeWarning, "FailedScaleDown",

controlplane/kubeadm/internal/controllers/status.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121

2222
"github.com/pkg/errors"
23-
"k8s.io/klog/v2"
2423
ctrl "sigs.k8s.io/controller-runtime"
2524

2625
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
@@ -34,7 +33,7 @@ import (
3433
// updateStatus is called after every reconcilitation loop in a defer statement to always make sure we have the
3534
// resource status subresourcs up-to-date.
3635
func (r *KubeadmControlPlaneReconciler) updateStatus(ctx context.Context, kcp *controlplanev1.KubeadmControlPlane, cluster *clusterv1.Cluster) error {
37-
log := ctrl.LoggerFrom(ctx, "cluster", klog.KObj(cluster))
36+
log := ctrl.LoggerFrom(ctx)
3837

3938
selector := collections.ControlPlaneSelectorForCluster(cluster.Name)
4039
// Copy label selector to its status counterpart in string format.

docs/book/src/developer/providers/implementers-guide/controllers_and_reconciliation.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type MailgunClusterReconciler struct {
2727
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=mailgunclusters,verbs=get;list;watch;create;update;patch;delete
2828
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=mailgunclusters/status,verbs=get;update;patch
2929

30-
func (r *MailgunClusterReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
30+
func (r *MailgunClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
3131
_ = context.Background()
3232
_ = r.Log.WithValues("mailguncluster", req.NamespacedName)
3333

@@ -86,7 +86,7 @@ Reconcile is only passed a name, not an object, so let's retrieve ours.
8686
Here's a naive example:
8787

8888
```
89-
func (r *MailgunClusterReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
89+
func (r *MailgunClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
9090
ctx := context.Background()
9191
_ = r.Log.WithValues("mailguncluster", req.NamespacedName)
9292

0 commit comments

Comments
 (0)