MCPcopy
hub / github.com/argoproj/argo-cd / autoSync

Method autoSync

controller/appcontroller.go:2180–2327  ·  view source on GitHub ↗

autoSync will initiate a sync operation for an application configured with automated sync

(app *appv1.Application, syncStatus *appv1.SyncStatus, resources []appv1.ResourceStatus, shouldCompareRevisions bool)

Source from the content-addressed store, hash-verified

2178
2179// autoSync will initiate a sync operation for an application configured with automated sync
2180func (ctrl *ApplicationController) autoSync(app *appv1.Application, syncStatus *appv1.SyncStatus, resources []appv1.ResourceStatus, shouldCompareRevisions bool) (*appv1.ApplicationCondition, time.Duration) {
2181 logCtx := log.WithFields(applog.GetAppLogFields(app))
2182 ts := stats.NewTimingStats()
2183 defer func() {
2184 for k, v := range ts.Timings() {
2185 logCtx = logCtx.WithField(k, v.Milliseconds())
2186 }
2187 logCtx = logCtx.WithField("time_ms", time.Since(ts.StartTime).Milliseconds())
2188 logCtx.Debug("Finished auto sync")
2189 }()
2190 if app.Spec.SyncPolicy == nil || !app.Spec.SyncPolicy.IsAutomatedSyncEnabled() {
2191 return nil, 0
2192 }
2193
2194 if app.Operation != nil {
2195 logCtx.Infof("Skipping auto-sync: another operation is in progress")
2196 return nil, 0
2197 }
2198 if app.DeletionTimestamp != nil && !app.DeletionTimestamp.IsZero() {
2199 logCtx.Infof("Skipping auto-sync: deletion in progress")
2200 return nil, 0
2201 }
2202
2203 // Only perform auto-sync if we detect OutOfSync status. This is to prevent us from attempting
2204 // a sync when application is already in a Synced or Unknown state
2205 if syncStatus.Status != appv1.SyncStatusCodeOutOfSync {
2206 logCtx.Infof("Skipping auto-sync: application status is %s", syncStatus.Status)
2207 return nil, 0
2208 }
2209
2210 if !app.Spec.SyncPolicy.Automated.GetPrune() {
2211 requirePruneOnly := true
2212 for _, r := range resources {
2213 if r.Status != appv1.SyncStatusCodeSynced && !r.RequiresPruning {
2214 requirePruneOnly = false
2215 break
2216 }
2217 }
2218 if requirePruneOnly {
2219 logCtx.Infof("Skipping auto-sync: need to prune extra resources only but automated prune is disabled")
2220 return nil, 0
2221 }
2222 }
2223
2224 source := new(app.Spec.GetSource())
2225 desiredRevisions := []string{syncStatus.Revision}
2226 if app.Spec.HasMultipleSources() {
2227 source = nil
2228 desiredRevisions = syncStatus.Revisions
2229 }
2230
2231 op := appv1.Operation{
2232 Sync: &appv1.SyncOperation{
2233 Source: source,
2234 Revision: syncStatus.Revision,
2235 Prune: app.Spec.SyncPolicy.Automated.GetPrune(),
2236 SyncOptions: app.Spec.SyncPolicy.SyncOptions,
2237 Sources: app.Spec.Sources,

Callers 9

TestAutoSyncFunction · 0.80
TestAutoSyncAllowEmptyFunction · 0.80
TestSkipAutoSyncFunction · 0.80

Calls 15

TimingsMethod · 0.95
AddCheckpointMethod · 0.95
requestAppRefreshMethod · 0.95
writeBackToInformerMethod · 0.95
logAppEventMethod · 0.95
NewTimingStatsFunction · 0.92
SetAppOperationFunction · 0.92
alreadyAttemptedSyncFunction · 0.85
HasMultipleSourcesMethod · 0.80
SuccessfulMethod · 0.80

Tested by 8

TestAutoSyncFunction · 0.64
TestAutoSyncAllowEmptyFunction · 0.64
TestSkipAutoSyncFunction · 0.64