Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d47b81d
Add basic SyncOperationResource
Sep 28, 2018
3a1f36c
Tweak Resources
Sep 28, 2018
4c8e45f
Add resources to proto
Sep 28, 2018
4e3f81b
Update application proto
Sep 28, 2018
dd76964
Add SyncResources to controller
Oct 1, 2018
d10ec1d
Update types.go
Oct 1, 2018
624f233
Update generated files
Oct 1, 2018
23cdc29
Pass sync resources into syncContext
Oct 1, 2018
a7eb87a
Update controller
Oct 1, 2018
6a9cf12
Fix pointers, access of name/kind/group
Oct 2, 2018
b9d9ed6
Refactor slightly for clarity
Oct 2, 2018
255605d
Simplify match code a bit
Oct 2, 2018
5a2ee59
Add resources flag
Oct 2, 2018
08d0d9f
Support resources on CLI
Oct 2, 2018
d1be2a6
Process resources from args properly
Oct 2, 2018
8d46ce3
Reverse order of fields
Oct 2, 2018
b736e58
Invert match logic
Oct 2, 2018
27fae73
Fix typo
Oct 2, 2018
f691584
Fail generateSyncTasks if no tasks created
Oct 3, 2018
893d689
Pass resources to app state comparison
Oct 4, 2018
eab3e6f
Revert "Pass resources to app state comparison"
Oct 4, 2018
be7852c
Factor out check
Oct 4, 2018
703e4c5
Factor out further
Oct 4, 2018
d6e5907
Add filter logic to CLI
Oct 4, 2018
30ff2fe
Simplify resource identity checks
Oct 4, 2018
a9fd8e6
First draft of sync check
Oct 4, 2018
e4a4711
Remove unneeded sync checks
Oct 5, 2018
f8e392f
Fix missing formatting option
Oct 5, 2018
95c4276
Allow for nil liveObj or targetObj, thanks @alexmt
Oct 9, 2018
13a5ac6
Add JSON annotations, thanks @alexmt
Oct 9, 2018
9d9f5da
Add test for contains sync resource
Oct 9, 2018
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
58 changes: 42 additions & 16 deletions cmd/argocd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ func NewApplicationWaitCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co
conn, appIf := argocdclient.NewClientOrDie(clientOpts).NewApplicationClientOrDie()
defer util.Close(conn)

