Skip to content

Commit

Permalink
feat: add projects filter to app sync command (argoproj#8320) (argopr…
Browse files Browse the repository at this point in the history
…oj#10133)

* feat: add projects filter to app sync command

Signed-off-by: Chetan Banavikalmutt <[email protected]>

* test: add e2e sync by project

Signed-off-by: Justin Watkinson <[email protected]>

* Retrigger CI pipeline

Signed-off-by: Justin Watkinson <[email protected]>

Co-authored-by: Chetan Banavikalmutt <[email protected]>
  • Loading branch information
2 people authored and ashutosh16 committed Aug 11, 2022
1 parent 33d4618 commit 8b4747c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
22 changes: 17 additions & 5 deletions cmd/argocd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1389,9 +1389,10 @@ func NewApplicationSyncCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co
infos []string
diffChanges bool
diffChangesConfirm bool
projects []string
)
var command = &cobra.Command{
Use: "sync [APPNAME... | -l selector]",
Use: "sync [APPNAME... | -l selector | --project project-name]",
Short: "Sync an application to its target state",
Example: ` # Sync an app
argocd app sync my-app
Expand All @@ -1411,7 +1412,8 @@ func NewApplicationSyncCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co
Run: func(c *cobra.Command, args []string) {
ctx := c.Context()

if len(args) == 0 && selector == "" {
if len(args) == 0 && selector == "" && len(projects) == 0 {

c.HelpFunc()(c, args)
os.Exit(1)
}
Expand All @@ -1423,13 +1425,22 @@ func NewApplicationSyncCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co
errors.CheckError(err)

appNames := args
if selector != "" {
list, err := appIf.List(ctx, &applicationpkg.ApplicationQuery{Selector: pointer.String(selector)})
if selector != "" || len(projects) > 0 {
list, err := appIf.List(ctx, &applicationpkg.ApplicationQuery{Selector: pointer.String(selector), Projects: projects})
errors.CheckError(err)

// unlike list, we'd want to fail if nothing was found
if len(list.Items) == 0 {
log.Fatalf("no apps match selector %v", selector)
errMsg := "No matching apps found for filter:"
if selector != "" {
errMsg += fmt.Sprintf(" selector %s", selector)
}
if len(projects) != 0 {
errMsg += fmt.Sprintf(" projects %v", projects)
}
log.Fatalf(errMsg)
}

for _, i := range list.Items {
appNames = append(appNames, i.Name)
}
Expand Down Expand Up @@ -1612,6 +1623,7 @@ func NewApplicationSyncCommand(clientOpts *argocdclient.ClientOptions) *cobra.Co
command.Flags().StringArrayVar(&infos, "info", []string{}, "A list of key-value pairs during sync process. These infos will be persisted in app.")
command.Flags().BoolVar(&diffChangesConfirm, "assumeYes", false, "Assume yes as answer for all user queries or prompts")
command.Flags().BoolVar(&diffChanges, "preview-changes", false, "Preview difference against the target and live state before syncing app and wait for user confirmation")
command.Flags().StringArrayVar(&projects, "project", []string{}, "Sync apps that belong to the specified projects. This option may be specified repeatedly.")
return command
}

Expand Down
3 changes: 2 additions & 1 deletion docs/user-guide/commands/argocd_app_sync.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Sync an application to its target state

```
argocd app sync [APPNAME... | -l selector] [flags]
argocd app sync [APPNAME... | -l selector | --project project-name] [flags]
```

### Examples
Expand Down Expand Up @@ -39,6 +39,7 @@ argocd app sync [APPNAME... | -l selector] [flags]
--local string Path to a local directory. When this flag is present no git queries will be made
--local-repo-root string Path to the repository root. Used together with --local allows setting the repository root (default "/")
--preview-changes Preview difference against the target and live state before syncing app and wait for user confirmation
--project stringArray Sync apps that belong to the specified projects. This option may be specified repeatedly.
--prune Allow deleting unexpected resources
--replace Use a kubectl create/replace instead apply
--resource stringArray Sync only specific resources as GROUP:KIND:NAME. Fields may be blank. This option may be specified repeatedly
Expand Down
20 changes: 19 additions & 1 deletion test/e2e/app_management_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ func TestAppLabels(t *testing.T) {
Sync("-l", "foo=rubbish").
DoNotIgnoreErrors().
Then().
Expect(Error("", "no apps match selector foo=rubbish")).
Expect(Error("", "No matching apps found for filter: selector foo=rubbish")).
// check we can update the app and it is then sync'd
Given().
When().
Expand Down Expand Up @@ -938,6 +938,24 @@ func TestSyncResourceByLabel(t *testing.T) {
})
}

func TestSyncResourceByProject(t *testing.T) {
Given(t).
Path(guestbookPath).
When().
CreateApp().
Sync().
Then().
And(func(app *Application) {
_, _ = RunCli("app", "sync", app.Name, "--project", app.Spec.Project)
}).
Expect(SyncStatusIs(SyncStatusCodeSynced)).
And(func(app *Application) {
_, err := RunCli("app", "sync", app.Name, "--project", "this-project-does-not-exist")
assert.Error(t, err)
assert.Contains(t, err.Error(), "level=fatal")
})
}

func TestLocalManifestSync(t *testing.T) {
Given(t).
Path(guestbookPath).
Expand Down

0 comments on commit 8b4747c

Please sign in to comment.