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(controller): app sync fix #3083

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
68 changes: 27 additions & 41 deletions internal/directives/argocd_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

const (
applicationOperationInitiator = "kargo-controller"
freightCollectionInfoKey = "kargo.akuity.io/freight-collection"
promotionInfoKey = "kargo.akuity.io/promotion"
)

func init() {
Expand Down Expand Up @@ -69,7 +69,6 @@ type argocdUpdater struct {
*PromotionStepContext,
*ArgoCDAppUpdate,
*argocd.Application,
argocd.ApplicationSources,
) (argocd.OperationPhase, bool, error)

syncApplicationFn func(
Expand Down Expand Up @@ -207,24 +206,8 @@ func (a *argocdUpdater) runPromotionStep(
DesiredRevisions: desiredRevisions,
}

// Build the desired source(s) for the Argo CD Application.
desiredSources, err := a.buildDesiredSourcesFn(
ctx,
stepCtx,
&stepCfg,
update,
desiredRevisions,
app,
)
if err != nil {
return PromotionStepResult{Status: kargoapi.PromotionPhaseErrored}, fmt.Errorf(
"error building desired sources for Argo CD Application %q in namespace %q: %w",
app.Name, app.Namespace, err,
)
}

// Check if the update needs to be performed and retrieve its phase.
phase, mustUpdate, err := a.mustPerformUpdateFn(stepCtx, update, app, desiredSources)
phase, mustUpdate, err := a.mustPerformUpdateFn(stepCtx, update, app)

// If we have a phase, append it to the results.
if phase != "" {
Expand Down Expand Up @@ -267,6 +250,22 @@ func (a *argocdUpdater) runPromotionStep(
logger.Debug(err.Error())
}

// Build the desired source(s) for the Argo CD Application.
desiredSources, err := a.buildDesiredSourcesFn(
ctx,
stepCtx,
&stepCfg,
update,
desiredRevisions,
app,
)
if err != nil {
return PromotionStepResult{Status: kargoapi.PromotionPhaseErrored}, fmt.Errorf(
"error building desired sources for Argo CD Application %q in namespace %q: %w",
app.Name, app.Namespace, err,
)
}

// Perform the update.
if err = a.syncApplicationFn(
ctx,
Expand Down Expand Up @@ -372,7 +371,6 @@ func (a *argocdUpdater) mustPerformUpdate(
stepCtx *PromotionStepContext,
update *ArgoCDAppUpdate,
app *argocd.Application,
desiredSources argocd.ApplicationSources,
) (phase argocd.OperationPhase, mustUpdate bool, err error) {
status := app.Status.OperationState
if status == nil {
Expand Down Expand Up @@ -400,23 +398,23 @@ func (a *argocdUpdater) mustPerformUpdate(

// Deal with the possibility that the operation was not initiated for the
// current freight collection. i.e. Not related to the current promotion.
var correctFreightColIDFound bool
var correctPromotionIDFound bool
for _, info := range status.Operation.Info {
if info.Name == freightCollectionInfoKey {
correctFreightColIDFound = info.Value == stepCtx.Freight.ID
if info.Name == promotionInfoKey {
correctPromotionIDFound = info.Value == stepCtx.Promotion
break
}
}
if !correctFreightColIDFound {
// The operation was not initiated for the current freight collection.
if !correctPromotionIDFound {
// The operation was not initiated for the current Promotion.
if !status.Phase.Completed() {
// We should wait for the operation to complete before attempting to
// apply an update ourselves.
// NB: We return the current phase here because we want the caller
// to know that an operation is still running.
return status.Phase, false, fmt.Errorf(
"current operation was not initiated for freight collection %q: waiting for operation to complete",
stepCtx.Freight.ID,
"current operation was not initiated for Promotion %s: waiting for operation to complete",
stepCtx.Promotion,
)
}
// Initiate our own operation.
Expand All @@ -434,18 +432,6 @@ func (a *argocdUpdater) mustPerformUpdate(
return "", true, errors.New("operation completed without a sync result")
}

// Check if the desired sources were applied.
if len(update.Sources) > 0 {
if (status.SyncResult.Source.RepoURL != "" && !status.SyncResult.Source.Equals(&desiredSources[0])) ||
(status.SyncResult.Source.RepoURL == "" && !status.SyncResult.Sources.Equals(desiredSources)) {
// The operation did not result in the desired sources being applied. We
// should attempt to retry the operation.
return "", true, fmt.Errorf(
"operation result source does not match desired source",
)
}
}

// Check if the desired revisions were applied.
desiredRevisions, err := a.getDesiredRevisions(stepCtx, update, app)
if err != nil {
Expand Down Expand Up @@ -514,8 +500,8 @@ func (a *argocdUpdater) syncApplication(
Value: "Promotion triggered a sync of this Application resource.",
},
{
Name: freightCollectionInfoKey,
Value: stepCtx.Freight.ID,
Name: promotionInfoKey,
Value: stepCtx.Promotion,
},
},
Sync: &argocd.SyncOperation{
Expand Down
Loading
Loading