Skip to content

Commit e8ed105

Browse files
committed
Fix issue in extraArgsCommand field
Signed-off-by: Rizwana777 <rizwananaaz177@gmail.com>
1 parent c93f2fe commit e8ed105

File tree

2 files changed

+84
-14
lines changed

2 files changed

+84
-14
lines changed

controllers/argocd/deployment.go

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,9 @@ func getArgoServerCommand(cr *argoproj.ArgoCD, useTLSForRedis bool) []string {
288288
cmd = append(cmd, "--repo-server-strict-tls")
289289
}
290290

291-
cmd = append(cmd, "--staticassets")
292-
cmd = append(cmd, "/shared/app")
291+
cmd = append(cmd, "--staticassets", "/shared/app")
293292

294-
cmd = append(cmd, "--dex-server")
295-
cmd = append(cmd, getDexServerAddress(cr))
293+
cmd = append(cmd, "--dex-server", getDexServerAddress(cr))
296294

297295
if cr.Spec.Repo.IsEnabled() {
298296
cmd = append(cmd, "--repo-server", getRepoServerAddress(cr))
@@ -315,22 +313,17 @@ func getArgoServerCommand(cr *argoproj.ArgoCD, useTLSForRedis bool) []string {
315313
}
316314
}
317315

318-
cmd = append(cmd, "--loglevel")
319-
cmd = append(cmd, getLogLevel(cr.Spec.Server.LogLevel))
320-
321-
cmd = append(cmd, "--logformat")
322-
cmd = append(cmd, getLogFormat(cr.Spec.Server.LogFormat))
316+
cmd = append(cmd, "--loglevel", getLogLevel(cr.Spec.Server.LogLevel))
317+
cmd = append(cmd, "--logformat", getLogFormat(cr.Spec.Server.LogFormat))
323318

319+
// Merge extraArgs while ignoring duplicates
324320
extraArgs := cr.Spec.Server.ExtraCommandArgs
325-
err := isMergable(extraArgs, cmd)
326-
if err != nil {
327-
return cmd
328-
}
321+
cmd = appendUniqueArgs(cmd, extraArgs)
322+
329323
if len(cr.Spec.SourceNamespaces) > 0 {
330324
cmd = append(cmd, "--application-namespaces", fmt.Sprint(strings.Join(cr.Spec.SourceNamespaces, ",")))
331325
}
332326

333-
cmd = append(cmd, extraArgs...)
334327
return cmd
335328
}
336329

@@ -350,6 +343,55 @@ func isMergable(extraArgs []string, cmd []string) error {
350343
return nil
351344
}
352345

346+
// appendUniqueArgs appends extraArgs to cmd while ignoring any duplicate flags.
347+
func appendUniqueArgs(cmd []string, extraArgs []string) []string {
348+
// Parse cmd into a map to track both flags and their associated values
349+
existingArgs := make(map[string]string)
350+
for i := 0; i < len(cmd); i++ {
351+
arg := cmd[i]
352+
if strings.HasPrefix(arg, "--") {
353+
// Check if the next item is a value (not another flag)
354+
if i+1 < len(cmd) && !strings.HasPrefix(cmd[i+1], "--") {
355+
existingArgs[arg] = cmd[i+1] // Store flag-value pair
356+
i++ // Skip the value
357+
} else {
358+
existingArgs[arg] = "" // Store flag without value
359+
}
360+
}
361+
}
362+
363+
// Iterate over extraArgs and append only if the flag and value do not already exist in cmd
364+
for i := 0; i < len(extraArgs); i++ {
365+
arg := extraArgs[i]
366+
if strings.HasPrefix(arg, "--") {
367+
// If this flag already exists in cmd, skip it
368+
if _, exists := existingArgs[arg]; exists {
369+
if i+1 < len(extraArgs) && !strings.HasPrefix(extraArgs[i+1], "--") {
370+
i++ // Skip the associated value
371+
}
372+
continue
373+
}
374+
375+
// Append the flag to cmd
376+
cmd = append(cmd, arg)
377+
378+
// Check if this flag has an associated value
379+
if i+1 < len(extraArgs) && !strings.HasPrefix(extraArgs[i+1], "--") {
380+
cmd = append(cmd, extraArgs[i+1])
381+
existingArgs[arg] = extraArgs[i+1] // Track flag-value pair
382+
i++ // Skip the value
383+
} else {
384+
existingArgs[arg] = "" // Track flag without a value
385+
}
386+
} else {
387+
// If it's not a flag, append it directly (non-flag argument)
388+
cmd = append(cmd, arg)
389+
}
390+
}
391+
392+
return cmd
393+
}
394+
353395
// getDexServerAddress will return the Dex server address.
354396
func getDexServerAddress(cr *argoproj.ArgoCD) string {
355397
return fmt.Sprintf("https://%s", fqdnServiceRef("dex-server", common.ArgoCDDefaultDexHTTPPort, cr))

controllers/argocd/deployment_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,34 @@ func TestArgoCDServerDeploymentCommand(t *testing.T) {
13171317

13181318
assert.Equal(t, baseCommand, deployment.Spec.Template.Spec.Containers[0].Command)
13191319

1320+
// When ExtraCommandArgs contains a non-duplicate argument along with a duplicate
1321+
a.Spec.Server.ExtraCommandArgs = []string{
1322+
"--rootpath",
1323+
"/argocd",
1324+
"--foo",
1325+
"bar",
1326+
"test",
1327+
"--logformat", // Duplicate flag and value
1328+
"text",
1329+
"--newarg", // Non-duplicate argument
1330+
"newvalue",
1331+
"--newarg", // Duplicate argument passing at once
1332+
"newvalue",
1333+
}
1334+
1335+
assert.NoError(t, r.reconcileServerDeployment(a, false))
1336+
assert.NoError(t, r.Client.Get(
1337+
context.TODO(),
1338+
types.NamespacedName{
1339+
Name: "argocd-server",
1340+
Namespace: a.Namespace,
1341+
},
1342+
deployment))
1343+
1344+
// Non-duplicate argument "--newarg" should be added, duplicate "--redis" is ignored
1345+
cmd = append(cmd, "--newarg", "newvalue")
1346+
assert.Equal(t, cmd, deployment.Spec.Template.Spec.Containers[0].Command)
1347+
13201348
// Remove all the command arguments that were added.
13211349
a.Spec.Server.ExtraCommandArgs = []string{}
13221350

0 commit comments

Comments
 (0)