Skip to content

Commit 55633e5

Browse files
merge self heal logic with oss
1 parent 727f540 commit 55633e5

19 files changed

+854
-1090
lines changed

assets/swagger.json

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9446,13 +9446,6 @@
94469446
"comparedTo": {
94479447
"$ref": "#/definitions/v1alpha1ComparedTo"
94489448
},
9449-
"manifestsChanged": {
9450-
"type": "object",
9451-
"title": "ManifestsChanged indicates whether the manifests have changed base on argocd.argoproj.io/manifest-generate-paths annotation",
9452-
"additionalProperties": {
9453-
"type": "boolean"
9454-
}
9455-
},
94569449
"revision": {
94579450
"type": "string",
94589451
"title": "Revision contains information about the revision the comparison has been performed to"

controller/appcontroller.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"math"
99
"math/rand"
1010
"net/http"
11-
"os"
1211
"reflect"
1312
"runtime/debug"
1413
"sort"
@@ -1623,7 +1622,7 @@ func (ctrl *ApplicationController) processAppRefreshQueueItem() (processNext boo
16231622
}
16241623

16251624
if project.Spec.SyncWindows.Matches(app).CanSync(false) {
1626-
syncErrCond, opMS := ctrl.autoSync(app, compareResult.syncStatus, compareResult.resources)
1625+
syncErrCond, opMS := ctrl.autoSync(app, compareResult.syncStatus, compareResult.resources, compareResult.revisionUpdated)
16271626
setOpMs = opMS
16281627
if syncErrCond != nil {
16291628
app.Status.SetConditions(
@@ -1841,7 +1840,7 @@ func (ctrl *ApplicationController) persistAppStatus(orig *appv1.Application, new
18411840
}
18421841

18431842
// autoSync will initiate a sync operation for an application configured with automated sync
1844-
func (ctrl *ApplicationController) autoSync(app *appv1.Application, syncStatus *appv1.SyncStatus, resources []appv1.ResourceStatus) (*appv1.ApplicationCondition, time.Duration) {
1843+
func (ctrl *ApplicationController) autoSync(app *appv1.Application, syncStatus *appv1.SyncStatus, resources []appv1.ResourceStatus, revisionUpdated bool) (*appv1.ApplicationCondition, time.Duration) {
18451844
if app.Spec.SyncPolicy == nil || app.Spec.SyncPolicy.Automated == nil {
18461845
return nil, 0
18471846
}
@@ -1879,7 +1878,7 @@ func (ctrl *ApplicationController) autoSync(app *appv1.Application, syncStatus *
18791878

18801879
desiredCommitSHA := syncStatus.Revision
18811880
desiredCommitSHAsMS := syncStatus.Revisions
1882-
alreadyAttempted, attemptPhase := alreadyAttemptedSync(app, desiredCommitSHA, desiredCommitSHAsMS, app.Spec.HasMultipleSources(), syncStatus.ManifestsChanged)
1881+
alreadyAttempted, attemptPhase := alreadyAttemptedSync(app, desiredCommitSHA, desiredCommitSHAsMS, app.Spec.HasMultipleSources(), revisionUpdated)
18831882
selfHeal := app.Spec.SyncPolicy.Automated.SelfHeal
18841883
op := appv1.Operation{
18851884
Sync: &appv1.SyncOperation{
@@ -1970,21 +1969,21 @@ func (ctrl *ApplicationController) autoSync(app *appv1.Application, syncStatus *
19701969

19711970
// alreadyAttemptedSync returns whether the most recent sync was performed against the
19721971
// commitSHA and with the same app source config which are currently set in the app
1973-
func alreadyAttemptedSync(app *appv1.Application, commitSHA string, commitSHAsMS []string, hasMultipleSources bool, manifestsChangedMap map[string]bool) (bool, synccommon.OperationPhase) {
1972+
func alreadyAttemptedSync(app *appv1.Application, commitSHA string, commitSHAsMS []string, hasMultipleSources bool, revisionUpdated bool) (bool, synccommon.OperationPhase) {
19741973
if app.Status.OperationState == nil || app.Status.OperationState.Operation.Sync == nil || app.Status.OperationState.SyncResult == nil {
19751974
return false, ""
19761975
}
19771976
if hasMultipleSources {
1978-
if !reflect.DeepEqual(app.Status.OperationState.SyncResult.Revisions, commitSHAsMS) {
1979-
return false, ""
1977+
if revisionUpdated {
1978+
if !reflect.DeepEqual(app.Status.OperationState.SyncResult.Revisions, commitSHAsMS) {
1979+
return false, ""
1980+
}
1981+
} else {
1982+
log.WithField("application", app.Name).Debugf("Skipping auto-sync: commitSHA %s has no changes", commitSHA)
19801983
}
19811984
} else {
1982-
manifestChanged, ok := manifestsChangedMap[commitSHA]
1983-
featureFlagDisabled := os.Getenv("PERSIST_CHANGE_REVISIONS") != "1"
1984-
// If record not exists, we need to do sync
1985-
if featureFlagDisabled || !ok || manifestChanged {
1986-
log.WithField("application", app.Name).Infof("Executing compare of syncResult.Revision and commitSha because of feature flag disabled or manifest changed: %v", commitSHA)
1987-
log.WithField("application", app.Name).Infof("Executing compare of syncResult.Revision and commitSha with context, map: %v, flag: %t, record exists: %t", manifestsChangedMap, featureFlagDisabled, ok)
1985+
if revisionUpdated {
1986+
log.WithField("application", app.Name).Infof("Executing compare of syncResult.Revision and commitSha because manifest changed: %v", commitSHA)
19881987
if app.Status.OperationState.SyncResult.Revision != commitSHA {
19891988
return false, ""
19901989
}

controller/appcontroller_test.go

Lines changed: 13 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,72 +2096,6 @@ func TestAddControllerNamespace(t *testing.T) {
20962096
})
20972097
}
20982098

2099-
func TestAlreadyAttemptSync(t *testing.T) {
2100-
app := newFakeApp()
2101-
t.Run("same manifest with sync result, with disabled flag", func(t *testing.T) {
2102-
manifestChangedMap := make(map[string]bool)
2103-
manifestChangedMap["sha"] = false
2104-
2105-
app.Status.Sync.ManifestsChanged = manifestChangedMap
2106-
2107-
attempted, _ := alreadyAttemptedSync(app, "sha", []string{}, false, nil)
2108-
assert.False(t, attempted)
2109-
})
2110-
2111-
t.Run("same manifest with sync result, with enabled flag", func(t *testing.T) {
2112-
_ = os.Setenv("PERSIST_CHANGE_REVISIONS", "1")
2113-
2114-
manifestChangedMap := make(map[string]bool)
2115-
manifestChangedMap["sha"] = false
2116-
2117-
app.Status.Sync.ManifestsChanged = manifestChangedMap
2118-
2119-
attempted, _ := alreadyAttemptedSync(app, "sha", []string{}, false, manifestChangedMap)
2120-
assert.True(t, attempted)
2121-
})
2122-
2123-
t.Run("different manifest with sync result, with disabled flag", func(t *testing.T) {
2124-
manifestChangedMap := make(map[string]bool)
2125-
manifestChangedMap["sha"] = true
2126-
2127-
app.Status.Sync.ManifestsChanged = manifestChangedMap
2128-
2129-
attempted, _ := alreadyAttemptedSync(app, "sha", []string{}, false, nil)
2130-
assert.False(t, attempted)
2131-
})
2132-
2133-
t.Run("different manifest with sync result, with enabled flag", func(t *testing.T) {
2134-
_ = os.Setenv("PERSIST_CHANGE_REVISIONS", "1")
2135-
2136-
manifestChangedMap := make(map[string]bool)
2137-
manifestChangedMap["sha"] = true
2138-
2139-
app.Status.Sync.ManifestsChanged = manifestChangedMap
2140-
2141-
attempted, _ := alreadyAttemptedSync(app, "sha", []string{}, false, manifestChangedMap)
2142-
assert.False(t, attempted)
2143-
})
2144-
2145-
t.Run("different manifest with sync result, with enabled flag", func(t *testing.T) {
2146-
_ = os.Setenv("PERSIST_CHANGE_REVISIONS", "1")
2147-
2148-
attempted, _ := alreadyAttemptedSync(app, "sha", []string{}, false, nil)
2149-
assert.False(t, attempted)
2150-
})
2151-
2152-
t.Run("different manifest with sync result, with enabled flag v2", func(t *testing.T) {
2153-
_ = os.Setenv("PERSIST_CHANGE_REVISIONS", "1")
2154-
2155-
manifestChangedMap := make(map[string]bool)
2156-
manifestChangedMap["sha"] = false
2157-
2158-
app.Status.Sync.ManifestsChanged = manifestChangedMap
2159-
2160-
attempted, _ := alreadyAttemptedSync(app, "sha", []string{}, false, manifestChangedMap)
2161-
assert.True(t, attempted)
2162-
})
2163-
}
2164-
21652099
func TestHelmValuesObjectHasReplaceStrategy(t *testing.T) {
21662100
app := v1alpha1.Application{
21672101
Status: v1alpha1.ApplicationStatus{Sync: v1alpha1.SyncStatus{ComparedTo: v1alpha1.ComparedTo{
@@ -2223,3 +2157,16 @@ func TestAppStatusIsReplaced(t *testing.T) {
22232157
require.True(t, has)
22242158
require.Nil(t, val)
22252159
}
2160+
2161+
func TestAlreadyAttemptSync(t *testing.T) {
2162+
app := newFakeApp()
2163+
t.Run("same manifest with sync result", func(t *testing.T) {
2164+
attempted, _ := alreadyAttemptedSync(app, "sha", []string{}, false, false)
2165+
assert.True(t, attempted)
2166+
})
2167+
2168+
t.Run("different manifest with sync result", func(t *testing.T) {
2169+
attempted, _ := alreadyAttemptedSync(app, "sha", []string{}, false, true)
2170+
assert.False(t, attempted)
2171+
})
2172+
}

0 commit comments

Comments
 (0)