From d3a23151d79d9dce64178de80804e8347074bb02 Mon Sep 17 00:00:00 2001 From: Martin Reinhardt Date: Wed, 20 Apr 2022 15:37:46 +0200 Subject: [PATCH] Adding pod annotations support --- deploy/crds/keycloak.org_keycloaks_crd.yaml | 5 +++++ .../keycloak/keycloak-with-metrics.yaml | 22 +++++++++++++++++++ pkg/apis/keycloak/v1alpha1/keycloak_types.go | 3 +++ .../v1alpha1/zz_generated.deepcopy.go | 7 ++++++ pkg/model/keycloak_deployment.go | 17 +++++++++----- pkg/model/rhsso_deployment.go | 17 +++++++++----- pkg/model/util.go | 20 +++++++++++++++++ pkg/model/util_test.go | 22 +++++++++++++++++++ 8 files changed, 101 insertions(+), 12 deletions(-) create mode 100644 deploy/examples/keycloak/keycloak-with-metrics.yaml diff --git a/deploy/crds/keycloak.org_keycloaks_crd.yaml b/deploy/crds/keycloak.org_keycloaks_crd.yaml index 78ef82d53..96e09d1f7 100644 --- a/deploy/crds/keycloak.org_keycloaks_crd.yaml +++ b/deploy/crds/keycloak.org_keycloaks_crd.yaml @@ -952,6 +952,11 @@ spec: type: array type: object type: object + podannotations: + additionalProperties: + type: string + description: List of annotations to set in the keycloak pods + type: object podlabels: additionalProperties: type: string diff --git a/deploy/examples/keycloak/keycloak-with-metrics.yaml b/deploy/examples/keycloak/keycloak-with-metrics.yaml new file mode 100644 index 000000000..d382505db --- /dev/null +++ b/deploy/examples/keycloak/keycloak-with-metrics.yaml @@ -0,0 +1,22 @@ +apiVersion: keycloak.org/v1alpha1 +kind: Keycloak +metadata: + name: example-keycloak + labels: + app: sso + annotations: + prometheus.io/scrape: "true" + prometheus.io/path: "/auth/realms/master/metrics" + prometheus.io/port: "8080" + prometheus.io/scheme: "http" +spec: + instances: 1 + extensions: + - https://github.com/aerogear/keycloak-metrics-spi/releases/download/2.5.3/keycloak-metrics-spi-2.5.3.jar + externalAccess: + enabled: True + podDisruptionBudget: + enabled: True + # User needs to provision the external database + externalDatabase: + enabled: True diff --git a/pkg/apis/keycloak/v1alpha1/keycloak_types.go b/pkg/apis/keycloak/v1alpha1/keycloak_types.go index 1241a4c22..5b5e57e15 100644 --- a/pkg/apis/keycloak/v1alpha1/keycloak_types.go +++ b/pkg/apis/keycloak/v1alpha1/keycloak_types.go @@ -97,6 +97,9 @@ type DeploymentSpec struct { type KeycloakDeploymentSpec struct { DeploymentSpec `json:",inline"` + // List of annotations to set in the keycloak pods + // +optional + PodAnnotations map[string]string `json:"podannotations,omitempty"` // List of labels to set in the keycloak pods // +optional PodLabels map[string]string `json:"podlabels,omitempty"` diff --git a/pkg/apis/keycloak/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/keycloak/v1alpha1/zz_generated.deepcopy.go index ae4ea1f91..f7cbd8239 100644 --- a/pkg/apis/keycloak/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/keycloak/v1alpha1/zz_generated.deepcopy.go @@ -1053,6 +1053,13 @@ func (in *KeycloakDeploymentSpec) DeepCopyInto(out *KeycloakDeploymentSpec) { (*out)[key] = val } } + if in.PodAnnotations != nil { + in, out := &in.PodAnnotations, &out.PodAnnotations + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } in.Experimental.DeepCopyInto(&out.Experimental) return } diff --git a/pkg/model/keycloak_deployment.go b/pkg/model/keycloak_deployment.go index e803895f7..be17b47e3 100644 --- a/pkg/model/keycloak_deployment.go +++ b/pkg/model/keycloak_deployment.go @@ -208,11 +208,13 @@ func KeycloakDeployment(cr *v1alpha1.Keycloak, dbSecret *v1.Secret, dbSSLSecret "component": KeycloakDeploymentComponent, } podLabels := AddPodLabels(cr, labels) + podAnnotations := cr.Spec.KeycloakDeploymentSpec.PodAnnotations keycloakStatefulset := &v13.StatefulSet{ ObjectMeta: v12.ObjectMeta{ - Name: KeycloakDeploymentName, - Namespace: cr.Namespace, - Labels: podLabels, + Name: KeycloakDeploymentName, + Namespace: cr.Namespace, + Labels: podLabels, + Annotations: podAnnotations, }, Spec: v13.StatefulSetSpec{ Replicas: SanitizeNumberOfReplicas(cr.Spec.Instances, true), @@ -221,9 +223,10 @@ func KeycloakDeployment(cr *v1alpha1.Keycloak, dbSecret *v1.Secret, dbSSLSecret }, Template: v1.PodTemplateSpec{ ObjectMeta: v12.ObjectMeta{ - Name: KeycloakDeploymentName, - Namespace: cr.Namespace, - Labels: podLabels, + Name: KeycloakDeploymentName, + Namespace: cr.Namespace, + Labels: podLabels, + Annotations: podAnnotations, }, Spec: v1.PodSpec{ InitContainers: KeycloakExtensionsInitContainers(cr), @@ -280,7 +283,9 @@ func KeycloakDeploymentReconciled(cr *v1alpha1.Keycloak, currentState *v13.State reconciled := currentState.DeepCopy() reconciled.ObjectMeta.Labels = AddPodLabels(cr, reconciled.ObjectMeta.Labels) + reconciled.ObjectMeta.Annotations = AddPodAnnotations(cr, reconciled.ObjectMeta.Annotations) reconciled.Spec.Template.ObjectMeta.Labels = AddPodLabels(cr, reconciled.Spec.Template.ObjectMeta.Labels) + reconciled.Spec.Template.ObjectMeta.Annotations = AddPodAnnotations(cr, reconciled.Spec.Template.ObjectMeta.Annotations) reconciled.ResourceVersion = currentState.ResourceVersion reconciled.Spec.Replicas = SanitizeNumberOfReplicas(cr.Spec.Instances, false) diff --git a/pkg/model/rhsso_deployment.go b/pkg/model/rhsso_deployment.go index a96f067da..6289c0925 100644 --- a/pkg/model/rhsso_deployment.go +++ b/pkg/model/rhsso_deployment.go @@ -153,11 +153,13 @@ func RHSSODeployment(cr *v1alpha1.Keycloak, dbSecret *v1.Secret, dbSSLSecret *v1 "component": KeycloakDeploymentComponent, } podLabels := AddPodLabels(cr, labels) + podAnnotations := cr.Spec.KeycloakDeploymentSpec.PodAnnotations rhssoStatefulSet := &v13.StatefulSet{ ObjectMeta: v12.ObjectMeta{ - Name: KeycloakDeploymentName, - Namespace: cr.Namespace, - Labels: podLabels, + Name: KeycloakDeploymentName, + Namespace: cr.Namespace, + Labels: podLabels, + Annotations: podAnnotations, }, Spec: v13.StatefulSetSpec{ Replicas: SanitizeNumberOfReplicas(cr.Spec.Instances, true), @@ -166,9 +168,10 @@ func RHSSODeployment(cr *v1alpha1.Keycloak, dbSecret *v1.Secret, dbSSLSecret *v1 }, Template: v1.PodTemplateSpec{ ObjectMeta: v12.ObjectMeta{ - Name: KeycloakDeploymentName, - Namespace: cr.Namespace, - Labels: podLabels, + Name: KeycloakDeploymentName, + Namespace: cr.Namespace, + Labels: podLabels, + Annotations: podAnnotations, }, Spec: v1.PodSpec{ Volumes: KeycloakVolumes(cr, dbSSLSecret), @@ -231,7 +234,9 @@ func RHSSODeploymentReconciled(cr *v1alpha1.Keycloak, currentState *v13.Stateful reconciled := currentState.DeepCopy() reconciled.ObjectMeta.Labels = AddPodLabels(cr, reconciled.ObjectMeta.Labels) + reconciled.ObjectMeta.Annotations = AddPodAnnotations(cr, reconciled.ObjectMeta.Annotations) reconciled.Spec.Template.ObjectMeta.Labels = AddPodLabels(cr, reconciled.Spec.Template.ObjectMeta.Labels) + reconciled.Spec.Template.ObjectMeta.Annotations = AddPodAnnotations(cr, reconciled.Spec.Template.ObjectMeta.Annotations) reconciled.ResourceVersion = currentState.ResourceVersion reconciled.Spec.Replicas = SanitizeNumberOfReplicas(cr.Spec.Instances, false) diff --git a/pkg/model/util.go b/pkg/model/util.go index 9924e7a94..5a77e3468 100644 --- a/pkg/model/util.go +++ b/pkg/model/util.go @@ -254,3 +254,23 @@ func AddPodLabels(cr *v1alpha1.Keycloak, labels map[string]string) map[string]st return mergedPodLabels } + +func AddPodAnnotations(cr *v1alpha1.Keycloak, annotations map[string]string) map[string]string { + if len(annotations) == 0 { + return nil + } + + mergedAnnotations := map[string]string{} + + // We add the labels + for key, value := range annotations { + mergedAnnotations[key] = value + } + + // We add the PodLabel labels coming from CR Env Vars + for key, value := range cr.Spec.KeycloakDeploymentSpec.PodAnnotations { + mergedAnnotations[key] = value + } + + return mergedAnnotations +} diff --git a/pkg/model/util_test.go b/pkg/model/util_test.go index 1a0b6d082..e37610cfe 100644 --- a/pkg/model/util_test.go +++ b/pkg/model/util_test.go @@ -185,3 +185,25 @@ func TestPodLabels_When_EnvVars_Then_FullListOfLabels(t *testing.T) { assert.Contains(t, totalLabels, "app") assert.Contains(t, totalLabels, "component") } +func TestPodAnnotations_When_EnvVars_Then_FullListOfAnnotations(t *testing.T) { + cr := v1alpha1.Keycloak{ + Spec: v1alpha1.KeycloakSpec{ + KeycloakDeploymentSpec: v1alpha1.KeycloakDeploymentSpec{ + PodAnnotations: map[string]string{ + "AnnotationToTest": "thisistheannotationvalue", + "SecondAnnotationToTest": "anotherthisistheannotationvalue", + }, + }, + }} + + annotations := map[string]string{ + "app": ApplicationName, + "component": KeycloakDeploymentComponent, + } + totalAnnotations := AddPodAnnotations(&cr, annotations) + assert.Equal(t, 4, len(totalAnnotations)) + assert.Contains(t, totalAnnotations, "AnnotationToTest") + assert.Contains(t, totalAnnotations, "SecondAnnotationToTest") + assert.Contains(t, totalAnnotations, "app") + assert.Contains(t, totalAnnotations, "component") +}