_, err := waitOnApplicationStatus(appIf, appName, timeout, watchSync, watchHealth, watchOperations)
_, err := waitOnApplicationStatus(appIf, appName, timeout, watchSync, watchHealth, watchOperations, nil)
errors.CheckError(err)
},
}
Expand Down Expand Up @@ -823,12 +823,17 @@ func printAppResources(w io.Writer, app *argoappv1.Application, showOperation bo
// NewApplicationSyncCommand returns a new instance of an `argocd app sync` command
func NewApplicationSyncCommand(clientOpts *argocdclient.ClientOptions) *cobra.Command {
var (
revision string
prune bool
dryRun bool
timeout uint
strategy string
force bool
revision string
resources *[]string
prune bool
dryRun bool
timeout uint
strategy string
force bool
)
const (
resourceFieldDelimiter = ":"
resourceFieldCount = 3
)
var command = &cobra.Command{
Use: "sync APPNAME",
Expand All @@ -841,11 +846,28 @@ func NewApplicationSyncCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co
conn, appIf := argocdclient.NewClientOrDie(clientOpts).NewApplicationClientOrDie()
defer util.Close(conn)
appName := args[0]
var syncResources []argoappv1.SyncOperationResource
if resources != nil {
syncResources = []argoappv1.SyncOperationResource{}
for _, r := range *resources {
fields := strings.Split(r, resourceFieldDelimiter)
if len(fields) != resourceFieldCount {
log.Fatalf("Resource should have GROUP%sKIND%sNAME, but instead got: %s", resourceFieldDelimiter, resourceFieldDelimiter, r)
}
rsrc := argoappv1.SyncOperationResource{
Group: fields[0],
Kind: fields[1],
Name: fields[2],
}
syncResources = append(syncResources, rsrc)
}
}
syncReq := application.ApplicationSyncRequest{
Name: &appName,
DryRun: dryRun,
Revision: revision,
Prune: prune,
Name: &appName,
DryRun: dryRun,
Revision: revision,
Resources: syncResources,
Prune: prune,
}
switch strategy {
case "apply":
Expand All @@ -861,7 +883,7 @@ func NewApplicationSyncCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co
_, err := appIf.Sync(ctx, &syncReq)
errors.CheckError(err)

app, err := waitOnApplicationStatus(appIf, appName, timeout, false, false, true)
app, err := waitOnApplicationStatus(appIf, appName, timeout, false, false, true, syncResources)
errors.CheckError(err)

pruningRequired := 0
Expand All @@ -882,6 +904,7 @@ func NewApplicationSyncCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co
command.Flags().BoolVar(&dryRun, "dry-run", false, "Preview apply without affecting cluster")
command.Flags().BoolVar(&prune, "prune", false, "Allow deleting unexpected resources")
command.Flags().StringVar(&revision, "revision", "", "Sync to a specific revision. Preserves parameter overrides")
resources = command.Flags().StringArray("resource", nil, fmt.Sprintf("Sync only specific resources as GROUP%sKIND%sNAME. Fields may be blank. This option may be specified repeatedly", resourceFieldDelimiter, resourceFieldDelimiter))
command.Flags().UintVar(&timeout, "timeout", defaultCheckTimeoutSeconds, "Time out after this many seconds")
command.Flags().StringVar(&strategy, "strategy", "", "Sync strategy (one of: apply|hook)")
command.Flags().BoolVar(&force, "force", false, "Use a force apply")
Expand Down Expand Up @@ -936,7 +959,7 @@ func (rs *resourceState) Merge(newState *resourceState) bool {
return updated
}

func calculateResourceStates(app *argoappv1.Application) map[string]*resourceState {
func calculateResourceStates(app *argoappv1.Application, syncResources []argoappv1.SyncOperationResource) map[string]*resourceState {
resStates := make(map[string]*resourceState)
for _, res := range app.Status.ComparisonResult.Resources {
obj, err := argoappv1.UnmarshalToUnstructured(res.TargetState)
Expand All @@ -945,6 +968,9 @@ func calculateResourceStates(app *argoappv1.Application) map[string]*resourceSta
obj, err = argoappv1.UnmarshalToUnstructured(res.LiveState)
errors.CheckError(err)
}
if syncResources != nil && !argo.ContainsSyncResource(obj, syncResources) {
continue
}
newState := newResourceState(obj.GetKind(), obj.GetName(), string(res.Status), res.Health.Status, "", "")
key := newState.Key()
if prev, ok := resStates[key]; ok {
Expand Down Expand Up @@ -988,7 +1014,7 @@ func calculateResourceStates(app *argoappv1.Application) map[string]*resourceSta
return resStates
}

func waitOnApplicationStatus(appClient application.ApplicationServiceClient, appName string, timeout uint, watchSync, watchHealth, watchOperation bool) (*argoappv1.Application, error) {
func waitOnApplicationStatus(appClient application.ApplicationServiceClient, appName string, timeout uint, watchSync bool, watchHealth bool, watchOperation bool, syncResources []argoappv1.SyncOperationResource) (*argoappv1.Application, error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down Expand Up @@ -1045,7 +1071,7 @@ func waitOnApplicationStatus(appClient application.ApplicationServiceClient, app
return app, nil
}

newStates := calculateResourceStates(app)
newStates := calculateResourceStates(app, syncResources)
for _, newState := range newStates {
var doPrint bool
stateKey := newState.Key()
Expand Down Expand Up @@ -1212,7 +1238,7 @@ func NewApplicationRollbackCommand(clientOpts *argocdclient.ClientOptions) *cobr
})
errors.CheckError(err)

_, err = waitOnApplicationStatus(appIf, appName, timeout, false, false, true)
_, err = waitOnApplicationStatus(appIf, appName, timeout, false, false, true, nil)
errors.CheckError(err)
},
}
Expand Down
17 changes: 12 additions & 5 deletions controller/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type syncContext struct {
namespace string
syncOp *appv1.SyncOperation
syncRes *appv1.SyncOperationResult
syncResources []appv1.SyncOperationResource
opState *appv1.OperationState
manifestInfo *repository.ManifestResponse
log *log.Entry
Expand All @@ -55,10 +56,12 @@ func (s *appStateManager) SyncAppState(app *appv1.Application, state *appv1.Oper
var revision string
var syncOp appv1.SyncOperation
var syncRes *appv1.SyncOperationResult
var syncResources []appv1.SyncOperationResource
var overrides []appv1.ComponentParameter

if state.Operation.Sync != nil {
syncOp = *state.Operation.Sync
syncResources = syncOp.Resources
overrides = []appv1.ComponentParameter(state.Operation.Sync.ParameterOverrides)
if state.SyncResult != nil {
syncRes = state.SyncResult
Expand Down Expand Up @@ -107,6 +110,7 @@ func (s *appStateManager) SyncAppState(app *appv1.Application, state *appv1.Oper
// Take the value in the requested operation. We will resolve this to a SHA later.
revision = syncOp.Revision
}

comparison, manifestInfo, conditions, err := s.CompareAppState(app, revision, overrides)
if err != nil {
state.Phase = appv1.OperationError
Expand Down Expand Up @@ -162,6 +166,7 @@ func (s *appStateManager) SyncAppState(app *appv1.Application, state *appv1.Oper
namespace: app.Spec.Destination.Namespace,
syncOp: &syncOp,
syncRes: syncRes,
syncResources: syncResources,
opState: state,
manifestInfo: manifestInfo,
log: log.WithFields(log.Fields{"application": app.Name}),
Expand Down Expand Up @@ -262,13 +267,15 @@ func (sc *syncContext) generateSyncTasks() ([]syncTask, bool) {
sc.setOperationPhase(appv1.OperationError, fmt.Sprintf("Failed to unmarshal target object: %v", err))
return nil, false
}
syncTask := syncTask{
liveObj: liveObj,
targetObj: targetObj,
if sc.syncResources == nil || argo.ContainsSyncResource(liveObj, sc.syncResources) || argo.ContainsSyncResource(targetObj, sc.syncResources) {
syncTask := syncTask{
liveObj: liveObj,
targetObj: targetObj,
}
syncTasks = append(syncTasks, syncTask)
}
syncTasks = append(syncTasks, syncTask)
}
return syncTasks, true
return syncTasks, len(syncTasks) > 0
}

// startedPreSyncPhase detects if we already started the PreSync stage of a sync operation.
Expand Down
Loading