Repository: argoproj-labs/applicationset
Pull request #10 (2020-07-09 17:03:50)

update applications & add owner reference


Patch:
diff --git a/pkg/controllers/applicationset_controller.go b/pkg/controllers/applicationset_controller.go
index 53acd53..191ddaf 100644
--- a/pkg/controllers/applicationset_controller.go
+++ b/pkg/controllers/applicationset_controller.go
@@ -23,6 +23,7 @@ import (
 	corev1 "k8s.io/api/core/v1"
 	"k8s.io/client-go/tools/record"
 	"k8s.io/kubernetes/pkg/apis/core"
+	"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
 	"sigs.k8s.io/controller-runtime/pkg/handler"
 	"sigs.k8s.io/controller-runtime/pkg/source"
 
@@ -102,17 +103,30 @@ func (r *ApplicationSetReconciler) createApplications(ctx context.Context, appli
 
 	for _, app := range appList {
 		app.Namespace = applicationSetInfo.Namespace
-		if err := r.Client.Create(ctx, &app); err != nil {
-			log.Error(err, fmt.Sprintf("failed to create Application %s resource for applicationSet %s", app.Name, applicationSetInfo.Name))
+
+		found := app
+		action, err := ctrl.CreateOrUpdate(ctx, r.Client, &found, func() error {
+			found.Spec = app.Spec
+			return controllerutil.SetControllerReference(&applicationSetInfo, &found, r.Scheme)
+		})
+
+		if err != nil {
+			log.Error(err, fmt.Sprintf("failed to get Application %s resource for applicationSet %s", app.Name, applicationSetInfo.Name))
 			continue
 		}
 
 		r.Recorder.Eventf(&applicationSetInfo, core.EventTypeNormal, "Created", "Created Application %q", app.Name)
-		log.Infof("created Application %s resource for applicationSet %s", app.Name, applicationSetInfo.Name)
+		log.Infof("%s Application %s resource for applicationSet %s", action, app.Name, applicationSetInfo.Name)
 	}
 
 	return nil
 
 }
 
+func (r *ApplicationSetReconciler) getApplications(ctx context.Context, applicationSetInfo argoprojiov1alpha1.ApplicationSet) ([]argov1alpha1.Application, error) {
+
+	return nil, nil
+
+}
+
 var _ handler.EventHandler = &clusterSecretEventHandler{}


Test patch:
diff --git a/pkg/controllers/applicationset_controller_test.go b/pkg/controllers/applicationset_controller_test.go
index 33fd0ea..cd050e6 100644
--- a/pkg/controllers/applicationset_controller_test.go
+++ b/pkg/controllers/applicationset_controller_test.go
@@ -8,30 +8,24 @@ import (
 	"k8s.io/apimachinery/pkg/runtime"
 	"k8s.io/client-go/tools/record"
 	crtclient "sigs.k8s.io/controller-runtime/pkg/client"
+	"sigs.k8s.io/controller-runtime/pkg/client/fake"
+	"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
 	"testing"
 
 	argoprojiov1alpha1 "github.com/argoproj-labs/applicationset/api/v1alpha1"
-
-	"sigs.k8s.io/controller-runtime/pkg/client/fake"
 )
 
 func TestCreateApplications(t *testing.T) {
 
 	scheme := runtime.NewScheme()
+	argoprojiov1alpha1.AddToScheme(scheme)
 	argov1alpha1.AddToScheme(scheme)
 
-	client := fake.NewFakeClientWithScheme(scheme)
-
-	r := ApplicationSetReconciler{
-		Client:   client,
-		Scheme:   scheme,
-		Recorder: record.NewFakeRecorder(1),
-	}
-
 	for _, c := range []struct {
-		appSet   argoprojiov1alpha1.ApplicationSet
-		apps     []argov1alpha1.Application
-		expected []argov1alpha1.Application
+		appSet     argoprojiov1alpha1.ApplicationSet
+		existsApps []argov1alpha1.Application
+		apps       []argov1alpha1.Application
+		expected   []argov1alpha1.Application
 	}{
 		{
 			appSet: argoprojiov1alpha1.ApplicationSet{
@@ -40,6 +34,7 @@ func TestCreateApplications(t *testing.T) {
 					Namespace: "namespace",
 				},
 			},
+			existsApps: nil,
 			apps: []argov1alpha1.Application{
 				{
 					ObjectMeta: metav1.ObjectMeta{
@@ -61,17 +56,90 @@ func TestCreateApplications(t *testing.T) {
 				},
 			},
 		},
+		{
+			appSet: argoprojiov1alpha1.ApplicationSet{
+				ObjectMeta: metav1.ObjectMeta{
+					Name:      "name",
+					Namespace: "namespace",
+				},
+				Spec: argoprojiov1alpha1.ApplicationSetSpec{
+					Template: argoprojiov1alpha1.ApplicationSetTemplate{
+						Spec: argov1alpha1.ApplicationSpec{
+							Project: "project",
+						},
+					},
+				},
+			},
+			existsApps: []argov1alpha1.Application{
+				argov1alpha1.Application{
+					TypeMeta: metav1.TypeMeta{
+						Kind:       "Application",
+						APIVersion: "argoproj.io/v1alpha1",
+					},
+					ObjectMeta: metav1.ObjectMeta{
+						Name:            "app1",
+						Namespace:       "namespace",
+						ResourceVersion: "2",
+					},
+					Spec: argov1alpha1.ApplicationSpec{
+						Project: "test",
+					},
+				},
+			},
+			apps: []argov1alpha1.Application{
+				{
+					ObjectMeta: metav1.ObjectMeta{
+						Name: "app1",
+					},
+					Spec: argov1alpha1.ApplicationSpec{
+						Project: "project",
+					},
+				},
+			},
+			expected: []argov1alpha1.Application{
+				{
+					TypeMeta: metav1.TypeMeta{
+						Kind:       "Application",
+						APIVersion: "argoproj.io/v1alpha1",
+					},
+					ObjectMeta: metav1.ObjectMeta{
+						Name:            "app1",
+						Namespace:       "namespace",
+						ResourceVersion: "3",
+					},
+					Spec: argov1alpha1.ApplicationSpec{
+						Project: "project",
+					},
+				},
+			},
+		},
 	} {
+		initObjs := []runtime.Object{&c.appSet}
+		for _, a := range c.existsApps {
+			controllerutil.SetControllerReference(&c.appSet, &a, scheme)
+			initObjs = append(initObjs, &a)
+		}
+
+		client := fake.NewFakeClientWithScheme(scheme, initObjs...)
+
+		r := ApplicationSetReconciler{
+			Client:   client,
+			Scheme:   scheme,
+			Recorder: record.NewFakeRecorder(1),
+		}
+
 		r.createApplications(context.TODO(), c.appSet, c.apps)
 
-		for _, e := range c.expected {
+		for _, obj := range c.expected {
 			got := &argov1alpha1.Application{}
 			_ = client.Get(context.Background(), crtclient.ObjectKey{
-				Namespace: e.Namespace,
-				Name:      e.Name,
+				Namespace: obj.Namespace,
+				Name:      obj.Name,
 			}, got)
 
-			assert.Equal(t, e, *got)
+			controllerutil.SetControllerReference(&c.appSet, &obj, r.Scheme)
+
+			assert.Equal(t, obj, *got)
 		}
 	}