Skip to content

Commit f222b54

Browse files
authored
Merge pull request #126 from Leaseweb/structlog
feat: Use contextual logging
2 parents f59f1cf + 27b8b7c commit f222b54

File tree

20 files changed

+301
-230
lines changed

20 files changed

+301
-230
lines changed

charts/cloudstack-csi/templates/csi-controller-deploy.yaml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ spec:
4545
args:
4646
- "--endpoint=$(CSI_ENDPOINT)"
4747
- "--cloudstackconfig=$(CLOUD_CONFIG)"
48-
- "--debug"
48+
- "--logging-format={{ .Values.logFormat }}"
49+
- "--v={{ .Values.logVerbosityLevel }}"
4950
{{- if .Values.controller.csiDriverController.extraArgs }}
5051
{{- with .Values.controller.csiDriverController.extraArgs }}
5152
{{- tpl . $ | trim | nindent 12 }}
@@ -77,7 +78,7 @@ spec:
7778
image: "{{ .Values.controller.provisioner.image.repository }}:{{ .Values.controller.provisioner.image.tag }}"
7879
imagePullPolicy: {{ .Values.controller.provisioner.image.pullPolicy }}
7980
args:
80-
- "-v={{ .Values.logVerbosityLevel }}"
81+
- "--v={{ .Values.logVerbosityLevel }}"
8182
- "--csi-address=$(ADDRESS)"
8283
- "--timeout={{ .Values.timeout }}"
8384
- "--feature-gates=Topology={{ .Values.controller.provisioner.topology }}"
@@ -110,7 +111,7 @@ spec:
110111
image: "{{ .Values.controller.attacher.image.repository }}:{{ .Values.controller.attacher.image.tag }}"
111112
imagePullPolicy: {{ .Values.controller.attacher.image.pullPolicy }}
112113
args:
113-
- "-v={{ .Values.logVerbosityLevel }}"
114+
- "--v={{ .Values.logVerbosityLevel }}"
114115
- "--csi-address=$(ADDRESS)"
115116
- "--timeout={{ .Values.timeout }}"
116117
- "--default-fstype=ext4"
@@ -140,7 +141,7 @@ spec:
140141
image: "{{ .Values.controller.resizer.image.repository }}:{{ .Values.controller.resizer.image.tag }}"
141142
imagePullPolicy: {{ .Values.controller.resizer.image.pullPolicy }}
142143
args:
143-
- "-v={{ .Values.logVerbosityLevel }}"
144+
- "--v={{ .Values.logVerbosityLevel }}"
144145
- "--csi-address=$(ADDRESS)"
145146
- "--timeout={{ .Values.timeout }}"
146147
{{- if $enableLeaderElection }}
@@ -170,7 +171,7 @@ spec:
170171
image: "{{ .Values.livenessProbe.image.repository }}:{{ .Values.livenessProbe.image.tag }}"
171172
imagePullPolicy: {{ .Values.livenessProbe.image.pullPolicy }}
172173
args:
173-
- "-v={{ .Values.logVerbosityLevel }}"
174+
- "--v={{ .Values.logVerbosityLevel }}"
174175
- "--csi-address=$(ADDRESS)"
175176
{{- if .Values.livenessProbe.extraArgs }}
176177
{{- with .Values.livenessProbe.extraArgs }}

charts/cloudstack-csi/templates/csi-node-ds.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ spec:
3333
args:
3434
- "--endpoint=$(CSI_ENDPOINT)"
3535
- "--cloudstackconfig=$(CLOUD_CONFIG)"
36+
- "--logging-format={{ .Values.logFormat }}"
3637
- "--nodeName=$(NODE_NAME)"
38+
- "--v={{ .Values.logVerbosityLevel }}"
3739
{{- if .Values.node.csiDriver.extraArgs }}
3840
{{- with .Values.node.csiDriver.extraArgs }}
3941
{{- tpl . $ | trim | nindent 12 }}

charts/cloudstack-csi/values.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ syncer:
188188
# for description of individual verbosity levels.
189189
logVerbosityLevel: 2
190190

191+
# Log format. Available options are "text" and "json"
192+
logFormat: text
193+
191194
# the secret should contain the cloudstack credentials
192195
# there are several options to inject the credentials:
193196
# 1) from kubernetes secret that doesn't exist: set "enabled" and "create" to true, this will create a secret from the values written to "data" down below

cmd/cloudstack-csi-driver/main.go

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
package main
77

