Skip to content

Commit

Permalink
Merge pull request #7652 from geoand/#7650
Browse files Browse the repository at this point in the history
Support Kubernetes expose property via Quarkus configuration
  • Loading branch information
machi1990 committed Mar 7, 2020
2 parents 692796b + 91ad414 commit 2b3ec9c
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 21 deletions.
Expand Up @@ -193,6 +193,12 @@ public class KubernetesConfig implements PlatformConfiguration {
@ConfigItem(defaultValue = "kubernetes")
List<String> deploymentTarget;

/**
* If true, a Kubernetes Ingress will be created
*/
@ConfigItem(defaultValue = "false")
boolean expose;

public Optional<String> getPartOf() {
return partOf;
}
Expand Down Expand Up @@ -306,4 +312,8 @@ public Map<String, ContainerConfig> getContainers() {
return containers;
}

@Override
public boolean isExpose() {
return expose;
}
}
Expand Up @@ -25,10 +25,14 @@
public class KubernetesConfigUtil {

private static final String DEKORATE_PREFIX = "dekorate.";
private static final String QUARKUS_PREFIX = "quarkus.";

private static final Set<String> ALLOWED_GENERATORS = new HashSet<>(
Arrays.asList(KUBERNETES, OPENSHIFT, KNATIVE, DOCKER, S2I));

private static final String EXPOSE_PROPERTY_NAME = "expose";
private static final String[] EXPOSABLE_GENERATORS = { OPENSHIFT, KUBERNETES };

public static List<String> getDeploymentTargets() {
Config config = ConfigProvider.getConfig();
return Arrays.stream(config.getOptionalValue(DEPLOYMENT_TARGET, String.class)
Expand Down Expand Up @@ -74,12 +78,28 @@ public static Map<String, Object> toMap(PlatformConfiguration... platformConfigu
}
}

// hard-coded support for exposed
handleExpose(config, unPrefixed);

result.putAll(unPrefixed);
result.putAll(quarkusPrefixed);
result.putAll(toS2iProperties(quarkusPrefixed));
return result;
}

private static void handleExpose(Config config, Map<String, Object> unPrefixed) {
for (String generator : EXPOSABLE_GENERATORS) {
boolean unprefixedExpose = config.getOptionalValue(generator + "." + EXPOSE_PROPERTY_NAME, Boolean.class)
.orElse(false);
boolean prefixedExpose = config
.getOptionalValue(QUARKUS_PREFIX + generator + "." + EXPOSE_PROPERTY_NAME, Boolean.class)
.orElse(false);
if (unprefixedExpose || prefixedExpose) {
unPrefixed.put(DEKORATE_PREFIX + generator + "." + EXPOSE_PROPERTY_NAME, true);
}
}
}

/**
* Returns the name of the generators that can handle the specified key.
*
Expand Down
Expand Up @@ -186,6 +186,12 @@ public class OpenshiftConfig implements PlatformConfiguration {
@ConfigItem
Map<String, ContainerConfig> containers;

/**
* If true, an Openshift Route will be created
*/
@ConfigItem(defaultValue = "false")
boolean expose;

public Optional<String> getPartOf() {
return partOf;
}
Expand Down Expand Up @@ -299,4 +305,8 @@ public Map<String, ContainerConfig> getContainers() {
return containers;
}

@Override
public boolean isExpose() {
return false;
}
}
Expand Up @@ -66,6 +66,10 @@ public interface PlatformConfiguration {

Map<String, ContainerConfig> getContainers();

default boolean isExpose() {
return false;
}

default String getConfigName() {
return getClass().getSimpleName().replaceAll("Config$", "").toLowerCase();
}
Expand Down
Expand Up @@ -17,6 +17,7 @@
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServiceAccount;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.extensions.Ingress;
import io.quarkus.test.ProdBuildResults;
import io.quarkus.test.ProdModeTestResults;
import io.quarkus.test.QuarkusProdModeTest;
Expand All @@ -42,40 +43,48 @@ public void assertGeneratedResources() throws IOException {
List<HasMetadata> kubernetesList = DeserializationUtil
.deserializeAsList(kubernetesDir.resolve("kubernetes.yml"));

assertThat(kubernetesList).hasSize(3);
assertThat(kubernetesList).hasSize(4);

assertThat(kubernetesList.get(0)).isInstanceOfSatisfying(Deployment.class, d -> {
assertThat(d.getMetadata()).satisfies(m -> {
assertThat(m.getName()).isEqualTo("test-it");
assertThat(m.getLabels()).contains(entry("foo", "bar"));
});
assertThat(kubernetesList).filteredOn(i -> "Deployment".equals(i.getKind())).hasOnlyOneElementSatisfying(i -> {
assertThat(i).isInstanceOfSatisfying(Deployment.class, d -> {
assertThat(d.getMetadata()).satisfies(m -> {
assertThat(m.getName()).isEqualTo("test-it");
assertThat(m.getLabels()).contains(entry("foo", "bar"));
});

assertThat(d.getSpec()).satisfies(deploymentSpec -> {
assertThat(deploymentSpec.getTemplate()).satisfies(t -> {
assertThat(t.getSpec()).satisfies(podSpec -> {
assertThat(podSpec.getContainers()).hasOnlyOneElementSatisfying(container -> {
assertThat(container.getEnv()).extracting("name", "value")
.contains(tuple("MY_ENV_VAR", "SOMEVALUE"));
assertThat(container.getImage())
.isEqualTo("quay.io/grp/kubernetes-with-application-properties:0.1-SNAPSHOT");
assertThat(container.getPorts()).hasOnlyOneElementSatisfying(p -> {
assertThat(p.getContainerPort()).isEqualTo(9090);
assertThat(d.getSpec()).satisfies(deploymentSpec -> {
assertThat(deploymentSpec.getTemplate()).satisfies(t -> {
assertThat(t.getSpec()).satisfies(podSpec -> {
assertThat(podSpec.getContainers()).hasOnlyOneElementSatisfying(container -> {
assertThat(container.getEnv()).extracting("name", "value")
.contains(tuple("MY_ENV_VAR", "SOMEVALUE"));
assertThat(container.getImage())
.isEqualTo("quay.io/grp/kubernetes-with-application-properties:0.1-SNAPSHOT");
assertThat(container.getPorts()).hasOnlyOneElementSatisfying(p -> {
assertThat(p.getContainerPort()).isEqualTo(9090);
});
});
});
});
});
});
});

assertThat(kubernetesList.get(1)).isInstanceOfSatisfying(Service.class, s -> {
assertThat(s.getSpec()).satisfies(spec -> {
assertThat(spec.getPorts()).hasSize(1).hasOnlyOneElementSatisfying(p -> {
assertThat(p.getPort()).isEqualTo(9090);
assertThat(kubernetesList).filteredOn(i -> "Service".equals(i.getKind())).hasOnlyOneElementSatisfying(i -> {
assertThat(i).isInstanceOfSatisfying(Service.class, s -> {
assertThat(s.getSpec()).satisfies(spec -> {
assertThat(spec.getPorts()).hasSize(1).hasOnlyOneElementSatisfying(p -> {
assertThat(p.getPort()).isEqualTo(9090);
});
});
});
});

assertThat(kubernetesList.get(2)).isInstanceOf(ServiceAccount.class);
assertThat(kubernetesList).filteredOn(i -> "ServiceAccount".equals(i.getKind())).hasSize(1)
.hasOnlyElementsOfType(ServiceAccount.class);

assertThat(kubernetesList).filteredOn(i -> "Ingress".equals(i.getKind())).hasSize(1)
.hasOnlyElementsOfType(Ingress.class);
}

}
Expand Up @@ -64,5 +64,7 @@ public void assertGeneratedResources() throws IOException {
});
});
});

assertThat(openshiftList).filteredOn(h -> "Route".equals(h.getKind())).hasSize(1);
}
}
Expand Up @@ -4,3 +4,4 @@ quarkus.kubernetes.labels.foo=bar
quarkus.kubernetes.env-vars.my-env-var.value=SOMEVALUE
quarkus.container-image.group=grp
quarkus.container-image.registry=quay.io
quarkus.kubernetes.expose=true
Expand Up @@ -4,4 +4,5 @@ quarkus.openshift.name=test-it
quarkus.openshift.labels.foo=bar
quarkus.openshift.env-vars.my-env-var.value=SOMEVALUE
quarkus.openshift.group=grp
quarkus.openshift.expose=true
quarkus.s2i.registry=quay.io

0 comments on commit 2b3ec9c

Please sign in to comment.