1
1
package main
2
2
3
3
import (
4
+ "context"
4
5
"flag"
5
6
"fmt"
6
7
"net"
7
8
"os"
8
9
"regexp"
9
10
"strings"
10
11
11
- "github.com/golang/glog"
12
12
api_v1 "k8s.io/api/core/v1"
13
13
"k8s.io/apimachinery/pkg/labels"
14
14
"k8s.io/apimachinery/pkg/util/validation"
15
+
16
+ nlog "github.com/nginxinc/kubernetes-ingress/internal/logger"
17
+ "github.com/nginxinc/kubernetes-ingress/internal/logger/levels"
15
18
)
16
19
17
20
const (
@@ -229,86 +232,95 @@ func parseFlags() {
229
232
}
230
233
}
231
234
232
- func initValidate () {
235
+ func initValidate (ctx context.Context ) {
236
+ l := nlog .LoggerFromContext (ctx )
233
237
logFormatValidationError := validateLogFormat (* logFormat )
234
238
if logFormatValidationError != nil {
235
- glog . Warningf ( "Invalid log format: %s. Valid options are: glog, text, json. Falling back to default: %s" , * logFormat , logFormatDefault )
239
+ l . Warn ( fmt . Sprintf ( "Invalid log format: %s. Valid options are: glog, text, json. Falling back to default: %s" , * logFormat , logFormatDefault ) )
236
240
}
237
241
238
242
logLevelValidationError := validateLogLevel (* logLevel )
239
243
if logLevelValidationError != nil {
240
- glog . Warningf ( "Invalid log level: %s. Valid options are: trace, debug, info, warning, error, fatal. Falling back to default: %s" , * logLevel , logLevelDefault )
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 ) )
241
245
}
242
246
243
247
if * enableLatencyMetrics && ! * enablePrometheusMetrics {
244
- glog . Warning ("enable-latency-metrics flag requires enable-prometheus-metrics, latency metrics will not be collected" )
248
+ l . Warn ("enable-latency-metrics flag requires enable-prometheus-metrics, latency metrics will not be collected" )
245
249
* enableLatencyMetrics = false
246
250
}
247
251
248
252
if * enableServiceInsight && ! * nginxPlus {
249
- glog . Warning ("enable-service-insight flag support is for NGINX Plus, service insight endpoint will not be exposed" )
253
+ l . Warn ("enable-service-insight flag support is for NGINX Plus, service insight endpoint will not be exposed" )
250
254
* enableServiceInsight = false
251
255
}
252
256
253
257
if * enableDynamicWeightChangesReload && ! * nginxPlus {
254
- glog . Warning ("weight-changes-dynamic-reload flag support is for NGINX Plus, Dynamic Weight Changes will not be enabled" )
258
+ l . Warn ("weight-changes-dynamic-reload flag support is for NGINX Plus, Dynamic Weight Changes will not be enabled" )
255
259
* enableDynamicWeightChangesReload = false
256
260
}
257
261
258
- mustValidateInitialChecks ()
259
- mustValidateWatchedNamespaces ()
260
- mustValidateFlags ()
262
+ mustValidateInitialChecks (ctx )
263
+ mustValidateWatchedNamespaces (ctx )
264
+ mustValidateFlags (ctx )
261
265
}
262
266
263
- func mustValidateInitialChecks () {
267
+ func mustValidateInitialChecks (ctx context.Context ) {
268
+ l := nlog .LoggerFromContext (ctx )
264
269
err := flag .Lookup ("logtostderr" ).Value .Set ("true" )
265
270
if err != nil {
266
- glog .Fatalf ("Error setting logtostderr to true: %v" , err )
271
+ l .Log (ctx , levels .LevelFatal , fmt .Sprintf ("Error setting logtostderr to true: %v" , err ))
272
+ os .Exit (1 )
267
273
}
268
274
269
275
err = flag .Lookup ("include_year" ).Value .Set ("true" )
270
276
if err != nil {
271
- glog .Fatalf ("Error setting include_year flag: %v" , err )
277
+ l .Log (ctx , levels .LevelFatal , fmt .Sprintf ("Error setting include_year flag: %v" , err ))
278
+ os .Exit (1 )
272
279
}
273
280
274
281
if startupCheckFn != nil {
275
282
err := startupCheckFn ()
276
283
if err != nil {
277
- glog .Fatalf ("Failed startup check: %v" , err )
284
+ l .Log (ctx , levels .LevelFatal , fmt .Sprintf ("Failed startup check: %v" , err ))
285
+ os .Exit (1 )
278
286
}
279
- glog .Info ("AWS startup check passed" )
287
+ l .Info ("AWS startup check passed" )
280
288
}
281
289
282
- glog . Infof ( "Starting with flags: %+q" , os .Args [1 :])
290
+ l . Info ( fmt . Sprintf ( "Starting with flags: %+q" , os .Args [1 :]) )
283
291
284
292
unparsed := flag .Args ()
285
293
if len (unparsed ) > 0 {
286
- glog . Warningf ( "Ignoring unhandled arguments: %+q" , unparsed )
294
+ l . Warn ( fmt . Sprintf ( "Ignoring unhandled arguments: %+q" , unparsed ) )
287
295
}
288
296
}
289
297
290
298
// mustValidateWatchedNamespaces calls internally os.Exit if it can't validate namespaces.
291
- func mustValidateWatchedNamespaces () {
299
+ func mustValidateWatchedNamespaces (ctx context.Context ) {
300
+ l := nlog .LoggerFromContext (ctx )
292
301
if * watchNamespace != "" && * watchNamespaceLabel != "" {
293
- glog .Fatal ("watch-namespace and -watch-namespace-label are mutually exclusive" )
302
+ l .Log (ctx , levels .LevelFatal , "watch-namespace and -watch-namespace-label are mutually exclusive" )
303
+ os .Exit (1 )
294
304
}
295
305
296
306
watchNamespaces = strings .Split (* watchNamespace , "," )
297
307
298
308
if * watchNamespace != "" {
299
- glog . Infof ( "Namespaces watched: %v" , watchNamespaces )
309
+ l . Info ( fmt . Sprintf ( "Namespaces watched: %v" , watchNamespaces ) )
300
310
namespacesNameValidationError := validateNamespaceNames (watchNamespaces )
301
311
if namespacesNameValidationError != nil {
302
- glog .Fatalf ("Invalid values for namespaces: %v" , namespacesNameValidationError )
312
+ l .Log (ctx , levels .LevelFatal , fmt .Sprintf ("Invalid values for namespaces: %v" , namespacesNameValidationError ))
313
+ os .Exit (1 )
303
314
}
304
315
}
305
316
306
317
if len (* watchSecretNamespace ) > 0 {
307
318
watchSecretNamespaces = strings .Split (* watchSecretNamespace , "," )
308
- glog . Infof ( "Namespaces watched for secrets: %v" , watchSecretNamespaces )
319
+ l . Debug ( fmt . Sprintf ( "Namespaces watched for secrets: %v" , watchSecretNamespaces ) )
309
320
namespacesNameValidationError := validateNamespaceNames (watchSecretNamespaces )
310
321
if namespacesNameValidationError != nil {
311
- glog .Fatalf ("Invalid values for secret namespaces: %v" , namespacesNameValidationError )
322
+ l .Log (ctx , levels .LevelFatal , fmt .Sprintf ("Invalid values for secret namespaces: %v" , namespacesNameValidationError ))
323
+ os .Exit (1 )
312
324
}
313
325
} else {
314
326
// empty => default to watched namespaces
@@ -319,107 +331,131 @@ func mustValidateWatchedNamespaces() {
319
331
var err error
320
332
_ , err = labels .Parse (* watchNamespaceLabel )
321
333
if err != nil {
322
- glog .Fatalf ("Unable to parse label %v for watch namespace label: %v" , * watchNamespaceLabel , err )
334
+ l .Log (ctx , levels .LevelFatal , fmt .Sprintf ("Unable to parse label %v for watch namespace label: %v" , * watchNamespaceLabel , err ))
335
+ os .Exit (1 )
323
336
}
324
337
}
325
338
}
326
339
327
340
// mustValidateFlags checks the values for various flags
328
341
// and calls os.Exit if any of the flags is invalid.
329
- func mustValidateFlags () {
342
+ // nolint:gocyclo
343
+ func mustValidateFlags (ctx context.Context ) {
344
+ l := nlog .LoggerFromContext (ctx )
330
345
healthStatusURIValidationError := validateLocation (* healthStatusURI )
331
346
if healthStatusURIValidationError != nil {
332
- glog .Fatalf ("Invalid value for health-status-uri: %v" , healthStatusURIValidationError )
347
+ l .Log (ctx , levels .LevelFatal , fmt .Sprintf ("Invalid value for health-status-uri: %v" , healthStatusURIValidationError ))
348
+ os .Exit (1 )
333
349
}
334
350
335
351
statusLockNameValidationError := validateResourceName (* leaderElectionLockName )
336
352
if statusLockNameValidationError != nil {
337
- glog .Fatalf ("Invalid value for leader-election-lock-name: %v" , statusLockNameValidationError )
353
+ l .Log (ctx , levels .LevelFatal , fmt .Sprintf ("Invalid value for leader-election-lock-name: %v" , statusLockNameValidationError ))
354
+ os .Exit (1 )
338
355
}
339
356
340
357
statusPortValidationError := validatePort (* nginxStatusPort )
341
358
if statusPortValidationError != nil {
342
- glog .Fatalf ("Invalid value for nginx-status-port: %v" , statusPortValidationError )
359
+ l .Log (ctx , levels .LevelFatal , fmt .Sprintf ("Invalid value for nginx-status-port: %v" , statusPortValidationError ))
360
+ os .Exit (1 )
343
361
}
344
362
345
363
metricsPortValidationError := validatePort (* prometheusMetricsListenPort )
346
364
if metricsPortValidationError != nil {
347
- glog .Fatalf ("Invalid value for prometheus-metrics-listen-port: %v" , metricsPortValidationError )
365
+ l .Log (ctx , levels .LevelFatal , fmt .Sprintf ("Invalid value for prometheus-metrics-listen-port: %v" , metricsPortValidationError ))
366
+ os .Exit (1 )
348
367
}
349
368
350
369
readyStatusPortValidationError := validatePort (* readyStatusPort )
351
370
if readyStatusPortValidationError != nil {
352
- glog .Fatalf ("Invalid value for ready-status-port: %v" , readyStatusPortValidationError )
371
+ l .Log (ctx , levels .LevelFatal , fmt .Sprintf ("Invalid value for ready-status-port: %v" , readyStatusPortValidationError ))
372
+ os .Exit (1 )
353
373
}
354
374
355
375
healthProbePortValidationError := validatePort (* serviceInsightListenPort )
356
376
if healthProbePortValidationError != nil {
357
- glog .Fatalf ("Invalid value for service-insight-listen-port: %v" , metricsPortValidationError )
377
+ l .Log (ctx , levels .LevelFatal , fmt .Sprintf ("Invalid value for service-insight-listen-port: %v" , metricsPortValidationError ))
378
+ os .Exit (1 )
358
379
}
359
380
360
381
var err error
361
382
allowedCIDRs , err = parseNginxStatusAllowCIDRs (* nginxStatusAllowCIDRs )
362
383
if err != nil {
363
- glog .Fatalf (`Invalid value for nginx-status-allow-cidrs: %v` , err )
384
+ l .Log (ctx , levels .LevelFatal , fmt .Sprintf ("Invalid value for nginx-status-allow-cidrs: %v" , err ))
385
+ os .Exit (1 )
364
386
}
365
387
366
388
if * appProtectLogLevel != appProtectLogLevelDefault && * appProtect && * nginxPlus {
367
389
appProtectlogLevelValidationError := validateLogLevel (* appProtectLogLevel )
368
390
if appProtectlogLevelValidationError != nil {
369
- glog .Fatalf ("Invalid value for app-protect-log-level: %v" , * appProtectLogLevel )
391
+ l .Log (ctx , levels .LevelFatal , fmt .Sprintf ("Invalid value for app-protect-log-level: %v" , * appProtectLogLevel ))
392
+ os .Exit (1 )
370
393
}
371
394
}
372
395
373
396
if * enableTLSPassthrough && ! * enableCustomResources {
374
- glog .Fatal ("enable-tls-passthrough flag requires -enable-custom-resources" )
397
+ l .Log (ctx , levels .LevelFatal , "enable-tls-passthrough flag requires -enable-custom-resources" )
398
+ os .Exit (1 )
375
399
}
376
400
377
401
if * appProtect && ! * nginxPlus {
378
- glog .Fatal ("NGINX App Protect support is for NGINX Plus only" )
402
+ l .Log (ctx , levels .LevelFatal , "NGINX App Protect support is for NGINX Plus only" )
403
+ os .Exit (1 )
379
404
}
380
405
381
406
if * appProtectLogLevel != appProtectLogLevelDefault && ! * appProtect && ! * nginxPlus {
382
- glog .Fatal ("app-protect-log-level support is for NGINX Plus only and App Protect is enable" )
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 )
383
409
}
384
410
385
411
if * appProtectDos && ! * nginxPlus {
386
- glog .Fatal ("NGINX App Protect Dos support is for NGINX Plus only" )
412
+ l .Log (ctx , levels .LevelFatal , "NGINX App Protect Dos support is for NGINX Plus only" )
413
+ os .Exit (1 )
387
414
}
388
415
389
416
if * appProtectDosDebug && ! * appProtectDos && ! * nginxPlus {
390
- glog .Fatal ("NGINX App Protect Dos debug support is for NGINX Plus only and App Protect Dos is enable" )
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 )
391
419
}
392
420
393
421
if * appProtectDosMaxDaemons != 0 && ! * appProtectDos && ! * nginxPlus {
394
- glog .Fatal ("NGINX App Protect Dos max daemons support is for NGINX Plus only and App Protect Dos is enable" )
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 )
395
424
}
396
425
397
426
if * appProtectDosMaxWorkers != 0 && ! * appProtectDos && ! * nginxPlus {
398
- glog .Fatal ("NGINX App Protect Dos max workers support is for NGINX Plus and App Protect Dos is enable" )
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 )
399
429
}
400
430
401
431
if * appProtectDosMemory != 0 && ! * appProtectDos && ! * nginxPlus {
402
- glog .Fatal ("NGINX App Protect Dos memory support is for NGINX Plus and App Protect Dos is enable" )
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 )
403
434
}
404
435
405
436
if * enableInternalRoutes && * spireAgentAddress == "" {
406
- glog .Fatal ("enable-internal-routes flag requires spire-agent-address" )
437
+ l .Log (ctx , levels .LevelFatal , "enable-internal-routes flag requires spire-agent-address" )
438
+ os .Exit (1 )
407
439
}
408
440
409
441
if * enableCertManager && ! * enableCustomResources {
410
- glog .Fatal ("enable-cert-manager flag requires -enable-custom-resources" )
442
+ l .Log (ctx , levels .LevelFatal , "enable-cert-manager flag requires -enable-custom-resources" )
443
+ os .Exit (1 )
411
444
}
412
445
413
446
if * enableExternalDNS && ! * enableCustomResources {
414
- glog .Fatal ("enable-external-dns flag requires -enable-custom-resources" )
447
+ l .Log (ctx , levels .LevelFatal , "enable-external-dns flag requires -enable-custom-resources" )
448
+ os .Exit (1 )
415
449
}
416
450
417
451
if * ingressLink != "" && * externalService != "" {
418
- glog .Fatal ("ingresslink and external-service cannot both be set" )
452
+ l .Log (ctx , levels .LevelFatal , "ingresslink and external-service cannot both be set" )
453
+ os .Exit (1 )
419
454
}
420
455
421
456
if * agent && ! * appProtect {
422
- glog .Fatal ("NGINX Agent is used to enable the Security Monitoring dashboard and requires NGINX App Protect to be enabled" )
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 )
423
459
}
424
460
}
425
461
0 commit comments