Skip to content

Commit c193b6d

Browse files
authored
feat: add migration shim and config for v2 logger
Signed-off-by: Evan Baker <[email protected]
1 parent d3bbfc8 commit c193b6d

File tree

6 files changed

+131
-30
lines changed

6 files changed

+131
-30
lines changed

cns/configuration/configuration.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type CNSConfig struct {
3030
EnableCNIConflistGeneration bool
3131
EnableIPAMv2 bool
3232
EnableK8sDevicePlugin bool
33+
EnableLoggerV2 bool
3334
EnablePprof bool
3435
EnableStateMigration bool
3536
EnableSubnetScarcity bool

cns/logger/cnslogger.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
// wait time for closing AI telemetry session.
1818
const waitTimeInSecs = 10
1919

20-
type CNSLogger struct {
20+
type logger struct {
2121
logger *log.Logger
2222
zapLogger *zap.Logger
2323
th ai.TelemetryHandle
@@ -30,7 +30,8 @@ type CNSLogger struct {
3030
metadata map[string]string
3131
}
3232

33-
func New(fileName string, logLevel, logTarget int, logDir string) (*CNSLogger, error) {
33+
// Deprecated: The v1 logger is deprecated. Migrate to zap using the cns/logger/v2 package.
34+
func New(fileName string, logLevel, logTarget int, logDir string) (loggershim, error) {
3435
l, err := log.NewLoggerE(fileName, logLevel, logTarget, logDir)
3536
if err != nil {
3637
return nil, errors.Wrap(err, "could not get new logger")
@@ -46,18 +47,18 @@ func New(fileName string, logLevel, logTarget int, logDir string) (*CNSLogger, e
4647
}
4748
zapLogger := zap.New(platformCore, zap.AddCaller()).With(zap.Int("pid", os.Getpid()))
4849

49-
return &CNSLogger{
50+
return &logger{
5051
logger: l,
5152
zapLogger: zapLogger,
5253
metadata: map[string]string{},
5354
}, nil
5455
}
5556

56-
func (c *CNSLogger) InitAI(aiConfig ai.AIConfig, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
57+
func (c *logger) InitAI(aiConfig ai.AIConfig, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
5758
c.InitAIWithIKey(aiConfig, aiMetadata, disableTraceLogging, disableMetricLogging, disableEventLogging)
5859
}
5960

60-
func (c *CNSLogger) InitAIWithIKey(aiConfig ai.AIConfig, instrumentationKey string, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
61+
func (c *logger) InitAIWithIKey(aiConfig ai.AIConfig, instrumentationKey string, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
6162
th, err := ai.NewAITelemetry("", instrumentationKey, aiConfig)
6263
if err != nil {
6364
c.logger.Errorf("Error initializing AI Telemetry:%v", err)
@@ -70,28 +71,28 @@ func (c *CNSLogger) InitAIWithIKey(aiConfig ai.AIConfig, instrumentationKey stri
7071
c.disableEventLogging = disableEventLogging
7172
}
7273

73-
func (c *CNSLogger) Close() {
74+
func (c *logger) Close() {
7475
c.logger.Close()
7576
if c.th != nil {
7677
c.th.Close(waitTimeInSecs)
7778
}
7879
}
7980

80-
func (c *CNSLogger) SetContextDetails(orchestrator, nodeID string) {
81+
func (c *logger) SetContextDetails(orchestrator, nodeID string) {
8182
c.logger.Logf("SetContext details called with: %v orchestrator nodeID %v", orchestrator, nodeID)
8283
c.m.Lock()
8384
c.metadata[orchestratorTypeKey] = orchestrator
8485
c.metadata[nodeIDKey] = nodeID
8586
c.m.Unlock()
8687
}
8788

88-
func (c *CNSLogger) SetAPIServer(apiserver string) {
89+
func (c *logger) SetAPIServer(apiserver string) {
8990
c.m.Lock()
9091
c.metadata[apiServerKey] = apiserver
9192
c.m.Unlock()
9293
}
9394

94-
func (c *CNSLogger) Printf(format string, args ...any) {
95+
func (c *logger) Printf(format string, args ...any) {
9596
c.logger.Logf(format, args...)
9697
c.zapLogger.Info(fmt.Sprintf(format, args...))
9798
if c.th == nil || c.disableTraceLogging {
@@ -101,7 +102,7 @@ func (c *CNSLogger) Printf(format string, args ...any) {
101102
c.sendTraceInternal(msg, ai.InfoLevel)
102103
}
103104

104-
func (c *CNSLogger) Debugf(format string, args ...any) {
105+
func (c *logger) Debugf(format string, args ...any) {
105106
c.logger.Debugf(format, args...)
106107
c.zapLogger.Debug(fmt.Sprintf(format, args...))
107108
if c.th == nil || c.disableTraceLogging {
@@ -111,7 +112,7 @@ func (c *CNSLogger) Debugf(format string, args ...any) {
111112
c.sendTraceInternal(msg, ai.DebugLevel)
112113
}
113114

114-
func (c *CNSLogger) Warnf(format string, args ...any) {
115+
func (c *logger) Warnf(format string, args ...any) {
115116
c.logger.Warnf(format, args...)
116117
c.zapLogger.Warn(fmt.Sprintf(format, args...))
117118
if c.th == nil || c.disableTraceLogging {
@@ -121,7 +122,7 @@ func (c *CNSLogger) Warnf(format string, args ...any) {
121122
c.sendTraceInternal(msg, ai.WarnLevel)
122123
}
123124

124-
func (c *CNSLogger) Errorf(format string, args ...any) {
125+
func (c *logger) Errorf(format string, args ...any) {
125126
c.logger.Errorf(format, args...)
126127
c.zapLogger.Error(fmt.Sprintf(format, args...))
127128
if c.th == nil || c.disableTraceLogging {
@@ -131,7 +132,7 @@ func (c *CNSLogger) Errorf(format string, args ...any) {
131132
c.sendTraceInternal(msg, ai.ErrorLevel)
132133
}
133134

134-
func (c *CNSLogger) Request(tag string, request any, err error) {
135+
func (c *logger) Request(tag string, request any, err error) {
135136
c.logger.Request(tag, request, err)
136137
if c.th == nil || c.disableTraceLogging {
137138
return
@@ -147,7 +148,7 @@ func (c *CNSLogger) Request(tag string, request any, err error) {
147148
c.sendTraceInternal(msg, lvl)
148149
}
149150

150-
func (c *CNSLogger) Response(tag string, response any, returnCode types.ResponseCode, err error) {
151+
func (c *logger) Response(tag string, response any, returnCode types.ResponseCode, err error) {
151152
c.logger.Response(tag, response, int(returnCode), returnCode.String(), err)
152153
if c.th == nil || c.disableTraceLogging {
153154
return
@@ -166,7 +167,7 @@ func (c *CNSLogger) Response(tag string, response any, returnCode types.Response
166167
c.sendTraceInternal(msg, lvl)
167168
}
168169

169-
func (c *CNSLogger) ResponseEx(tag string, request, response any, returnCode types.ResponseCode, err error) {
170+
func (c *logger) ResponseEx(tag string, request, response any, returnCode types.ResponseCode, err error) {
170171
c.logger.ResponseEx(tag, request, response, int(returnCode), returnCode.String(), err)
171172
if c.th == nil || c.disableTraceLogging {
172173
return
@@ -185,7 +186,7 @@ func (c *CNSLogger) ResponseEx(tag string, request, response any, returnCode typ
185186
c.sendTraceInternal(msg, lvl)
186187
}
187188

188-
func (c *CNSLogger) sendTraceInternal(msg string, lvl ai.Level) {
189+
func (c *logger) sendTraceInternal(msg string, lvl ai.Level) {
189190
report := ai.Report{
190191
Message: msg,
191192
Level: lvl,
@@ -198,7 +199,7 @@ func (c *CNSLogger) sendTraceInternal(msg string, lvl ai.Level) {
198199
c.th.TrackLog(report)
199200
}
200201

201-
func (c *CNSLogger) LogEvent(event ai.Event) {
202+
func (c *logger) LogEvent(event ai.Event) {
202203
if c.th == nil || c.disableEventLogging {
203204
return
204205
}
@@ -208,7 +209,7 @@ func (c *CNSLogger) LogEvent(event ai.Event) {
208209
c.th.TrackEvent(event)
209210
}
210211

211-
func (c *CNSLogger) SendMetric(metric ai.Metric) {
212+
func (c *logger) SendMetric(metric ai.Metric) {
212213
if c.th == nil || c.disableMetricLogging {
213214
return
214215
}

cns/logger/log.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,66 +6,95 @@ import (
66
"github.com/Azure/azure-container-networking/cns/types"
77
)
88

9+
type loggershim interface {
10+
Close()
11+
InitAI(aitelemetry.AIConfig, bool, bool, bool)
12+
InitAIWithIKey(aitelemetry.AIConfig, string, bool, bool, bool)
13+
SetContextDetails(string, string)
14+
SetAPIServer(string)
15+
Printf(string, ...any)
16+
Debugf(string, ...any)
17+
Warnf(string, ...any)
18+
LogEvent(aitelemetry.Event)
19+
Errorf(string, ...any)
20+
Request(string, any, error)
21+
Response(string, any, types.ResponseCode, error)
22+
ResponseEx(string, any, any, types.ResponseCode, error)
23+
SendMetric(aitelemetry.Metric)
24+
}
25+
926
var (
10-
Log *CNSLogger
11-
aiMetadata string // this var is set at build time.
27+
Log loggershim
1228
AppInsightsIKey = aiMetadata
29+
aiMetadata string // this var is set at build time.
1330
)
1431

15-
// todo: the functions below should be removed. CNSLogger should be injected where needed and not used from package level scope.
16-
32+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
1733
func Close() {
1834
Log.Close()
1935
}
2036

37+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
2138
func InitLogger(fileName string, logLevel, logTarget int, logDir string) {
2239
Log, _ = New(fileName, logLevel, logTarget, logDir)
2340
}
2441

42+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
2543
func InitAI(aiConfig aitelemetry.AIConfig, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
2644
Log.InitAI(aiConfig, disableTraceLogging, disableMetricLogging, disableEventLogging)
2745
}
2846

47+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
2948
func InitAIWithIKey(aiConfig aitelemetry.AIConfig, instrumentationKey string, disableTraceLogging, disableMetricLogging, disableEventLogging bool) {
3049
Log.InitAIWithIKey(aiConfig, instrumentationKey, disableTraceLogging, disableMetricLogging, disableEventLogging)
3150
}
3251

52+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
3353
func SetContextDetails(orchestrator, nodeID string) {
3454
Log.SetContextDetails(orchestrator, nodeID)
3555
}
3656

57+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
3758
func Printf(format string, args ...any) {
3859
Log.Printf(format, args...)
3960
}
4061

62+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
4163
func Debugf(format string, args ...any) {
4264
Log.Debugf(format, args...)
4365
}
4466

67+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
4568
func Warnf(format string, args ...any) {
4669
Log.Warnf(format, args...)
4770
}
4871

72+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
4973
func LogEvent(event aitelemetry.Event) {
5074
Log.LogEvent(event)
5175
}
5276

77+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
5378
func Errorf(format string, args ...any) {
5479
Log.Errorf(format, args...)
5580
}
5681

82+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
5783
func Request(tag string, request any, err error) {
5884
Log.Request(tag, request, err)
5985
}
6086

87+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
6188
func Response(tag string, response any, returnCode types.ResponseCode, err error) {
6289
Log.Response(tag, response, returnCode, err)
6390
}
6491

92+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
6593
func ResponseEx(tag string, request, response any, returnCode types.ResponseCode, err error) {
6694
Log.ResponseEx(tag, request, response, returnCode, err)
6795
}
6896

97+
// Deprecated: The global logger is deprecated. Migrate to zap using the cns/logger/v2 package and pass the logger instead.
6998
func SendMetric(metric aitelemetry.Metric) {
7099
Log.SendMetric(metric)
71100
}

cns/logger/v2/logger.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Package logger provides an opinionated logger for CNS which knows how to
2+
// log to Application Insights, file, stdout and ETW (based on platform).
13
package logger
24

35
import (

cns/logger/v2/shim.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package logger
2+
3+
import (
4+
"github.com/Azure/azure-container-networking/aitelemetry"
5+
"github.com/Azure/azure-container-networking/cns/types"
6+
"go.uber.org/zap"
7+
)
8+
9+
// shim wraps the Zap logger to provide a compatible interface to the
10+
// legacy CNS logger. This is temporary and exists to make migration
11+
// feasible and optional.
12+
type shim struct {
13+
z *zap.Logger
14+
closer func()
15+
}
16+
17+
func (s *shim) Close() {
18+
_ = s.z.Sync()
19+
s.closer()
20+
}
21+
22+
func (s *shim) Printf(format string, a ...any) {
23+
s.z.Sugar().Infof(format, a...)
24+
}
25+
26+
func (s *shim) Debugf(format string, a ...any) {
27+
s.z.Sugar().Debugf(format, a...)
28+
}
29+
30+
func (s *shim) Warnf(format string, a ...any) {
31+
s.z.Sugar().Warnf(format, a...)
32+
}
33+
34+
func (s *shim) Errorf(format string, a ...any) {
35+
s.z.Sugar().Errorf(format, a...)
36+
}
37+
38+
func (s *shim) Request(msg string, data any, err error) {
39+
s.z.Sugar().Infow("Request", "message", msg, "data", data, "error", err)
40+
}
41+
42+
func (s *shim) Response(msg string, data any, code types.ResponseCode, err error) {
43+
s.z.Sugar().Infow("Response", "message", msg, "data", data, "code", code, "error", err)
44+
}
45+
46+
func (s *shim) ResponseEx(msg string, request, response any, code types.ResponseCode, err error) {
47+
s.z.Sugar().Infow("ResponseEx", "message", msg, "request", request, "response", response, "code", code, "error", err)
48+
}
49+
50+
func (*shim) InitAI(aitelemetry.AIConfig, bool, bool, bool) {}
51+
52+
func (*shim) InitAIWithIKey(aitelemetry.AIConfig, string, bool, bool, bool) {}
53+
54+
func (s *shim) SetContextDetails(string, string) {}
55+
56+
func (s *shim) SetAPIServer(string) {}
57+
58+
func (s *shim) SendMetric(aitelemetry.Metric) {}
59+
60+
func (s *shim) LogEvent(aitelemetry.Event) {}
61+
62+
func AsV1(z *zap.Logger, closer func()) *shim { //nolint:revive // I want it to be annoying to use.
63+
return &shim{z: z, closer: closer}
64+
}

cns/service/main.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
nncctrl "github.com/Azure/azure-container-networking/cns/kubecontroller/nodenetworkconfig"
4242
podctrl "github.com/Azure/azure-container-networking/cns/kubecontroller/pod"
4343
"github.com/Azure/azure-container-networking/cns/logger"
44+
loggerv2 "github.com/Azure/azure-container-networking/cns/logger/v2"
4445
"github.com/Azure/azure-container-networking/cns/metric"
4546
"github.com/Azure/azure-container-networking/cns/middlewares"
4647
"github.com/Azure/azure-container-networking/cns/multitenantcontroller"
@@ -69,7 +70,6 @@ import (
6970
"github.com/google/go-cmp/cmp"
7071
"github.com/pkg/errors"
7172
"go.uber.org/zap"
72-
"go.uber.org/zap/zapcore"
7373
"golang.org/x/time/rate"
7474
corev1 "k8s.io/api/core/v1"
7575
apierrors "k8s.io/apimachinery/pkg/api/errors"
@@ -130,7 +130,6 @@ const (
130130
var (
131131
rootCtx context.Context
132132
rootErrCh chan error
133-
z *zap.Logger
134133
)
135134

136135
// Version is populated by make during build.
@@ -629,13 +628,18 @@ func main() {
629628
}
630629
}
631630

632-
// configure zap logger
633-
zconfig := zap.NewProductionConfig()
634-
zconfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
635-
if z, err = zconfig.Build(); err != nil {
631+
// build the zap logger
632+
z, c, err := loggerv2.New(&loggerv2.Config{})
633+
defer c()
634+
if err != nil {
636635
fmt.Printf("failed to create logger: %v", err)
637636
os.Exit(1)
638637
}
638+
// set the logger to the global logger if configured
639+
if cnsconfig.EnableLoggerV2 {
640+
logger.Printf("hotswapping logger v2")
641+
logger.Log = loggerv2.AsV1(z, c)
642+
}
639643

640644
// start the healthz/readyz/metrics server
641645
readyCh := make(chan interface{})
@@ -866,7 +870,7 @@ func main() {
866870

867871
logger.Printf("Set GlobalPodInfoScheme %v (InitializeFromCNI=%t)", cns.GlobalPodInfoScheme, cnsconfig.InitializeFromCNI)
868872

869-
err = InitializeCRDState(rootCtx, httpRemoteRestService, cnsconfig)
873+
err = InitializeCRDState(rootCtx, z, httpRemoteRestService, cnsconfig)
870874
if err != nil {
871875
logger.Errorf("Failed to start CRD Controller, err:%v.\n", err)
872876
return
@@ -1369,7 +1373,7 @@ func reconcileInitialCNSState(ctx context.Context, cli nodeNetworkConfigGetter,
13691373
}
13701374

13711375
// InitializeCRDState builds and starts the CRD controllers.
1372-
func InitializeCRDState(ctx context.Context, httpRestService cns.HTTPService, cnsconfig *configuration.CNSConfig) error {
1376+
func InitializeCRDState(ctx context.Context, z *zap.Logger, httpRestService cns.HTTPService, cnsconfig *configuration.CNSConfig) error {
13731377
// convert interface type to implementation type
13741378
httpRestServiceImplementation, ok := httpRestService.(*restserver.HTTPRestService)
13751379
if !ok {

0 commit comments

Comments
 (0)