Skip to content
This repository was archived by the owner on Nov 16, 2022. It is now read-only.

Commit d3a2315

Browse files
authored
Adding pod annotations support
1 parent 9e4bd42 commit d3a2315

File tree

8 files changed

+101
-12
lines changed

8 files changed

+101
-12
lines changed

deploy/crds/keycloak.org_keycloaks_crd.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,11 @@ spec:
952952
type: array
953953
type: object
954954
type: object
955+
podannotations:
956+
additionalProperties:
957+
type: string
958+
description: List of annotations to set in the keycloak pods
959+
type: object
955960
podlabels:
956961
additionalProperties:
957962
type: string
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
apiVersion: keycloak.org/v1alpha1
2+
kind: Keycloak
3+
metadata:
4+
name: example-keycloak
5+
labels:
6+
app: sso
7+
annotations:
8+
prometheus.io/scrape: "true"
9+
prometheus.io/path: "/auth/realms/master/metrics"
10+
prometheus.io/port: "8080"
11+
prometheus.io/scheme: "http"
12+
spec:
13+
instances: 1
14+
extensions:
15+
- https://github.com/aerogear/keycloak-metrics-spi/releases/download/2.5.3/keycloak-metrics-spi-2.5.3.jar
16+
externalAccess:
17+
enabled: True
18+
podDisruptionBudget:
19+
enabled: True
20+
# User needs to provision the external database
21+
externalDatabase:
22+
enabled: True

