diff --git a/internal/pkg/argocd/argocd.go b/internal/pkg/argocd/argocd.go index db798428..d1dd1551 100644 --- a/internal/pkg/argocd/argocd.go +++ b/internal/pkg/argocd/argocd.go @@ -410,6 +410,36 @@ func createTempAppObjectFroNewApp(ctx context.Context, componentPath string, rep } } +// deleteTempAppObject deletes a temporary app object created for diff generation +// returns an error if the deletion fails +func deleteTempAppObject(ctx context.Context, ac argoCdClients, app *argoappv1.Application) error { + _, err := ac.app.Delete(ctx, &application.ApplicationDeleteRequest{Name: &app.Name, AppNamespace: &app.Namespace}) + if err != nil { + log.Errorf("Error deleting temporary app object: %v", err) + return err + } + + log.Debugf("Deleted temporary app object: %s", app.Name) + return nil +} + +// appComparisonError returns an error if there are comparison errors in app conditions +func appComparisonError(app *argoappv1.Application) (err error) { + ac := app.Status.GetConditions( + map[string]bool{string(argoappv1.ApplicationConditionComparisonError): true}, + ) + if ac == nil { + return nil + } + + cerr := "" + for _, c := range ac { + cerr = fmt.Sprintln(cerr, c.Message) + } + + return fmt.Errorf("%s", cerr) +} + func generateDiffOfAComponent(ctx context.Context, commentDiff bool, componentPath string, prBranch string, repo string, ac argoCdClients, argoSettings *settings.Settings, useSHALabelForArgoDicovery bool, createTempAppObjectFromNewApps bool) (componentDiffResult DiffResult) { componentDiffResult.ComponentPath = componentPath @@ -508,14 +538,25 @@ func generateDiffOfAComponent(ctx context.Context, commentDiff bool, componentPa log.Debugf("Generating diff for component %s", componentPath) componentDiffResult.HasDiff, componentDiffResult.DiffElements, componentDiffResult.DiffError = generateArgocdAppDiff(ctx, commentDiff, app, detailedProject.Project, resources, argoSettings, diffOption) - if componentDiffResult.AppWasTemporarilyCreated { - // Delete the temporary app object - _, err = ac.app.Delete(ctx, &application.ApplicationDeleteRequest{Name: &app.Name, AppNamespace: &app.Namespace}) + var cerr error + if componentDiffResult.DiffError != nil { + // wait a couple of seconds before checking for condition errors + log.Error("waiting to get the result") + time.Sleep(5 * time.Second) + cerr = appComparisonError(app) + if cerr != nil { + componentDiffResult.DiffError = fmt.Errorf("%w\n%w", cerr, componentDiffResult.DiffError) + } + } + + // we only want to delete the app if it was temproarily created + // in case of a diff error if we found a condition error, we delete the app + // if we didn't find a condition error we keep the app for further investigation + if componentDiffResult.AppWasTemporarilyCreated && componentDiffResult.DiffError == nil && cerr != nil { + err := deleteTempAppObject(ctx, ac, app) if err != nil { - log.Errorf("Error deleting temporary app object: %v", err) + // TODO: i think this is not optimal, if we have a diff error and a deletion error, we should return the deletion error as the main error componentDiffResult.DiffError = err - } else { - log.Debugf("Deleted temporary app object: %s", app.Name) } }