Skip to content

Commit f0a713e

Browse files
committed
Switch logging implementation from zap to klog v2 for consistency
This commit removes the dependencies on go.uber.org/zap and sigs.k8s.io/controller-runtime/pkg/log/zap, replacing them with klog v2. Other Metal3 controllers—including CAPM3, Ironic, and IPAM— already use klog v2, so this change aligns the logging approach across the project and ensures a consistent logging format. Signed-off-by: Feruzjon Muyassarov <[email protected]>
1 parent e8c5bb6 commit f0a713e

File tree

4 files changed

+33
-41
lines changed

4 files changed

+33
-41
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ require (
1414
github.com/prometheus/client_golang v1.23.2
1515
github.com/stretchr/testify v1.11.1
1616
go.etcd.io/etcd/client/pkg/v3 v3.6.6
17-
go.uber.org/zap v1.27.0
1817
k8s.io/api v0.34.2
1918
k8s.io/apimachinery v0.34.2
2019
k8s.io/client-go v0.34.2
@@ -77,6 +76,7 @@ require (
7776
go.opentelemetry.io/otel/trace v1.35.0 // indirect
7877
go.opentelemetry.io/proto/otlp v1.5.0 // indirect
7978
go.uber.org/multierr v1.11.0 // indirect
79+
go.uber.org/zap v1.27.0 // indirect
8080
go.yaml.in/yaml/v2 v2.4.2 // indirect
8181
go.yaml.in/yaml/v3 v3.0.4 // indirect
8282
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect

main.go

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,15 @@ import (
3636
"github.com/metal3-io/baremetal-operator/pkg/secretutils"
3737
"github.com/metal3-io/baremetal-operator/pkg/version"
3838
ironicv1alpha1 "github.com/metal3-io/ironic-standalone-operator/api/v1alpha1"
39-
"go.uber.org/zap/zapcore"
4039
k8sruntime "k8s.io/apimachinery/pkg/runtime"
4140
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
4241
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
4342
cliflag "k8s.io/component-base/cli/flag"
43+
"k8s.io/klog/v2"
4444
ctrl "sigs.k8s.io/controller-runtime"
4545
"sigs.k8s.io/controller-runtime/pkg/cache"
4646
"sigs.k8s.io/controller-runtime/pkg/client"
4747
"sigs.k8s.io/controller-runtime/pkg/healthz"
48-
"sigs.k8s.io/controller-runtime/pkg/log/zap"
4948
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
5049
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
5150
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -65,7 +64,7 @@ type TLSOptions struct {
6564

6665
var (
6766
scheme = k8sruntime.NewScheme()
68-
setupLog = ctrl.Log.WithName("setup")
67+
setupLog = klog.NewKlogr().WithName("setup")
6968
healthAddr string
7069
tlsOptions = TLSOptions{}
7170
tlsSupportedVersions = []string{TLSVersion12, TLSVersion13}
@@ -177,14 +176,7 @@ func main() {
177176

178177
flag.Parse()
179178

180-
logOpts := zap.Options{}
181-
if devLogging {
182-
logOpts.Development = true
183-
logOpts.TimeEncoder = zapcore.ISO8601TimeEncoder
184-
} else {
185-
logOpts.TimeEncoder = zapcore.EpochTimeEncoder
186-
}
187-
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&logOpts)))
179+
ctrl.SetLogger(klog.NewKlogr())
188180

189181
printVersion()
190182

@@ -206,16 +198,16 @@ func main() {
206198
watchNamespaces := make(map[string]cache.Config)
207199

208200
if strings.Contains(watchNamespace, ",") {
209-
setupLog.Info("Manager set up with multiple namespaces to watch", "namespaces", watchNamespace)
201+
klog.Info("Manager set up with multiple namespaces to watch", "namespaces", watchNamespace)
210202
namespaces := strings.Split(watchNamespace, ",")
211203
for _, namespace := range namespaces {
212204
watchNamespaces[namespace] = cache.Config{}
213205
}
214206
} else if watchNamespace != "" {
215-
setupLog.Info("Manager set up to watch a single namespace", "namespace", watchNamespace)
207+
klog.Info("Manager set up to watch a single namespace", "namespace", watchNamespace)
216208
watchNamespaces[watchNamespace] = cache.Config{}
217209
} else {
218-
setupLog.Info("Manager set up with cluster scope")
210+
klog.Info("Manager set up with cluster scope")
219211
}
220212

221213
// Setup cache options
@@ -302,13 +294,14 @@ func main() {
302294

303295
var provisionerFactory provisioner.Factory
304296
if runInTestMode {
305-
ctrl.Log.Info("using test provisioner")
297+
klog.Info("using test provisioner")
306298
provisionerFactory = &fixture.Fixture{}
307299
} else if runInDemoMode {
308-
ctrl.Log.Info("using demo provisioner")
300+
klog.Info("using demo provisioner")
309301
provisionerFactory = &demo.Demo{}
310302
} else {
311-
provLog := zap.New(zap.UseFlagOptions(&logOpts)).WithName("provisioner")
303+
provLog := klog.NewKlogr().WithName("provisioner")
304+
ctrl.SetLogger(provLog)
312305
// Check if we should use Ironic CR integration
313306
if ironicName != "" && ironicNamespace != "" {
314307
provisionerFactory, err = ironic.NewProvisionerFactoryWithClient(provLog, preprovImgEnable,
@@ -330,7 +323,7 @@ func main() {
330323

331324
if err = (&metal3iocontroller.BareMetalHostReconciler{
332325
Client: mgr.GetClient(),
333-
Log: ctrl.Log.WithName("controllers").WithName("BareMetalHost"),
326+
Log: ctrl.Log.WithName("BareMetalHost"),
334327
ProvisionerFactory: provisionerFactory,
335328
APIReader: mgr.GetAPIReader(),
336329
}).SetupWithManager(mgr, preprovImgEnable, maxConcurrency); err != nil {
@@ -341,7 +334,7 @@ func main() {
341334
if preprovImgEnable {
342335
imgReconciler := metal3iocontroller.PreprovisioningImageReconciler{
343336
Client: mgr.GetClient(),
344-
Log: ctrl.Log.WithName("controllers").WithName("PreprovisioningImage"),
337+
Log: ctrl.Log.WithName("PreprovisioningImage"),
345338
APIReader: mgr.GetAPIReader(),
346339
Scheme: mgr.GetScheme(),
347340
ImageProvider: imageprovider.NewDefaultImageProvider(),
@@ -357,7 +350,7 @@ func main() {
357350

358351
if err = (&metal3iocontroller.HostFirmwareSettingsReconciler{
359352
Client: mgr.GetClient(),
360-
Log: ctrl.Log.WithName("controllers").WithName("HostFirmwareSettings"),
353+
Log: ctrl.Log.WithName("HostFirmwareSettings"),
361354
ProvisionerFactory: provisionerFactory,
362355
}).SetupWithManager(mgr, maxConcurrency); err != nil {
363356
setupLog.Error(err, "unable to create controller", "controller", "HostFirmwareSettings")
@@ -366,7 +359,7 @@ func main() {
366359

367360
if err = (&metal3iocontroller.BMCEventSubscriptionReconciler{
368361
Client: mgr.GetClient(),
369-
Log: ctrl.Log.WithName("controllers").WithName("BMCEventSubscription"),
362+
Log: ctrl.Log.WithName("BMCEventSubscription"),
370363
ProvisionerFactory: provisionerFactory,
371364
}).SetupWithManager(mgr, maxConcurrency); err != nil {
372365
setupLog.Error(err, "unable to create controller", "controller", "BMCEventSubscription")
@@ -375,7 +368,7 @@ func main() {
375368

376369
if err = (&metal3iocontroller.HostFirmwareComponentsReconciler{
377370
Client: mgr.GetClient(),
378-
Log: ctrl.Log.WithName("controllers").WithName("HostFirmwareComponents"),
371+
Log: ctrl.Log.WithName("HostFirmwareComponents"),
379372
ProvisionerFactory: provisionerFactory,
380373
}).SetupWithManager(mgr, maxConcurrency); err != nil {
381374
setupLog.Error(err, "unable to create controller", "controller", "HostFirmwareComponents")
@@ -384,7 +377,7 @@ func main() {
384377

385378
if err = (&metal3iocontroller.DataImageReconciler{
386379
Client: mgr.GetClient(),
387-
Log: ctrl.Log.WithName("controllers").WithName("DataImage"),
380+
Log: ctrl.Log.WithName("DataImage"),
388381
ProvisionerFactory: provisionerFactory,
389382
}).SetupWithManager(mgr, maxConcurrency); err != nil {
390383
setupLog.Error(err, "unable to create controller", "controller", "DataImage")
@@ -397,7 +390,7 @@ func main() {
397390
setupWebhooks(mgr)
398391
}
399392

400-
setupLog.Info("starting manager")
393+
klog.Info("starting manager")
401394
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
402395
setupLog.Error(err, "problem running manager")
403396
os.Exit(1)
@@ -439,7 +432,7 @@ func GetTLSOptionOverrideFuncs(options TLSOptions) ([]func(*tls.Config), error)
439432
// Cipher suites should not be set if empty.
440433
if tlsMinVersion >= tls.VersionTLS13 &&
441434
options.TLSCipherSuites != "" {
442-
setupLog.Info("warning: Cipher suites should not be set for TLS version 1.3. Ignoring ciphers")
435+
klog.Info("warning: Cipher suites should not be set for TLS version 1.3. Ignoring ciphers")
443436
options.TLSCipherSuites = ""
444437
}
445438

@@ -454,7 +447,7 @@ func GetTLSOptionOverrideFuncs(options TLSOptions) ([]func(*tls.Config), error)
454447
for _, cipher := range tlsCipherSuites {
455448
for _, insecureCipherName := range insecureCipherValues {
456449
if insecureCipherName == cipher {
457-
setupLog.Info(fmt.Sprintf("warning: use of insecure cipher '%s' detected.", cipher))
450+
klog.Infof("warning: use of insecure cipher '%s' detected.", cipher)
458451
}
459452
}
460453
}

pkg/provisioner/fixture/fixture.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
metal3api "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
99
"github.com/metal3-io/baremetal-operator/pkg/hardwareutils/bmc"
1010
"github.com/metal3-io/baremetal-operator/pkg/provisioner"
11-
logz "sigs.k8s.io/controller-runtime/pkg/log/zap"
11+
"k8s.io/klog/v2"
1212
)
1313

14-
var log = logz.New().WithName("provisioner").WithName("fixture")
14+
var log = klog.NewKlogr().WithName("provisioner").WithName("fixture")
1515
var deprovisionRequeueDelay = time.Second * 10
1616
var provisionRequeueDelay = time.Second * 10
1717
var inspectionRequeueDelay = time.Second * 2

pkg/provisioner/ironic/factory.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,18 @@ func (f *ironicProvisionerFactory) init(havePreprovImgBuilder bool) error {
9393
}
9494

9595
tlsConf := loadTLSConfigFromEnv()
96-
9796
f.log.Info("ironic settings from environment variables",
98-
"endpoint", ironicEndpoint,
99-
"ironicAuthType", ironicAuth.Type,
100-
"deployKernelURL", f.config.deployKernelURL,
101-
"deployRamdiskURL", f.config.deployRamdiskURL,
102-
"deployISOURL", f.config.deployISOURL,
103-
"liveISOForcePersistentBootDevice", f.config.liveISOForcePersistentBootDevice,
104-
"CACertFile", tlsConf.TrustedCAFile,
105-
"ClientCertFile", tlsConf.ClientCertificateFile,
106-
"ClientPrivKeyFile", tlsConf.ClientPrivateKeyFile,
107-
"TLSInsecure", tlsConf.InsecureSkipVerify,
108-
"SkipClientSANVerify", tlsConf.SkipClientSANVerify,
97+
"\n endpoint", ironicEndpoint,
98+
"\n ironicAuthType", ironicAuth.Type,
99+
"\n deployKernelURL", f.config.deployKernelURL,
100+
"\n deployRamdiskURL", f.config.deployRamdiskURL,
101+
"\n deployISOURL", f.config.deployISOURL,
102+
"\n liveISOForcePersistentBootDevice", f.config.liveISOForcePersistentBootDevice,
103+
"\n CACertFile", tlsConf.TrustedCAFile,
104+
"\n ClientCertFile", tlsConf.ClientCertificateFile,
105+
"\n ClientPrivKeyFile", tlsConf.ClientPrivateKeyFile,
106+
"\n TLSInsecure", tlsConf.InsecureSkipVerify,
107+
"\n SkipClientSANVerify", tlsConf.SkipClientSANVerify,
109108
)
110109

111110
f.clientIronic, err = clients.IronicClient(ironicEndpoint, ironicAuth, tlsConf)

0 commit comments

Comments
 (0)