From 60c832aee5cc38fd8a69adbe221eb6c936bd6ac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Bonilla?= Date: Thu, 29 Feb 2024 11:24:18 -0600 Subject: [PATCH] feat: add suport for annotations in application resource MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: César Bonilla --- apis/applications/v1alpha1/types.go | 3 ++ .../v1alpha1/zz_generated.deepcopy.go | 7 +++++ .../application-with-annotations.yaml | 22 +++++++++++++++ ...ons.argocd.crossplane.io_applications.yaml | 5 ++++ pkg/controller/applications/comp.go | 2 +- pkg/controller/applications/controller.go | 6 ++-- .../applications/controller_test.go | 28 ++++++++++++++----- 7 files changed, 63 insertions(+), 10 deletions(-) create mode 100644 examples/application/application-with-annotations.yaml diff --git a/apis/applications/v1alpha1/types.go b/apis/applications/v1alpha1/types.go index 8742f5a..d4b5ea0 100644 --- a/apis/applications/v1alpha1/types.go +++ b/apis/applications/v1alpha1/types.go @@ -48,6 +48,9 @@ type ApplicationParameters struct { // Sources is a reference to the location of the application's manifests or chart Sources ApplicationSources `json:"sources,omitempty" protobuf:"bytes,8,opt,name=sources"` + + // Annotations that will be applied to the ArgoCD Application + Annotations map[string]string `json:"annotations,omitempty" protobuf:"bytes,12,opt,name=annotations"` } // ResourceIgnoreDifferences contains resource filter and list of json paths which should be ignored during comparison with live state. diff --git a/apis/applications/v1alpha1/zz_generated.deepcopy.go b/apis/applications/v1alpha1/zz_generated.deepcopy.go index d8790ff..6f14001 100644 --- a/apis/applications/v1alpha1/zz_generated.deepcopy.go +++ b/apis/applications/v1alpha1/zz_generated.deepcopy.go @@ -192,6 +192,13 @@ func (in *ApplicationParameters) DeepCopyInto(out *ApplicationParameters) { (*in)[i].DeepCopyInto(&(*out)[i]) } } + if in.Annotations != nil { + in, out := &in.Annotations, &out.Annotations + *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 ApplicationParameters. diff --git a/examples/application/application-with-annotations.yaml b/examples/application/application-with-annotations.yaml new file mode 100644 index 0000000..2a9a581 --- /dev/null +++ b/examples/application/application-with-annotations.yaml @@ -0,0 +1,22 @@ +--- +# Example using annotations +apiVersion: applications.argocd.crossplane.io/v1alpha1 +kind: Application +metadata: + name: example-application-annotations +spec: + providerConfigRef: + name: argocd-provider + forProvider: + annotations: + notifications.argoproj.io/subscribe.on-deployed.slack: slack-channel-name + notifications.argoproj.io/subscribe.on-failure.slack: slack-channel-name + destination: + namespace: default + server: https://kubernetes.default.svc + project: default + source: + repoURL: https://github.com/bonilla-cesar/argocd + path: resources/cm + targetRevision: HEAD + diff --git a/package/crds/applications.argocd.crossplane.io_applications.yaml b/package/crds/applications.argocd.crossplane.io_applications.yaml index 9a06f83..967fa84 100644 --- a/package/crds/applications.argocd.crossplane.io_applications.yaml +++ b/package/crds/applications.argocd.crossplane.io_applications.yaml @@ -63,6 +63,11 @@ spec: description: ApplicationParameters define the desired state of an ArgoCD Git Application properties: + annotations: + additionalProperties: + type: string + description: Annotations that will be applied to the ArgoCD Application + type: object destination: description: Destination is a reference to the target Kubernetes server and namespace diff --git a/pkg/controller/applications/comp.go b/pkg/controller/applications/comp.go index 0512b2d..b331be1 100644 --- a/pkg/controller/applications/comp.go +++ b/pkg/controller/applications/comp.go @@ -18,5 +18,5 @@ func IsApplicationUpToDate(cr *v1alpha1.ApplicationParameters, remote *argocdv1a // the unexported fields should not bother here, since we don't copy them or write them cmpopts.IgnoreUnexported(argocdv1alpha1.ApplicationDestination{}), } - return cmp.Equal(*cluster, remote.Spec, opts...) + return cmp.Equal(*cluster, remote.Spec, opts...) && cmp.Equal(cr.Annotations, remote.Annotations, opts...) } diff --git a/pkg/controller/applications/controller.go b/pkg/controller/applications/controller.go index d7f71a4..01d71b3 100644 --- a/pkg/controller/applications/controller.go +++ b/pkg/controller/applications/controller.go @@ -215,7 +215,8 @@ func generateCreateApplicationRequest(cr *v1alpha1.Application) *application.App app := &argocdv1alpha1.Application{ TypeMeta: metav1.TypeMeta{}, ObjectMeta: metav1.ObjectMeta{ - Name: meta.GetExternalName(cr), + Name: meta.GetExternalName(cr), + Annotations: cr.Spec.ForProvider.Annotations, }, Spec: *spec, } @@ -235,7 +236,8 @@ func generateUpdateRepositoryOptions(cr *v1alpha1.Application) *application.Appl app := &argocdv1alpha1.Application{ TypeMeta: metav1.TypeMeta{}, ObjectMeta: metav1.ObjectMeta{ - Name: meta.GetExternalName(cr), + Name: meta.GetExternalName(cr), + Annotations: cr.Spec.ForProvider.Annotations, }, Spec: *spec, } diff --git a/pkg/controller/applications/controller_test.go b/pkg/controller/applications/controller_test.go index 4d3849a..670e773 100644 --- a/pkg/controller/applications/controller_test.go +++ b/pkg/controller/applications/controller_test.go @@ -47,6 +47,7 @@ var ( chartPath = "charts/podinfo" revision = "HEAD" selfHealEnabled = true + testApplicationAnnotations = map[string]string{"annotation1": "value1"} ) type args struct { @@ -123,7 +124,8 @@ func TestObserve(t *testing.T) { Items: []argocdv1alpha1.Application{{ TypeMeta: metav1.TypeMeta{}, ObjectMeta: metav1.ObjectMeta{ - Name: testApplicationExternalName, + Name: testApplicationExternalName, + Annotations: testApplicationAnnotations, }, Spec: argocdv1alpha1.ApplicationSpec{ Project: testProjectName, @@ -162,6 +164,7 @@ func TestObserve(t *testing.T) { SelfHeal: &selfHealEnabled, }, }, + Annotations: testApplicationAnnotations, }), ), }, @@ -183,6 +186,7 @@ func TestObserve(t *testing.T) { SelfHeal: &selfHealEnabled, }, }, + Annotations: testApplicationAnnotations, }), withConditions(xpv1.Available()), withObservation(initializedArgoAppStatus()), @@ -210,7 +214,8 @@ func TestObserve(t *testing.T) { Items: []argocdv1alpha1.Application{{ TypeMeta: metav1.TypeMeta{}, ObjectMeta: metav1.ObjectMeta{ - Name: testApplicationExternalName, + Name: testApplicationExternalName, + Annotations: testApplicationAnnotations, }, Spec: argocdv1alpha1.ApplicationSpec{ Project: testProjectName, @@ -244,6 +249,7 @@ func TestObserve(t *testing.T) { SelfHeal: &selfHealEnabled, }, }, + Annotations: testApplicationAnnotations, }), ), }, @@ -265,6 +271,7 @@ func TestObserve(t *testing.T) { SelfHeal: &selfHealEnabled, }, }, + Annotations: testApplicationAnnotations, }), withConditions(xpv1.Available()), withObservation(initializedArgoAppStatus()), @@ -496,7 +503,8 @@ func TestUpdate(t *testing.T) { &argocdApplication.ApplicationUpdateRequest{ Application: &argocdv1alpha1.Application{ ObjectMeta: metav1.ObjectMeta{ - Name: testApplicationExternalName, + Name: testApplicationExternalName, + Annotations: testApplicationAnnotations, }, Spec: argocdv1alpha1.ApplicationSpec{ Project: testProjectName, @@ -508,7 +516,8 @@ func TestUpdate(t *testing.T) { }, ).Return(&argocdv1alpha1.Application{ ObjectMeta: metav1.ObjectMeta{ - Name: testApplicationExternalName, + Name: testApplicationExternalName, + Annotations: testApplicationAnnotations, }, }, nil) }), @@ -519,6 +528,7 @@ func TestUpdate(t *testing.T) { Destination: v1alpha1.ApplicationDestination{ Namespace: &testDestinationNamespace, }, + Annotations: testApplicationAnnotations, }), withExternalName(testApplicationExternalName), ), @@ -531,6 +541,7 @@ func TestUpdate(t *testing.T) { Destination: v1alpha1.ApplicationDestination{ Namespace: &testDestinationNamespace, }, + Annotations: testApplicationAnnotations, }), withExternalName(testApplicationExternalName), ), @@ -546,7 +557,8 @@ func TestUpdate(t *testing.T) { &argocdApplication.ApplicationUpdateRequest{ Application: &argocdv1alpha1.Application{ ObjectMeta: metav1.ObjectMeta{ - Name: testApplicationExternalName, + Name: testApplicationExternalName, + Annotations: testApplicationAnnotations, }, Spec: argocdv1alpha1.ApplicationSpec{ Project: testProjectName, @@ -557,7 +569,8 @@ func TestUpdate(t *testing.T) { }), cr: Application( withSpec(v1alpha1.ApplicationParameters{ - Project: testProjectName, + Project: testProjectName, + Annotations: testApplicationAnnotations, }), withExternalName(testApplicationExternalName), ), @@ -565,7 +578,8 @@ func TestUpdate(t *testing.T) { want: want{ cr: Application( withSpec(v1alpha1.ApplicationParameters{ - Project: testProjectName, + Project: testProjectName, + Annotations: testApplicationAnnotations, }), withExternalName(testApplicationExternalName), ),