From b11ac5d7055a3290a774fe6707e96f77a4b86637 Mon Sep 17 00:00:00 2001 From: Ismail KABOUBI Date: Mon, 23 Oct 2023 21:58:30 +0200 Subject: [PATCH 1/4] feat(projectlabels): propagate labels from MR to External Resource Signed-off-by: Ismail KABOUBI --- examples/projects/project.yaml | 5 +++- examples/providerconfig/provider.yaml | 2 +- pkg/controller/projects/controller.go | 2 +- pkg/controller/projects/controller_test.go | 30 ++++++++++++++++------ 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/examples/projects/project.yaml b/examples/projects/project.yaml index 8afa5c0..7535381 100644 --- a/examples/projects/project.yaml +++ b/examples/projects/project.yaml @@ -3,7 +3,10 @@ apiVersion: projects.argocd.crossplane.io/v1alpha1 kind: Project metadata: name: example-project + labels: + global-project: "true" spec: - forProvider: {} + forProvider: + description: nothing but an example providerConfigRef: name: argocd-provider diff --git a/examples/providerconfig/provider.yaml b/examples/providerconfig/provider.yaml index d7ad119..0dc44b3 100644 --- a/examples/providerconfig/provider.yaml +++ b/examples/providerconfig/provider.yaml @@ -5,7 +5,7 @@ kind: ProviderConfig metadata: name: argocd-provider spec: - serverAddr: argocd-server.argocd.svc:443 + serverAddr: localhost:8443 insecure: true plainText: false credentials: diff --git a/pkg/controller/projects/controller.go b/pkg/controller/projects/controller.go index 1f5110f..9ea6b10 100644 --- a/pkg/controller/projects/controller.go +++ b/pkg/controller/projects/controller.go @@ -319,7 +319,7 @@ func generateCreateProjectOptions(p *v1alpha1.Project) *project.ProjectCreateReq projectCreateRequest := &project.ProjectCreateRequest{ Project: &argocdv1alpha1.AppProject{ Spec: projSpec, - ObjectMeta: metav1.ObjectMeta{Name: p.Name}, + ObjectMeta: metav1.ObjectMeta{Name: p.Name, Labels: p.ObjectMeta.Labels}, }, Upsert: false, } diff --git a/pkg/controller/projects/controller_test.go b/pkg/controller/projects/controller_test.go index 7e385e2..050916b 100644 --- a/pkg/controller/projects/controller_test.go +++ b/pkg/controller/projects/controller_test.go @@ -44,6 +44,7 @@ var ( testProjectExternalName = "testproject" testDescription = "This is a Test" testDescription2 = "This description changed" + testLabels = map[string]string{"label1": "value1"} ) type args struct { @@ -84,6 +85,10 @@ func withObjectMeta(p metav1.ObjectMeta) ProjectModifier { return func(r *v1alpha1.Project) { r.ObjectMeta = p } } +func withLabels(l map[string]string) ProjectModifier { + return func(r *v1alpha1.Project) { r.Labels = l } +} + func withObservation(p v1alpha1.ProjectObservation) ProjectModifier { return func(r *v1alpha1.Project) { r.Status.AtProvider = p } } @@ -125,6 +130,7 @@ func TestObserve(t *testing.T) { }), cr: Project( withExternalName(testProjectExternalName), + withLabels(testLabels), withSpec(v1alpha1.ProjectParameters{ Description: &testDescription, }), @@ -133,6 +139,7 @@ func TestObserve(t *testing.T) { want: want{ cr: Project( withExternalName(testProjectExternalName), + withLabels(testLabels), withSpec(v1alpha1.ProjectParameters{ Description: &testDescription, }), @@ -322,7 +329,7 @@ func TestCreate(t *testing.T) { context.Background(), &project.ProjectCreateRequest{ Project: &argocdv1alpha1.AppProject{ - ObjectMeta: metav1.ObjectMeta{Name: testProjectExternalName}, + ObjectMeta: metav1.ObjectMeta{Name: testProjectExternalName, Labels: testLabels}, Spec: argocdv1alpha1.AppProjectSpec{ Description: testDescription, }, @@ -332,7 +339,8 @@ func TestCreate(t *testing.T) { &argocdv1alpha1.AppProject{ TypeMeta: metav1.TypeMeta{}, ObjectMeta: metav1.ObjectMeta{ - Name: testProjectExternalName, + Name: testProjectExternalName, + Labels: testLabels, }, Spec: argocdv1alpha1.AppProjectSpec{ Description: testDescription, @@ -342,7 +350,8 @@ func TestCreate(t *testing.T) { }), cr: Project( withObjectMeta(metav1.ObjectMeta{ - Name: testProjectExternalName, + Name: testProjectExternalName, + Labels: testLabels, }), withSpec(v1alpha1.ProjectParameters{ Description: &testDescription, @@ -355,7 +364,8 @@ func TestCreate(t *testing.T) { Description: &testDescription, }), withObjectMeta(metav1.ObjectMeta{ - Name: testProjectExternalName, + Name: testProjectExternalName, + Labels: testLabels, }), withExternalName(testProjectExternalName), ), @@ -450,7 +460,8 @@ func TestUpdate(t *testing.T) { &argocdv1alpha1.AppProject{ TypeMeta: metav1.TypeMeta{}, ObjectMeta: metav1.ObjectMeta{ - Name: testProjectExternalName, + Name: testProjectExternalName, + Labels: testLabels, }, Spec: argocdv1alpha1.AppProjectSpec{ Description: testDescription, @@ -473,7 +484,8 @@ func TestUpdate(t *testing.T) { ).Return(&argocdv1alpha1.AppProject{ TypeMeta: metav1.TypeMeta{}, ObjectMeta: metav1.ObjectMeta{ - Name: testProjectExternalName, + Name: testProjectExternalName, + Labels: testLabels, }, Spec: argocdv1alpha1.AppProjectSpec{ Description: testDescription2, @@ -483,7 +495,8 @@ func TestUpdate(t *testing.T) { }), cr: Project( withObjectMeta(metav1.ObjectMeta{ - Name: testProjectExternalName, + Name: testProjectExternalName, + Labels: testLabels, }), withSpec(v1alpha1.ProjectParameters{ Description: &testDescription2, @@ -497,7 +510,8 @@ func TestUpdate(t *testing.T) { Description: &testDescription2, }), withObjectMeta(metav1.ObjectMeta{ - Name: testProjectExternalName, + Name: testProjectExternalName, + Labels: testLabels, }), withExternalName(testProjectExternalName), ), From c136a0544da5e35ac9e810d779f85a020f89c765 Mon Sep 17 00:00:00 2001 From: Ismail KABOUBI Date: Mon, 23 Oct 2023 22:00:44 +0200 Subject: [PATCH 2/4] feat(projectlabels): restore examples Signed-off-by: Ismail KABOUBI --- examples/projects/project.yaml | 3 +-- examples/providerconfig/provider.yaml | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/examples/projects/project.yaml b/examples/projects/project.yaml index 7535381..5e4326b 100644 --- a/examples/projects/project.yaml +++ b/examples/projects/project.yaml @@ -6,7 +6,6 @@ metadata: labels: global-project: "true" spec: - forProvider: - description: nothing but an example + forProvider: {} providerConfigRef: name: argocd-provider diff --git a/examples/providerconfig/provider.yaml b/examples/providerconfig/provider.yaml index 0dc44b3..d7ad119 100644 --- a/examples/providerconfig/provider.yaml +++ b/examples/providerconfig/provider.yaml @@ -5,7 +5,7 @@ kind: ProviderConfig metadata: name: argocd-provider spec: - serverAddr: localhost:8443 + serverAddr: argocd-server.argocd.svc:443 insecure: true plainText: false credentials: From aa7d042a8b9ae99e03b0fdcd3bfc7cd520ac962a Mon Sep 17 00:00:00 2001 From: Ismail KABOUBI Date: Wed, 25 Oct 2023 19:01:22 +0200 Subject: [PATCH 3/4] feat(projectlabels): add projectLabels to the forProvider spec Signed-off-by: Ismail KABOUBI --- apis/projects/v1alpha1/types.go | 3 ++ .../v1alpha1/zz_generated.deepcopy.go | 7 +++++ examples/projects/project.yaml | 6 ++-- go.mod | 1 + go.sum | 3 +- ...rojects.argocd.crossplane.io_projects.yaml | 6 ++++ pkg/controller/projects/controller.go | 2 +- pkg/controller/projects/controller_test.go | 31 +++++++++++-------- 8 files changed, 41 insertions(+), 18 deletions(-) diff --git a/apis/projects/v1alpha1/types.go b/apis/projects/v1alpha1/types.go index 74e7046..3bc5043 100644 --- a/apis/projects/v1alpha1/types.go +++ b/apis/projects/v1alpha1/types.go @@ -68,6 +68,9 @@ type ProjectParameters struct { // ClusterResourceBlacklist contains list of blacklisted cluster level resources // +optional ClusterResourceBlacklist []metav1.GroupKind `json:"clusterResourceBlacklist,omitempty"` + // ProjectLabels labels that will be applied to the AppProject + // +optional + ProjectLabels map[string]string `json:"projectLabels,omitempty"` } // ApplicationDestination holds information about the application's destination diff --git a/apis/projects/v1alpha1/zz_generated.deepcopy.go b/apis/projects/v1alpha1/zz_generated.deepcopy.go index f2a0e77..28c9e5f 100644 --- a/apis/projects/v1alpha1/zz_generated.deepcopy.go +++ b/apis/projects/v1alpha1/zz_generated.deepcopy.go @@ -328,6 +328,13 @@ func (in *ProjectParameters) DeepCopyInto(out *ProjectParameters) { *out = make([]metav1.GroupKind, len(*in)) copy(*out, *in) } + if in.ProjectLabels != nil { + in, out := &in.ProjectLabels, &out.ProjectLabels + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ProjectParameters. diff --git a/examples/projects/project.yaml b/examples/projects/project.yaml index 5e4326b..4e86094 100644 --- a/examples/projects/project.yaml +++ b/examples/projects/project.yaml @@ -3,9 +3,9 @@ apiVersion: projects.argocd.crossplane.io/v1alpha1 kind: Project metadata: name: example-project - labels: - global-project: "true" spec: - forProvider: {} + forProvider: + projectLabels: + argocd.crossplane.io/global-project: "true" providerConfigRef: name: argocd-provider diff --git a/go.mod b/go.mod index 4ccde4e..b200157 100644 --- a/go.mod +++ b/go.mod @@ -160,6 +160,7 @@ require ( gopkg.in/warnings.v0 v0.1.2 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect + gotest.tools/v3 v3.5.1 // indirect k8s.io/apiserver v0.26.3 // indirect k8s.io/cli-runtime v0.26.3 // indirect k8s.io/component-base v0.26.3 // indirect diff --git a/go.sum b/go.sum index 0bea2e5..0b77a99 100644 --- a/go.sum +++ b/go.sum @@ -1029,7 +1029,8 @@ gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gotest.tools/v3 v3.0.2/go.mod h1:3SzNCllyD9/Y+b5r9JIKQ474KzkZyqLqEfYqMsX94Bk= -gotest.tools/v3 v3.0.3 h1:4AuOwCGf4lLR9u3YOe2awrHygurzhO/HeQ6laiA6Sx0= +gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU= +gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/package/crds/projects.argocd.crossplane.io_projects.yaml b/package/crds/projects.argocd.crossplane.io_projects.yaml index bad7871..a21d6fa 100644 --- a/package/crds/projects.argocd.crossplane.io_projects.yaml +++ b/package/crds/projects.argocd.crossplane.io_projects.yaml @@ -258,6 +258,12 @@ spec: created for apps which have orphaned resources type: boolean type: object + projectLabels: + additionalProperties: + type: string + description: ProjectLabels labels that will be applied to the + AppProject + type: object roles: description: Roles are user defined RBAC roles associated with this project diff --git a/pkg/controller/projects/controller.go b/pkg/controller/projects/controller.go index 9ea6b10..5fb0fe3 100644 --- a/pkg/controller/projects/controller.go +++ b/pkg/controller/projects/controller.go @@ -319,7 +319,7 @@ func generateCreateProjectOptions(p *v1alpha1.Project) *project.ProjectCreateReq projectCreateRequest := &project.ProjectCreateRequest{ Project: &argocdv1alpha1.AppProject{ Spec: projSpec, - ObjectMeta: metav1.ObjectMeta{Name: p.Name, Labels: p.ObjectMeta.Labels}, + ObjectMeta: metav1.ObjectMeta{Name: p.Name, Labels: p.Spec.ForProvider.ProjectLabels}, }, Upsert: false, } diff --git a/pkg/controller/projects/controller_test.go b/pkg/controller/projects/controller_test.go index 050916b..736ef01 100644 --- a/pkg/controller/projects/controller_test.go +++ b/pkg/controller/projects/controller_test.go @@ -85,8 +85,12 @@ func withObjectMeta(p metav1.ObjectMeta) ProjectModifier { return func(r *v1alpha1.Project) { r.ObjectMeta = p } } -func withLabels(l map[string]string) ProjectModifier { - return func(r *v1alpha1.Project) { r.Labels = l } +func withObjectMetaLabels(l map[string]string) ProjectModifier { + return func(r *v1alpha1.Project) { r.ObjectMeta.Labels = l } +} + +func withProjectLabels(l map[string]string) ProjectModifier { + return func(r *v1alpha1.Project) { r.Spec.ForProvider.ProjectLabels = l } } func withObservation(p v1alpha1.ProjectObservation) ProjectModifier { @@ -120,7 +124,8 @@ func TestObserve(t *testing.T) { &argocdv1alpha1.AppProject{ TypeMeta: metav1.TypeMeta{}, ObjectMeta: metav1.ObjectMeta{ - Name: testProjectExternalName, + Name: testProjectExternalName, + Labels: testLabels, }, Spec: argocdv1alpha1.AppProjectSpec{ Description: testDescription, @@ -130,18 +135,18 @@ func TestObserve(t *testing.T) { }), cr: Project( withExternalName(testProjectExternalName), - withLabels(testLabels), withSpec(v1alpha1.ProjectParameters{ - Description: &testDescription, + Description: &testDescription, + ProjectLabels: testLabels, }), ), }, want: want{ cr: Project( withExternalName(testProjectExternalName), - withLabels(testLabels), withSpec(v1alpha1.ProjectParameters{ - Description: &testDescription, + Description: &testDescription, + ProjectLabels: testLabels, }), withConditions(xpv1.Available()), withObservation(v1alpha1.ProjectObservation{ @@ -350,22 +355,22 @@ func TestCreate(t *testing.T) { }), cr: Project( withObjectMeta(metav1.ObjectMeta{ - Name: testProjectExternalName, - Labels: testLabels, + Name: testProjectExternalName, }), withSpec(v1alpha1.ProjectParameters{ - Description: &testDescription, + Description: &testDescription, + ProjectLabels: testLabels, }), ), }, want: want{ cr: Project( withSpec(v1alpha1.ProjectParameters{ - Description: &testDescription, + Description: &testDescription, + ProjectLabels: testLabels, }), withObjectMeta(metav1.ObjectMeta{ - Name: testProjectExternalName, - Labels: testLabels, + Name: testProjectExternalName, }), withExternalName(testProjectExternalName), ), From 0321b95c236278716ef763e6b7a6db04d8263eea Mon Sep 17 00:00:00 2001 From: Ismail KABOUBI Date: Wed, 25 Oct 2023 19:05:49 +0200 Subject: [PATCH 4/4] feat(projectlabels): delete unused helper functions Signed-off-by: Ismail KABOUBI --- pkg/controller/projects/controller_test.go | 8 -------- 1 file changed, 8 deletions(-) diff --git a/pkg/controller/projects/controller_test.go b/pkg/controller/projects/controller_test.go index 736ef01..1a84d45 100644 --- a/pkg/controller/projects/controller_test.go +++ b/pkg/controller/projects/controller_test.go @@ -85,14 +85,6 @@ func withObjectMeta(p metav1.ObjectMeta) ProjectModifier { return func(r *v1alpha1.Project) { r.ObjectMeta = p } } -func withObjectMetaLabels(l map[string]string) ProjectModifier { - return func(r *v1alpha1.Project) { r.ObjectMeta.Labels = l } -} - -func withProjectLabels(l map[string]string) ProjectModifier { - return func(r *v1alpha1.Project) { r.Spec.ForProvider.ProjectLabels = l } -} - func withObservation(p v1alpha1.ProjectObservation) ProjectModifier { return func(r *v1alpha1.Project) { r.Status.AtProvider = p } }