Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use contextual logging #126

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions charts/cloudstack-csi/templates/csi-controller-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ spec:
args:
- "--endpoint=$(CSI_ENDPOINT)"
- "--cloudstackconfig=$(CLOUD_CONFIG)"
- "--debug"
- "--logging-format={{ .Values.logFormat }}"
- "--v={{ .Values.logVerbosityLevel }}"
{{- if .Values.controller.csiDriverController.extraArgs }}
{{- with .Values.controller.csiDriverController.extraArgs }}
{{- tpl . $ | trim | nindent 12 }}
Expand Down Expand Up @@ -77,7 +78,7 @@ spec:
image: "{{ .Values.controller.provisioner.image.repository }}:{{ .Values.controller.provisioner.image.tag }}"
imagePullPolicy: {{ .Values.controller.provisioner.image.pullPolicy }}
args:
- "-v={{ .Values.logVerbosityLevel }}"
- "--v={{ .Values.logVerbosityLevel }}"
- "--csi-address=$(ADDRESS)"
- "--timeout={{ .Values.timeout }}"
- "--feature-gates=Topology={{ .Values.controller.provisioner.topology }}"
Expand Down Expand Up @@ -110,7 +111,7 @@ spec:
image: "{{ .Values.controller.attacher.image.repository }}:{{ .Values.controller.attacher.image.tag }}"
imagePullPolicy: {{ .Values.controller.attacher.image.pullPolicy }}
args:
- "-v={{ .Values.logVerbosityLevel }}"
- "--v={{ .Values.logVerbosityLevel }}"
- "--csi-address=$(ADDRESS)"
- "--timeout={{ .Values.timeout }}"
- "--default-fstype=ext4"
Expand Down Expand Up @@ -140,7 +141,7 @@ spec:
image: "{{ .Values.controller.resizer.image.repository }}:{{ .Values.controller.resizer.image.tag }}"
imagePullPolicy: {{ .Values.controller.resizer.image.pullPolicy }}
args:
- "-v={{ .Values.logVerbosityLevel }}"
- "--v={{ .Values.logVerbosityLevel }}"
- "--csi-address=$(ADDRESS)"
- "--timeout={{ .Values.timeout }}"
{{- if $enableLeaderElection }}
Expand Down Expand Up @@ -170,7 +171,7 @@ spec:
image: "{{ .Values.livenessProbe.image.repository }}:{{ .Values.livenessProbe.image.tag }}"
imagePullPolicy: {{ .Values.livenessProbe.image.pullPolicy }}
args:
- "-v={{ .Values.logVerbosityLevel }}"
- "--v={{ .Values.logVerbosityLevel }}"
- "--csi-address=$(ADDRESS)"
{{- if .Values.livenessProbe.extraArgs }}
{{- with .Values.livenessProbe.extraArgs }}
Expand Down
2 changes: 2 additions & 0 deletions charts/cloudstack-csi/templates/csi-node-ds.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ spec:
args:
- "--endpoint=$(CSI_ENDPOINT)"
- "--cloudstackconfig=$(CLOUD_CONFIG)"
- "--logging-format={{ .Values.logFormat }}"
- "--nodeName=$(NODE_NAME)"
- "--v={{ .Values.logVerbosityLevel }}"
{{- if .Values.node.csiDriver.extraArgs }}
{{- with .Values.node.csiDriver.extraArgs }}
{{- tpl . $ | trim | nindent 12 }}
Expand Down
3 changes: 3 additions & 0 deletions charts/cloudstack-csi/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ syncer:
# for description of individual verbosity levels.
logVerbosityLevel: 2

# Log format. Available options are "text" and "json"
logFormat: text

# the secret should contain the cloudstack credentials
# there are several options to inject the credentials:
# 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
Expand Down
77 changes: 37 additions & 40 deletions cmd/cloudstack-csi-driver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
package main

import (
"context"
"flag"
"fmt"
"os"
"path"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"k8s.io/component-base/featuregate"
"k8s.io/component-base/logs"
logsapi "k8s.io/component-base/logs/api/v1"
"k8s.io/component-base/logs/json"
"k8s.io/klog/v2"

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

// Version is set by the build process.
version = ""
isDevEnv = false
version = ""
)

