Skip to content

Commit

Permalink
chore: add logic to tell user if app was updated
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Helfand <helfand.4@gmail.com>
  • Loading branch information
danielhelfand committed Jul 15, 2022
1 parent 733cb0d commit 353fc24
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
23 changes: 18 additions & 5 deletions cmd/argocd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,23 @@ func NewApplicationCreateCommand(clientOpts *argocdclient.ClientOptions) *cobra.
Validate: &appOpts.Validate,
}

// Get app before creating to see if it is being updated or no change
existing, _ := appIf.Get(ctx, &applicationpkg.ApplicationQuery{Name: &app.Name})

created, err := appIf.Create(ctx, &appCreateRequest)
errors.CheckError(err)

if !hasAppChanged(appCreateRequest.Application, created, upsert) {
fmt.Printf("application '%s' unchanged\n", created.ObjectMeta.Name)
var action string
// App created for first time if generation==1
if existing == nil {
action = "created"
} else if !hasAppChanged(existing, created, upsert) {
action = "unchanged"
} else {
fmt.Printf("application '%s' created\n", created.ObjectMeta.Name)
action = "updated"
}

fmt.Printf("application '%s' %s\n", created.ObjectMeta.Name, action)
}
},
}
Expand Down Expand Up @@ -217,6 +226,11 @@ func getRefreshType(refresh bool, hardRefresh bool) *string {
}

func hasAppChanged(appReq, appRes *argoappv1.Application, upsert bool) bool {
// upsert==false means no way change occurred from create command
if !upsert {
return false
}

// If no project, assume default project
if appReq.Spec.Project == "" {
appReq.Spec.Project = "default"
Expand All @@ -235,8 +249,7 @@ func hasAppChanged(appReq, appRes *argoappv1.Application, upsert bool) bool {
if reflect.DeepEqual(appRes.Spec, appReq.Spec) &&
reflect.DeepEqual(appRes.Labels, appReq.Labels) &&
reflect.DeepEqual(appRes.ObjectMeta.Annotations, appReq.Annotations) &&
reflect.DeepEqual(appRes.Finalizers, appReq.Finalizers) &&
!upsert {
reflect.DeepEqual(appRes.Finalizers, appReq.Finalizers) {
return false
}

Expand Down
9 changes: 6 additions & 3 deletions cmd/argocd/commands/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,7 @@ func Test_hasAppChanged(t *testing.T) {
args: args{
appReq: testApp("foo", "default", map[string]string{}, map[string]string{}, []string{}),
appRes: testApp("foo", "foo", nil, nil, nil),
upsert: true,
},
want: true,
},
Expand All @@ -1179,6 +1180,7 @@ func Test_hasAppChanged(t *testing.T) {
args: args{
appReq: testApp("foo", "default", map[string]string{"foo": "bar"}, map[string]string{"foo": "bar"}, []string{"foo"}),
appRes: testApp("foo", "default", map[string]string{"foo": "bar"}, map[string]string{"foo": "bar"}, []string{"foo"}),
upsert: true,
},
want: false,
},
Expand All @@ -1187,6 +1189,7 @@ func Test_hasAppChanged(t *testing.T) {
args: args{
appReq: testApp("foo", "default", map[string]string{}, map[string]string{}, []string{}),
appRes: testApp("foo", "default", nil, nil, nil),
upsert: true,
},
want: false,
},
Expand All @@ -1199,13 +1202,13 @@ func Test_hasAppChanged(t *testing.T) {
want: false,
},
{
name: "App changed - From upsert",
name: "App unchanged - From upsert=false",
args: args{
appReq: testApp("foo", "foo", map[string]string{}, map[string]string{}, []string{}),
appRes: testApp("foo", "default", nil, nil, nil),
upsert: true,
upsert: false,
},
want: true,
want: false,
},
}
for _, tt := range tests {
Expand Down

0 comments on commit 353fc24

Please sign in to comment.