Skip to content

Commit b35bd70

Browse files
authored
🌱 Drop internal log package & improve logs and errors (#11025)
* Drop internal log package Signed-off-by: Stefan Büringer [email protected] * fix review findings --------- Signed-off-by: Stefan Büringer [email protected]
1 parent 50a0714 commit b35bd70

File tree

18 files changed

+284
-474
lines changed

18 files changed

+284
-474
lines changed

exp/runtime/internal/controllers/extensionconfig_controller.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828
"k8s.io/apimachinery/pkg/types"
2929
kerrors "k8s.io/apimachinery/pkg/util/errors"
30+
"k8s.io/klog/v2"
3031
ctrl "sigs.k8s.io/controller-runtime"
3132
"sigs.k8s.io/controller-runtime/pkg/cache"
3233
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -37,7 +38,6 @@ import (
3738

3839
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3940
runtimev1 "sigs.k8s.io/cluster-api/exp/runtime/api/v1alpha1"
40-
tlog "sigs.k8s.io/cluster-api/internal/log"
4141
runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
4242
"sigs.k8s.io/cluster-api/util/annotations"
4343
"sigs.k8s.io/cluster-api/util/conditions"
@@ -185,7 +185,7 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, extensionConfig *runti
185185
log := ctrl.LoggerFrom(ctx)
186186
log.Info("Unregistering ExtensionConfig information from registry")
187187
if err := r.RuntimeClient.Unregister(extensionConfig); err != nil {
188-
return ctrl.Result{}, errors.Wrapf(err, "failed to unregister %s", tlog.KObj{Obj: extensionConfig})
188+
return ctrl.Result{}, errors.Wrapf(err, "failed to unregister ExtensionConfig %s", klog.KObj(extensionConfig))
189189
}
190190
return ctrl.Result{}, nil
191191
}
@@ -221,7 +221,7 @@ func discoverExtensionConfig(ctx context.Context, runtimeClient runtimeclient.Cl
221221
if err != nil {
222222
modifiedExtensionConfig := extensionConfig.DeepCopy()
223223
conditions.MarkFalse(modifiedExtensionConfig, runtimev1.RuntimeExtensionDiscoveredCondition, runtimev1.DiscoveryFailedReason, clusterv1.ConditionSeverityError, "error in discovery: %v", err)
224-
return modifiedExtensionConfig, errors.Wrapf(err, "failed to discover %s", tlog.KObj{Obj: extensionConfig})
224+
return modifiedExtensionConfig, errors.Wrapf(err, "failed to discover ExtensionConfig %s", klog.KObj(extensionConfig))
225225
}
226226

227227
conditions.MarkTrue(discoveredExtension, runtimev1.RuntimeExtensionDiscoveredCondition)

exp/topology/desiredstate/desired_state.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ import (
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2828
"k8s.io/apimachinery/pkg/runtime/schema"
29+
"k8s.io/klog/v2"
2930
"k8s.io/utils/ptr"
31+
ctrl "sigs.k8s.io/controller-runtime"
3032
"sigs.k8s.io/controller-runtime/pkg/client"
3133

3234
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
@@ -40,7 +42,6 @@ import (
4042
"sigs.k8s.io/cluster-api/internal/contract"
4143
"sigs.k8s.io/cluster-api/internal/controllers/topology/cluster/patches"
4244
"sigs.k8s.io/cluster-api/internal/hooks"
43-
tlog "sigs.k8s.io/cluster-api/internal/log"
4445
runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
4546
"sigs.k8s.io/cluster-api/internal/topology/clustershim"
4647
topologynames "sigs.k8s.io/cluster-api/internal/topology/names"
@@ -399,7 +400,7 @@ func (g *generator) computeControlPlane(ctx context.Context, s *scope.Scope, inf
399400
// The version is calculated using the state of the current machine deployments, the current control plane
400401
// and the version defined in the topology.
401402
func (g *generator) computeControlPlaneVersion(ctx context.Context, s *scope.Scope) (string, error) {
402-
log := tlog.LoggerFrom(ctx)
403+
log := ctrl.LoggerFrom(ctx)
403404
desiredVersion := s.Blueprint.Topology.Version
404405
// If we are creating the control plane object (current control plane is nil), use version from topology.
405406
if s.Current.ControlPlane == nil || s.Current.ControlPlane.Object == nil {
@@ -477,7 +478,7 @@ func (g *generator) computeControlPlaneVersion(ctx context.Context, s *scope.Sco
477478
// change the UpgradeTracker accordingly, otherwise the hook call is completed and we
478479
// can remove this hook from the list of pending-hooks.
479480
if hookResponse.RetryAfterSeconds != 0 {
480-
log.Infof("MachineDeployments/MachinePools upgrade to version %q are blocked by %q hook", desiredVersion, runtimecatalog.HookName(runtimehooksv1.AfterControlPlaneUpgrade))
481+
log.Info(fmt.Sprintf("MachineDeployments/MachinePools upgrade to version %q are blocked by %q hook", desiredVersion, runtimecatalog.HookName(runtimehooksv1.AfterControlPlaneUpgrade)))
481482
} else {
482483
if err := hooks.MarkAsDone(ctx, g.Client, s.Current.Cluster, runtimehooksv1.AfterControlPlaneUpgrade); err != nil {
483484
return "", err
@@ -527,7 +528,7 @@ func (g *generator) computeControlPlaneVersion(ctx context.Context, s *scope.Sco
527528
s.HookResponseTracker.Add(runtimehooksv1.BeforeClusterUpgrade, hookResponse)
528529
if hookResponse.RetryAfterSeconds != 0 {
529530
// Cannot pickup the new version right now. Need to try again later.
530-
log.Infof("Cluster upgrade to version %q is blocked by %q hook", desiredVersion, runtimecatalog.HookName(runtimehooksv1.BeforeClusterUpgrade))
531+
log.Info(fmt.Sprintf("Cluster upgrade to version %q is blocked by %q hook", desiredVersion, runtimecatalog.HookName(runtimehooksv1.BeforeClusterUpgrade)))
531532
return *currentVersion, nil
532533
}
533534

@@ -627,7 +628,7 @@ func (g *generator) computeMachineDeployment(ctx context.Context, s *scope.Scope
627628
className := machineDeploymentTopology.Class
628629
machineDeploymentBlueprint, ok := s.Blueprint.MachineDeployments[className]
629630
if !ok {
630-
return nil, errors.Errorf("MachineDeployment class %s not found in %s", className, tlog.KObj{Obj: s.Blueprint.ClusterClass})
631+
return nil, errors.Errorf("MachineDeployment class %s not found in ClusterClass %s", className, klog.KObj(s.Blueprint.ClusterClass))
631632
}
632633

633634
var machineDeploymentClass *clusterv1.MachineDeploymentClass
@@ -638,7 +639,7 @@ func (g *generator) computeMachineDeployment(ctx context.Context, s *scope.Scope
638639
}
639640
}
640641
if machineDeploymentClass == nil {
641-
return nil, errors.Errorf("MachineDeployment class %s not found in %s", className, tlog.KObj{Obj: s.Blueprint.ClusterClass})
642+
return nil, errors.Errorf("MachineDeployment class %s not found in ClusterClass %s", className, klog.KObj(s.Blueprint.ClusterClass))
642643
}
643644

644645
// Compute the bootstrap template.
@@ -952,7 +953,7 @@ func (g *generator) computeMachinePool(_ context.Context, s *scope.Scope, machin
952953
className := machinePoolTopology.Class
953954
machinePoolBlueprint, ok := s.Blueprint.MachinePools[className]
954955
if !ok {
955-
return nil, errors.Errorf("MachinePool class %s not found in %s", className, tlog.KObj{Obj: s.Blueprint.ClusterClass})
956+
return nil, errors.Errorf("MachinePool class %s not found in ClusterClass %s", className, klog.KObj(s.Blueprint.ClusterClass))
956957
}
957958

958959
var machinePoolClass *clusterv1.MachinePoolClass
@@ -963,7 +964,7 @@ func (g *generator) computeMachinePool(_ context.Context, s *scope.Scope, machin
963964
}
964965
}
965966
if machinePoolClass == nil {
966-
return nil, errors.Errorf("MachinePool class %s not found in %s", className, tlog.KObj{Obj: s.Blueprint.ClusterClass})
967+
return nil, errors.Errorf("MachinePool class %s not found in ClusterClass %s", className, klog.KObj(s.Blueprint.ClusterClass))
967968
}
968969

969970
// Compute the bootstrap config.

internal/controllers/clusterclass/clusterclass_controller.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
kerrors "k8s.io/apimachinery/pkg/util/errors"
3333
"k8s.io/apimachinery/pkg/util/sets"
3434
"k8s.io/apimachinery/pkg/util/validation/field"
35+
"k8s.io/klog/v2"
3536
ctrl "sigs.k8s.io/controller-runtime"
3637
"sigs.k8s.io/controller-runtime/pkg/client"
3738
"sigs.k8s.io/controller-runtime/pkg/controller"
@@ -44,7 +45,6 @@ import (
4445
runtimev1 "sigs.k8s.io/cluster-api/exp/runtime/api/v1alpha1"
4546
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
4647
"sigs.k8s.io/cluster-api/feature"
47-
tlog "sigs.k8s.io/cluster-api/internal/log"
4848
runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
4949
"sigs.k8s.io/cluster-api/internal/topology/variables"
5050
"sigs.k8s.io/cluster-api/util/annotations"
@@ -391,7 +391,7 @@ func (r *Reconciler) reconcileExternal(ctx context.Context, clusterClass *cluste
391391

392392
// Set external object ControllerReference to the ClusterClass.
393393
if err := controllerutil.SetOwnerReference(clusterClass, obj, r.Client.Scheme()); err != nil {
394-
return errors.Wrapf(err, "failed to set ClusterClass owner reference for %s", tlog.KObj{Obj: obj})
394+
return errors.Wrapf(err, "failed to set ClusterClass owner reference for %s %s", obj.GetKind(), klog.KObj(obj))
395395
}
396396

397397
// Patch the external object.

internal/controllers/clusterclass/clusterclass_controller_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"k8s.io/apimachinery/pkg/util/version"
3333
utilversion "k8s.io/apiserver/pkg/util/version"
3434
utilfeature "k8s.io/component-base/featuregate/testing"
35+
"k8s.io/klog/v2"
3536
"k8s.io/utils/ptr"
3637
"sigs.k8s.io/controller-runtime/pkg/client"
3738
"sigs.k8s.io/controller-runtime/pkg/client/fake"
@@ -42,7 +43,6 @@ import (
4243
runtimecatalog "sigs.k8s.io/cluster-api/exp/runtime/catalog"
4344
runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1"
4445
"sigs.k8s.io/cluster-api/feature"
45-
tlog "sigs.k8s.io/cluster-api/internal/log"
4646
fakeruntimeclient "sigs.k8s.io/cluster-api/internal/runtime/client/fake"
4747
"sigs.k8s.io/cluster-api/internal/test/builder"
4848
)
@@ -384,7 +384,9 @@ func assertHasOwnerReference(obj client.Object, ownerRef metav1.OwnerReference)
384384
}
385385
}
386386
if !found {
387-
return fmt.Errorf("object %s does not have OwnerReference %s", tlog.KObj{Obj: obj}, &ownerRef)
387+
// Note: Kind might be empty here as it's usually only set on Unstructured objects.
388+
// But as this is just test code we don't care too much about it.
389+
return fmt.Errorf("%s %s does not have OwnerReference %s", obj.GetObjectKind().GroupVersionKind().Kind, klog.KObj(obj), &ownerRef)
388390
}
389391
return nil
390392
}

internal/controllers/topology/cluster/blueprint.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ import (
2020
"context"
2121

2222
"github.com/pkg/errors"
23+
"k8s.io/klog/v2"
2324

2425
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
2526
"sigs.k8s.io/cluster-api/exp/topology/scope"
26-
tlog "sigs.k8s.io/cluster-api/internal/log"
2727
)
2828

2929
// getBlueprint gets a ClusterBlueprint with the ClusterClass and the referenced templates to be used for a managed Cluster topology.
@@ -41,21 +41,21 @@ func (r *Reconciler) getBlueprint(ctx context.Context, cluster *clusterv1.Cluste
4141
// Get ClusterClass.spec.infrastructure.
4242
blueprint.InfrastructureClusterTemplate, err = r.getReference(ctx, blueprint.ClusterClass.Spec.Infrastructure.Ref)
4343
if err != nil {
44-
return nil, errors.Wrapf(err, "failed to get infrastructure cluster template for %s", tlog.KObj{Obj: blueprint.ClusterClass})
44+
return nil, errors.Wrapf(err, "failed to get infrastructure cluster template for ClusterClass %s", klog.KObj(blueprint.ClusterClass))
4545
}
4646

4747
// Get ClusterClass.spec.controlPlane.
4848
blueprint.ControlPlane = &scope.ControlPlaneBlueprint{}
4949
blueprint.ControlPlane.Template, err = r.getReference(ctx, blueprint.ClusterClass.Spec.ControlPlane.Ref)
5050
if err != nil {
51-
return nil, errors.Wrapf(err, "failed to get control plane template for %s", tlog.KObj{Obj: blueprint.ClusterClass})
51+
return nil, errors.Wrapf(err, "failed to get control plane template for ClusterClass %s", klog.KObj(blueprint.ClusterClass))
5252
}
5353

5454
// If the clusterClass mandates the controlPlane has infrastructureMachines, read it.
5555
if blueprint.HasControlPlaneInfrastructureMachine() {
5656
blueprint.ControlPlane.InfrastructureMachineTemplate, err = r.getReference(ctx, blueprint.ClusterClass.Spec.ControlPlane.MachineInfrastructure.Ref)
5757
if err != nil {
58-
return nil, errors.Wrapf(err, "failed to get control plane's machine template for %s", tlog.KObj{Obj: blueprint.ClusterClass})
58+
return nil, errors.Wrapf(err, "failed to get control plane's machine template for ClusterClass %s", klog.KObj(blueprint.ClusterClass))
5959
}
6060
}
6161

@@ -77,13 +77,13 @@ func (r *Reconciler) getBlueprint(ctx context.Context, cluster *clusterv1.Cluste
7777
// Get the infrastructure machine template.
7878
machineDeploymentBlueprint.InfrastructureMachineTemplate, err = r.getReference(ctx, machineDeploymentClass.Template.Infrastructure.Ref)
7979
if err != nil {
80-
return nil, errors.Wrapf(err, "failed to get infrastructure machine template for %s, MachineDeployment class %q", tlog.KObj{Obj: blueprint.ClusterClass}, machineDeploymentClass.Class)
80+
return nil, errors.Wrapf(err, "failed to get infrastructure machine template for ClusterClass %s, MachineDeployment class %q", klog.KObj(blueprint.ClusterClass), machineDeploymentClass.Class)
8181
}
8282

8383
// Get the bootstrap config template.
8484
machineDeploymentBlueprint.BootstrapTemplate, err = r.getReference(ctx, machineDeploymentClass.Template.Bootstrap.Ref)
8585
if err != nil {
86-
return nil, errors.Wrapf(err, "failed to get bootstrap config template for %s, MachineDeployment class %q", tlog.KObj{Obj: blueprint.ClusterClass}, machineDeploymentClass.Class)
86+
return nil, errors.Wrapf(err, "failed to get bootstrap config template for ClusterClass %s, MachineDeployment class %q", klog.KObj(blueprint.ClusterClass), machineDeploymentClass.Class)
8787
}
8888

8989
// If the machineDeploymentClass defines a MachineHealthCheck add it to the blueprint.
@@ -106,13 +106,13 @@ func (r *Reconciler) getBlueprint(ctx context.Context, cluster *clusterv1.Cluste
106106
// Get the InfrastructureMachinePoolTemplate.
107107
machinePoolBlueprint.InfrastructureMachinePoolTemplate, err = r.getReference(ctx, machinePoolClass.Template.Infrastructure.Ref)
108108
if err != nil {
109-
return nil, errors.Wrapf(err, "failed to get InfrastructureMachinePoolTemplate for %s, MachinePool class %q", tlog.KObj{Obj: blueprint.ClusterClass}, machinePoolClass.Class)
109+
return nil, errors.Wrapf(err, "failed to get InfrastructureMachinePoolTemplate for ClusterClass %s, MachinePool class %q", klog.KObj(blueprint.ClusterClass), machinePoolClass.Class)
110110
}
111111

112112
// Get the bootstrap config template.
113113
machinePoolBlueprint.BootstrapTemplate, err = r.getReference(ctx, machinePoolClass.Template.Bootstrap.Ref)
114114
if err != nil {
115-
return nil, errors.Wrapf(err, "failed to get bootstrap config for %s, MachinePool class %q", tlog.KObj{Obj: blueprint.ClusterClass}, machinePoolClass.Class)
115+
return nil, errors.Wrapf(err, "failed to get bootstrap config for ClusterClass %s, MachinePool class %q", klog.KObj(blueprint.ClusterClass), machinePoolClass.Class)
116116
}
117117

118118
blueprint.MachinePools[machinePoolClass.Class] = machinePoolBlueprint

internal/controllers/topology/cluster/cluster_controller.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import (
4545
"sigs.k8s.io/cluster-api/feature"
4646
"sigs.k8s.io/cluster-api/internal/controllers/topology/cluster/structuredmerge"
4747
"sigs.k8s.io/cluster-api/internal/hooks"
48-
tlog "sigs.k8s.io/cluster-api/internal/log"
4948
runtimeclient "sigs.k8s.io/cluster-api/internal/runtime/client"
5049
"sigs.k8s.io/cluster-api/internal/util/ssa"
5150
"sigs.k8s.io/cluster-api/internal/webhooks"
@@ -316,7 +315,7 @@ func (r *Reconciler) setupDynamicWatches(ctx context.Context, s *scope.Scope) er
316315
func (r *Reconciler) callBeforeClusterCreateHook(ctx context.Context, s *scope.Scope) (reconcile.Result, error) {
317316
// If the cluster objects (InfraCluster, ControlPlane, etc) are not yet created we are in the creation phase.
318317
// Call the BeforeClusterCreate hook before proceeding.
319-
log := tlog.LoggerFrom(ctx)
318+
log := ctrl.LoggerFrom(ctx)
320319
if s.Current.Cluster.Spec.InfrastructureRef == nil && s.Current.Cluster.Spec.ControlPlaneRef == nil {
321320
hookRequest := &runtimehooksv1.BeforeClusterCreateRequest{
322321
Cluster: *s.Current.Cluster,
@@ -327,7 +326,7 @@ func (r *Reconciler) callBeforeClusterCreateHook(ctx context.Context, s *scope.S
327326
}
328327
s.HookResponseTracker.Add(runtimehooksv1.BeforeClusterCreate, hookResponse)
329328
if hookResponse.RetryAfterSeconds != 0 {
330-
log.Infof("Creation of Cluster topology is blocked by %s hook", runtimecatalog.HookName(runtimehooksv1.BeforeClusterCreate))
329+
log.Info(fmt.Sprintf("Creation of Cluster topology is blocked by %s hook", runtimecatalog.HookName(runtimehooksv1.BeforeClusterCreate)))
331330
return ctrl.Result{RequeueAfter: time.Duration(hookResponse.RetryAfterSeconds) * time.Second}, nil
332331
}
333332
}
@@ -402,7 +401,7 @@ func (r *Reconciler) machinePoolToCluster(_ context.Context, o client.Object) []
402401
func (r *Reconciler) reconcileDelete(ctx context.Context, cluster *clusterv1.Cluster) (ctrl.Result, error) {
403402
// Call the BeforeClusterDelete hook if the 'ok-to-delete' annotation is not set
404403
// and add the annotation to the cluster after receiving a successful non-blocking response.
405-
log := tlog.LoggerFrom(ctx)
404+
log := ctrl.LoggerFrom(ctx)
406405
if feature.Gates.Enabled(feature.RuntimeSDK) {
407406
if !hooks.IsOkToDelete(cluster) {
408407
hookRequest := &runtimehooksv1.BeforeClusterDeleteRequest{
@@ -413,7 +412,7 @@ func (r *Reconciler) reconcileDelete(ctx context.Context, cluster *clusterv1.Clu
413412
return ctrl.Result{}, err
414413
}
415414
if hookResponse.RetryAfterSeconds != 0 {
416-
log.Infof("Cluster deletion is blocked by %q hook", runtimecatalog.HookName(runtimehooksv1.BeforeClusterDelete))
415+
log.Info(fmt.Sprintf("Cluster deletion is blocked by %q hook", runtimecatalog.HookName(runtimehooksv1.BeforeClusterDelete)))
417416
return ctrl.Result{RequeueAfter: time.Duration(hookResponse.RetryAfterSeconds) * time.Second}, nil
418417
}
419418
// The BeforeClusterDelete hook returned a non-blocking response. Now the cluster is ready to be deleted.

0 commit comments

Comments
 (0)