Skip to content

Commit

Permalink
fix(promotions): multiple stages can sync same argocd app
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Ivanov <[email protected]>
  • Loading branch information
shmargum committed Sep 18, 2024
1 parent eca5560 commit 94d8f06
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 20 deletions.
51 changes: 40 additions & 11 deletions internal/controller/promotion/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type argoCDMechanism struct {
desiredSource *argocd.ApplicationSource,
desiredSources argocd.ApplicationSources,
freightColID string,
stage *kargoapi.Stage,
) error
getAuthorizedApplicationFn func(
ctx context.Context,
Expand Down Expand Up @@ -201,6 +202,7 @@ func (a *argoCDMechanism) Promote(
desiredSource,
desiredSources,
promo.Status.FreightCollection.ID,
stage,
); err != nil {
return err
}
Expand Down Expand Up @@ -301,7 +303,12 @@ func (a *argoCDMechanism) mustPerformUpdate(
// current freight collection. i.e. Not related to the current promotion.
var correctFreightColIDFound bool
for _, info := range status.Operation.Info {
if info.Name == freightCollectionInfoKey {
if info.Name == fmt.Sprintf(
"%s/%s/%s",
freightCollectionInfoKey,
stage.ObjectMeta.Namespace,
stage.ObjectMeta.Name,
) {
correctFreightColIDFound = info.Value == freightCol.ID
break
}
Expand Down Expand Up @@ -374,6 +381,7 @@ func (a *argoCDMechanism) syncApplication(
desiredSource *argocd.ApplicationSource,
desiredSources argocd.ApplicationSources,
freightColID string,
stage *kargoapi.Stage,
) error {
// Initiate a "hard" refresh.
if app.ObjectMeta.Annotations == nil {
Expand All @@ -385,22 +393,43 @@ func (a *argoCDMechanism) syncApplication(
app.Spec.Source = desiredSource.DeepCopy()
app.Spec.Sources = desiredSources.DeepCopy()

kargoKey := fmt.Sprintf("%s/%s/%s", freightCollectionInfoKey, stage.ObjectMeta.Namespace, stage.ObjectMeta.Name)
reason := "Promotion triggered a sync of this Application resource."

newInfo := make(map[string]string)
defaultInfo := map[string]string{
"Reason": reason,
kargoKey: freightColID,
}

if app.Status.OperationState != nil && app.Status.OperationState.Operation.Info != nil {
for _, info := range app.Status.OperationState.Operation.Info {
newInfo[info.Name] = info.Value
}
if newInfo["Reason"] == reason {
newInfo[kargoKey] = freightColID
} else {
newInfo = defaultInfo
}
} else {
newInfo = defaultInfo
}

infoArray := []*argocd.Info{}
for k, v := range newInfo {
infoArray = append(infoArray, &argocd.Info{
Name: k,
Value: v,
})
}

// Initiate a new operation.
app.Operation = &argocd.Operation{
InitiatedBy: argocd.OperationInitiator{
Username: applicationOperationInitiator,
Automated: true,
},
Info: []*argocd.Info{
{
Name: "Reason",
Value: "Promotion triggered a sync of this Application resource.",
},
{
Name: freightCollectionInfoKey,
Value: freightColID,
},
},
Info: infoArray,
Sync: &argocd.SyncOperation{
Revisions: []string{},
},
Expand Down
39 changes: 30 additions & 9 deletions internal/controller/promotion/argocd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ func TestArgoCDPromote(t *testing.T) {
*argocd.ApplicationSource,
argocd.ApplicationSources,
string,
*kargoapi.Stage,
) error {
return nil
},
Expand Down Expand Up @@ -452,6 +453,7 @@ func TestArgoCDPromote(t *testing.T) {
*argocd.ApplicationSource,
argocd.ApplicationSources,
string,
*kargoapi.Stage,
) error {
return errors.New("something went wrong")
},
Expand Down Expand Up @@ -538,6 +540,7 @@ func TestArgoCDPromote(t *testing.T) {
*argocd.ApplicationSource,
argocd.ApplicationSources,
string,
*kargoapi.Stage,
) error {
return nil
},
Expand Down Expand Up @@ -990,7 +993,7 @@ func TestArgoCDMustPerformUpdate(t *testing.T) {
Username: applicationOperationInitiator,
},
Info: []*argocd.Info{{
Name: freightCollectionInfoKey,
Name: fmt.Sprintf("%s/fake-namespace/fake-name", freightCollectionInfoKey),
Value: "wrong-freight-collection",
}},
},
Expand All @@ -1013,7 +1016,7 @@ func TestArgoCDMustPerformUpdate(t *testing.T) {
Username: applicationOperationInitiator,
},
Info: []*argocd.Info{{
Name: freightCollectionInfoKey,
Name: fmt.Sprintf("%s/fake-namespace/fake-name", freightCollectionInfoKey),
Value: "wrong-freight-collection",
}},
},
Expand All @@ -1035,7 +1038,7 @@ func TestArgoCDMustPerformUpdate(t *testing.T) {
Username: applicationOperationInitiator,
},
Info: []*argocd.Info{{
Name: freightCollectionInfoKey,
Name: fmt.Sprintf("%s/fake-namespace/fake-name", freightCollectionInfoKey),
Value: testFreightCollectionID,
}},
},
Expand All @@ -1057,7 +1060,7 @@ func TestArgoCDMustPerformUpdate(t *testing.T) {
Username: applicationOperationInitiator,
},
Info: []*argocd.Info{{
Name: freightCollectionInfoKey,
Name: fmt.Sprintf("%s/fake-namespace/fake-name", freightCollectionInfoKey),
Value: testFreightCollectionID,
}},
},
Expand All @@ -1083,7 +1086,7 @@ func TestArgoCDMustPerformUpdate(t *testing.T) {
Username: applicationOperationInitiator,
},
Info: []*argocd.Info{{
Name: freightCollectionInfoKey,
Name: fmt.Sprintf("%s/fake-namespace/fake-name", freightCollectionInfoKey),
Value: testFreightCollectionID,
}},
},
Expand Down Expand Up @@ -1116,7 +1119,7 @@ func TestArgoCDMustPerformUpdate(t *testing.T) {
Username: applicationOperationInitiator,
},
Info: []*argocd.Info{{
Name: freightCollectionInfoKey,
Name: fmt.Sprintf("%s/fake-namespace/fake-name", freightCollectionInfoKey),
Value: testFreightCollectionID,
}},
},
Expand Down Expand Up @@ -1153,7 +1156,7 @@ func TestArgoCDMustPerformUpdate(t *testing.T) {
Username: applicationOperationInitiator,
},
Info: []*argocd.Info{{
Name: freightCollectionInfoKey,
Name: fmt.Sprintf("%s/fake-namespace/fake-name", freightCollectionInfoKey),
Value: testFreightCollectionID,
}},
},
Expand Down Expand Up @@ -1189,7 +1192,7 @@ func TestArgoCDMustPerformUpdate(t *testing.T) {
Username: applicationOperationInitiator,
},
Info: []*argocd.Info{{
Name: freightCollectionInfoKey,
Name: fmt.Sprintf("%s/fake-namespace/fake-name", freightCollectionInfoKey),
Value: testFreightCollectionID,
}},
},
Expand Down Expand Up @@ -1228,7 +1231,7 @@ func TestArgoCDMustPerformUpdate(t *testing.T) {
Username: applicationOperationInitiator,
},
Info: []*argocd.Info{{
Name: freightCollectionInfoKey,
Name: fmt.Sprintf("%s/fake-namespace/fake-name", freightCollectionInfoKey),
Value: testFreightCollectionID,
}},
},
Expand Down Expand Up @@ -1276,6 +1279,10 @@ func TestArgoCDMustPerformUpdate(t *testing.T) {
require.True(t, ok)

stage := &kargoapi.Stage{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-name",
Namespace: "fake-namespace",
},
Spec: kargoapi.StageSpec{
PromotionMechanisms: &kargoapi.PromotionMechanisms{
ArgoCDAppUpdates: []kargoapi.ArgoCDAppUpdate{{
Expand Down Expand Up @@ -1317,6 +1324,7 @@ func TestArgoCDSyncApplication(t *testing.T) {
desiredSource *argocd.ApplicationSource
desiredSources argocd.ApplicationSources
assertions func(*testing.T, error)
stage *kargoapi.Stage
}{
{
name: "error patching Application",
Expand All @@ -1342,6 +1350,12 @@ func TestArgoCDSyncApplication(t *testing.T) {
require.ErrorContains(t, err, "error patching Argo CD Application")
require.ErrorContains(t, err, "something went wrong")
},
stage: &kargoapi.Stage{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-name",
Namespace: "fake-namespace",
},
},
},
{
name: "success",
Expand All @@ -1367,6 +1381,12 @@ func TestArgoCDSyncApplication(t *testing.T) {
assertions: func(t *testing.T, err error) {
require.NoError(t, err)
},
stage: &kargoapi.Stage{
ObjectMeta: metav1.ObjectMeta{
Name: "fake-name",
Namespace: "fake-namespace",
},
},
},
}
for _, testCase := range testCases {
Expand All @@ -1379,6 +1399,7 @@ func TestArgoCDSyncApplication(t *testing.T) {
testCase.desiredSource,
testCase.desiredSources,
"fake-freight-collection-id",
testCase.stage,
),
)
})
Expand Down

0 comments on commit 94d8f06

Please sign in to comment.