Skip to content

Commit bbd4402

Browse files
author
Jim Ryan
committed
refactor plus check
1 parent 0d06f86 commit bbd4402

File tree

3 files changed

+21
-32
lines changed

3 files changed

+21
-32
lines changed

cmd/nginx-ingress/main.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,11 @@ func main() {
142142

143143
mustProcessGlobalConfiguration(ctx)
144144

145-
mgmtCfgParams := processMGMTConfigMap(kubeClient, configs.NewDefaultMGMTConfigParams(ctx))
146-
147-
// validate any secrets in mgmtCfgParams.Secrets.
145+
var mgmtCfgParams *configs.MGMTConfigParams
148146
if *nginxPlus {
147+
mgmtCfgParams = processMGMTConfigMap(kubeClient, configs.NewDefaultMGMTConfigParams(ctx))
148+
149+
// validate any secrets in mgmtCfgParams.Secrets.
149150
mustValidateMGMTSecrets(kubeClient, *mgmtCfgParams, controllerNamespace)
150151
mustFindMGMTSecretsOnPod(*mgmtCfgParams, staticSSLPath)
151152
}
@@ -321,7 +322,7 @@ func mustFindMGMTSecretsOnPod(mgmtConfigParams configs.MGMTConfigParams, secrets
321322
}
322323

323324
func tryFindFile(filePath string) error {
324-
if _, err := os.Stat("filePath"); errors.Is(err, os.ErrNotExist) {
325+
if _, err := os.Stat(filePath); errors.Is(err, os.ErrNotExist) {
325326
return fmt.Errorf("could not find file at path [%s]: %w", filePath, err)
326327
}
327328
return nil
@@ -967,9 +968,6 @@ func processConfigMaps(kubeClient *kubernetes.Clientset, cfgParams *configs.Conf
967968

968969
func processMGMTConfigMap(kubeClient *kubernetes.Clientset, mgmtCfgParams *configs.MGMTConfigParams) *configs.MGMTConfigParams {
969970
l := nl.LoggerFromContext(mgmtCfgParams.Context)
970-
if !*nginxPlus {
971-
return mgmtCfgParams
972-
}
973971

974972
if *mgmtConfigMap != "" {
975973
ns, name, err := k8s.ParseNamespaceName(*mgmtConfigMap)
@@ -980,10 +978,7 @@ func processMGMTConfigMap(kubeClient *kubernetes.Clientset, mgmtCfgParams *confi
980978
if err != nil {
981979
nl.Fatalf(l, "Error when getting %v: %v", *mgmtConfigMap, err)
982980
}
983-
mgmtCfgParams, err = configs.ParseMGMTConfigMap(mgmtCfgParams.Context, cfm, *nginxPlus)
984-
if err != nil {
985-
nl.Fatalf(l, "Error when getting %v: %v", mgmtCfgParams, err)
986-
}
981+
mgmtCfgParams = configs.ParseMGMTConfigMap(mgmtCfgParams.Context, cfm)
987982
}
988983
return mgmtCfgParams
989984
}

internal/configs/configmaps.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package configs
22

33
import (
44
"context"
5-
"fmt"
65
"strings"
76
"time"
87

@@ -538,11 +537,8 @@ func ParseConfigMap(ctx context.Context, cfgm *v1.ConfigMap, nginxPlus bool, has
538537
}
539538

540539
// ParseMGMTConfigMap parses the mgmt block ConfigMap into MGMTConfigParams.
541-
func ParseMGMTConfigMap(ctx context.Context, cfgm *v1.ConfigMap, nginxPlus bool) (*MGMTConfigParams, error) {
540+
func ParseMGMTConfigMap(ctx context.Context, cfgm *v1.ConfigMap) *MGMTConfigParams {
542541
l := nl.LoggerFromContext(ctx)
543-
if !nginxPlus {
544-
return nil, fmt.Errorf("mgmt ConfigMap requires NGINX Plus")
545-
}
546542

547543
mgmtCfgParams := NewDefaultMGMTConfigParams(ctx)
548544

@@ -594,19 +590,22 @@ func ParseMGMTConfigMap(ctx context.Context, cfgm *v1.ConfigMap, nginxPlus bool)
594590
mgmtCfgParams.Secrets.ClientAuth = strings.TrimSpace(clientAuthSecret)
595591
}
596592

597-
return mgmtCfgParams, nil
593+
return mgmtCfgParams
598594
}
599595

600596
// GenerateNginxMainConfig generates MainConfig.
601597
func GenerateNginxMainConfig(staticCfgParams *StaticConfigParams, config *ConfigParams, mgmtCfgParams *MGMTConfigParams) *version1.MainConfig {
602-
mgmtConfig := version1.MGMTConfig{
603-
SSLVerify: mgmtCfgParams.SSLVerify,
604-
Resolver: mgmtCfgParams.Resolver,
605-
EnforceInitialReport: mgmtCfgParams.EnforceInitialReport,
606-
Endpoint: mgmtCfgParams.Endpoint,
607-
Interval: mgmtCfgParams.Interval,
608-
EnableClientAuth: mgmtCfgParams.Secrets.ClientAuth != "",
609-
TrustedCertificateFile: mgmtCfgParams.TrustedCertFile,
598+
var mgmtConfig version1.MGMTConfig
599+
if mgmtCfgParams != nil {
600+
mgmtConfig = version1.MGMTConfig{
601+
SSLVerify: mgmtCfgParams.SSLVerify,
602+
Resolver: mgmtCfgParams.Resolver,
603+
EnforceInitialReport: mgmtCfgParams.EnforceInitialReport,
604+
Endpoint: mgmtCfgParams.Endpoint,
605+
Interval: mgmtCfgParams.Interval,
606+
EnableClientAuth: mgmtCfgParams.Secrets.ClientAuth != "",
607+
TrustedCertificateFile: mgmtCfgParams.TrustedCertFile,
608+
}
610609
}
611610

612611
nginxCfg := &version1.MainConfig{

internal/k8s/controller.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -878,13 +878,8 @@ func (lbc *LoadBalancerController) updateAllConfigs() {
878878
if lbc.configMap != nil {
879879
cfgParams = configs.ParseConfigMap(ctx, lbc.configMap, lbc.isNginxPlus, lbc.appProtectEnabled, lbc.appProtectDosEnabled, lbc.configuration.isTLSPassthroughEnabled)
880880
}
881-
if lbc.mgmtConfigMap != nil {
882-
var err error
883-
mgmtCfgParams, err = configs.ParseMGMTConfigMap(ctx, lbc.mgmtConfigMap, lbc.isNginxPlus)
884-
if err != nil {
885-
nl.Errorf(lbc.Logger, "Error parsing MGMT ConfigMap: %v", err)
886-
}
887-
881+
if lbc.mgmtConfigMap != nil && lbc.isNginxPlus {
882+
mgmtCfgParams = configs.ParseMGMTConfigMap(ctx, lbc.mgmtConfigMap)
888883
}
889884

890885
resources := lbc.configuration.GetResources()

0 commit comments

Comments
 (0)