88
import (
9+
"context"
910
"flag"
1011
"fmt"
1112
"os"
1213
"path"
1314

14-
"go.uber.org/zap"
15-
"go.uber.org/zap/zapcore"
15+
"k8s.io/component-base/featuregate"
16+
"k8s.io/component-base/logs"
17+
logsapi "k8s.io/component-base/logs/api/v1"
18+
"k8s.io/component-base/logs/json"
19+
"k8s.io/klog/v2"
1620

1721
"github.com/leaseweb/cloudstack-csi-driver/pkg/cloud"
1822
"github.com/leaseweb/cloudstack-csi-driver/pkg/driver"
@@ -22,65 +26,58 @@ var (
2226
endpoint = flag.String("endpoint", "unix:///tmp/csi.sock", "CSI endpoint")
2327
cloudstackconfig = flag.String("cloudstackconfig", "./cloud-config", "CloudStack configuration file")
2428
nodeName = flag.String("nodeName", "", "Node name")
25-
debug = flag.Bool("debug", false, "Enable debug logging")
2629
showVersion = flag.Bool("version", false, "Show version")
2730

2831
// Version is set by the build process.
29-
version = ""
30-
isDevEnv = false
32+
version = ""
3133
)
3234

3335
func main() {
34-
flag.Parse()
35-
36-
if *showVersion {
37-
baseName := path.Base(os.Args[0])
38-
fmt.Println(baseName, version) //nolint:forbidigo
39-
40-
return
36+
if err := logsapi.RegisterLogFormat(logsapi.JSONLogFormat, json.Factory{}, logsapi.LoggingBetaOptions); err != nil {
37+
klog.ErrorS(err, "failed to register JSON log format")
4138
}
4239

43-
if version == "" {
44-
isDevEnv = true
40+
fg := featuregate.NewFeatureGate()
41+
err := logsapi.AddFeatureGates(fg)
42+
if err != nil {
43+
klog.ErrorS(err, "failed to add feature gates")
4544
}
4645

47-
run()
48-
os.Exit(0)
49-
}
50-
51-
func run() {
52-
// Setup logging.
53-
var logConfig zap.Config
54-
if isDevEnv {
55-
logConfig = zap.NewDevelopmentConfig()
56-
} else {
57-
logConfig = zap.NewProductionConfig()
46+
c := logsapi.NewLoggingConfiguration()
47+
logsapi.AddGoFlags(c, flag.CommandLine)
48+
flag.Parse()
49+
logs.InitLogs()
50+
logger := klog.Background()
51+
if err = logsapi.ValidateAndApply(c, fg); err != nil {
52+
logger.Error(err, "LoggingConfiguration is invalid")
53+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
5854
}
59-
if *debug {
60-
logConfig.Level.SetLevel(zapcore.DebugLevel)
55+
56+
if *showVersion {
57+
baseName := path.Base(os.Args[0])
58+
fmt.Println(baseName, version) //nolint:forbidigo
59+
os.Exit(0)
6160
}
62-
logger, _ := logConfig.Build()
63-
defer func() { _ = logger.Sync() }()
64-
undo := zap.ReplaceGlobals(logger)
65-
defer undo()
6661

6762
// Setup cloud connector.
6863
config, err := cloud.ReadConfig(*cloudstackconfig)
6964
if err != nil {
70-
logger.Sugar().Errorw("Cannot read CloudStack configuration", "error", err)
71-
os.Exit(1) //nolint:gocritic
65+
logger.Error(err, "Cannot read CloudStack configuration")
66+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
7267
}
73-
logger.Sugar().Debugf("Successfully read CloudStack configuration %v", *cloudstackconfig)
68+
logger.Info("Successfully read CloudStack configuration", "cloudstackconfig", *cloudstackconfig)
69+
70+
ctx := klog.NewContext(context.Background(), logger)
7471
csConnector := cloud.New(config)
7572

76-
d, err := driver.New(*endpoint, csConnector, nil, *nodeName, version, logger)
73+
d, err := driver.New(*endpoint, csConnector, nil, *nodeName, version)
7774
if err != nil {
78-
logger.Sugar().Errorw("Failed to initialize driver", "error", err)
79-
os.Exit(1)
75+
logger.Error(err, "Failed to initialize driver")
76+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
8077
}
8178

82-
if err = d.Run(); err != nil {
83-
logger.Sugar().Errorw("Server error", "error", err)
84-
os.Exit(1)
79+
if err = d.Run(ctx); err != nil {
80+
logger.Error(err, "Failed to run driver")
81+
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
8582
}
8683
}

deploy/k8s/controller-deployment.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ spec:
3838
args:
3939
- "-endpoint=$(CSI_ENDPOINT)"
4040
- "-cloudstackconfig=/etc/cloudstack-csi-driver/cloud-config"
41-
- "-debug"
41+
- "-logging-format=text"
42+
- "-v=4"
4243
env:
4344
- name: CSI_ENDPOINT
4445
value: unix:///var/lib/csi/sockets/pluginproxy/csi.sock

deploy/k8s/node-daemonset.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ spec:
3030
args:
3131
- "-endpoint=$(CSI_ENDPOINT)"
3232
- "-cloudstackconfig=/etc/cloudstack-csi-driver/cloud-config"
33+
- "-logging-format=text"
3334
- "-nodeName=$(NODE_NAME)"
34-
- "-debug"
35+
- "-v=4"
3536
env:
3637
- name: CSI_ENDPOINT
3738
value: unix:///csi/csi.sock

go.mod

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,29 @@ go 1.21
55
require (
66
github.com/apache/cloudstack-go/v2 v2.16.1
77
github.com/container-storage-interface/spec v1.9.0
8-
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
98
github.com/hashicorp/go-uuid v1.0.3
109
github.com/kubernetes-csi/csi-lib-utils v0.17.0
1110
github.com/kubernetes-csi/csi-test/v5 v5.2.0
12-
go.uber.org/zap v1.27.0
1311
golang.org/x/text v0.16.0
1412
google.golang.org/grpc v1.64.0
1513
gopkg.in/gcfg.v1 v1.2.3
1614
k8s.io/api v0.29.6
1715
k8s.io/apimachinery v0.29.6
1816
k8s.io/client-go v0.29.6
17+
k8s.io/component-base v0.29.6
18+
k8s.io/klog/v2 v2.110.1
1919
k8s.io/mount-utils v0.29.6
2020
k8s.io/utils v0.0.0-20240102154912-e7106e64919e
2121
)
2222

2323
require (
24+
github.com/beorn7/perks v1.0.1 // indirect
25+
github.com/blang/semver/v4 v4.0.0 // indirect
26+
github.com/cespare/xxhash/v2 v2.2.0 // indirect
2427
github.com/davecgh/go-spew v1.1.1 // indirect
2528
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
2629
github.com/go-logr/logr v1.3.0 // indirect
30+
github.com/go-logr/zapr v1.2.3 // indirect
2731
github.com/go-openapi/jsonpointer v0.19.6 // indirect
2832
github.com/go-openapi/jsonreference v0.20.2 // indirect
2933
github.com/go-openapi/swag v0.22.3 // indirect
@@ -37,17 +41,25 @@ require (
3741
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
3842
github.com/google/uuid v1.6.0 // indirect
3943
github.com/imdario/mergo v0.3.6 // indirect
44+
github.com/inconshreveable/mousetrap v1.1.0 // indirect
4045
github.com/josharian/intern v1.0.0 // indirect
4146
github.com/json-iterator/go v1.1.12 // indirect
4247
github.com/mailru/easyjson v0.7.7 // indirect
48+
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
4349
github.com/moby/sys/mountinfo v0.6.2 // indirect
4450
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4551
github.com/modern-go/reflect2 v1.0.2 // indirect
4652
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
4753
github.com/onsi/ginkgo/v2 v2.13.1 // indirect
4854
github.com/onsi/gomega v1.30.0 // indirect
55+
github.com/prometheus/client_golang v1.16.0 // indirect
56+
github.com/prometheus/client_model v0.4.0 // indirect
57+
github.com/prometheus/common v0.44.0 // indirect
58+
github.com/prometheus/procfs v0.10.1 // indirect
59+
github.com/spf13/cobra v1.7.0 // indirect
4960
github.com/spf13/pflag v1.0.5 // indirect
50-
go.uber.org/multierr v1.10.0 // indirect
61+
go.uber.org/multierr v1.11.0 // indirect
62+
go.uber.org/zap v1.27.0 // indirect
5163
golang.org/x/net v0.25.0 // indirect
5264
golang.org/x/oauth2 v0.18.0 // indirect
5365
golang.org/x/sys v0.20.0 // indirect
@@ -61,7 +73,6 @@ require (
6173
gopkg.in/warnings.v0 v0.1.2 // indirect
6274
gopkg.in/yaml.v2 v2.4.0 // indirect
6375
gopkg.in/yaml.v3 v3.0.1 // indirect
64-
k8s.io/klog/v2 v2.110.1 // indirect
6576
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
6677
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
6778
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect

0 commit comments

Comments
 (0)