Repository: argoproj-labs/applicationset (go)

Issue:
Add shortnames to the ApplicationSet CRD
Add `appset`/`appsets` as shortnames to the CRD resource, making for less work when using `kubectl`/`oc` :smile: 

Pull request:
Add shortnames to the ApplicationSet CRD
Courtesy of @christianh814 via https://github.com/argoproj-labs/applicationset/pull/184#issue-607674456 who submitted the original PR

Fixes #196 

Patch:
diff --git a/api/v1alpha1/applicationset_types.go b/api/v1alpha1/applicationset_types.go
index ed4ed1b..4c879cf 100644
--- a/api/v1alpha1/applicationset_types.go
+++ b/api/v1alpha1/applicationset_types.go
@@ -23,6 +23,7 @@ import (
 
 // ApplicationSet is a set of Application resources
 // +kubebuilder:object:root=true
+// +kubebuilder:resource:path=applicationsets,shortName=appset;appsets
 type ApplicationSet struct {
 	metav1.TypeMeta   `json:",inline"`
 	metav1.ObjectMeta `json:"metadata"`
diff --git a/manifests/crds/argoproj.io_applicationsets.yaml b/manifests/crds/argoproj.io_applicationsets.yaml
index e15bc0f..c0bc97c 100644
--- a/manifests/crds/argoproj.io_applicationsets.yaml
+++ b/manifests/crds/argoproj.io_applicationsets.yaml
@@ -13,6 +13,9 @@ spec:
     kind: ApplicationSet
     listKind: ApplicationSetList
     plural: applicationsets
+    shortNames:
+    - appset
+    - appsets
     singular: applicationset
   scope: Namespaced
   versions:
diff --git a/manifests/install-with-argo-cd.yaml b/manifests/install-with-argo-cd.yaml
index a5b8ea6..1141748 100644
--- a/manifests/install-with-argo-cd.yaml
+++ b/manifests/install-with-argo-cd.yaml
@@ -1773,6 +1773,9 @@ spec:
     kind: ApplicationSet
     listKind: ApplicationSetList
     plural: applicationsets
+    shortNames:
+    - appset
+    - appsets
     singular: applicationset
   scope: Namespaced
   versions:
diff --git a/manifests/install.yaml b/manifests/install.yaml
index d88632b..ac0df8f 100644
--- a/manifests/install.yaml
+++ b/manifests/install.yaml
@@ -12,6 +12,9 @@ spec:
     kind: ApplicationSet
     listKind: ApplicationSetList
     plural: applicationsets
+    shortNames:
+    - appset
+    - appsets
     singular: applicationset
   scope: Namespaced
   versions:
diff --git a/pkg/controllers/applicationset_controller.go b/pkg/controllers/applicationset_controller.go
index 7c659f4..d1064da 100644
--- a/pkg/controllers/applicationset_controller.go
+++ b/pkg/controllers/applicationset_controller.go
@@ -53,6 +53,13 @@ import (
 	"github.com/imdario/mergo"
 )
 
+const (
+	// Rather than importing the whole argocd-notifications controller, just copying the const here
+	//   https://github.com/argoproj-labs/argocd-notifications/blob/33d345fa838829bb50fca5c08523aba380d2c12b/pkg/controller/subscriptions.go#L12
+	//   https://github.com/argoproj-labs/argocd-notifications/blob/33d345fa838829bb50fca5c08523aba380d2c12b/pkg/controller/state.go#L17
+	NotifiedAnnotationKey = "notified.notifications.argoproj.io"
+)
+
 // ApplicationSetReconciler reconciles a ApplicationSet object
 type ApplicationSetReconciler struct {
 	client.Client
@@ -438,7 +445,16 @@ func (r *ApplicationSetReconciler) createOrUpdateInCluster(ctx context.Context,
 		action, err := utils.CreateOrUpdate(ctx, r.Client, found, func() error {
 			// Copy only the Application/ObjectMeta fields that are significant, from the generatedApp
 			found.Spec = generatedApp.Spec
+
+			// Preserve argo cd notifications state (https://github.com/argoproj-labs/applicationset/issues/180)
+			if state, exists := found.ObjectMeta.Annotations[NotifiedAnnotationKey]; exists {
+				if generatedApp.Annotations == nil {
+					generatedApp.Annotations = map[string]string{}
+				}
+				generatedApp.Annotations[NotifiedAnnotationKey] = state
+			}
 			found.ObjectMeta.Annotations = generatedApp.Annotations
+
 			found.ObjectMeta.Finalizers = generatedApp.Finalizers
 			found.ObjectMeta.Labels = generatedApp.Labels
 			return controllerutil.SetControllerReference(&applicationSet, found, r.Scheme)


Test patch:
diff --git a/pkg/controllers/applicationset_controller_test.go b/pkg/controllers/applicationset_controller_test.go
index 3c4c78c..723435a 100644
--- a/pkg/controllers/applicationset_controller_test.go
+++ b/pkg/controllers/applicationset_controller_test.go
@@ -736,6 +736,72 @@ func TestCreateOrUpdateInCluster(t *testing.T) {
 				},
 			},
 		},
+		{
+			name: "Ensure that argocd notifications state annotation is preserved from an existing app",
+			appSet: argoprojiov1alpha1.ApplicationSet{
+				ObjectMeta: metav1.ObjectMeta{
+					Name:      "name",
+					Namespace: "namespace",
+				},
+				Spec: argoprojiov1alpha1.ApplicationSetSpec{
+					Template: argoprojiov1alpha1.ApplicationSetTemplate{
+						Spec: argov1alpha1.ApplicationSpec{
+							Project: "project",
+						},
+					},
+				},
+			},
+			existingApps: []argov1alpha1.Application{
+				{
+					TypeMeta: metav1.TypeMeta{
+						Kind:       "Application",
+						APIVersion: "argoproj.io/v1alpha1",
+					},
+					ObjectMeta: metav1.ObjectMeta{
+						Name:            "app1",
+						Namespace:       "namespace",
+						ResourceVersion: "2",
+						Labels:          map[string]string{"label-key": "label-value"},
+						Annotations: map[string]string{
+							"annot-key":           "annot-value",
+							NotifiedAnnotationKey: `{"b620d4600c771a6f4cxxxxxxx:on-deployed:[0].y7b5sbwa2Q329JYHxxxxxx-fBs:slack:slack-test":1617144614}`,
+						},
+					},
+					Spec: argov1alpha1.ApplicationSpec{
+						Project: "project",
+					},
+				},
+			},
+			desiredApps: []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",
+						Annotations: map[string]string{
+							NotifiedAnnotationKey: `{"b620d4600c771a6f4cxxxxxxx:on-deployed:[0].y7b5sbwa2Q329JYHxxxxxx-fBs:slack:slack-test":1617144614}`,
+						},
+					},
+					Spec: argov1alpha1.ApplicationSpec{
+						Project: "project",
+					},
+				},
+			},
+		},
 	} {
 
 		t.Run(c.name, func(t *testing.T) {