Skip to content

Commit

Permalink
fix(appset): normalize app spec before applying (#14481) (#14555)
Browse files Browse the repository at this point in the history
* fix(appset): normalize app spec before applying



* fix nil ref, add test



* fix another test



---------

Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>
Co-authored-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com>
  • Loading branch information
gcp-cherry-pick-bot[bot] and crenshaw-dev committed Jul 17, 2023
1 parent da5dab7 commit 697f2d4
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 28 deletions.
3 changes: 3 additions & 0 deletions applicationset/controllers/applicationset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,9 @@ func (r *ApplicationSetReconciler) createOrUpdateInCluster(ctx context.Context,
appLog := log.WithFields(log.Fields{"app": generatedApp.Name, "appSet": applicationSet.Name})
generatedApp.Namespace = applicationSet.Namespace

// Normalize to avoid fighting with the application controller.
generatedApp.Spec = *argoutil.NormalizeApplicationSpec(&generatedApp.Spec)

found := &argov1alpha1.Application{
ObjectMeta: metav1.ObjectMeta{
Name: generatedApp.Name,
Expand Down
119 changes: 92 additions & 27 deletions applicationset/controllers/applicationset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ func TestCreateOrUpdateInCluster(t *testing.T) {
Namespace: "namespace",
ResourceVersion: "1",
},
Spec: v1alpha1.ApplicationSpec{Project: "default"},
},
},
},
Expand Down Expand Up @@ -892,6 +893,60 @@ func TestCreateOrUpdateInCluster(t *testing.T) {
},
},
},
}, {
name: "Ensure that the app spec is normalized before applying",
appSet: v1alpha1.ApplicationSet{
ObjectMeta: metav1.ObjectMeta{
Name: "name",
Namespace: "namespace",
},
Spec: v1alpha1.ApplicationSetSpec{
Template: v1alpha1.ApplicationSetTemplate{
Spec: v1alpha1.ApplicationSpec{
Project: "project",
Source: &v1alpha1.ApplicationSource{
Directory: &v1alpha1.ApplicationSourceDirectory{
Jsonnet: v1alpha1.ApplicationSourceJsonnet{},
},
},
},
},
},
},
desiredApps: []v1alpha1.Application{
{
ObjectMeta: metav1.ObjectMeta{
Name: "app1",
},
Spec: v1alpha1.ApplicationSpec{
Project: "project",
Source: &v1alpha1.ApplicationSource{
Directory: &v1alpha1.ApplicationSourceDirectory{
Jsonnet: v1alpha1.ApplicationSourceJsonnet{},
},
},
},
},
},
expected: []v1alpha1.Application{
{
TypeMeta: metav1.TypeMeta{
Kind: "Application",
APIVersion: "argoproj.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "app1",
Namespace: "namespace",
ResourceVersion: "1",
},
Spec: v1alpha1.ApplicationSpec{
Project: "project",
Source: &v1alpha1.ApplicationSource{
// Directory and jsonnet block are removed
},
},
},
},
},
} {

Expand Down Expand Up @@ -1223,13 +1278,15 @@ func TestCreateApplications(t *testing.T) {
err = v1alpha1.AddToScheme(scheme)
assert.Nil(t, err)

for _, c := range []struct {
testCases := []struct {
name string
appSet v1alpha1.ApplicationSet
existsApps []v1alpha1.Application
apps []v1alpha1.Application
expected []v1alpha1.Application
}{
{
name: "no existing apps",
appSet: v1alpha1.ApplicationSet{
ObjectMeta: metav1.ObjectMeta{
Name: "name",
Expand All @@ -1255,10 +1312,14 @@ func TestCreateApplications(t *testing.T) {
Namespace: "namespace",
ResourceVersion: "1",
},
Spec: v1alpha1.ApplicationSpec{
Project: "default",
},
},
},
},
{
name: "existing apps",
appSet: v1alpha1.ApplicationSet{
ObjectMeta: metav1.ObjectMeta{
Name: "name",
Expand Down Expand Up @@ -1316,6 +1377,7 @@ func TestCreateApplications(t *testing.T) {
},
},
{
name: "existing apps with different project",
appSet: v1alpha1.ApplicationSet{
ObjectMeta: metav1.ObjectMeta{
Name: "name",
Expand Down Expand Up @@ -1372,39 +1434,42 @@ func TestCreateApplications(t *testing.T) {
},
},
},
} {
initObjs := []crtclient.Object{&c.appSet}
for _, a := range c.existsApps {
err = controllerutil.SetControllerReference(&c.appSet, &a, scheme)
assert.Nil(t, err)
initObjs = append(initObjs, &a)
}

client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(initObjs...).Build()
}

r := ApplicationSetReconciler{
Client: client,
Scheme: scheme,
Recorder: record.NewFakeRecorder(len(initObjs) + len(c.expected)),
}
for _, c := range testCases {
t.Run(c.name, func(t *testing.T) {
initObjs := []crtclient.Object{&c.appSet}
for _, a := range c.existsApps {
err = controllerutil.SetControllerReference(&c.appSet, &a, scheme)
assert.Nil(t, err)
initObjs = append(initObjs, &a)
}

err = r.createInCluster(context.TODO(), c.appSet, c.apps)
assert.Nil(t, err)
client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(initObjs...).Build()

for _, obj := range c.expected {
got := &v1alpha1.Application{}
_ = client.Get(context.Background(), crtclient.ObjectKey{
Namespace: obj.Namespace,
Name: obj.Name,
}, got)
r := ApplicationSetReconciler{
Client: client,
Scheme: scheme,
Recorder: record.NewFakeRecorder(len(initObjs) + len(c.expected)),
}

err = controllerutil.SetControllerReference(&c.appSet, &obj, r.Scheme)
err = r.createInCluster(context.TODO(), c.appSet, c.apps)
assert.Nil(t, err)

assert.Equal(t, obj, *got)
}
}
for _, obj := range c.expected {
got := &v1alpha1.Application{}
_ = client.Get(context.Background(), crtclient.ObjectKey{
Namespace: obj.Namespace,
Name: obj.Name,
}, got)

err = controllerutil.SetControllerReference(&c.appSet, &obj, r.Scheme)
assert.Nil(t, err)

assert.Equal(t, obj, *got)
}
})
}
}

func TestDeleteInCluster(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion util/argo/argo.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,8 @@ func NormalizeApplicationSpec(spec *argoappv1.ApplicationSpec) *argoappv1.Applic
for _, source := range spec.Sources {
NormalizeSource(&source)
}
} else {
} else if spec.Source != nil {
// In practice, spec.Source should never be nil.
NormalizeSource(spec.Source)
}
return spec
Expand Down

0 comments on commit 697f2d4

Please sign in to comment.