Skip to content

Commit b3b7468

Browse files
authored
Merge pull request #7152 from sbueringer/pr-finalize-log-hierarchy
🌱 logging: adjust reconcilers to log object owners
2 parents 9cd41ec + 934bc6b commit b3b7468

File tree

16 files changed

+396
-11
lines changed

16 files changed

+396
-11
lines changed

Tiltfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ def deploy_observability():
474474

475475
if "grafana" in settings.get("deploy_observability", []):
476476
k8s_yaml(read_file("./.tiltbuild/yaml/grafana.observability.yaml"), allow_duplicates = True)
477-
k8s_resource(workload = "grafana", port_forwards = "3001:3000", extra_pod_selectors = [{"app": "grafana"}], labels = ["observability"])
477+
k8s_resource(workload = "grafana", port_forwards = "3001:3000", extra_pod_selectors = [{"app": "grafana"}], labels = ["observability"], objects = ["grafana:serviceaccount"])
478478

479479
if "prometheus" in settings.get("deploy_observability", []):
480480
k8s_yaml(read_file("./.tiltbuild/yaml/prometheus.observability.yaml"), allow_duplicates = True)

bootstrap/kubeadm/config/rbac/role.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ rules:
4242
- machinepools/status
4343
- machines
4444
- machines/status
45+
- machinesets
4546
verbs:
4647
- get
4748
- list

bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import (
5252
"sigs.k8s.io/cluster-api/util"
5353
"sigs.k8s.io/cluster-api/util/annotations"
5454
"sigs.k8s.io/cluster-api/util/conditions"
55+
clog "sigs.k8s.io/cluster-api/util/log"
5556
"sigs.k8s.io/cluster-api/util/patch"
5657
"sigs.k8s.io/cluster-api/util/predicates"
5758
"sigs.k8s.io/cluster-api/util/secret"
@@ -74,7 +75,7 @@ type InitLocker interface {
7475
}
7576

7677
// +kubebuilder:rbac:groups=bootstrap.cluster.x-k8s.io,resources=kubeadmconfigs;kubeadmconfigs/status;kubeadmconfigs/finalizers,verbs=get;list;watch;create;update;patch;delete
77-
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters;clusters/status;machines;machines/status;machinepools;machinepools/status,verbs=get;list;watch
78+
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters;clusters/status;machinesets;machines;machines/status;machinepools;machinepools/status,verbs=get;list;watch
7879
// +kubebuilder:rbac:groups="",resources=secrets;events;configmaps,verbs=get;list;watch;create;update;patch;delete
7980

8081
// KubeadmConfigReconciler reconciles a KubeadmConfig object.
@@ -160,6 +161,13 @@ func (r *KubeadmConfigReconciler) Reconcile(ctx context.Context, req ctrl.Reques
160161
return ctrl.Result{}, err
161162
}
162163

164+
// AddOwners adds the owners of KubeadmConfig as k/v pairs to the logger.
165+
// Specifically, it will add KubeadmControlPlane, MachineSet and MachineDeployment.
166+
ctx, log, err := clog.AddOwners(ctx, r.Client, config)
167+
if err != nil {
168+
return ctrl.Result{}, err
169+
}
170+
163171
// Look up the owner of this kubeadm config if there is one
164172
configOwner, err := bsutil.GetConfigOwner(ctx, r.Client, config)
165173
if apierrors.IsNotFound(err) {

docs/book/src/developer/logging.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ Will return logs from the `capi-controller-manager` which are parsed in json. Pa
180180
```
181181
Will return logs from the `capi-controller-manager` that are associated with the Cluster `my-cluster`.
182182
183+
```
184+
{app="capi-controller-manager"} | json | cluster_name="my-cluster" | v <= 2
185+
```
186+
Will return logs from the `capi-controller-manager` that are associated with the Cluster `my-cluster` with log level <= 2.
187+
183188
```
184189
{app="capi-controller-manager"} | json | cluster_name="my-cluster" reconcileID="6f6ad971-bdb6-4fa3-b803-xxxxxxxxxxxx"
185190
```

docs/book/src/developer/providers/v1.2-to-v1.3.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@ in Cluster API are kept in sync with the versions used by `sigs.k8s.io/controlle
3636
* the default test timeout has been [changed to 1h](https://onsi.github.io/ginkgo/MIGRATING_TO_V2#timeout-behavior)
3737
* the `--junit-report` argument [replaces JUnit custom reporter](https://onsi.github.io/ginkgo/MIGRATING_TO_V2#improved-reporting-infrastructure) code
3838
* see the ["Update tests to Ginkgo v2" PR](https://github.com/kubernetes-sigs/cluster-api/pull/6906) for a reference example
39+
- Custer API introduced new [logging guidelines](../../developer/logging.md). All reconcilers in the core repository were updated
40+
to [log the entire object hierarchy](../../developer/logging.md#keyvalue-pairs). It would be great if providers would be adjusted
41+
as well to make it possible to cross-reference log entries across providers (please see CAPD for an infra provider reference implementation).

exp/internal/controllers/machinepool_controller_phases.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func (r *MachinePoolReconciler) reconcileExternal(ctx context.Context, cluster *
134134
// Add watcher for external object, if there isn't one already.
135135
_, loaded := r.externalWatchers.LoadOrStore(obj.GroupVersionKind().String(), struct{}{})
136136
if !loaded && r.controller != nil {
137-
log.Info("Adding watcher on external object", "gvk", obj.GroupVersionKind())
137+
log.Info("Adding watcher on external object", "groupVersionKind", obj.GroupVersionKind())
138138
err := r.controller.Watch(
139139
&source.Kind{Type: obj},
140140
&handler.EnqueueRequestForOwner{OwnerType: &expv1.MachinePool{}},

internal/controllers/machine/machine_controller.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import (
5050
"sigs.k8s.io/cluster-api/util/annotations"
5151
"sigs.k8s.io/cluster-api/util/collections"
5252
"sigs.k8s.io/cluster-api/util/conditions"
53+
clog "sigs.k8s.io/cluster-api/util/log"
5354
"sigs.k8s.io/cluster-api/util/patch"
5455
"sigs.k8s.io/cluster-api/util/predicates"
5556
)
@@ -137,8 +138,6 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
137138
}
138139

139140
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, reterr error) {
140-
log := ctrl.LoggerFrom(ctx)
141-
142141
// Fetch the Machine instance
143142
m := &clusterv1.Machine{}
144143
if err := r.Client.Get(ctx, req.NamespacedName, m); err != nil {
@@ -152,6 +151,13 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
152151
return ctrl.Result{}, err
153152
}
154153

154+
// AddOwners adds the owners of Machine as k/v pairs to the logger.
155+
// Specifically, it will add KubeadmControlPlane, MachineSet and MachineDeployment.
156+
ctx, log, err := clog.AddOwners(ctx, r.Client, m)
157+
if err != nil {
158+
return ctrl.Result{}, err
159+
}
160+
155161
log = log.WithValues("Cluster", klog.KRef(m.ObjectMeta.Namespace, m.Spec.ClusterName))
156162
ctx = ctrl.LoggerInto(ctx, log)
157163

internal/controllers/machine/machine_controller_phases.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (r *Reconciler) reconcileExternal(ctx context.Context, cluster *clusterv1.C
9898
obj, err := external.Get(ctx, r.Client, ref, m.Namespace)
9999
if err != nil {
100100
if apierrors.IsNotFound(errors.Cause(err)) {
101-
log.Info("could not find external ref, requeueing", "refGVK", ref.GroupVersionKind(), "refName", ref.Name, "Machine", klog.KObj(m))
101+
log.Info("could not find external ref, requeueing", ref.Kind, klog.KRef(m.Namespace, ref.Name))
102102
return external.ReconcileOutput{RequeueAfter: externalReadyWait}, nil
103103
}
104104
return external.ReconcileOutput{}, err

internal/controllers/machineset/machineset_controller.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
"sigs.k8s.io/cluster-api/util/collections"
4747
"sigs.k8s.io/cluster-api/util/conditions"
4848
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
49+
clog "sigs.k8s.io/cluster-api/util/log"
4950
"sigs.k8s.io/cluster-api/util/patch"
5051
"sigs.k8s.io/cluster-api/util/predicates"
5152
)
@@ -118,8 +119,6 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
118119
}
119120

120121
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Result, reterr error) {
121-
log := ctrl.LoggerFrom(ctx)
122-
123122
machineSet := &clusterv1.MachineSet{}
124123
if err := r.Client.Get(ctx, req.NamespacedName, machineSet); err != nil {
125124
if apierrors.IsNotFound(err) {
@@ -131,6 +130,13 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
131130
return ctrl.Result{}, err
132131
}
133132

133+
// AddOwners adds the owners of MachineSet as k/v pairs to the logger.
134+
// Specifically, it will add MachineDeployment.
135+
ctx, log, err := clog.AddOwners(ctx, r.Client, machineSet)
136+
if err != nil {
137+
return ctrl.Result{}, err
138+
}
139+
134140
log = log.WithValues("Cluster", klog.KRef(machineSet.ObjectMeta.Namespace, machineSet.Spec.ClusterName))
135141
ctx = ctrl.LoggerInto(ctx, log)
136142

internal/controllers/topology/machineset/machineset_controller.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
tlog "sigs.k8s.io/cluster-api/internal/log"
3434
"sigs.k8s.io/cluster-api/util"
3535
"sigs.k8s.io/cluster-api/util/annotations"
36+
clog "sigs.k8s.io/cluster-api/util/log"
3637
"sigs.k8s.io/cluster-api/util/patch"
3738
"sigs.k8s.io/cluster-api/util/predicates"
3839
)
@@ -86,8 +87,6 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
8687
// We don't have to set the finalizer, as it's already set during MachineSet creation
8788
// in the MachineSet controller.
8889
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
89-
log := ctrl.LoggerFrom(ctx)
90-
9190
// Fetch the MachineSet instance.
9291
ms := &clusterv1.MachineSet{}
9392
if err := r.Client.Get(ctx, req.NamespacedName, ms); err != nil {
@@ -99,6 +98,13 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
9998
return ctrl.Result{}, errors.Wrapf(err, "failed to get MachineSet/%s", req.NamespacedName.Name)
10099
}
101100

101+
// AddOwners adds the owners of MachineSet as k/v pairs to the logger.
102+
// Specifically, it will add MachineDeployment.
103+
ctx, log, err := clog.AddOwners(ctx, r.Client, ms)
104+
if err != nil {
105+
return ctrl.Result{}, err
106+
}
107+
102108
log = log.WithValues("Cluster", klog.KRef(ms.Namespace, ms.Spec.ClusterName))
103109
ctx = ctrl.LoggerInto(ctx, log)
104110

0 commit comments

Comments
 (0)