@@ -13,8 +13,7 @@ import (
13
13
"k8s.io/apimachinery/pkg/labels"
14
14
"k8s.io/apimachinery/pkg/util/validation"
15
15
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"
18
17
)
19
18
20
19
const (
@@ -233,29 +232,29 @@ func parseFlags() {
233
232
}
234
233
235
234
func initValidate (ctx context.Context ) {
236
- l := nlog .LoggerFromContext (ctx )
235
+ l := nl .LoggerFromContext (ctx )
237
236
logFormatValidationError := validateLogFormat (* logFormat )
238
237
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 )
240
239
}
241
240
242
241
logLevelValidationError := validateLogLevel (* logLevel )
243
242
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 )
245
244
}
246
245
247
246
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" )
249
248
* enableLatencyMetrics = false
250
249
}
251
250
252
251
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" )
254
253
* enableServiceInsight = false
255
254
}
256
255
257
256
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" )
259
258
* enableDynamicWeightChangesReload = false
260
259
}
261
260
@@ -265,24 +264,21 @@ func initValidate(ctx context.Context) {
265
264
}
266
265
267
266
func mustValidateInitialChecks (ctx context.Context ) {
268
- l := nlog .LoggerFromContext (ctx )
267
+ l := nl .LoggerFromContext (ctx )
269
268
err := flag .Lookup ("logtostderr" ).Value .Set ("true" )
270
269
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 )
273
271
}
274
272
275
273
err = flag .Lookup ("include_year" ).Value .Set ("true" )
276
274
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 )
279
276
}
280
277
281
278
if startupCheckFn != nil {
282
279
err := startupCheckFn ()
283
280
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 )
286
282
}
287
283
l .Info ("AWS startup check passed" )
288
284
}
@@ -291,16 +287,15 @@ func mustValidateInitialChecks(ctx context.Context) {
291
287
292
288
unparsed := flag .Args ()
293
289
if len (unparsed ) > 0 {
294
- l . Warn ( fmt . Sprintf ( "Ignoring unhandled arguments: %+q" , unparsed ) )
290
+ nl . Warnf ( l , "Ignoring unhandled arguments: %+q" , unparsed )
295
291
}
296
292
}
297
293
298
294
// mustValidateWatchedNamespaces calls internally os.Exit if it can't validate namespaces.
299
295
func mustValidateWatchedNamespaces (ctx context.Context ) {
300
- l := nlog .LoggerFromContext (ctx )
296
+ l := nl .LoggerFromContext (ctx )
301
297
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" )
304
299
}
305
300
306
301
watchNamespaces = strings .Split (* watchNamespace , "," )
@@ -309,8 +304,7 @@ func mustValidateWatchedNamespaces(ctx context.Context) {
309
304
l .Info (fmt .Sprintf ("Namespaces watched: %v" , watchNamespaces ))
310
305
namespacesNameValidationError := validateNamespaceNames (watchNamespaces )
311
306
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 )
314
308
}
315
309
}
316
310
@@ -319,8 +313,7 @@ func mustValidateWatchedNamespaces(ctx context.Context) {
319
313
l .Debug (fmt .Sprintf ("Namespaces watched for secrets: %v" , watchSecretNamespaces ))
320
314
namespacesNameValidationError := validateNamespaceNames (watchSecretNamespaces )
321
315
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 )
324
317
}
325
318
} else {
326
319
// empty => default to watched namespaces
@@ -331,8 +324,7 @@ func mustValidateWatchedNamespaces(ctx context.Context) {
331
324
var err error
332
325
_ , err = labels .Parse (* watchNamespaceLabel )
333
326
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 )
336
328
}
337
329
}
338
330
}
@@ -341,121 +333,100 @@ func mustValidateWatchedNamespaces(ctx context.Context) {
341
333
// and calls os.Exit if any of the flags is invalid.
342
334
// nolint:gocyclo
343
335
func mustValidateFlags (ctx context.Context ) {
344
- l := nlog .LoggerFromContext (ctx )
336
+ l := nl .LoggerFromContext (ctx )
345
337
healthStatusURIValidationError := validateLocation (* healthStatusURI )
346
338
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 )
349
340
}
350
341
351
342
statusLockNameValidationError := validateResourceName (* leaderElectionLockName )
352
343
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 )
355
345
}
356
346
357
347
statusPortValidationError := validatePort (* nginxStatusPort )
358
348
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 )
361
350
}
362
351
363
352
metricsPortValidationError := validatePort (* prometheusMetricsListenPort )
364
353
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 )
367
355
}
368
356
369
357
readyStatusPortValidationError := validatePort (* readyStatusPort )
370
358
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 )
373
360
}
374
361
375
362
healthProbePortValidationError := validatePort (* serviceInsightListenPort )
376
363
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 )
379
365
}
380
366
381
367
var err error
382
368
allowedCIDRs , err = parseNginxStatusAllowCIDRs (* nginxStatusAllowCIDRs )
383
369
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 )
386
371
}
387
372
388
373
if * appProtectLogLevel != appProtectLogLevelDefault && * appProtect && * nginxPlus {
389
374
appProtectlogLevelValidationError := validateLogLevel (* appProtectLogLevel )
390
375
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 )
393
377
}
394
378
}
395
379
396
380
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" )
399
382
}
400
383
401
384
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" )
404
386
}
405
387
406
388
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" )
409
390
}
410
391
411
392
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" )
414
394
}
415
395
416
396
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" )
419
398
}
420
399
421
400
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" )
424
402
}
425
403
426
404
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" )
429
406
}
430
407
431
408
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" )
434
410
}
435
411
436
412
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" )
439
414
}
440
415
441
416
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" )
444
418
}
445
419
446
420
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" )
449
422
}
450
423
451
424
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" )
454
426
}
455
427
456
428
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" )
459
430
}
460
431
}
461
432
0 commit comments