Skip to content

Commit a867db9

Browse files
committed
Align logging practices with cluster-api
1 parent b38366d commit a867db9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+144
-123
lines changed

api/v1beta2/awscluster_webhook.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@ import (
2424
"k8s.io/apimachinery/pkg/runtime"
2525
"k8s.io/apimachinery/pkg/util/validation/field"
2626
ctrl "sigs.k8s.io/controller-runtime"
27-
logf "sigs.k8s.io/controller-runtime/pkg/log"
2827
"sigs.k8s.io/controller-runtime/pkg/webhook"
2928

3029
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3130
"sigs.k8s.io/cluster-api/util/annotations"
3231
)
3332

3433
// log is for logging in this package.
35-
var _ = logf.Log.WithName("awscluster-resource")
34+
var _ = ctrl.Log.WithName("awscluster-resource")
3635

3736
func (r *AWSCluster) SetupWebhookWithManager(mgr ctrl.Manager) error {
3837
return ctrl.NewWebhookManagedBy(mgr).

api/v1beta2/awsclustercontrolleridentity_webhook.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ import (
2626
"k8s.io/apimachinery/pkg/runtime"
2727
"k8s.io/apimachinery/pkg/util/validation/field"
2828
ctrl "sigs.k8s.io/controller-runtime"
29-
logf "sigs.k8s.io/controller-runtime/pkg/log"
3029
"sigs.k8s.io/controller-runtime/pkg/webhook"
3130
)
3231

3332
// log is for logging in this package.
34-
var _ = logf.Log.WithName("awsclustercontrolleridentity-resource")
33+
var _ = ctrl.Log.WithName("awsclustercontrolleridentity-resource")
3534

3635
func (r *AWSClusterControllerIdentity) SetupWebhookWithManager(mgr ctrl.Manager) error {
3736
return ctrl.NewWebhookManagedBy(mgr).

api/v1beta2/awsclusterroleidentity_webhook.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@ import (
2424
"k8s.io/apimachinery/pkg/runtime"
2525
"k8s.io/apimachinery/pkg/util/validation/field"
2626
ctrl "sigs.k8s.io/controller-runtime"
27-
logf "sigs.k8s.io/controller-runtime/pkg/log"
2827
"sigs.k8s.io/controller-runtime/pkg/webhook"
2928
)
3029

3130
// log is for logging in this package.
32-
var _ = logf.Log.WithName("awsclusterroleidentity-resource")
31+
var _ = ctrl.Log.WithName("awsclusterroleidentity-resource")
3332

3433
func (r *AWSClusterRoleIdentity) SetupWebhookWithManager(mgr ctrl.Manager) error {
3534
return ctrl.NewWebhookManagedBy(mgr).

api/v1beta2/awsclusterstaticidentity_webhook.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,11 @@ import (
2424
"k8s.io/apimachinery/pkg/runtime"
2525
"k8s.io/apimachinery/pkg/util/validation/field"
2626
ctrl "sigs.k8s.io/controller-runtime"
27-
logf "sigs.k8s.io/controller-runtime/pkg/log"
2827
"sigs.k8s.io/controller-runtime/pkg/webhook"
2928
)
3029

3130
// log is for logging in this package.
32-
var _ = logf.Log.WithName("awsclusterstaticidentity-resource")
31+
var _ = ctrl.Log.WithName("awsclusterstaticidentity-resource")
3332

3433
func (r *AWSClusterStaticIdentity) SetupWebhookWithManager(mgr ctrl.Manager) error {
3534
return ctrl.NewWebhookManagedBy(mgr).

api/v1beta2/awsmachine_webhook.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ import (
2323
"k8s.io/apimachinery/pkg/runtime"
2424
"k8s.io/apimachinery/pkg/util/validation/field"
2525
ctrl "sigs.k8s.io/controller-runtime"
26-
logf "sigs.k8s.io/controller-runtime/pkg/log"
2726
"sigs.k8s.io/controller-runtime/pkg/webhook"
2827

2928
"sigs.k8s.io/cluster-api-provider-aws/feature"
3029
)
3130

3231
// log is for logging in this package.
33-
var log = logf.Log.WithName("awsmachine-resource")
32+
var log = ctrl.Log.WithName("awsmachine-resource")
3433

3534
func (r *AWSMachine) SetupWebhookWithManager(mgr ctrl.Manager) error {
3635
return ctrl.NewWebhookManagedBy(mgr).

bootstrap/eks/controllers/eksconfig_controller.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
apierrors "k8s.io/apimachinery/pkg/api/errors"
2727
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2828
"k8s.io/apimachinery/pkg/runtime"
29+
"k8s.io/klog/v2"
2930
"k8s.io/utils/pointer"
3031
ctrl "sigs.k8s.io/controller-runtime"
3132
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -104,7 +105,7 @@ func (r *EKSConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
104105
log.Error(err, "Could not get cluster with metadata")
105106
return ctrl.Result{}, err
106107
}
107-
log = log.WithValues("cluster", cluster.Name)
108+
log = log.WithValues("cluster", klog.KObj(cluster))
108109

109110
if annotations.IsPaused(cluster, config) {
110111
log.Info("Reconciliation is paused for this object")
@@ -283,7 +284,7 @@ func (r *EKSConfigReconciler) storeBootstrapData(ctx context.Context, cluster *c
283284
if err := r.createBootstrapSecret(ctx, cluster, config, data); err != nil {
284285
return errors.Wrap(err, "failed to create bootstrap data secret for EKSConfig")
285286
}
286-
log.Info("created bootstrap data secret for EKSConfig", "secret", secret.Name)
287+
log.Info("created bootstrap data secret for EKSConfig", "secret", klog.KObj(secret))
287288
} else {
288289
return errors.Wrap(err, "failed to get data secret for EKSConfig")
289290
}
@@ -293,9 +294,9 @@ func (r *EKSConfigReconciler) storeBootstrapData(ctx context.Context, cluster *c
293294
return errors.Wrap(err, "failed to update data secret for EKSConfig")
294295
}
295296
if updated {
296-
log.Info("updated bootstrap data secret for EKSConfig", "secret", secret.Name)
297+
log.Info("updated bootstrap data secret for EKSConfig", "secret", klog.KObj(secret))
297298
} else {
298-
log.V(4).Info("no change in bootstrap data secret for EKSConfig", "secret", secret.Name)
299+
log.V(4).Info("no change in bootstrap data secret for EKSConfig", "secret", klog.KObj(secret))
299300
}
300301
}
301302

cmd/clusterawsadm/cloudformation/service/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (s *Service) ReconcileBootstrapStack(stackName string, t go_cfn.Template, t
6666
//nolint:nestif
6767
if err := s.createStack(stackName, processedYaml, stackTags); err != nil {
6868
if code, _ := awserrors.Code(errors.Cause(err)); code == "AlreadyExistsException" {
69-
klog.Infof("AWS Cloudformation stack %q already exists, updating", stackName)
69+
klog.Infof("AWS Cloudformation stack %q already exists, updating", klog.KRef("", stackName))
7070
updateErr := s.updateStack(stackName, processedYaml, stackTags)
7171
if updateErr != nil {
7272
code, ok := awserrors.Code(errors.Cause(updateErr))

cmd/clusterawsadm/cmd/ami/common/encryptedcopy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import (
2121
"os"
2222

2323
"github.com/spf13/cobra"
24+
ctrl "sigs.k8s.io/controller-runtime"
2425

2526
"sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/ami"
2627
"sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/cmd/flags"
2728
cmdout "sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/printers"
2829
"sigs.k8s.io/cluster-api/cmd/clusterctl/cmd"
29-
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
3030
)
3131

3232
var (
@@ -86,7 +86,7 @@ func EncryptedCopyAMICmd() *cobra.Command {
8686
fmt.Printf("Failed to parse dry-run value: %v. Defaulting to --dry-run=false\n", err)
8787
}
8888

89-
log := logf.Log
89+
log := ctrl.Log
9090

9191
ami, err := ami.Copy(ami.CopyInput{
9292
DestinationRegion: region,

cmd/clusterawsadm/cmd/root.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424

2525
"github.com/spf13/cobra"
2626
"github.com/spf13/pflag"
27+
"k8s.io/klog/v2"
28+
ctrl "sigs.k8s.io/controller-runtime"
2729

2830
"sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/cmd/ami"
2931
"sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/cmd/bootstrap"
@@ -33,7 +35,6 @@ import (
3335
"sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/cmd/resource"
3436
"sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/cmd/version"
3537
"sigs.k8s.io/cluster-api/cmd/clusterctl/cmd"
36-
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
3738
)
3839

3940
var (
@@ -92,7 +93,7 @@ func Execute() {
9293

9394
func init() {
9495
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
95-
verbosity := flag.CommandLine.Int("v", 2, "Set the log level verbosity.")
96+
verbosity = flag.CommandLine.Int("v", 2, "Set the log level verbosity.")
9697
_ = flag.Set("v", strconv.Itoa(*verbosity))
9798
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
9899
}
@@ -102,5 +103,5 @@ func init() {
102103
}
103104

104105
func initConfig() {
105-
logf.SetLogger(logf.NewLogger(logf.WithThreshold(verbosity)))
106+
ctrl.SetLogger(klog.NewKlogr().V(*verbosity))
106107
}

controllers/awscluster_controller.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
apierrors "k8s.io/apimachinery/pkg/api/errors"
2929
"k8s.io/apimachinery/pkg/types"
3030
"k8s.io/client-go/tools/record"
31+
"k8s.io/klog/v2"
3132
ctrl "sigs.k8s.io/controller-runtime"
3233
"sigs.k8s.io/controller-runtime/pkg/client"
3334
"sigs.k8s.io/controller-runtime/pkg/controller"
@@ -157,7 +158,7 @@ func (r *AWSClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
157158
return reconcile.Result{}, nil
158159
}
159160

160-
log = log.WithValues("cluster", cluster.Name)
161+
log = log.WithValues("cluster", klog.KObj(cluster))
161162
helper, err := patch.NewHelper(awsCluster, r.Client)
162163
if err != nil {
163164
return reconcile.Result{}, errors.Wrap(err, "failed to init patch helper")
@@ -402,7 +403,7 @@ func (r *AWSClusterReconciler) requeueAWSClusterForUnpausedCluster(ctx context.C
402403
panic(fmt.Sprintf("Expected a Cluster but got a %T", o))
403404
}
404405

405-
log := log.WithValues("objectMapper", "clusterToAWSCluster", "namespace", c.Namespace, "cluster", c.Name)
406+
log := log.WithValues("objectMapper", "clusterToAWSCluster", "cluster", klog.KRef(c.Namespace, c.Name))
406407

407408
// Don't handle deleted clusters
408409
if !c.ObjectMeta.DeletionTimestamp.IsZero() {

0 commit comments

Comments
 (0)