Skip to content

Commit

Permalink
Fallback specialized kubernetes config to vanilla kubernetes
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Jul 3, 2023
1 parent 8bcd0a9 commit 16451cb
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
4 changes: 4 additions & 0 deletions extensions/kubernetes/openshift/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
<artifactId>quarkus-junit5-internal</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.smallrye.config</groupId>
<artifactId>smallrye-config-source-yaml</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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<String, String>() {
@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);
} else if (name.startsWith("quarkus.minikube.")) {
return "quarkus.kubernetes." + name.substring(17);
}
return name;
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.quarkus.kubernetes.runtime.config.KubernetesConfigFallback

0 comments on commit 16451cb

Please sign in to comment.