Skip to content

Commit

Permalink
fix(controller): app sync fix (#3083)
Browse files Browse the repository at this point in the history
Signed-off-by: Kent Rancourt <kent.rancourt@gmail.com>
  • Loading branch information
krancour authored Dec 9, 2024
1 parent 2655a03 commit a77eedd
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 205 deletions.
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

0 comments on commit a77eedd

Please sign in to comment.