func main() {
flag.Parse()

if *showVersion {
baseName := path.Base(os.Args[0])
fmt.Println(baseName, version) //nolint:forbidigo

return
if err := logsapi.RegisterLogFormat(logsapi.JSONLogFormat, json.Factory{}, logsapi.LoggingBetaOptions); err != nil {
klog.ErrorS(err, "failed to register JSON log format")
}

if version == "" {
isDevEnv = true
fg := featuregate.NewFeatureGate()
err := logsapi.AddFeatureGates(fg)
if err != nil {
klog.ErrorS(err, "failed to add feature gates")
}

run()
os.Exit(0)
}

func run() {
// Setup logging.
var logConfig zap.Config
if isDevEnv {
logConfig = zap.NewDevelopmentConfig()
} else {
logConfig = zap.NewProductionConfig()
c := logsapi.NewLoggingConfiguration()
logsapi.AddGoFlags(c, flag.CommandLine)
flag.Parse()
logs.InitLogs()
logger := klog.Background()
if err = logsapi.ValidateAndApply(c, fg); err != nil {
logger.Error(err, "LoggingConfiguration is invalid")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
if *debug {
logConfig.Level.SetLevel(zapcore.DebugLevel)

if *showVersion {
baseName := path.Base(os.Args[0])
fmt.Println(baseName, version) //nolint:forbidigo
os.Exit(0)
}
logger, _ := logConfig.Build()
defer func() { _ = logger.Sync() }()
undo := zap.ReplaceGlobals(logger)
defer undo()

// Setup cloud connector.
config, err := cloud.ReadConfig(*cloudstackconfig)
if err != nil {
logger.Sugar().Errorw("Cannot read CloudStack configuration", "error", err)
os.Exit(1) //nolint:gocritic
logger.Error(err, "Cannot read CloudStack configuration")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
logger.Sugar().Debugf("Successfully read CloudStack configuration %v", *cloudstackconfig)
logger.Info("Successfully read CloudStack configuration", "cloudstackconfig", *cloudstackconfig)

ctx := klog.NewContext(context.Background(), logger)
csConnector := cloud.New(config)

d, err := driver.New(*endpoint, csConnector, nil, *nodeName, version, logger)
d, err := driver.New(*endpoint, csConnector, nil, *nodeName, version)
if err != nil {
logger.Sugar().Errorw("Failed to initialize driver", "error", err)
os.Exit(1)
logger.Error(err, "Failed to initialize driver")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}

if err = d.Run(); err != nil {
logger.Sugar().Errorw("Server error", "error", err)
os.Exit(1)
if err = d.Run(ctx); err != nil {
logger.Error(err, "Failed to run driver")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
}
3 changes: 2 additions & 1 deletion deploy/k8s/controller-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ spec:
args:
- "-endpoint=$(CSI_ENDPOINT)"
- "-cloudstackconfig=/etc/cloudstack-csi-driver/cloud-config"
- "-debug"
- "-logging-format=text"
- "-v=4"
env:
- name: CSI_ENDPOINT
value: unix:///var/lib/csi/sockets/pluginproxy/csi.sock
Expand Down
3 changes: 2 additions & 1 deletion deploy/k8s/node-daemonset.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ spec:
args:
- "-endpoint=$(CSI_ENDPOINT)"
- "-cloudstackconfig=/etc/cloudstack-csi-driver/cloud-config"
- "-logging-format=text"
- "-nodeName=$(NODE_NAME)"
- "-debug"
- "-v=4"
env:
- name: CSI_ENDPOINT
value: unix:///csi/csi.sock
Expand Down
19 changes: 15 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@ go 1.21
require (
github.com/apache/cloudstack-go/v2 v2.16.1
github.com/container-storage-interface/spec v1.9.0
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
github.com/hashicorp/go-uuid v1.0.3
github.com/kubernetes-csi/csi-lib-utils v0.17.0
github.com/kubernetes-csi/csi-test/v5 v5.2.0
go.uber.org/zap v1.27.0
golang.org/x/text v0.16.0
google.golang.org/grpc v1.64.0
gopkg.in/gcfg.v1 v1.2.3
k8s.io/api v0.29.6
k8s.io/apimachinery v0.29.6
k8s.io/client-go v0.29.6
k8s.io/component-base v0.29.6
k8s.io/klog/v2 v2.110.1
k8s.io/mount-utils v0.29.6
k8s.io/utils v0.0.0-20240102154912-e7106e64919e
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/zapr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
Expand All @@ -37,17 +41,25 @@ require (
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/moby/sys/mountinfo v0.6.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/onsi/ginkgo/v2 v2.13.1 // indirect
github.com/onsi/gomega v1.30.0 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/sys v0.20.0 // indirect
Expand All @@ -61,7 +73,6 @@ require (
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
Expand Down
Loading
Loading