Skip to content

Commit 33775f3

Browse files
authored
Fix crash on 2.7.x due to use of List to read applications (#273)
1 parent 89a6cff commit 33775f3

File tree

6 files changed

+128
-104
lines changed

6 files changed

+128
-104
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
strategy:
4949
fail-fast: false
5050
matrix:
51-
argocd_version: ["v2.4.0", "v2.4.28", "v2.5.0", "v2.5.16", "v2.6.0", "v2.6.7"]
51+
argocd_version: ["v2.5.0", "v2.5.16", "v2.6.0", "v2.6.7", "v2.7.1"]
5252
steps:
5353
- name: Check out code
5454
uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0

GNUmakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ ARGOCD_INSECURE?=true
44
ARGOCD_SERVER?=127.0.0.1:8080
55
ARGOCD_AUTH_USERNAME?=admin
66
ARGOCD_AUTH_PASSWORD?=acceptancetesting
7-
ARGOCD_VERSION?=v2.6.7
7+
ARGOCD_VERSION?=v2.7.1
88

99
export
1010

argocd/resource_argocd_application.go

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -98,29 +98,33 @@ func resourceArgoCDApplicationCreate(ctx context.Context, d *schema.ResourceData
9898
return []diag.Diagnostic{
9999
{
100100
Severity: diag.Error,
101-
Summary: fmt.Sprintf("application %s could not be created", objectMeta.Name),
101+
Summary: fmt.Sprintf("failed to get application %s", objectMeta.Name),
102102
Detail: err.Error(),
103103
},
104104
}
105105
}
106106

107107
if apps != nil {
108-
if len(apps.Items) != 1 {
108+
l := len(apps.Items)
109+
110+
switch {
111+
case l < 1:
112+
break
113+
case l == 1:
114+
switch apps.Items[0].DeletionTimestamp {
115+
case nil:
116+
default:
117+
// Pre-existing app is still in Kubernetes soft deletion queue
118+
time.Sleep(time.Duration(*apps.Items[0].DeletionGracePeriodSeconds))
119+
}
120+
case l > 1:
109121
return []diag.Diagnostic{
110122
{
111123
Severity: diag.Error,
112124
Summary: fmt.Sprintf("found multiple applications matching name '%s' and namespace '%s'", objectMeta.Name, objectMeta.Namespace),
113-
Detail: err.Error(),
114125
},
115126
}
116127
}
117-
118-
switch apps.Items[0].DeletionTimestamp {
119-
case nil:
120-
default:
121-
// Pre-existing app is still in Kubernetes soft deletion queue
122-
time.Sleep(time.Duration(*apps.Items[0].DeletionGracePeriodSeconds))
123-
}
124128
}
125129

126130
featureApplicationLevelSyncOptionsSupported, err := si.isFeatureSupported(featureApplicationLevelSyncOptions)
@@ -271,7 +275,7 @@ func resourceArgoCDApplicationCreate(ctx context.Context, d *schema.ResourceData
271275
}
272276

273277
if len(list.Items) != 1 {
274-
return resource.NonRetryableError(fmt.Errorf("found multiple applications matching name '%s' and namespace '%s'", app.Name, app.Namespace))
278+
return resource.NonRetryableError(fmt.Errorf("found unexpected number of applications matching name '%s' and namespace '%s'. Items: %d", app.Name, app.Namespace, len(list.Items)))
275279
}
276280

277281
if list.Items[0].Status.Health.Status != health.HealthStatusHealthy {
@@ -326,18 +330,25 @@ func resourceArgoCDApplicationRead(ctx context.Context, d *schema.ResourceData,
326330
return []diag.Diagnostic{
327331
{
328332
Severity: diag.Error,
329-
Summary: fmt.Sprintf("application %s not found", appName),
333+
Summary: fmt.Sprintf("failed to get application %s", appName),
330334
Detail: err.Error(),
331335
},
332336
}
333337
}
334338

335-
if len(apps.Items) != 1 {
339+
l := len(apps.Items)
340+
341+
switch {
342+
case l < 1:
343+
d.SetId("")
344+
return diag.Diagnostics{}
345+
case l == 1:
346+
break
347+
case l > 1:
336348
return []diag.Diagnostic{
337349
{
338350
Severity: diag.Error,
339351
Summary: fmt.Sprintf("found multiple applications matching name '%s' and namespace '%s'", appName, namespace),
340-
Detail: err.Error(),
341352
},
342353
}
343354
}
@@ -506,7 +517,7 @@ func resourceArgoCDApplicationUpdate(ctx context.Context, d *schema.ResourceData
506517
// appRequest.ResourceVersion = app.ResourceVersion
507518
// }
508519

509-
if len(apps.Items) != 1 {
520+
if len(apps.Items) > 1 {
510521
return []diag.Diagnostic{
511522
{
512523
Severity: diag.Error,
@@ -543,7 +554,7 @@ func resourceArgoCDApplicationUpdate(ctx context.Context, d *schema.ResourceData
543554
}
544555

545556
if len(list.Items) != 1 {
546-
return resource.NonRetryableError(fmt.Errorf("found multiple applications matching name '%s' and namespace '%s'", *appQuery.Name, *appQuery.AppNamespace))
557+
return resource.NonRetryableError(fmt.Errorf("found unexpected number of applications matching name '%s' and namespace '%s'. Items: %d", *appQuery.Name, *appQuery.AppNamespace, len(list.Items)))
547558
}
548559

549560
if list.Items[0].Status.ReconciledAt.Equal(apps.Items[0].Status.ReconciledAt) {
@@ -606,17 +617,20 @@ func resourceArgoCDApplicationDelete(ctx context.Context, d *schema.ResourceData
606617

607618
if wait, ok := d.GetOk("wait"); ok && wait.(bool) {
608619
if err := resource.RetryContext(ctx, d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
609-
_, err := si.ApplicationClient.List(ctx, &applicationClient.ApplicationQuery{
620+
apps, err := si.ApplicationClient.List(ctx, &applicationClient.ApplicationQuery{
610621
Name: &appName,
611622
AppNamespace: &namespace,
612623
})
613624

614-
if err == nil {
615-
return resource.RetryableError(fmt.Errorf("application %s is still present", appName))
616-
}
617-
618-
if !strings.Contains(err.Error(), "NotFound") {
619-
return resource.NonRetryableError(err)
625+
switch err {
626+
case nil:
627+
if apps != nil && len(apps.Items) > 0 {
628+
return resource.RetryableError(fmt.Errorf("application %s is still present", appName))
629+
}
630+
default:
631+
if !strings.Contains(err.Error(), "NotFound") {
632+
return resource.NonRetryableError(err)
633+
}
620634
}
621635

622636
d.SetId("")

0 commit comments

Comments
 (0)