Skip to content

Commit

Permalink
feat(appset): Add argocd.argoproj.io/application-set-name to rendered…
Browse files Browse the repository at this point in the history
… appset applications (argoproj#13456)

* Add app.kubernetes.io/appset-instance to rendered applications

Signed-off-by: Mmadu Manasseh <mmadumanasseh@gmail.com>

* initialize labels map if nil

Signed-off-by: Mmadu Manasseh <mmadumanasseh@gmail.com>

* Fix tests

Signed-off-by: Mmadu Manasseh <mmadumanasseh@gmail.com>

* Fix tests

Signed-off-by: Mmadu Manasseh <mmadumanasseh@gmail.com>

* Fix tests

Signed-off-by: Mmadu Manasseh <mmadumanasseh@gmail.com>

* fix tests

Signed-off-by: Mmadu Manasseh <mmadumanasseh@gmail.com>

* Fix tests

Signed-off-by: Mmadu Manasseh <mmadumanasseh@gmail.com>

* Updated label ->argocd.argoproj.io/application-set-name

Signed-off-by: Mmadu Manasseh <mmadumanasseh@gmail.com>

---------

Signed-off-by: Mmadu Manasseh <mmadumanasseh@gmail.com>
  • Loading branch information
MeNsaaH authored and xiaowu.zhu committed Aug 9, 2023
1 parent 2fba47a commit 4e06b84
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 31 deletions.
8 changes: 8 additions & 0 deletions applicationset/controllers/applicationset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ const (
// https://github.com/argoproj-labs/argocd-notifications/blob/33d345fa838829bb50fca5c08523aba380d2c12b/pkg/controller/state.go#L17
NotifiedAnnotationKey = "notified.notifications.argoproj.io"
ReconcileRequeueOnValidationError = time.Minute * 3

// LabelKeyAppSetInstance is the label key to use to uniquely identify the apps of an applicationset
// The ArgoCD applicationset name is used as the instance name
LabelKeyAppSetInstance = "argocd.argoproj.io/application-set-name"
)

var (
Expand Down Expand Up @@ -504,6 +508,10 @@ func (r *ApplicationSetReconciler) generateApplications(applicationSetInfo argov

for _, a := range t {
tmplApplication := getTempApplication(a.Template)
if tmplApplication.Labels == nil {
tmplApplication.Labels = make(map[string]string)
}
tmplApplication.Labels[LabelKeyAppSetInstance] = applicationSetInfo.Name

for _, p := range a.Params {
app, err := r.Renderer.RenderTemplateParams(tmplApplication, applicationSetInfo.Spec.SyncPolicy, p, applicationSetInfo.Spec.GoTemplate)
Expand Down
38 changes: 23 additions & 15 deletions applicationset/controllers/applicationset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,14 @@ func TestExtractApplications(t *testing.T) {
if cc.generateParamsError == nil {
for _, p := range cc.params {

tmpApplication := getTempApplication(cc.template)
tmpApplication.Labels[LabelKeyAppSetInstance] = appSet.Name

if cc.rendererError != nil {
rendererMock.On("RenderTemplateParams", getTempApplication(cc.template), p, false).
rendererMock.On("RenderTemplateParams", tmpApplication, p, false).
Return(nil, cc.rendererError)
} else {
rendererMock.On("RenderTemplateParams", getTempApplication(cc.template), p, false).
rendererMock.On("RenderTemplateParams", tmpApplication, p, false).
Return(&app, nil)
expectedApps = append(expectedApps, app)
}
Expand Down Expand Up @@ -285,7 +288,21 @@ func TestMergeTemplateApplications(t *testing.T) {

rendererMock := rendererMock{}

rendererMock.On("RenderTemplateParams", getTempApplication(cc.expectedMerged), cc.params[0], false).
appSet := &v1alpha1.ApplicationSet{
ObjectMeta: metav1.ObjectMeta{
Name: "name",
Namespace: "namespace",
},
Spec: v1alpha1.ApplicationSetSpec{
Generators: []v1alpha1.ApplicationSetGenerator{generator},
Template: cc.template,
},
}

tmpApplication := getTempApplication(cc.expectedMerged)
tmpApplication.Labels[LabelKeyAppSetInstance] = appSet.Name

rendererMock.On("RenderTemplateParams", tmpApplication, cc.params[0], false).
Return(&cc.expectedApps[0], nil)

r := ApplicationSetReconciler{
Expand All @@ -299,17 +316,7 @@ func TestMergeTemplateApplications(t *testing.T) {
KubeClientset: kubefake.NewSimpleClientset(),
}

got, _, _ := r.generateApplications(v1alpha1.ApplicationSet{
ObjectMeta: metav1.ObjectMeta{
Name: "name",
Namespace: "namespace",
},
Spec: v1alpha1.ApplicationSetSpec{
Generators: []v1alpha1.ApplicationSetGenerator{generator},
Template: cc.template,
},
},
)
got, _, _ := r.generateApplications(*appSet)

assert.Equal(t, cc.expectedApps, got)
})
Expand Down Expand Up @@ -2041,7 +2048,8 @@ func TestGenerateAppsUsingPullRequestGenerator(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "AppSet-branch1-1",
Labels: map[string]string{
"app1": "label1",
"app1": "label1",
LabelKeyAppSetInstance: "",
},
},
Spec: v1alpha1.ApplicationSpec{
Expand Down
79 changes: 70 additions & 9 deletions test/e2e/applicationset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var (
Reason: v1alpha1.ApplicationSetReasonApplicationSetUpToDate,
},
}
LabelKeyAppSetInstance = "argocd.argoproj.io/application-set-name"
)

func TestSimpleListGenerator(t *testing.T) {
Expand All @@ -53,8 +54,11 @@ func TestSimpleListGenerator(t *testing.T) {
APIVersion: "argoproj.io/v1alpha1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "my-cluster-guestbook",
Namespace: fixture.TestNamespace(),
Name: "my-cluster-guestbook",
Namespace: fixture.TestNamespace(),
Labels: map[string]string{
LabelKeyAppSetInstance: "simple-list-generator",
},
Finalizers: []string{"resources-finalizer.argocd.argoproj.io"},
},
Spec: argov1alpha1.ApplicationSpec{
Expand Down Expand Up @@ -121,7 +125,10 @@ func TestSimpleListGenerator(t *testing.T) {
And(func() {
expectedAppNewMetadata = expectedAppNewNamespace.DeepCopy()
expectedAppNewMetadata.ObjectMeta.Annotations = map[string]string{"annotation-key": "annotation-value"}
expectedAppNewMetadata.ObjectMeta.Labels = map[string]string{"label-key": "label-value"}
expectedAppNewMetadata.ObjectMeta.Labels = map[string]string{
LabelKeyAppSetInstance: "simple-list-generator",
"label-key": "label-value",
}
}).
Update(func(appset *v1alpha1.ApplicationSet) {
appset.Spec.Template.Annotations = map[string]string{"annotation-key": "annotation-value"}
Expand All @@ -148,6 +155,9 @@ func TestSimpleListGeneratorGoTemplate(t *testing.T) {
Name: "my-cluster-guestbook",
Namespace: fixture.TestNamespace(),
Finalizers: []string{"resources-finalizer.argocd.argoproj.io"},
Labels: map[string]string{
LabelKeyAppSetInstance: "simple-list-generator",
},
},
Spec: argov1alpha1.ApplicationSpec{
Project: "default",
Expand Down Expand Up @@ -214,7 +224,10 @@ func TestSimpleListGeneratorGoTemplate(t *testing.T) {
And(func() {
expectedAppNewMetadata = expectedAppNewNamespace.DeepCopy()
expectedAppNewMetadata.ObjectMeta.Annotations = map[string]string{"annotation-key": "annotation-value"}
expectedAppNewMetadata.ObjectMeta.Labels = map[string]string{"label-key": "label-value"}
expectedAppNewMetadata.ObjectMeta.Labels = map[string]string{
LabelKeyAppSetInstance: "simple-list-generator",
"label-key": "label-value",
}
}).
Update(func(appset *v1alpha1.ApplicationSet) {
appset.Spec.Template.Annotations = map[string]string{"annotation-key": "annotation-value"}
Expand All @@ -241,6 +254,9 @@ func TestSimpleGitDirectoryGenerator(t *testing.T) {
Name: name,
Namespace: fixture.TestNamespace(),
Finalizers: []string{"resources-finalizer.argocd.argoproj.io"},
Labels: map[string]string{
LabelKeyAppSetInstance: "simple-git-generator",
},
},
Spec: argov1alpha1.ApplicationSpec{
Project: "default",
Expand Down Expand Up @@ -322,7 +338,10 @@ func TestSimpleGitDirectoryGenerator(t *testing.T) {
for _, expectedApp := range expectedAppsNewNamespace {
expectedAppNewMetadata := expectedApp.DeepCopy()
expectedAppNewMetadata.ObjectMeta.Annotations = map[string]string{"annotation-key": "annotation-value"}
expectedAppNewMetadata.ObjectMeta.Labels = map[string]string{"label-key": "label-value"}
expectedAppNewMetadata.ObjectMeta.Labels = map[string]string{
LabelKeyAppSetInstance: "simple-git-generator",
"label-key": "label-value",
}
expectedAppsNewMetadata = append(expectedAppsNewMetadata, *expectedAppNewMetadata)
}
}).
Expand Down Expand Up @@ -350,6 +369,9 @@ func TestSimpleGitDirectoryGeneratorGoTemplate(t *testing.T) {
Name: name,
Namespace: fixture.TestNamespace(),
Finalizers: []string{"resources-finalizer.argocd.argoproj.io"},
Labels: map[string]string{
LabelKeyAppSetInstance: "simple-git-generator",
},
},
Spec: argov1alpha1.ApplicationSpec{
Project: "default",
Expand Down Expand Up @@ -432,7 +454,10 @@ func TestSimpleGitDirectoryGeneratorGoTemplate(t *testing.T) {
for _, expectedApp := range expectedAppsNewNamespace {
expectedAppNewMetadata := expectedApp.DeepCopy()
expectedAppNewMetadata.ObjectMeta.Annotations = map[string]string{"annotation-key": "annotation-value"}
expectedAppNewMetadata.ObjectMeta.Labels = map[string]string{"label-key": "label-value"}
expectedAppNewMetadata.ObjectMeta.Labels = map[string]string{
LabelKeyAppSetInstance: "simple-git-generator",
"label-key": "label-value",
}
expectedAppsNewMetadata = append(expectedAppsNewMetadata, *expectedAppNewMetadata)
}
}).
Expand Down Expand Up @@ -461,6 +486,9 @@ func TestSimpleGitFilesGenerator(t *testing.T) {
Name: name,
Namespace: fixture.TestNamespace(),
Finalizers: []string{"resources-finalizer.argocd.argoproj.io"},
Labels: map[string]string{
LabelKeyAppSetInstance: "simple-git-generator",
},
},
Spec: argov1alpha1.ApplicationSpec{
Project: "default",
Expand Down Expand Up @@ -541,7 +569,10 @@ func TestSimpleGitFilesGenerator(t *testing.T) {
for _, expectedApp := range expectedAppsNewNamespace {
expectedAppNewMetadata := expectedApp.DeepCopy()
expectedAppNewMetadata.ObjectMeta.Annotations = map[string]string{"annotation-key": "annotation-value"}
expectedAppNewMetadata.ObjectMeta.Labels = map[string]string{"label-key": "label-value"}
expectedAppNewMetadata.ObjectMeta.Labels = map[string]string{
LabelKeyAppSetInstance: "simple-git-generator",
"label-key": "label-value",
}
expectedAppsNewMetadata = append(expectedAppsNewMetadata, *expectedAppNewMetadata)
}
}).
Expand Down Expand Up @@ -570,6 +601,9 @@ func TestSimpleGitFilesGeneratorGoTemplate(t *testing.T) {
Name: name,
Namespace: fixture.TestNamespace(),
Finalizers: []string{"resources-finalizer.argocd.argoproj.io"},
Labels: map[string]string{
LabelKeyAppSetInstance: "simple-git-generator",
},
},
Spec: argov1alpha1.ApplicationSpec{
Project: "default",
Expand Down Expand Up @@ -651,7 +685,10 @@ func TestSimpleGitFilesGeneratorGoTemplate(t *testing.T) {
for _, expectedApp := range expectedAppsNewNamespace {
expectedAppNewMetadata := expectedApp.DeepCopy()
expectedAppNewMetadata.ObjectMeta.Annotations = map[string]string{"annotation-key": "annotation-value"}
expectedAppNewMetadata.ObjectMeta.Labels = map[string]string{"label-key": "label-value"}
expectedAppNewMetadata.ObjectMeta.Labels = map[string]string{
LabelKeyAppSetInstance: "simple-git-generator",
"label-key": "label-value",
}
expectedAppsNewMetadata = append(expectedAppsNewMetadata, *expectedAppNewMetadata)
}
}).
Expand Down Expand Up @@ -993,6 +1030,9 @@ func TestSimpleSCMProviderGenerator(t *testing.T) {
Name: "argo-cd-guestbook",
Namespace: fixture.TestNamespace(),
Finalizers: []string{"resources-finalizer.argocd.argoproj.io"},
Labels: map[string]string{
LabelKeyAppSetInstance: "simple-scm-provider-generator",
},
},
Spec: argov1alpha1.ApplicationSpec{
Project: "default",
Expand Down Expand Up @@ -1066,6 +1106,9 @@ func TestSimpleSCMProviderGeneratorGoTemplate(t *testing.T) {
Name: "argo-cd-guestbook",
Namespace: fixture.TestNamespace(),
Finalizers: []string{"resources-finalizer.argocd.argoproj.io"},
Labels: map[string]string{
LabelKeyAppSetInstance: "simple-scm-provider-generator",
},
},
Spec: argov1alpha1.ApplicationSpec{
Project: "default",
Expand Down Expand Up @@ -1135,6 +1178,9 @@ func TestCustomApplicationFinalizers(t *testing.T) {
Name: "my-cluster-guestbook",
Namespace: fixture.TestNamespace(),
Finalizers: []string{"resources-finalizer.argocd.argoproj.io/background"},
Labels: map[string]string{
LabelKeyAppSetInstance: "simple-list-generator",
},
},
Spec: argov1alpha1.ApplicationSpec{
Project: "default",
Expand Down Expand Up @@ -1201,6 +1247,9 @@ func TestCustomApplicationFinalizersGoTemplate(t *testing.T) {
Name: "my-cluster-guestbook",
Namespace: fixture.TestNamespace(),
Finalizers: []string{"resources-finalizer.argocd.argoproj.io/background"},
Labels: map[string]string{
LabelKeyAppSetInstance: "simple-list-generator",
},
},
Spec: argov1alpha1.ApplicationSpec{
Project: "default",
Expand Down Expand Up @@ -1305,6 +1354,9 @@ func TestSimplePullRequestGenerator(t *testing.T) {
Name: "guestbook-1",
Namespace: fixture.TestNamespace(),
Finalizers: []string{"resources-finalizer.argocd.argoproj.io"},
Labels: map[string]string{
LabelKeyAppSetInstance: "simple-pull-request-generator",
},
},
Spec: argov1alpha1.ApplicationSpec{
Project: "default",
Expand Down Expand Up @@ -1380,7 +1432,10 @@ func TestSimplePullRequestGeneratorGoTemplate(t *testing.T) {
Name: "guestbook-1",
Namespace: fixture.TestNamespace(),
Finalizers: []string{"resources-finalizer.argocd.argoproj.io"},
Labels: map[string]string{"app": "preview"},
Labels: map[string]string{
"app": "preview",
LabelKeyAppSetInstance: "simple-pull-request-generator",
},
},
Spec: argov1alpha1.ApplicationSpec{
Project: "default",
Expand Down Expand Up @@ -1456,6 +1511,9 @@ func TestGitGeneratorPrivateRepo(t *testing.T) {
Name: name,
Namespace: fixture.TestNamespace(),
Finalizers: []string{"resources-finalizer.argocd.argoproj.io"},
Labels: map[string]string{
LabelKeyAppSetInstance: "simple-git-generator-private",
},
},
Spec: argov1alpha1.ApplicationSpec{
Project: "default",
Expand Down Expand Up @@ -1531,6 +1589,9 @@ func TestGitGeneratorPrivateRepoGoTemplate(t *testing.T) {
Name: name,
Namespace: fixture.TestNamespace(),
Finalizers: []string{"resources-finalizer.argocd.argoproj.io"},
Labels: map[string]string{
LabelKeyAppSetInstance: "simple-git-generator-private",
},
},
Spec: argov1alpha1.ApplicationSpec{
Project: "default",
Expand Down
22 changes: 20 additions & 2 deletions test/e2e/cluster_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func TestSimpleClusterGenerator(t *testing.T) {
Name: "cluster1-guestbook",
Namespace: fixture.TestNamespace(),
Finalizers: []string{"resources-finalizer.argocd.argoproj.io"},
Labels: map[string]string{
LabelKeyAppSetInstance: "simple-cluster-generator",
},
},
Spec: argov1alpha1.ApplicationSpec{
Project: "default",
Expand Down Expand Up @@ -97,7 +100,10 @@ func TestSimpleClusterGenerator(t *testing.T) {
And(func() {
expectedAppNewMetadata = expectedAppNewNamespace.DeepCopy()
expectedAppNewMetadata.ObjectMeta.Annotations = map[string]string{"annotation-key": "annotation-value"}
expectedAppNewMetadata.ObjectMeta.Labels = map[string]string{"label-key": "label-value"}
expectedAppNewMetadata.ObjectMeta.Labels = map[string]string{
LabelKeyAppSetInstance: "simple-cluster-generator",
"label-key": "label-value",
}
}).
Update(func(appset *v1alpha1.ApplicationSet) {
appset.Spec.Template.Annotations = map[string]string{"annotation-key": "annotation-value"}
Expand All @@ -119,6 +125,9 @@ func TestClusterGeneratorWithLocalCluster(t *testing.T) {
Name: "in-cluster-guestbook",
Namespace: fixture.TestNamespace(),
Finalizers: []string{"resources-finalizer.argocd.argoproj.io"},
Labels: map[string]string{
LabelKeyAppSetInstance: "in-cluster-generator",
},
},
Spec: argov1alpha1.ApplicationSpec{
Project: "default",
Expand Down Expand Up @@ -211,7 +220,10 @@ func TestClusterGeneratorWithLocalCluster(t *testing.T) {
And(func() {
expectedAppNewMetadata = expectedAppNewNamespace.DeepCopy()
expectedAppNewMetadata.ObjectMeta.Annotations = map[string]string{"annotation-key": "annotation-value"}
expectedAppNewMetadata.ObjectMeta.Labels = map[string]string{"label-key": "label-value"}
expectedAppNewMetadata.ObjectMeta.Labels = map[string]string{
"label-key": "label-value",
LabelKeyAppSetInstance: "in-cluster-generator",
}
}).
Update(func(appset *v1alpha1.ApplicationSet) {
appset.Spec.Template.Annotations = map[string]string{"annotation-key": "annotation-value"}
Expand All @@ -236,6 +248,9 @@ func TestSimpleClusterGeneratorAddingCluster(t *testing.T) {
Name: "{{name}}-guestbook",
Namespace: fixture.TestNamespace(),
Finalizers: []string{"resources-finalizer.argocd.argoproj.io"},
Labels: map[string]string{
LabelKeyAppSetInstance: "simple-cluster-generator",
},
},
Spec: argov1alpha1.ApplicationSpec{
Project: "default",
Expand Down Expand Up @@ -318,6 +333,9 @@ func TestSimpleClusterGeneratorDeletingCluster(t *testing.T) {
Name: "{{name}}-guestbook",
Namespace: fixture.TestNamespace(),
Finalizers: []string{"resources-finalizer.argocd.argoproj.io"},
Labels: map[string]string{
LabelKeyAppSetInstance: "simple-cluster-generator",
},
},
Spec: argov1alpha1.ApplicationSpec{
Project: "default",
Expand Down
Loading

0 comments on commit 4e06b84

Please sign in to comment.