From 4ee3676f16cd17f4cf7c570af000548c122fcfed Mon Sep 17 00:00:00 2001 From: AlexFenlon Date: Thu, 26 Sep 2024 09:26:43 +0100 Subject: [PATCH] refactor to prepare for struct logs (#6501) --- cmd/nginx-ingress/flags.go | 114 +++++++++++++++++++------------------ cmd/nginx-ingress/main.go | 2 +- 2 files changed, 59 insertions(+), 57 deletions(-) diff --git a/cmd/nginx-ingress/flags.go b/cmd/nginx-ingress/flags.go index 587eca2cd6..a3b6acf53b 100644 --- a/cmd/nginx-ingress/flags.go +++ b/cmd/nginx-ingress/flags.go @@ -217,50 +217,12 @@ var ( func parseFlags() { flag.Parse() - if *versionFlag { + if *versionFlag { // printed in main os.Exit(0) } +} - mustValidateInitialChecks() - mustValidateWatchedNamespaces() - mustValidateFlags() - - if *enableTLSPassthrough && !*enableCustomResources { - glog.Fatal("enable-tls-passthrough flag requires -enable-custom-resources") - } - - if *appProtect && !*nginxPlus { - glog.Fatal("NGINX App Protect support is for NGINX Plus only") - } - - if *appProtectLogLevel != appProtectLogLevelDefault && !*appProtect && !*nginxPlus { - glog.Fatal("app-protect-log-level support is for NGINX Plus only and App Protect is enable") - } - - if *appProtectDos && !*nginxPlus { - glog.Fatal("NGINX App Protect Dos support is for NGINX Plus only") - } - - if *appProtectDosDebug && !*appProtectDos && !*nginxPlus { - glog.Fatal("NGINX App Protect Dos debug support is for NGINX Plus only and App Protect Dos is enable") - } - - if *appProtectDosMaxDaemons != 0 && !*appProtectDos && !*nginxPlus { - glog.Fatal("NGINX App Protect Dos max daemons support is for NGINX Plus only and App Protect Dos is enable") - } - - if *appProtectDosMaxWorkers != 0 && !*appProtectDos && !*nginxPlus { - glog.Fatal("NGINX App Protect Dos max workers support is for NGINX Plus and App Protect Dos is enable") - } - - if *appProtectDosMemory != 0 && !*appProtectDos && !*nginxPlus { - glog.Fatal("NGINX App Protect Dos memory support is for NGINX Plus and App Protect Dos is enable") - } - - if *enableInternalRoutes && *spireAgentAddress == "" { - glog.Fatal("enable-internal-routes flag requires spire-agent-address") - } - +func initValidate() { if *enableLatencyMetrics && !*enablePrometheusMetrics { glog.Warning("enable-latency-metrics flag requires enable-prometheus-metrics, latency metrics will not be collected") *enableLatencyMetrics = false @@ -271,26 +233,14 @@ func parseFlags() { *enableServiceInsight = false } - if *enableCertManager && !*enableCustomResources { - glog.Fatal("enable-cert-manager flag requires -enable-custom-resources") - } - - if *enableExternalDNS && !*enableCustomResources { - glog.Fatal("enable-external-dns flag requires -enable-custom-resources") - } - - if *ingressLink != "" && *externalService != "" { - glog.Fatal("ingresslink and external-service cannot both be set") - } - if *enableDynamicWeightChangesReload && !*nginxPlus { glog.Warning("weight-changes-dynamic-reload flag support is for NGINX Plus, Dynamic Weight Changes will not be enabled") *enableDynamicWeightChangesReload = false } - if *agent && !*appProtect { - glog.Fatal("NGINX Agent is used to enable the Security Monitoring dashboard and requires NGINX App Protect to be enabled") - } + mustValidateInitialChecks() + mustValidateWatchedNamespaces() + mustValidateFlags() } func mustValidateInitialChecks() { @@ -402,6 +352,58 @@ func mustValidateFlags() { glog.Fatalf("Invalid value for app-protect-log-level: %v", *appProtectLogLevel) } } + + if *enableTLSPassthrough && !*enableCustomResources { + glog.Fatal("enable-tls-passthrough flag requires -enable-custom-resources") + } + + if *appProtect && !*nginxPlus { + glog.Fatal("NGINX App Protect support is for NGINX Plus only") + } + + if *appProtectLogLevel != appProtectLogLevelDefault && !*appProtect && !*nginxPlus { + glog.Fatal("app-protect-log-level support is for NGINX Plus only and App Protect is enable") + } + + if *appProtectDos && !*nginxPlus { + glog.Fatal("NGINX App Protect Dos support is for NGINX Plus only") + } + + if *appProtectDosDebug && !*appProtectDos && !*nginxPlus { + glog.Fatal("NGINX App Protect Dos debug support is for NGINX Plus only and App Protect Dos is enable") + } + + if *appProtectDosMaxDaemons != 0 && !*appProtectDos && !*nginxPlus { + glog.Fatal("NGINX App Protect Dos max daemons support is for NGINX Plus only and App Protect Dos is enable") + } + + if *appProtectDosMaxWorkers != 0 && !*appProtectDos && !*nginxPlus { + glog.Fatal("NGINX App Protect Dos max workers support is for NGINX Plus and App Protect Dos is enable") + } + + if *appProtectDosMemory != 0 && !*appProtectDos && !*nginxPlus { + glog.Fatal("NGINX App Protect Dos memory support is for NGINX Plus and App Protect Dos is enable") + } + + if *enableInternalRoutes && *spireAgentAddress == "" { + glog.Fatal("enable-internal-routes flag requires spire-agent-address") + } + + if *enableCertManager && !*enableCustomResources { + glog.Fatal("enable-cert-manager flag requires -enable-custom-resources") + } + + if *enableExternalDNS && !*enableCustomResources { + glog.Fatal("enable-external-dns flag requires -enable-custom-resources") + } + + if *ingressLink != "" && *externalService != "" { + glog.Fatal("ingresslink and external-service cannot both be set") + } + + if *agent && !*appProtect { + glog.Fatal("NGINX Agent is used to enable the Security Monitoring dashboard and requires NGINX App Protect to be enabled") + } } // validateNamespaceNames validates the namespaces are in the correct format diff --git a/cmd/nginx-ingress/main.go b/cmd/nginx-ingress/main.go index ccb6dd77f9..fe70d76eb1 100644 --- a/cmd/nginx-ingress/main.go +++ b/cmd/nginx-ingress/main.go @@ -62,8 +62,8 @@ const ( func main() { commitHash, commitTime, dirtyBuild := getBuildInfo() fmt.Printf("NGINX Ingress Controller Version=%v Commit=%v Date=%v DirtyState=%v Arch=%v/%v Go=%v\n", version, commitHash, commitTime, dirtyBuild, runtime.GOOS, runtime.GOARCH, runtime.Version()) - parseFlags() + initValidate() parsedFlags := os.Args[1:] buildOS := os.Getenv("BUILD_OS")