From e5ec4546cafa91abf70a74719e3ebd49a20f3c68 Mon Sep 17 00:00:00 2001 From: Rohan Kumar Date: Fri, 23 Nov 2018 22:38:49 +0530 Subject: [PATCH] Fix #1019: Custom Liveness/Readiness probes are not being created Added CustomProbeEnricher for adding probes via XML config --- CHANGELOG.md | 1 + .../maven/core/config/ResourceConfig.java | 33 +++++++++++++++++++ .../maven/core/handler/ContainerHandler.java | 1 + .../maven/core/handler/ProbeHandler.java | 10 +++--- .../maven/core/handler/ProbeHandlerTest.java | 8 ++--- .../standard/DefaultControllerEnricher.java | 10 +++--- .../it/env-metadata/expected/kubernetes.yml | 4 +-- it/src/it/env-metadata/expected/openshift.yml | 4 +-- 8 files changed, 53 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f89276a43..7f5336c047 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ After this we will switch probably to real [Semantic Versioning 2.0.0](http://se * Fix 712: Add possibility to configure cluster access fexibly. * Upgraded Jgit to version 5.2.0.201812061821-r - https://github.com/fabric8io/fabric8-maven-plugin/pull/1452 * Fix 222: The SeviceEnricher could check the Docker image configuration for specific labels +* Fix 1019: Custom liveness/readiness probes are not being created ### 3.5-SNAPSHOT * Fix 1021: Avoids empty deployment selector value in generated yaml resource diff --git a/core/src/main/java/io/fabric8/maven/core/config/ResourceConfig.java b/core/src/main/java/io/fabric8/maven/core/config/ResourceConfig.java index c88599fc51..9d0b0293a2 100644 --- a/core/src/main/java/io/fabric8/maven/core/config/ResourceConfig.java +++ b/core/src/main/java/io/fabric8/maven/core/config/ResourceConfig.java @@ -154,6 +154,29 @@ public ConfigMap getConfigMap() { public static class Builder { private ResourceConfig config = new ResourceConfig(); + public Builder() { } + + public Builder(ResourceConfig config) { + if(config != null) { + this.config.env = config.getEnv().orElse(null); + this.config.controllerName = config.getControllerName(); + this.config.imagePullPolicy = config.getImagePullPolicy(); + this.config.replicas = config.getReplicas(); + this.config.liveness = config.getLiveness(); + this.config.readiness = config.getReadiness(); + this.config.annotations = config.getAnnotations(); + this.config.serviceAccount = config.getServiceAccount(); + this.config.configMap = config.getConfigMap(); + this.config.volumes = config.getVolumes(); + this.config.labels = config.getLabels(); + this.config.annotations = config.getAnnotations(); + this.config.secrets = config.getSecrets(); + this.config.services = config.getServices(); + this.config.metrics = config.getMetrics(); + this.config.namespace = config.getNamespace(); + } + } + public Builder env(Map env) { config.env = env; return this; @@ -189,6 +212,16 @@ public Builder withConfigMap(ConfigMap configMap) { return this; } + public Builder withLiveness(ProbeConfig liveness) { + config.liveness = liveness; + return this; + } + + public Builder withReadiness(ProbeConfig readiness) { + config.readiness = readiness; + return this; + } + public ResourceConfig build() { return config; } diff --git a/core/src/main/java/io/fabric8/maven/core/handler/ContainerHandler.java b/core/src/main/java/io/fabric8/maven/core/handler/ContainerHandler.java index 4a623a814f..312d53d8b3 100644 --- a/core/src/main/java/io/fabric8/maven/core/handler/ContainerHandler.java +++ b/core/src/main/java/io/fabric8/maven/core/handler/ContainerHandler.java @@ -89,6 +89,7 @@ private List getEnvVars(ResourceConfig config) { List envVars = KubernetesResourceUtil.convertToEnvVarList(config.getEnv().orElse(Collections.emptyMap())); // TODO: This should go into an extra enricher so that this behaviour can be switched on / off + envVars.removeIf(obj -> obj.getName().equals("KUBERNETES_NAMESPACE")); envVars.add(0, new EnvVarBuilder() .withName("KUBERNETES_NAMESPACE") diff --git a/core/src/main/java/io/fabric8/maven/core/handler/ProbeHandler.java b/core/src/main/java/io/fabric8/maven/core/handler/ProbeHandler.java index 74281b81ee..81aa67908f 100644 --- a/core/src/main/java/io/fabric8/maven/core/handler/ProbeHandler.java +++ b/core/src/main/java/io/fabric8/maven/core/handler/ProbeHandler.java @@ -85,11 +85,11 @@ private HTTPGetAction getHTTPGetAction(String getUrl) { } try { URL url = new URL(getUrl); - return new HTTPGetAction(url.getHost(), - null /* headers */, - url.getPath(), - new IntOrString(url.getPort()), - url.getProtocol()); + return new HTTPGetAction(url.getHost(), + null /* headers */, + url.getPath(), + new IntOrString(url.getPort()), + url.getProtocol().toUpperCase()); } catch (MalformedURLException e) { throw new IllegalArgumentException("Invalid URL " + getUrl + " given for HTTP GET readiness check"); } diff --git a/core/src/test/java/io/fabric8/maven/core/handler/ProbeHandlerTest.java b/core/src/test/java/io/fabric8/maven/core/handler/ProbeHandlerTest.java index 95620d7fde..9322cddb64 100644 --- a/core/src/test/java/io/fabric8/maven/core/handler/ProbeHandlerTest.java +++ b/core/src/test/java/io/fabric8/maven/core/handler/ProbeHandlerTest.java @@ -84,7 +84,7 @@ public void getHTTPProbeWithHTTPURLTest() { assertEquals(null,probe.getHttpGet().getHttpHeaders()); assertEquals("/healthz",probe.getHttpGet().getPath()); assertEquals(8080,probe.getHttpGet().getPort().getIntVal().intValue()); - assertEquals("http",probe.getHttpGet().getScheme()); + assertEquals("HTTP",probe.getHttpGet().getScheme()); assertNull(probe.getExec()); assertNull(probe.getTcpSocket()); } @@ -199,7 +199,7 @@ public void getTCPProbeWithHTTPURLAndPortTest() { assertEquals(null,probe.getHttpGet().getHttpHeaders()); assertEquals("/healthz",probe.getHttpGet().getPath()); assertEquals(8080,probe.getHttpGet().getPort().getIntVal().intValue()); - assertEquals("http",probe.getHttpGet().getScheme()); + assertEquals("HTTP",probe.getHttpGet().getScheme()); } @Test @@ -272,7 +272,7 @@ public void getTCPWithHTTPURLAndWithoutPort() { assertEquals(null,probe.getHttpGet().getHttpHeaders()); assertEquals("/healthz",probe.getHttpGet().getPath()); assertEquals(8080,probe.getHttpGet().getPort().getIntVal().intValue()); - assertEquals("http",probe.getHttpGet().getScheme()); + assertEquals("HTTP",probe.getHttpGet().getScheme()); } @Test @@ -300,4 +300,4 @@ public void getTCPProbeWithInvalidURLTest() { probe = probeHandler.getProbe(probeConfig); } -} \ No newline at end of file +} diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DefaultControllerEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DefaultControllerEnricher.java index d7f213cb77..86912cc72f 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DefaultControllerEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DefaultControllerEnricher.java @@ -97,11 +97,11 @@ public DefaultControllerEnricher(MavenEnricherContext buildContext) { @Override public void addMissingResources(KubernetesListBuilder builder) { final String name = getConfig(Config.name, MavenUtil.createDefaultResourceName(getContext().getGav().getSanitizedArtifactId())); - final ResourceConfig config = new ResourceConfig.Builder() - .controllerName(name) - .imagePullPolicy(getConfig(Config.pullPolicy)) - .withReplicas(Configs.asInt(getConfig(Config.replicaCount))) - .build(); + ResourceConfig config = new ResourceConfig.Builder(getConfiguration().getResource().orElse(null)) + .controllerName(name) + .imagePullPolicy(getConfig(Config.pullPolicy)) + .withReplicas(Configs.asInt(getConfig(Config.replicaCount))) + .build(); final List images = getImages().orElse(Collections.emptyList()); diff --git a/it/src/it/env-metadata/expected/kubernetes.yml b/it/src/it/env-metadata/expected/kubernetes.yml index ca173e904a..8d9e2f3812 100644 --- a/it/src/it/env-metadata/expected/kubernetes.yml +++ b/it/src/it/env-metadata/expected/kubernetes.yml @@ -75,12 +75,12 @@ items: spec: containers: - env: + - name: MY_ENV_key + value: MY_ENV_value - name: KUBERNETES_NAMESPACE valueFrom: fieldRef: fieldPath: metadata.namespace - - name: MY_ENV_key - value: MY_ENV_value image: "@matches('fabric8/fabric8-maven-sample-env-metadata:.*$')@" imagePullPolicy: IfNotPresent name: spring-boot diff --git a/it/src/it/env-metadata/expected/openshift.yml b/it/src/it/env-metadata/expected/openshift.yml index daccea990a..e6fb774a66 100644 --- a/it/src/it/env-metadata/expected/openshift.yml +++ b/it/src/it/env-metadata/expected/openshift.yml @@ -74,12 +74,12 @@ items: spec: containers: - env: + - name: MY_ENV_key + value: MY_ENV_value - name: KUBERNETES_NAMESPACE valueFrom: fieldRef: fieldPath: metadata.namespace - - name: MY_ENV_key - value: MY_ENV_value image: "@matches('fabric8/fabric8-maven-sample-env-metadata:.*$')@" imagePullPolicy: IfNotPresent name: spring-boot