pkg/apis/keycloak/v1alpha1/keycloak_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ type DeploymentSpec struct {
9797

9898
type KeycloakDeploymentSpec struct {
9999
DeploymentSpec `json:",inline"`
100+
// List of annotations to set in the keycloak pods
101+
// +optional
102+
PodAnnotations map[string]string `json:"podannotations,omitempty"`
100103
// List of labels to set in the keycloak pods
101104
// +optional
102105
PodLabels map[string]string `json:"podlabels,omitempty"`

pkg/apis/keycloak/v1alpha1/zz_generated.deepcopy.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/model/keycloak_deployment.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,13 @@ func KeycloakDeployment(cr *v1alpha1.Keycloak, dbSecret *v1.Secret, dbSSLSecret
208208
"component": KeycloakDeploymentComponent,
209209
}
210210
podLabels := AddPodLabels(cr, labels)
211+
podAnnotations := cr.Spec.KeycloakDeploymentSpec.PodAnnotations
211212
keycloakStatefulset := &v13.StatefulSet{
212213
ObjectMeta: v12.ObjectMeta{
213-
Name: KeycloakDeploymentName,
214-
Namespace: cr.Namespace,
215-
Labels: podLabels,
214+
Name: KeycloakDeploymentName,
215+
Namespace: cr.Namespace,
216+
Labels: podLabels,
217+
Annotations: podAnnotations,
216218
},
217219
Spec: v13.StatefulSetSpec{
218220
Replicas: SanitizeNumberOfReplicas(cr.Spec.Instances, true),
@@ -221,9 +223,10 @@ func KeycloakDeployment(cr *v1alpha1.Keycloak, dbSecret *v1.Secret, dbSSLSecret
221223
},
222224
Template: v1.PodTemplateSpec{
223225
ObjectMeta: v12.ObjectMeta{
224-
Name: KeycloakDeploymentName,
225-
Namespace: cr.Namespace,
226-
Labels: podLabels,
226+
Name: KeycloakDeploymentName,
227+
Namespace: cr.Namespace,
228+
Labels: podLabels,
229+
Annotations: podAnnotations,
227230
},
228231
Spec: v1.PodSpec{
229232
InitContainers: KeycloakExtensionsInitContainers(cr),
@@ -280,7 +283,9 @@ func KeycloakDeploymentReconciled(cr *v1alpha1.Keycloak, currentState *v13.State
280283
reconciled := currentState.DeepCopy()
281284

282285
reconciled.ObjectMeta.Labels = AddPodLabels(cr, reconciled.ObjectMeta.Labels)
286+
reconciled.ObjectMeta.Annotations = AddPodAnnotations(cr, reconciled.ObjectMeta.Annotations)
283287
reconciled.Spec.Template.ObjectMeta.Labels = AddPodLabels(cr, reconciled.Spec.Template.ObjectMeta.Labels)
288+
reconciled.Spec.Template.ObjectMeta.Annotations = AddPodAnnotations(cr, reconciled.Spec.Template.ObjectMeta.Annotations)
284289

285290
reconciled.ResourceVersion = currentState.ResourceVersion
286291
reconciled.Spec.Replicas = SanitizeNumberOfReplicas(cr.Spec.Instances, false)

pkg/model/rhsso_deployment.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,11 +153,13 @@ func RHSSODeployment(cr *v1alpha1.Keycloak, dbSecret *v1.Secret, dbSSLSecret *v1
153153
"component": KeycloakDeploymentComponent,
154154
}
155155
podLabels := AddPodLabels(cr, labels)
156+
podAnnotations := cr.Spec.KeycloakDeploymentSpec.PodAnnotations
156157
rhssoStatefulSet := &v13.StatefulSet{
157158
ObjectMeta: v12.ObjectMeta{
158-
Name: KeycloakDeploymentName,
159-
Namespace: cr.Namespace,
160-
Labels: podLabels,
159+
Name: KeycloakDeploymentName,
160+
Namespace: cr.Namespace,
161+
Labels: podLabels,
162+
Annotations: podAnnotations,
161163
},
162164
Spec: v13.StatefulSetSpec{
163165
Replicas: SanitizeNumberOfReplicas(cr.Spec.Instances, true),
@@ -166,9 +168,10 @@ func RHSSODeployment(cr *v1alpha1.Keycloak, dbSecret *v1.Secret, dbSSLSecret *v1
166168
},
167169
Template: v1.PodTemplateSpec{
168170
ObjectMeta: v12.ObjectMeta{
169-
Name: KeycloakDeploymentName,
170-
Namespace: cr.Namespace,
171-
Labels: podLabels,
171+
Name: KeycloakDeploymentName,
172+
Namespace: cr.Namespace,
173+
Labels: podLabels,
174+
Annotations: podAnnotations,
172175
},
173176
Spec: v1.PodSpec{
174177
Volumes: KeycloakVolumes(cr, dbSSLSecret),
@@ -231,7 +234,9 @@ func RHSSODeploymentReconciled(cr *v1alpha1.Keycloak, currentState *v13.Stateful
231234
reconciled := currentState.DeepCopy()
232235

233236
reconciled.ObjectMeta.Labels = AddPodLabels(cr, reconciled.ObjectMeta.Labels)
237+
reconciled.ObjectMeta.Annotations = AddPodAnnotations(cr, reconciled.ObjectMeta.Annotations)
234238
reconciled.Spec.Template.ObjectMeta.Labels = AddPodLabels(cr, reconciled.Spec.Template.ObjectMeta.Labels)
239+
reconciled.Spec.Template.ObjectMeta.Annotations = AddPodAnnotations(cr, reconciled.Spec.Template.ObjectMeta.Annotations)
235240

236241
reconciled.ResourceVersion = currentState.ResourceVersion
237242
reconciled.Spec.Replicas = SanitizeNumberOfReplicas(cr.Spec.Instances, false)

pkg/model/util.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,23 @@ func AddPodLabels(cr *v1alpha1.Keycloak, labels map[string]string) map[string]st
254254

255255
return mergedPodLabels
256256
}
257+
258+
func AddPodAnnotations(cr *v1alpha1.Keycloak, annotations map[string]string) map[string]string {
259+
if len(annotations) == 0 {
260+
return nil
261+
}
262+
263+
mergedAnnotations := map[string]string{}
264+
265+
// We add the labels
266+
for key, value := range annotations {
267+
mergedAnnotations[key] = value
268+
}
269+
270+
// We add the PodLabel labels coming from CR Env Vars
271+
for key, value := range cr.Spec.KeycloakDeploymentSpec.PodAnnotations {
272+
mergedAnnotations[key] = value
273+
}
274+
275+
return mergedAnnotations
276+
}

pkg/model/util_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,25 @@ func TestPodLabels_When_EnvVars_Then_FullListOfLabels(t *testing.T) {
185185
assert.Contains(t, totalLabels, "app")
186186
assert.Contains(t, totalLabels, "component")
187187
}
188+
func TestPodAnnotations_When_EnvVars_Then_FullListOfAnnotations(t *testing.T) {
189+
cr := v1alpha1.Keycloak{
190+
Spec: v1alpha1.KeycloakSpec{
191+
KeycloakDeploymentSpec: v1alpha1.KeycloakDeploymentSpec{
192+
PodAnnotations: map[string]string{
193+
"AnnotationToTest": "thisistheannotationvalue",
194+
"SecondAnnotationToTest": "anotherthisistheannotationvalue",
195+
},
196+
},
197+
}}
198+
199+
annotations := map[string]string{
200+
"app": ApplicationName,
201+
"component": KeycloakDeploymentComponent,
202+
}
203+
totalAnnotations := AddPodAnnotations(&cr, annotations)
204+
assert.Equal(t, 4, len(totalAnnotations))
205+
assert.Contains(t, totalAnnotations, "AnnotationToTest")
206+
assert.Contains(t, totalAnnotations, "SecondAnnotationToTest")
207+
assert.Contains(t, totalAnnotations, "app")
208+
assert.Contains(t, totalAnnotations, "component")
209+
}

0 commit comments

Comments
 (0)