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

Add the app conditions error to diff comment #25

Closed
wants to merge 9 commits into from
53 changes: 47 additions & 6 deletions internal/pkg/argocd/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,36 @@
}
}

// 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 {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not happy with the temp in the function name but it's following the createTempAppObjectFroNewApp naming (which has a typo 😄 )

_, 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

Expand Down Expand Up @@ -508,20 +538,31 @@
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)
}
}

return componentDiffResult
}

Check failure on line 565 in internal/pkg/argocd/argocd.go

View workflow job for this annotation

GitHub Actions / golangci-lint

`temproarily` is a misspelling of `temporarily` (misspell)
// GenerateDiffOfChangedComponents generates diff of changed components
func GenerateDiffOfChangedComponents(ctx context.Context, componentsToDiff map[string]bool, prBranch string, repo string, useSHALabelForArgoDicovery bool, createTempAppObjectFromNewApps bool) (hasComponentDiff bool, hasComponentDiffErrors bool, diffResults []DiffResult, err error) {
hasComponentDiff = false
Expand Down
Loading