Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit 4fbdd82

Browse files
committed
Updated component-base/logs instead of old logging style
1 parent aa78f38 commit 4fbdd82

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

main.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@ import (
2929
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3030
cgrecord "k8s.io/client-go/tools/record"
3131
"k8s.io/component-base/version"
32-
"k8s.io/klog/v2/klogr"
32+
"k8s.io/klog/v2"
3333
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3434
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1"
3535
"sigs.k8s.io/cluster-api/util/record"
3636
ctrl "sigs.k8s.io/controller-runtime"
3737
"sigs.k8s.io/controller-runtime/pkg/controller"
3838

39+
"k8s.io/component-base/logs"
40+
logsv1 "k8s.io/component-base/logs/api/v1"
41+
3942
infrav1alpha3 "sigs.k8s.io/cluster-api-provider-packet/api/v1alpha3"
4043
infrav1beta1 "sigs.k8s.io/cluster-api-provider-packet/api/v1beta1"
4144
"sigs.k8s.io/cluster-api-provider-packet/controllers"
@@ -44,8 +47,9 @@ import (
4447
)
4548

4649
var (
47-
scheme = runtime.NewScheme()
48-
setupLog = ctrl.Log.WithName("setup")
50+
scheme = runtime.NewScheme()
51+
logOptions = logs.NewOptions()
52+
setupLog = ctrl.Log.WithName("setup")
4953
)
5054

5155
func init() {
@@ -54,7 +58,6 @@ func init() {
5458
utilruntime.Must(infrav1beta1.AddToScheme(scheme))
5559
utilruntime.Must(clusterv1.AddToScheme(scheme))
5660
utilruntime.Must(bootstrapv1.AddToScheme(scheme))
57-
5861
// +kubebuilder:scaffold:scheme
5962
}
6063

@@ -81,6 +84,11 @@ func main() {
8184
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
8285
pflag.Parse()
8386

87+
if err := logsv1.ValidateAndApply(logOptions, nil); err != nil {
88+
setupLog.Error(err, "unable to start manager")
89+
os.Exit(1)
90+
}
91+
8492
if watchNamespace != "" {
8593
setupLog.Info("Watching cluster-api objects only in namespace for reconciliation", "namespace", watchNamespace)
8694
}
@@ -95,7 +103,8 @@ func main() {
95103
}()
96104
}
97105

98-
ctrl.SetLogger(klogr.New())
106+
// klog.Background will automatically use the right logger.
107+
ctrl.SetLogger(klog.Background())
99108

100109
// Machine and cluster operations can create enough events to trigger the event recorder spam filter
101110
// Setting the burst size higher ensures all events will be recorded and submitted to the API
@@ -284,4 +293,6 @@ func initFlags(fs *pflag.FlagSet) {
284293
":9440",
285294
"The address the health endpoint binds to.",
286295
)
296+
297+
logsv1.AddFlags(logOptions, fs)
287298
}

0 commit comments

Comments
 (0)