From 3ad41640f6650551e60a0959fbab4626569656f7 Mon Sep 17 00:00:00 2001 From: Roberto Cortez Date: Mon, 3 Jul 2023 15:16:06 +0100 Subject: [PATCH] Fallback specialized kubernetes config to vanilla kubernetes --- .../kubernetes/openshift/deployment/pom.xml | 5 ++ .../config/OpenshiftConfigFallbackTest.java | 47 +++++++++++++++++++ .../config/KubernetesConfigFallback.java | 21 +++++++++ ...io.smallrye.config.ConfigSourceInterceptor | 1 + 4 files changed, 74 insertions(+) create mode 100644 extensions/kubernetes/openshift/deployment/src/test/java/io/quarkus/openshift/deployment/config/OpenshiftConfigFallbackTest.java create mode 100644 extensions/kubernetes/vanilla/runtime/src/main/java/io/quarkus/kubernetes/runtime/config/KubernetesConfigFallback.java create mode 100644 extensions/kubernetes/vanilla/runtime/src/main/resources/META-INF/services/io.smallrye.config.ConfigSourceInterceptor diff --git a/extensions/kubernetes/openshift/deployment/pom.xml b/extensions/kubernetes/openshift/deployment/pom.xml index d4c4f315d6628..d6db4ae0a1272 100644 --- a/extensions/kubernetes/openshift/deployment/pom.xml +++ b/extensions/kubernetes/openshift/deployment/pom.xml @@ -49,6 +49,11 @@ quarkus-junit5-internal test + + io.smallrye.config + smallrye-config-source-yaml + test + diff --git a/extensions/kubernetes/openshift/deployment/src/test/java/io/quarkus/openshift/deployment/config/OpenshiftConfigFallbackTest.java b/extensions/kubernetes/openshift/deployment/src/test/java/io/quarkus/openshift/deployment/config/OpenshiftConfigFallbackTest.java new file mode 100644 index 0000000000000..dabca1dacb383 --- /dev/null +++ b/extensions/kubernetes/openshift/deployment/src/test/java/io/quarkus/openshift/deployment/config/OpenshiftConfigFallbackTest.java @@ -0,0 +1,47 @@ +package io.quarkus.openshift.deployment.config; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import java.nio.file.Path; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.ProdBuildResults; +import io.quarkus.test.ProdModeTestResults; +import io.quarkus.test.QuarkusProdModeTest; +import io.smallrye.config.source.yaml.YamlConfigSource; + +public class OpenshiftConfigFallbackTest { + @RegisterExtension + static final QuarkusProdModeTest TEST = new QuarkusProdModeTest() + .setApplicationName("config") + .setApplicationVersion("0.1-SNAPSHOT") + .overrideConfigKey("quarkus.kubernetes.replicas", "10") + .overrideConfigKey("quarkus.openshift.version", "999-SNAPSHOT") + .overrideConfigKey("quarkus.openshift.labels.app", "openshift") + .setRun(true); + + @ProdBuildResults + private ProdModeTestResults prodModeTestResults; + + @Test + void configFallback() throws Exception { + Path kubernetesDir = prodModeTestResults.getBuildDir().resolve("kubernetes"); + YamlConfigSource kubernetes = new YamlConfigSource(kubernetesDir.resolve("kubernetes.yml").toUri().toURL()); + YamlConfigSource openshift = new YamlConfigSource(kubernetesDir.resolve("openshift.yml").toUri().toURL()); + + // Only in Kubernetes, must fallback to Openshift + assertEquals("10", kubernetes.getValue("spec.replicas")); + assertEquals("10", openshift.getValue("spec.replicas")); + + // In both, each should retain the value + assertEquals("0.1-SNAPSHOT", kubernetes.getValue("spec.template.metadata.labels.\"app.kubernetes.io/version\"")); + assertEquals("999-SNAPSHOT", openshift.getValue("spec.template.metadata.labels.\"app.kubernetes.io/version\"")); + + // Only in Openshift + assertNull(kubernetes.getValue("spec.template.metadata.labels.app")); + assertEquals("openshift", openshift.getValue("spec.template.metadata.labels.app")); + } +} diff --git a/extensions/kubernetes/vanilla/runtime/src/main/java/io/quarkus/kubernetes/runtime/config/KubernetesConfigFallback.java b/extensions/kubernetes/vanilla/runtime/src/main/java/io/quarkus/kubernetes/runtime/config/KubernetesConfigFallback.java new file mode 100644 index 0000000000000..4bfbb4ef6750d --- /dev/null +++ b/extensions/kubernetes/vanilla/runtime/src/main/java/io/quarkus/kubernetes/runtime/config/KubernetesConfigFallback.java @@ -0,0 +1,21 @@ +package io.quarkus.kubernetes.runtime.config; + +import java.util.function.Function; + +import io.smallrye.config.FallbackConfigSourceInterceptor; + +public class KubernetesConfigFallback extends FallbackConfigSourceInterceptor { + public KubernetesConfigFallback() { + super(new Function() { + @Override + public String apply(final String name) { + if (name.startsWith("quarkus.openshift.")) { + return "quarkus.kubernetes." + name.substring(18); + } else if (name.startsWith("quarkus.knative.")) { + return "quarkus.kubernetes." + name.substring(16); + } + return name; + } + }); + } +} diff --git a/extensions/kubernetes/vanilla/runtime/src/main/resources/META-INF/services/io.smallrye.config.ConfigSourceInterceptor b/extensions/kubernetes/vanilla/runtime/src/main/resources/META-INF/services/io.smallrye.config.ConfigSourceInterceptor new file mode 100644 index 0000000000000..9928c53030f95 --- /dev/null +++ b/extensions/kubernetes/vanilla/runtime/src/main/resources/META-INF/services/io.smallrye.config.ConfigSourceInterceptor @@ -0,0 +1 @@ +io.quarkus.kubernetes.runtime.config.KubernetesConfigFallback