forked from fabric8io/fabric8-maven-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix fabric8io#1019: Custom Liveness/Readiness probes are not being cr…
…eated Added CustomProbeEnricher for adding probes via XML config
- Loading branch information
1 parent
2f95153
commit 26d7d2f
Showing
8 changed files
with
214 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/CustomProbeEnricher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Copyright 2016 Red Hat, Inc. | ||
* | ||
* Red Hat licenses this file to you under the Apache License, version | ||
* 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package io.fabric8.maven.enricher.standard; | ||
|
||
import io.fabric8.kubernetes.api.builder.TypedVisitor; | ||
import io.fabric8.kubernetes.api.model.ContainerBuilder; | ||
import io.fabric8.kubernetes.api.model.KubernetesListBuilder; | ||
import io.fabric8.kubernetes.api.model.Probe; | ||
import io.fabric8.maven.core.config.ProbeConfig; | ||
import io.fabric8.maven.core.config.ResourceConfig; | ||
import io.fabric8.maven.core.handler.ProbeHandler; | ||
import io.fabric8.maven.enricher.api.BaseEnricher; | ||
import io.fabric8.maven.enricher.api.MavenEnricherContext; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
public class CustomProbeEnricher extends BaseEnricher { | ||
public CustomProbeEnricher(MavenEnricherContext buildContext) { | ||
super(buildContext, "fmp-customprobe"); | ||
} | ||
|
||
@Override | ||
public void addMissingResources(KubernetesListBuilder builder) { | ||
builder.accept(new TypedVisitor<ContainerBuilder>() { | ||
@Override | ||
public void visit(ContainerBuilder containerBuilder) { | ||
try { | ||
ResourceConfig resourceConfig = getConfiguration().getResource().get(); | ||
|
||
containerBuilder.withReadinessProbe(getProbe(resourceConfig.getReadiness())); | ||
containerBuilder.withLivenessProbe(getProbe(resourceConfig.getLiveness())); | ||
} catch (NoSuchElementException ex) { | ||
// In this case resource is not being configured for plugin, let's just ignore. | ||
} | ||
} | ||
}); | ||
} | ||
|
||
private Probe getProbe(ProbeConfig pc) { | ||
ProbeHandler probeHandler = new ProbeHandler(); | ||
if(pc != null) { | ||
return probeHandler.getProbe(pc); | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
...er/standard/src/test/java/io/fabric8/maven/enricher/standard/CustomProbeEnricherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/** | ||
* Copyright 2016 Red Hat, Inc. | ||
* | ||
* Red Hat licenses this file to you under the Apache License, version | ||
* 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
package io.fabric8.maven.enricher.standard; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.jayway.jsonpath.matchers.JsonPathMatchers; | ||
import io.fabric8.kubernetes.api.model.Container; | ||
import io.fabric8.kubernetes.api.model.ContainerBuilder; | ||
import io.fabric8.kubernetes.api.model.ContainerPortBuilder; | ||
import io.fabric8.kubernetes.api.model.KubernetesList; | ||
import io.fabric8.kubernetes.api.model.KubernetesListBuilder; | ||
import io.fabric8.kubernetes.api.model.PodTemplateBuilder; | ||
import io.fabric8.maven.core.config.ProbeConfig; | ||
import io.fabric8.maven.core.config.ProcessorConfig; | ||
import io.fabric8.maven.core.config.ResourceConfig; | ||
import io.fabric8.maven.core.model.Configuration; | ||
import io.fabric8.maven.core.util.ResourceUtil; | ||
import io.fabric8.maven.docker.config.ImageConfiguration; | ||
import io.fabric8.maven.enricher.api.MavenEnricherContext; | ||
import mockit.Expectations; | ||
import mockit.Mocked; | ||
import org.hamcrest.Matchers; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.TreeMap; | ||
|
||
import static org.junit.Assert.assertThat; | ||
|
||
public class CustomProbeEnricherTest { | ||
@Mocked | ||
private MavenEnricherContext context; | ||
|
||
@Mocked | ||
ImageConfiguration imageConfiguration; | ||
|
||
@Test | ||
public void testReadinessProbeEnrichment() throws JsonProcessingException { | ||
final TreeMap controllerConfig = new TreeMap(); | ||
controllerConfig.put("type", "LoadBalancer"); | ||
ResourceConfig resourceConfig = new ResourceConfig.Builder() | ||
.withReadiness(new ProbeConfig.Builder().getUrl("http://192.168.42.20:8443/").build()) | ||
.build(); | ||
|
||
setupExpectations(controllerConfig, resourceConfig); | ||
|
||
// Enrich | ||
CustomProbeEnricher enricher = new CustomProbeEnricher(context); | ||
KubernetesListBuilder builder = getPodTemplateList(); | ||
enricher.addMissingResources(builder); | ||
|
||
// Validate that the generated resource contains | ||
KubernetesList list = builder.build(); | ||
|
||
String json = ResourceUtil.toJson(list.getItems().get(0)); | ||
assertThat(json, JsonPathMatchers.isJson()); | ||
assertThat(json, JsonPathMatchers.hasJsonPath("$.template.spec.containers[0].readinessProbe.httpGet.host", Matchers.equalTo("192.168.42.20"))); | ||
assertThat(json, JsonPathMatchers.hasJsonPath("$.template.spec.containers[0].readinessProbe.httpGet.port", Matchers.equalTo(8443))); | ||
assertThat(json, JsonPathMatchers.hasJsonPath("$.template.spec.containers[0].readinessProbe.httpGet.path", Matchers.equalTo("/"))); | ||
assertThat(json, JsonPathMatchers.hasJsonPath("$.template.spec.containers[0].readinessProbe.httpGet.scheme", Matchers.equalTo("HTTP"))); | ||
} | ||
|
||
@Test | ||
public void testLivenessProbeEnrichment() throws JsonProcessingException { | ||
final TreeMap controllerConfig = new TreeMap(); | ||
controllerConfig.put("type", "LoadBalancer"); | ||
ResourceConfig resourceConfig = new ResourceConfig.Builder() | ||
.withLiveness(new ProbeConfig.Builder().initialDelaySeconds(5).exec("cat /tmp/probe").build()) | ||
.build(); | ||
|
||
setupExpectations(controllerConfig, resourceConfig); | ||
|
||
// Enrich | ||
CustomProbeEnricher enricher = new CustomProbeEnricher(context); | ||
KubernetesListBuilder builder = getPodTemplateList(); | ||
enricher.addMissingResources(builder); | ||
|
||
// Validate that the generated resources contain desired enrichments | ||
KubernetesList list = builder.build(); | ||
|
||
String json = ResourceUtil.toJson(list.getItems().get(0)); | ||
assertThat(json, JsonPathMatchers.isJson()); | ||
assertThat(json, JsonPathMatchers.hasJsonPath("$.template.spec.containers[0].livenessProbe.initialDelaySeconds", Matchers.equalTo(5))); | ||
assertThat(json, JsonPathMatchers.hasJsonPath("$.template.spec.containers[0].livenessProbe.exec.command[0]", Matchers.equalTo("cat"))); | ||
} | ||
|
||
private KubernetesListBuilder getPodTemplateList() { | ||
Container container = new ContainerBuilder() | ||
.withName("test-port-enricher") | ||
.withImage("test-image") | ||
.withPorts(new ContainerPortBuilder().withContainerPort(80).withProtocol("TCP").build()) | ||
.build(); | ||
PodTemplateBuilder ptb = new PodTemplateBuilder() | ||
.withNewMetadata().withName("test-pod") | ||
.endMetadata() | ||
.withNewTemplate() | ||
.withNewSpec() | ||
.withContainers(container) | ||
.endSpec() | ||
.endTemplate(); | ||
return new KubernetesListBuilder().addToPodTemplateItems(ptb.build()); | ||
} | ||
|
||
private void setupExpectations(final TreeMap controllerConfig, ResourceConfig resourceConfig) { | ||
new Expectations() {{ | ||
|
||
Configuration config = new Configuration.Builder() | ||
.images(Arrays.asList(imageConfiguration)) | ||
.processorConfig(new ProcessorConfig(null, null, Collections.singletonMap("fmp-customprobe", controllerConfig))) | ||
.resource(resourceConfig).build(); | ||
|
||
context.getConfiguration(); | ||
result = config; | ||
}}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters