Added logic to strip any existing hyphens before processing the args. (#3662)

Updated the logic to handle if extra args are passed with existing hyphens in the arg. The test was updated to add the additional case of having pre-existing hyphens. The method name was also refactored based on previous feedback.
This commit is contained in:
Jamie Phillips 2021-07-28 16:04:19 -04:00 committed by GitHub
parent a1d7a62493
commit fc19b805d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 13 deletions

View File

@ -139,7 +139,7 @@ func getConntrackConfig(nodeConfig *daemonconfig.Node) (*kubeproxyconfig.KubePro
} }
cmd := app2.NewProxyCommand() cmd := app2.NewProxyCommand()
if err := cmd.ParseFlags(daemonconfig.GetArgsList(map[string]string{}, nodeConfig.AgentConfig.ExtraKubeProxyArgs)); err != nil { if err := cmd.ParseFlags(daemonconfig.GetArgs(map[string]string{}, nodeConfig.AgentConfig.ExtraKubeProxyArgs)); err != nil {
return nil, err return nil, err
} }
maxPerCore, err := cmd.Flags().GetInt32("conntrack-max-per-core") maxPerCore, err := cmd.Flags().GetInt32("conntrack-max-per-core")

View File

@ -43,7 +43,7 @@ func Agent(ctx context.Context, nodeConfig *daemonconfig.Node, proxy proxy.Proxy
func startKubeProxy(cfg *daemonconfig.Agent) error { func startKubeProxy(cfg *daemonconfig.Agent) error {
argsMap := kubeProxyArgs(cfg) argsMap := kubeProxyArgs(cfg)
args := daemonconfig.GetArgsList(argsMap, cfg.ExtraKubeProxyArgs) args := daemonconfig.GetArgs(argsMap, cfg.ExtraKubeProxyArgs)
logrus.Infof("Running kube-proxy %s", daemonconfig.ArgString(args)) logrus.Infof("Running kube-proxy %s", daemonconfig.ArgString(args))
return executor.KubeProxy(args) return executor.KubeProxy(args)
} }
@ -51,7 +51,7 @@ func startKubeProxy(cfg *daemonconfig.Agent) error {
func startKubelet(cfg *daemonconfig.Agent) error { func startKubelet(cfg *daemonconfig.Agent) error {
argsMap := kubeletArgs(cfg) argsMap := kubeletArgs(cfg)
args := daemonconfig.GetArgsList(argsMap, cfg.ExtraKubeletArgs) args := daemonconfig.GetArgs(argsMap, cfg.ExtraKubeletArgs)
logrus.Infof("Running kubelet %s", daemonconfig.ArgString(args)) logrus.Infof("Running kubelet %s", daemonconfig.ArgString(args))
return executor.Kubelet(args) return executor.Kubelet(args)

View File

@ -257,10 +257,13 @@ func (a ArgString) String() string {
return b.String() return b.String()
} }
func GetArgsList(argsMap map[string]string, extraArgs []string) []string { // GetArgs appends extra arguments to existing arguments overriding any default options.
func GetArgs(argsMap map[string]string, extraArgs []string) []string {
const hyphens = "--"
// add extra args to args map to override any default option // add extra args to args map to override any default option
for _, arg := range extraArgs { for _, arg := range extraArgs {
splitArg := strings.SplitN(arg, "=", 2) splitArg := strings.SplitN(strings.TrimPrefix(arg, hyphens), "=", 2)
if len(splitArg) < 2 { if len(splitArg) < 2 {
argsMap[splitArg[0]] = "true" argsMap[splitArg[0]] = "true"
continue continue
@ -269,7 +272,7 @@ func GetArgsList(argsMap map[string]string, extraArgs []string) []string {
} }
var args []string var args []string
for arg, value := range argsMap { for arg, value := range argsMap {
cmd := fmt.Sprintf("--%s=%s", arg, value) cmd := fmt.Sprintf("%s%s=%s", hyphens, strings.TrimPrefix(arg, hyphens), value)
args = append(args, cmd) args = append(args, cmd)
} }
sort.Strings(args) sort.Strings(args)

View File

@ -5,7 +5,7 @@ import (
"testing" "testing"
) )
func Test_UnitGetArgsList(t *testing.T) { func Test_UnitGetArgs(t *testing.T) {
type args struct { type args struct {
argsMap map[string]string argsMap map[string]string
extraArgs []string extraArgs []string
@ -35,6 +35,38 @@ func Test_UnitGetArgsList(t *testing.T) {
}, },
}, },
want: []string{
"--aaa=A",
"--bbb=BB",
"--ccc=C",
"--ddd=DD",
"--eee=e",
"--fff=f",
"--ggg=g",
"--hhh=h",
"--iii=II",
},
},
{
name: "Args with existing hyphens Test",
args: args{
argsMap: map[string]string{
"aaa": "A",
"bbb": "B",
"ccc": "C",
"ddd": "d",
"eee": "e",
"fff": "f",
"ggg": "g",
"hhh": "h",
},
extraArgs: []string{
"--bbb=BB",
"--ddd=DD",
"--iii=II",
},
},
want: []string{ want: []string{
"--aaa=A", "--aaa=A",
"--bbb=BB", "--bbb=BB",
@ -50,8 +82,8 @@ func Test_UnitGetArgsList(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if got := GetArgsList(tt.args.argsMap, tt.args.extraArgs); !reflect.DeepEqual(got, tt.want) { if got := GetArgs(tt.args.argsMap, tt.args.extraArgs); !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetArgsList() = %+v\nWant = %+v", got, tt.want) t.Errorf("GetArgs() = %+v\nWant = %+v", got, tt.want)
} }
}) })
} }

View File

@ -121,7 +121,7 @@ func controllerManager(cfg *config.Control, runtime *config.ControlRuntime) erro
argsMap["controllers"] = "*,-service,-route,-cloud-node-lifecycle" argsMap["controllers"] = "*,-service,-route,-cloud-node-lifecycle"
} }
args := config.GetArgsList(argsMap, cfg.ExtraControllerArgs) args := config.GetArgs(argsMap, cfg.ExtraControllerArgs)
logrus.Infof("Running kube-controller-manager %s", config.ArgString(args)) logrus.Infof("Running kube-controller-manager %s", config.ArgString(args))
return executor.ControllerManager(runtime.APIServerReady, args) return executor.ControllerManager(runtime.APIServerReady, args)
@ -139,7 +139,7 @@ func scheduler(cfg *config.Control, runtime *config.ControlRuntime) error {
if cfg.NoLeaderElect { if cfg.NoLeaderElect {
argsMap["leader-elect"] = "false" argsMap["leader-elect"] = "false"
} }
args := config.GetArgsList(argsMap, cfg.ExtraSchedulerAPIArgs) args := config.GetArgs(argsMap, cfg.ExtraSchedulerAPIArgs)
logrus.Infof("Running kube-scheduler %s", config.ArgString(args)) logrus.Infof("Running kube-scheduler %s", config.ArgString(args))
return executor.Scheduler(runtime.APIServerReady, args) return executor.Scheduler(runtime.APIServerReady, args)
@ -192,7 +192,7 @@ func apiServer(ctx context.Context, cfg *config.Control, runtime *config.Control
if cfg.EncryptSecrets { if cfg.EncryptSecrets {
argsMap["encryption-provider-config"] = runtime.EncryptionConfig argsMap["encryption-provider-config"] = runtime.EncryptionConfig
} }
args := config.GetArgsList(argsMap, cfg.ExtraAPIArgs) args := config.GetArgs(argsMap, cfg.ExtraAPIArgs)
logrus.Infof("Running kube-apiserver %s", config.ArgString(args)) logrus.Infof("Running kube-apiserver %s", config.ArgString(args))
@ -302,7 +302,7 @@ func cloudControllerManager(ctx context.Context, cfg *config.Control, runtime *c
if cfg.NoLeaderElect { if cfg.NoLeaderElect {
argsMap["leader-elect"] = "false" argsMap["leader-elect"] = "false"
} }
args := config.GetArgsList(argsMap, cfg.ExtraCloudControllerArgs) args := config.GetArgs(argsMap, cfg.ExtraCloudControllerArgs)
logrus.Infof("Running cloud-controller-manager %s", config.ArgString(args)) logrus.Infof("Running cloud-controller-manager %s", config.ArgString(args))