Skip to content

Commit

Permalink
fix(application): fix use of List to read applications
Browse files Browse the repository at this point in the history
  • Loading branch information
onematchfox committed May 4, 2023
1 parent fa86a9b commit b289b3f
Showing 1 changed file with 28 additions and 22 deletions.
50 changes: 28 additions & 22 deletions argocd/resource_argocd_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,33 +94,37 @@ func resourceArgoCDApplicationCreate(ctx context.Context, d *schema.ResourceData
Name: &objectMeta.Name,
AppNamespace: &objectMeta.Namespace,
})
if err != nil && !strings.Contains(err.Error(), "NotFound") {
if err != nil {
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 @@ -318,26 +322,28 @@ func resourceArgoCDApplicationRead(ctx context.Context, d *schema.ResourceData,
AppNamespace: &namespace,
})
if err != nil {
if strings.Contains(err.Error(), "NotFound") {
d.SetId("")
return diag.Diagnostics{}
}

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 +512,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 +549,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

0 comments on commit b289b3f

Please sign in to comment.