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

Commit

Permalink
Adding pod annotations support
Browse files Browse the repository at this point in the history
  • Loading branch information
hypery2k authored Apr 20, 2022
1 parent 9e4bd42 commit d3a2315
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 12 deletions.
5 changes: 5 additions & 0 deletions deploy/crds/keycloak.org_keycloaks_crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions deploy/examples/keycloak/keycloak-with-metrics.yaml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions pkg/apis/keycloak/v1alpha1/keycloak_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/keycloak/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 11 additions & 6 deletions pkg/model/keycloak_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 11 additions & 6 deletions pkg/model/rhsso_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions pkg/model/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
22 changes: 22 additions & 0 deletions pkg/model/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit d3a2315

Please sign in to comment.