From f627e9535fcadf729ebeba48a59f0be7f90277a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Muzik=C3=A1=C5=99?= Date: Thu, 1 Jun 2023 13:39:41 +0200 Subject: [PATCH] Add support to the Operator for setting default labels on Keycloak pods (#20661) Closes #20625 --- operator/pom.xml | 10 ++++++-- .../java/org/keycloak/operator/Config.java | 4 +++ .../controllers/KeycloakDeployment.java | 7 +++++- .../src/main/kubernetes/kustomization.yml | 3 +-- .../resources/application-rhbk.properties | 7 ++++++ .../integration/KeycloakDeploymentTest.java | 25 +++++++++++++++++++ .../testsuite/unit/PodTemplateTest.java | 7 ++++++ .../src/test/resources/application.properties | 3 +++ pom.xml | 12 +++++++++ 9 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 operator/src/main/resources/application-rhbk.properties diff --git a/operator/pom.xml b/operator/pom.xml index 11e570b5ee..bbb4fd0701 100644 --- a/operator/pom.xml +++ b/operator/pom.xml @@ -289,6 +289,12 @@ + + + + my-value + + @@ -317,9 +323,9 @@ - native + operator-prod - native + rhbk diff --git a/operator/src/main/java/org/keycloak/operator/Config.java b/operator/src/main/java/org/keycloak/operator/Config.java index e54c356078..9dcdf70d74 100644 --- a/operator/src/main/java/org/keycloak/operator/Config.java +++ b/operator/src/main/java/org/keycloak/operator/Config.java @@ -19,6 +19,8 @@ package org.keycloak.operator; import io.smallrye.config.ConfigMapping; +import java.util.Map; + /** * @author Vaclav Muzikar */ @@ -29,5 +31,7 @@ public interface Config { interface Keycloak { String image(); String imagePullPolicy(); + + Map podLabels(); } } diff --git a/operator/src/main/java/org/keycloak/operator/controllers/KeycloakDeployment.java b/operator/src/main/java/org/keycloak/operator/controllers/KeycloakDeployment.java index 0fa0cdf4e4..80a748bd50 100644 --- a/operator/src/main/java/org/keycloak/operator/controllers/KeycloakDeployment.java +++ b/operator/src/main/java/org/keycloak/operator/controllers/KeycloakDeployment.java @@ -378,7 +378,12 @@ public class KeycloakDeployment extends OperatorManagedResource implements Statu baseDeployment.getMetadata().setNamespace(getNamespace()); baseDeployment.getSpec().getSelector().setMatchLabels(Constants.DEFAULT_LABELS); baseDeployment.getSpec().setReplicas(keycloakCR.getSpec().getInstances()); - baseDeployment.getSpec().getTemplate().getMetadata().setLabels(Constants.DEFAULT_LABELS); + + Map labels = new HashMap<>(Constants.DEFAULT_LABELS); + if (operatorConfig.keycloak().podLabels() != null) { + labels.putAll(operatorConfig.keycloak().podLabels()); + } + baseDeployment.getSpec().getTemplate().getMetadata().setLabels(labels); Container container = baseDeployment.getSpec().getTemplate().getSpec().getContainers().get(0); var customImage = Optional.ofNullable(keycloakCR.getSpec().getImage()); diff --git a/operator/src/main/kubernetes/kustomization.yml b/operator/src/main/kubernetes/kustomization.yml index d36e04c87d..f3c53b1141 100644 --- a/operator/src/main/kubernetes/kustomization.yml +++ b/operator/src/main/kubernetes/kustomization.yml @@ -8,6 +8,5 @@ resources: - kubernetes/keycloakrealmimports.k8s.keycloak.org-v1.yml - kubernetes/kubernetes.yml -# patchesStrategicMerge -patches: +patchesStrategicMerge: - kubernetes/minikube.yml diff --git a/operator/src/main/resources/application-rhbk.properties b/operator/src/main/resources/application-rhbk.properties new file mode 100644 index 0000000000..d817376958 --- /dev/null +++ b/operator/src/main/resources/application-rhbk.properties @@ -0,0 +1,7 @@ +operator.keycloak.pod-labels."com.company"=Red_Hat +operator.keycloak.pod-labels."rht.prod_name"=Red_Hat_Runtimes +operator.keycloak.pod-labels."rht.prod_ver"=${SSO_QUARTER:sso_quarter_placeholder} +operator.keycloak.pod-labels."rht.comp"=SSO +operator.keycloak.pod-labels."rht.comp_ver"=${SSO_VER:${quarkus.application.version}} +operator.keycloak.pod-labels."rht.subcomp"=SSO_Server +operator.keycloak.pod-labels."rht.subcomp_t"=application \ No newline at end of file diff --git a/operator/src/test/java/org/keycloak/operator/testsuite/integration/KeycloakDeploymentTest.java b/operator/src/test/java/org/keycloak/operator/testsuite/integration/KeycloakDeploymentTest.java index 12618404ef..27f58beff2 100644 --- a/operator/src/test/java/org/keycloak/operator/testsuite/integration/KeycloakDeploymentTest.java +++ b/operator/src/test/java/org/keycloak/operator/testsuite/integration/KeycloakDeploymentTest.java @@ -27,6 +27,7 @@ import io.fabric8.kubernetes.api.model.apps.StatefulSetSpecBuilder; import io.quarkus.logging.Log; import io.quarkus.test.junit.QuarkusTest; import org.awaitility.Awaitility; +import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfSystemProperty; import org.keycloak.operator.Constants; @@ -622,6 +623,30 @@ public class KeycloakDeploymentTest extends BaseOperatorTest { } } + @Test + public void testPreconfiguredPodLabels() { + Assumptions.assumeTrue(operatorDeployment == OperatorDeployment.local, + "Skipping the test when Operator deployed remotely to keep stuff simple, it's just SmallRye, we don't need to retest it"); + + try { + var kc = getDefaultKeycloakDeployment(); + deployKeycloak(k8sclient, kc, true); + + // labels are set in test/resources/application.properties + var labels = k8sclient.apps().statefulSets().inNamespace(namespace).withName(kc.getMetadata().getName()).get() + .getSpec().getTemplate().getMetadata().getLabels(); + + var expected = Map.of( + "test.label", "foobar", + "testLabelWithExpression", "my-value" + ); + assertThat(labels).containsAllEntriesOf(expected); + } catch (Exception e) { + savePodLogs(); + throw e; + } + } + private void handleFakeImagePullSecretCreation(Keycloak keycloakCR, String secretDescriptorFilename) { diff --git a/operator/src/test/java/org/keycloak/operator/testsuite/unit/PodTemplateTest.java b/operator/src/test/java/org/keycloak/operator/testsuite/unit/PodTemplateTest.java index f436c6d978..0df4642947 100644 --- a/operator/src/test/java/org/keycloak/operator/testsuite/unit/PodTemplateTest.java +++ b/operator/src/test/java/org/keycloak/operator/testsuite/unit/PodTemplateTest.java @@ -33,6 +33,9 @@ import org.keycloak.operator.crds.v2alpha1.deployment.spec.HostnameSpecBuilder; import org.keycloak.operator.crds.v2alpha1.deployment.spec.HttpSpecBuilder; import org.keycloak.operator.crds.v2alpha1.deployment.spec.UnsupportedSpec; +import java.util.Collections; +import java.util.Map; + import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -53,6 +56,10 @@ public class PodTemplateTest { public String imagePullPolicy() { return "Never"; } + @Override + public Map podLabels() { + return Collections.emptyMap(); + } }; } }; diff --git a/operator/src/test/resources/application.properties b/operator/src/test/resources/application.properties index 639358ee59..998495f629 100644 --- a/operator/src/test/resources/application.properties +++ b/operator/src/test/resources/application.properties @@ -5,3 +5,6 @@ quarkus.operator-sdk.start-operator=false quarkus.log.level=INFO quarkus.log.category."io.javaoperatorsdk".level=DEBUG + +operator.keycloak.pod-labels."test.label"=foobar +operator.keycloak.pod-labels."testLabelWithExpression"=${OPERATOR_TEST_LABEL_EXPRESSION} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 2481f565e5..d93b647fb0 100644 --- a/pom.xml +++ b/pom.xml @@ -2146,6 +2146,18 @@ + + operator-prod + + + operator-prod + + + + operator + + + doclint-java8-disable