Skip to content

Commit dee69a3

Browse files
authored
refactor: cmd slog wrapper (#6573)
1 parent c22e87b commit dee69a3

File tree

3 files changed

+118
-182
lines changed

3 files changed

+118
-182
lines changed

cmd/nginx-ingress/flags.go

Lines changed: 39 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import (
1313
"k8s.io/apimachinery/pkg/labels"
1414
"k8s.io/apimachinery/pkg/util/validation"
1515

16-
nlog "github.com/nginxinc/kubernetes-ingress/internal/logger"
17-
"github.com/nginxinc/kubernetes-ingress/internal/logger/levels"
16+
nl "github.com/nginxinc/kubernetes-ingress/internal/logger"
1817
)
1918

2019
const (
@@ -233,29 +232,29 @@ func parseFlags() {
233232
}
234233

235234
func initValidate(ctx context.Context) {
236-
l := nlog.LoggerFromContext(ctx)
235+
l := nl.LoggerFromContext(ctx)
237236
logFormatValidationError := validateLogFormat(*logFormat)
238237
if logFormatValidationError != nil {
239-
l.Warn(fmt.Sprintf("Invalid log format: %s. Valid options are: glog, text, json. Falling back to default: %s", *logFormat, logFormatDefault))
238+
nl.Warnf(l, "Invalid log format: %s. Valid options are: glog, text, json. Falling back to default: %s", *logFormat, logFormatDefault)
240239
}
241240

242241
logLevelValidationError := validateLogLevel(*logLevel)
243242
if logLevelValidationError != nil {
244-
l.Warn(fmt.Sprintf("Invalid log level: %s. Valid options are: trace, debug, info, warning, error, fatal. Falling back to default: %s", *logLevel, logLevelDefault))
243+
nl.Warnf(l, "Invalid log level: %s. Valid options are: trace, debug, info, warning, error, fatal. Falling back to default: %s", *logLevel, logLevelDefault)
245244
}
246245

247246
if *enableLatencyMetrics && !*enablePrometheusMetrics {
248-
l.Warn("enable-latency-metrics flag requires enable-prometheus-metrics, latency metrics will not be collected")
247+
nl.Warn(l, "enable-latency-metrics flag requires enable-prometheus-metrics, latency metrics will not be collected")
249248
*enableLatencyMetrics = false
250249
}
251250

252251
if *enableServiceInsight && !*nginxPlus {
253-
l.Warn("enable-service-insight flag support is for NGINX Plus, service insight endpoint will not be exposed")
252+
nl.Warn(l, "enable-service-insight flag support is for NGINX Plus, service insight endpoint will not be exposed")
254253
*enableServiceInsight = false
255254
}
256255

257256
if *enableDynamicWeightChangesReload && !*nginxPlus {
258-
l.Warn("weight-changes-dynamic-reload flag support is for NGINX Plus, Dynamic Weight Changes will not be enabled")
257+
nl.Warn(l, "weight-changes-dynamic-reload flag support is for NGINX Plus, Dynamic Weight Changes will not be enabled")
259258
*enableDynamicWeightChangesReload = false
260259
}
261260

@@ -265,24 +264,21 @@ func initValidate(ctx context.Context) {
265264
}
266265

267266
func mustValidateInitialChecks(ctx context.Context) {
268-
l := nlog.LoggerFromContext(ctx)
267+
l := nl.LoggerFromContext(ctx)
269268
err := flag.Lookup("logtostderr").Value.Set("true")
270269
if err != nil {
271-
l.Log(ctx, levels.LevelFatal, fmt.Sprintf("Error setting logtostderr to true: %v", err))
272-
os.Exit(1)
270+
nl.Fatalf(l, "Error setting logtostderr to true: %v", err)
273271
}
274272

275273
err = flag.Lookup("include_year").Value.Set("true")
276274
if err != nil {
277-
l.Log(ctx, levels.LevelFatal, fmt.Sprintf("Error setting include_year flag: %v", err))
278-
os.Exit(1)
275+
nl.Fatalf(l, "Error setting include_year flag: %v", err)
279276
}
280277

281278
if startupCheckFn != nil {
282279
err := startupCheckFn()
283280
if err != nil {
284-
l.Log(ctx, levels.LevelFatal, fmt.Sprintf("Failed startup check: %v", err))
285-
os.Exit(1)
281+
nl.Fatalf(l, "Failed startup check: %v", err)
286282
}
287283
l.Info("AWS startup check passed")
288284
}
@@ -291,16 +287,15 @@ func mustValidateInitialChecks(ctx context.Context) {
291287

292288
unparsed := flag.Args()
293289
if len(unparsed) > 0 {
294-
l.Warn(fmt.Sprintf("Ignoring unhandled arguments: %+q", unparsed))
290+
nl.Warnf(l, "Ignoring unhandled arguments: %+q", unparsed)
295291
}
296292
}
297293

298294
// mustValidateWatchedNamespaces calls internally os.Exit if it can't validate namespaces.
299295
func mustValidateWatchedNamespaces(ctx context.Context) {
300-
l := nlog.LoggerFromContext(ctx)
296+
l := nl.LoggerFromContext(ctx)
301297
if *watchNamespace != "" && *watchNamespaceLabel != "" {
302-
l.Log(ctx, levels.LevelFatal, "watch-namespace and -watch-namespace-label are mutually exclusive")
303-
os.Exit(1)
298+
nl.Fatal(l, "watch-namespace and -watch-namespace-label are mutually exclusive")
304299
}
305300

306301
watchNamespaces = strings.Split(*watchNamespace, ",")
@@ -309,8 +304,7 @@ func mustValidateWatchedNamespaces(ctx context.Context) {
309304
l.Info(fmt.Sprintf("Namespaces watched: %v", watchNamespaces))
310305
namespacesNameValidationError := validateNamespaceNames(watchNamespaces)
311306
if namespacesNameValidationError != nil {
312-
l.Log(ctx, levels.LevelFatal, fmt.Sprintf("Invalid values for namespaces: %v", namespacesNameValidationError))
313-
os.Exit(1)
307+
nl.Fatalf(l, "Invalid values for namespaces: %v", namespacesNameValidationError)
314308
}
315309
}
316310

@@ -319,8 +313,7 @@ func mustValidateWatchedNamespaces(ctx context.Context) {
319313
l.Debug(fmt.Sprintf("Namespaces watched for secrets: %v", watchSecretNamespaces))
320314
namespacesNameValidationError := validateNamespaceNames(watchSecretNamespaces)
321315
if namespacesNameValidationError != nil {
322-
l.Log(ctx, levels.LevelFatal, fmt.Sprintf("Invalid values for secret namespaces: %v", namespacesNameValidationError))
323-
os.Exit(1)
316+
nl.Fatalf(l, "Invalid values for secret namespaces: %v", namespacesNameValidationError)
324317
}
325318
} else {
326319
// empty => default to watched namespaces
@@ -331,8 +324,7 @@ func mustValidateWatchedNamespaces(ctx context.Context) {
331324
var err error
332325
_, err = labels.Parse(*watchNamespaceLabel)
333326
if err != nil {
334-
l.Log(ctx, levels.LevelFatal, fmt.Sprintf("Unable to parse label %v for watch namespace label: %v", *watchNamespaceLabel, err))
335-
os.Exit(1)
327+
nl.Fatalf(l, "Unable to parse label %v for watch namespace label: %v", *watchNamespaceLabel, err)
336328
}
337329
}
338330
}
@@ -341,121 +333,100 @@ func mustValidateWatchedNamespaces(ctx context.Context) {
341333
// and calls os.Exit if any of the flags is invalid.
342334
// nolint:gocyclo
343335
func mustValidateFlags(ctx context.Context) {
344-
l := nlog.LoggerFromContext(ctx)
336+
l := nl.LoggerFromContext(ctx)
345337
healthStatusURIValidationError := validateLocation(*healthStatusURI)
346338
if healthStatusURIValidationError != nil {
347-
l.Log(ctx, levels.LevelFatal, fmt.Sprintf("Invalid value for health-status-uri: %v", healthStatusURIValidationError))
348-
os.Exit(1)
339+
nl.Fatalf(l, "Invalid value for health-status-uri: %v", healthStatusURIValidationError)
349340
}
350341

351342
statusLockNameValidationError := validateResourceName(*leaderElectionLockName)
352343
if statusLockNameValidationError != nil {
353-
l.Log(ctx, levels.LevelFatal, fmt.Sprintf("Invalid value for leader-election-lock-name: %v", statusLockNameValidationError))
354-
os.Exit(1)
344+
nl.Fatalf(l, "Invalid value for leader-election-lock-name: %v", statusLockNameValidationError)
355345
}
356346

357347
statusPortValidationError := validatePort(*nginxStatusPort)
358348
if statusPortValidationError != nil {
359-
l.Log(ctx, levels.LevelFatal, fmt.Sprintf("Invalid value for nginx-status-port: %v", statusPortValidationError))
360-
os.Exit(1)
349+
nl.Fatalf(l, "Invalid value for nginx-status-port: %v", statusPortValidationError)
361350
}
362351

363352
metricsPortValidationError := validatePort(*prometheusMetricsListenPort)
364353
if metricsPortValidationError != nil {
365-
l.Log(ctx, levels.LevelFatal, fmt.Sprintf("Invalid value for prometheus-metrics-listen-port: %v", metricsPortValidationError))
366-
os.Exit(1)
354+
nl.Fatalf(l, "Invalid value for prometheus-metrics-listen-port: %v", metricsPortValidationError)
367355
}
368356

369357
readyStatusPortValidationError := validatePort(*readyStatusPort)
370358
if readyStatusPortValidationError != nil {
371-
l.Log(ctx, levels.LevelFatal, fmt.Sprintf("Invalid value for ready-status-port: %v", readyStatusPortValidationError))
372-
os.Exit(1)
359+
nl.Fatalf(l, "Invalid value for ready-status-port: %v", readyStatusPortValidationError)
373360
}
374361

375362
healthProbePortValidationError := validatePort(*serviceInsightListenPort)
376363
if healthProbePortValidationError != nil {
377-
l.Log(ctx, levels.LevelFatal, fmt.Sprintf("Invalid value for service-insight-listen-port: %v", metricsPortValidationError))
378-
os.Exit(1)
364+
nl.Fatalf(l, "Invalid value for service-insight-listen-port: %v", metricsPortValidationError)
379365
}
380366

381367
var err error
382368
allowedCIDRs, err = parseNginxStatusAllowCIDRs(*nginxStatusAllowCIDRs)
383369
if err != nil {
384-
l.Log(ctx, levels.LevelFatal, fmt.Sprintf("Invalid value for nginx-status-allow-cidrs: %v", err))
385-
os.Exit(1)
370+
nl.Fatalf(l, "Invalid value for nginx-status-allow-cidrs: %v", err)
386371
}
387372

388373
if *appProtectLogLevel != appProtectLogLevelDefault && *appProtect && *nginxPlus {
389374
appProtectlogLevelValidationError := validateLogLevel(*appProtectLogLevel)
390375
if appProtectlogLevelValidationError != nil {
391-
l.Log(ctx, levels.LevelFatal, fmt.Sprintf("Invalid value for app-protect-log-level: %v", *appProtectLogLevel))
392-
os.Exit(1)
376+
nl.Fatalf(l, "Invalid value for app-protect-log-level: %v", *appProtectLogLevel)
393377
}
394378
}
395379

396380
if *enableTLSPassthrough && !*enableCustomResources {
397-
l.Log(ctx, levels.LevelFatal, "enable-tls-passthrough flag requires -enable-custom-resources")
398-
os.Exit(1)
381+
nl.Fatal(l, "enable-tls-passthrough flag requires -enable-custom-resources")
399382
}
400383

401384
if *appProtect && !*nginxPlus {
402-
l.Log(ctx, levels.LevelFatal, "NGINX App Protect support is for NGINX Plus only")
403-
os.Exit(1)
385+
nl.Fatal(l, "NGINX App Protect support is for NGINX Plus only")
404386
}
405387

406388
if *appProtectLogLevel != appProtectLogLevelDefault && !*appProtect && !*nginxPlus {
407-
l.Log(ctx, levels.LevelFatal, "app-protect-log-level support is for NGINX Plus only and App Protect is enable")
408-
os.Exit(1)
389+
nl.Fatal(l, "app-protect-log-level support is for NGINX Plus only and App Protect is enable")
409390
}
410391

411392
if *appProtectDos && !*nginxPlus {
412-
l.Log(ctx, levels.LevelFatal, "NGINX App Protect Dos support is for NGINX Plus only")
413-
os.Exit(1)
393+
nl.Fatal(l, "NGINX App Protect Dos support is for NGINX Plus only")
414394
}
415395

416396
if *appProtectDosDebug && !*appProtectDos && !*nginxPlus {
417-
l.Log(ctx, levels.LevelFatal, "NGINX App Protect Dos debug support is for NGINX Plus only and App Protect Dos is enable")
418-
os.Exit(1)
397+
nl.Fatal(l, "NGINX App Protect Dos debug support is for NGINX Plus only and App Protect Dos is enable")
419398
}
420399

421400
if *appProtectDosMaxDaemons != 0 && !*appProtectDos && !*nginxPlus {
422-
l.Log(ctx, levels.LevelFatal, "NGINX App Protect Dos max daemons support is for NGINX Plus only and App Protect Dos is enable")
423-
os.Exit(1)
401+
nl.Fatal(l, "NGINX App Protect Dos max daemons support is for NGINX Plus only and App Protect Dos is enable")
424402
}
425403

426404
if *appProtectDosMaxWorkers != 0 && !*appProtectDos && !*nginxPlus {
427-
l.Log(ctx, levels.LevelFatal, "NGINX App Protect Dos max workers support is for NGINX Plus and App Protect Dos is enable")
428-
os.Exit(1)
405+
nl.Fatal(l, "NGINX App Protect Dos max workers support is for NGINX Plus and App Protect Dos is enable")
429406
}
430407

431408
if *appProtectDosMemory != 0 && !*appProtectDos && !*nginxPlus {
432-
l.Log(ctx, levels.LevelFatal, "NGINX App Protect Dos memory support is for NGINX Plus and App Protect Dos is enable")
433-
os.Exit(1)
409+
nl.Fatal(l, "NGINX App Protect Dos memory support is for NGINX Plus and App Protect Dos is enable")
434410
}
435411

436412
if *enableInternalRoutes && *spireAgentAddress == "" {
437-
l.Log(ctx, levels.LevelFatal, "enable-internal-routes flag requires spire-agent-address")
438-
os.Exit(1)
413+
nl.Fatal(l, "enable-internal-routes flag requires spire-agent-address")
439414
}
440415

441416
if *enableCertManager && !*enableCustomResources {
442-
l.Log(ctx, levels.LevelFatal, "enable-cert-manager flag requires -enable-custom-resources")
443-
os.Exit(1)
417+
nl.Fatal(l, "enable-cert-manager flag requires -enable-custom-resources")
444418
}
445419

446420
if *enableExternalDNS && !*enableCustomResources {
447-
l.Log(ctx, levels.LevelFatal, "enable-external-dns flag requires -enable-custom-resources")
448-
os.Exit(1)
421+
nl.Fatal(l, "enable-external-dns flag requires -enable-custom-resources")
449422
}
450423

451424
if *ingressLink != "" && *externalService != "" {
452-
l.Log(ctx, levels.LevelFatal, "ingresslink and external-service cannot both be set")
453-
os.Exit(1)
425+
nl.Fatal(l, "ingresslink and external-service cannot both be set")
454426
}
455427

456428
if *agent && !*appProtect {
457-
l.Log(ctx, levels.LevelFatal, "NGINX Agent is used to enable the Security Monitoring dashboard and requires NGINX App Protect to be enabled")
458-
os.Exit(1)
429+
nl.Fatal(l, "NGINX Agent is used to enable the Security Monitoring dashboard and requires NGINX App Protect to be enabled")
459430
}
460431
}
461432

0 commit comments

Comments
 (0)