Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash on 2.7.x due to use of List to read applications #273

Merged
merged 4 commits into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
strategy:
fail-fast: false
matrix:
argocd_version: ["v2.4.0", "v2.4.28", "v2.5.0", "v2.5.16", "v2.6.0", "v2.6.7"]
argocd_version: ["v2.5.0", "v2.5.16", "v2.6.0", "v2.6.7", "v2.7.1"]
steps:
- name: Check out code
uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # v3.3.0
Expand Down
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ARGOCD_SERVER?=127.0.0.1:8080
ARGOCD_AUTH_USERNAME?=admin
ARGOCD_AUTH_PASSWORD?=acceptancetesting
ARGOCD_CONTEXT?=kind-argocd
ARGOCD_VERSION?=v2.6.7
ARGOCD_VERSION?=v2.7.1

export

Expand Down
60 changes: 37 additions & 23 deletions argocd/resource_argocd_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,29 +98,33 @@ func resourceArgoCDApplicationCreate(ctx context.Context, d *schema.ResourceData
return []diag.Diagnostic{
{
Severity: diag.Error,
Summary: fmt.Sprintf("application %s could not be created", objectMeta.Name),
Summary: fmt.Sprintf("failed to get application %s", objectMeta.Name),
Detail: err.Error(),
},
}
}

if apps != nil {
if len(apps.Items) != 1 {
l := len(apps.Items)

switch {
case l < 1:
break
case l == 1:
switch apps.Items[0].DeletionTimestamp {
case nil:
default:
// Pre-existing app is still in Kubernetes soft deletion queue
time.Sleep(time.Duration(*apps.Items[0].DeletionGracePeriodSeconds))
}
case l > 1:
return []diag.Diagnostic{
{
Severity: diag.Error,
Summary: fmt.Sprintf("found multiple applications matching name '%s' and namespace '%s'", objectMeta.Name, objectMeta.Namespace),
Detail: err.Error(),
},
}
}

switch apps.Items[0].DeletionTimestamp {
case nil:
default:
// Pre-existing app is still in Kubernetes soft deletion queue
time.Sleep(time.Duration(*apps.Items[0].DeletionGracePeriodSeconds))
}
}

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

if len(list.Items) != 1 {
return resource.NonRetryableError(fmt.Errorf("found multiple applications matching name '%s' and namespace '%s'", app.Name, app.Namespace))
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)))
}

if list.Items[0].Status.Health.Status != health.HealthStatusHealthy {
Expand Down Expand Up @@ -326,18 +330,25 @@ func resourceArgoCDApplicationRead(ctx context.Context, d *schema.ResourceData,
return []diag.Diagnostic{
{
Severity: diag.Error,
Summary: fmt.Sprintf("application %s not found", appName),
Summary: fmt.Sprintf("failed to get application %s", appName),
Detail: err.Error(),
},
}
}

if len(apps.Items) != 1 {
l := len(apps.Items)

switch {
case l < 1:
d.SetId("")
return diag.Diagnostics{}
case l == 1:
break
case l > 1:
return []diag.Diagnostic{
{
Severity: diag.Error,
Summary: fmt.Sprintf("found multiple applications matching name '%s' and namespace '%s'", appName, namespace),
Detail: err.Error(),
},
}
}
Expand Down Expand Up @@ -506,7 +517,7 @@ func resourceArgoCDApplicationUpdate(ctx context.Context, d *schema.ResourceData
// appRequest.ResourceVersion = app.ResourceVersion
// }

if len(apps.Items) != 1 {
if len(apps.Items) > 1 {
return []diag.Diagnostic{
{
Severity: diag.Error,
Expand Down Expand Up @@ -543,7 +554,7 @@ func resourceArgoCDApplicationUpdate(ctx context.Context, d *schema.ResourceData
}

if len(list.Items) != 1 {
return resource.NonRetryableError(fmt.Errorf("found multiple applications matching name '%s' and namespace '%s'", *appQuery.Name, *appQuery.AppNamespace))
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)))
}

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

if wait, ok := d.GetOk("wait"); ok && wait.(bool) {
if err := resource.RetryContext(ctx, d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
_, err := si.ApplicationClient.List(ctx, &applicationClient.ApplicationQuery{
apps, err := si.ApplicationClient.List(ctx, &applicationClient.ApplicationQuery{
Name: &appName,
AppNamespace: &namespace,
})

if err == nil {
return resource.RetryableError(fmt.Errorf("application %s is still present", appName))
}

if !strings.Contains(err.Error(), "NotFound") {
return resource.NonRetryableError(err)
switch err {
case nil:
if apps != nil && len(apps.Items) > 0 {
return resource.RetryableError(fmt.Errorf("application %s is still present", appName))
}
default:
if !strings.Contains(err.Error(), "NotFound") {
return resource.NonRetryableError(err)
}
}

d.SetId("")
Expand Down
Loading