下面列出了io.fabric8.kubernetes.api.model.ExecAction#io.fabric8.kubernetes.api.model.Probe 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Probe discoverQuarkusHealthCheck(int initialDelay) {
if (!getContext().hasDependency("io.quarkus", "quarkus-smallrye-health")) {
return null;
}
return new ProbeBuilder()
.withNewHttpGet()
.withNewPort(asInteger(getConfig(Config.port)))
.withPath(getConfig(Config.path))
.withScheme(getConfig(Config.scheme))
.endHttpGet()
.withFailureThreshold(asInteger(getConfig(Config.failureThreshold)))
.withSuccessThreshold(asInteger(getConfig(Config.successThreshold)))
.withInitialDelaySeconds(initialDelay)
.build();
}
Probe create() {
HTTPGetActionBuilder httpGetActionBuilder = new HTTPGetActionBuilder()
.withPath(getProbePath())
.withNewPort(getPort());
List<HTTPHeader> httpHeaders = getHttpHeaders();
if (!httpHeaders.isEmpty()) {
httpGetActionBuilder.withHttpHeaders(httpHeaders);
}
return new ProbeBuilder()
.withHttpGet(httpGetActionBuilder.build())
.withTimeoutSeconds(getTimeout())
.withInitialDelaySeconds(getInitialDelay())
.withPeriodSeconds(getPeriod())
.build();
}
@Test
public void testDefaultInitialDelayForLivenessAndReadiness() {
SpringBootHealthCheckEnricher enricher = new SpringBootHealthCheckEnricher(context);
withProjectProperties(new Properties());
new Expectations(){{
context.getProjectClassLoaders();
result = new ProjectClassLoaders(
new URLClassLoader(new URL[0], AbstractSpringBootHealthCheckEnricherTestSupport.class.getClassLoader())) {
@Override
public boolean isClassInCompileClasspath(boolean all, String... clazz) {
return true;
}
};
}};
Probe probe = enricher.getReadinessProbe();
assertNotNull(probe);
assertEquals(10, probe.getInitialDelaySeconds().intValue());
probe = enricher.getLivenessProbe();
assertNotNull(probe);
assertEquals(180, probe.getInitialDelaySeconds().intValue());
}
private String describe(Probe probe) {
StringBuilder desc = new StringBuilder("probe");
if (probe.getHttpGet() != null) {
desc.append(" on port ");
desc.append(probe.getHttpGet().getPort().getIntVal());
desc.append(", path='");
desc.append(probe.getHttpGet().getPath());
desc.append("'");
desc.append(", scheme='");
desc.append(probe.getHttpGet().getScheme());
desc.append("'");
}
if (probe.getInitialDelaySeconds() != null) {
desc.append(", with initial delay ");
desc.append(probe.getInitialDelaySeconds());
desc.append(" seconds");
}
if (probe.getPeriodSeconds() != null) {
desc.append(", with period ");
desc.append(probe.getPeriodSeconds());
desc.append(" seconds");
}
return desc.toString();
}
@Test
public void testWithServerPortAndManagementPortAndManagementContextPathAndServletPathAndActuatorDefaultBasePath() {
SpringBootHealthCheckEnricher enricher = new SpringBootHealthCheckEnricher(context);
Properties props = new Properties();
props.put(propertyHelper.getServerPortPropertyKey(), "8282");
props.put(propertyHelper.getManagementPortPropertyKey(), "8383");
props.put(propertyHelper.getServerContextPathPropertyKey(), "/p1");
props.put(propertyHelper.getManagementContextPathPropertyKey(), "/p2");
props.put(propertyHelper.getServletPathPropertyKey(), "/servlet");
props.put(propertyHelper.getActuatorBasePathPropertyKey(), "/p3");
Probe probe = enricher.buildProbe(props, 10, null, null, 3, 1);
assertNotNull(probe);
assertNotNull(probe.getHttpGet());
assertEquals("/p2/p3" + "/health", probe.getHttpGet().getPath());
assertEquals(8383, probe.getHttpGet().getPort().getIntVal().intValue());
}
@Test
public void noEnrichmentIfNoPath() {
// given
WebAppHealthCheckEnricher enricher = new WebAppHealthCheckEnricher(context);
setupExpectations(new HashMap<>());
// when
Probe probeLiveness = enricher.getLivenessProbe();
Probe probeReadiness = enricher.getReadinessProbe();
// then
assertThat(probeLiveness).isNull();
assertThat(probeReadiness).isNull();
}
List<Container> getContainers(ResourceConfig config, List<ImageConfiguration> images) {
List<Container> ret = new ArrayList<>();
for (ImageConfiguration imageConfig : images) {
if (imageConfig.getBuildConfiguration() != null) {
Probe livenessProbe = probeHandler.getProbe(config.getLiveness());
Probe readinessProbe = probeHandler.getProbe(config.getReadiness());
Container container = new ContainerBuilder()
.withName(KubernetesResourceUtil.extractContainerName(this.groupArtifactVersion, imageConfig))
.withImage(getImageName(imageConfig))
.withImagePullPolicy(getImagePullPolicy(config))
.withEnv(getEnvVars(config))
.withSecurityContext(createSecurityContext(config))
.withPorts(getContainerPorts(imageConfig))
.withVolumeMounts(getVolumeMounts(config))
.withLivenessProbe(livenessProbe)
.withReadinessProbe(readinessProbe)
.build();
ret.add(container);
}
}
return ret;
}
@Test
public void testDefaultConfiguration_Enabled() {
VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context);
final Properties props = new Properties();
props.put("vertx.health.path", "/ping");
setupExpectations(props);
Probe probe = enricher.getLivenessProbe();
assertNotNull(probe);
assertNull(probe.getHttpGet().getHost());
assertEquals(probe.getHttpGet().getScheme(), "HTTP");
assertEquals(probe.getHttpGet().getPort().getIntVal().intValue(), 8080);
assertEquals(probe.getHttpGet().getPath(), "/ping");
probe = enricher.getReadinessProbe();
assertNotNull(probe);
assertEquals(probe.getHttpGet().getScheme(), "HTTP");
assertNull(probe.getHttpGet().getHost());
assertEquals(probe.getHttpGet().getPort().getIntVal().intValue(), 8080);
assertEquals(probe.getHttpGet().getPath(), "/ping");
}
@Test
public void testWithCustomConfigurationComingFromConf() {
final Map<String, Object> config = createFakeConfig("{\"path\":\"health\",\"port\":\"1234\",\"scheme\":\"https\"}");
setupExpectations(config);
VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context);
Probe probe = enricher.getLivenessProbe();
assertNotNull(probe);
assertNull(probe.getHttpGet().getHost());
assertEquals(probe.getHttpGet().getScheme(), "HTTPS");
assertEquals(probe.getHttpGet().getPort().getIntVal().intValue(), 1234);
assertEquals(probe.getHttpGet().getPath(), "/health");
probe = enricher.getReadinessProbe();
assertNotNull(probe);
assertEquals(probe.getHttpGet().getScheme(), "HTTPS");
assertNull(probe.getHttpGet().getHost());
assertEquals(probe.getHttpGet().getPort().getIntVal().intValue(), 1234);
assertEquals(probe.getHttpGet().getPath(), "/health");
}
@Test
public void testWithCustomConfigurationForLivenessAndReadinessComingFromConf() {
final Map<String, Object> config = createFakeConfig(
"{\"path\":\"health\",\"port\":\"1234\",\"scheme\":\"https\",\"readiness\":{\"path\":\"/ready\"}}");
setupExpectations(config);
VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context);
Probe probe = enricher.getLivenessProbe();
assertNotNull(probe);
assertNull(probe.getHttpGet().getHost());
assertEquals(probe.getHttpGet().getScheme(), "HTTPS");
assertEquals(probe.getHttpGet().getPort().getIntVal().intValue(), 1234);
assertEquals(probe.getHttpGet().getPath(), "/health");
probe = enricher.getReadinessProbe();
assertNotNull(probe);
assertEquals(probe.getHttpGet().getScheme(), "HTTPS");
assertNull(probe.getHttpGet().getHost());
assertEquals(probe.getHttpGet().getPort().getIntVal().intValue(), 1234);
assertEquals(probe.getHttpGet().getPath(), "/ready");
}
@Test
public void testCustomConfiguration() {
VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context);
final Properties props = new Properties();
props.put("vertx.health.path", "/health");
props.put("vertx.health.port", " 8081 ");
props.put("vertx.health.scheme", " https");
setupExpectations(props);
Probe probe = enricher.getLivenessProbe();
assertNotNull(probe);
assertNull(probe.getHttpGet().getHost());
assertThat(probe.getHttpGet().getScheme()).isEqualToIgnoringCase("https");
assertEquals(probe.getHttpGet().getPort().getIntVal().intValue(), 8081);
assertEquals(probe.getHttpGet().getPath(), "/health");
probe = enricher.getReadinessProbe();
assertNotNull(probe);
assertThat(probe.getHttpGet().getScheme()).isEqualToIgnoringCase("https");
assertNull(probe.getHttpGet().getHost());
assertEquals(probe.getHttpGet().getPort().getIntVal().intValue(), 8081);
assertEquals(probe.getHttpGet().getPath(), "/health");
}
@Test
public void testWithHttpHeaders() {
final Map<String, Object> config = createFakeConfig("{\"path\":\"health\",\"headers\":{\"X-Header\":\"X\",\"Y-Header\":\"Y\"}}");
setupExpectations(config);
VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context);
Probe probe = enricher.getLivenessProbe();
assertNotNull(probe);
assertEquals(probe.getHttpGet().getPath(), "/health");
assertThat(probe.getHttpGet().getPort().getIntVal()).isEqualTo(8080);
assertThat(probe.getHttpGet().getHttpHeaders()).hasSize(2)
.contains(new HTTPHeader("X-Header", "X"), new HTTPHeader("Y-Header", "Y"));
probe = enricher.getReadinessProbe();
assertNotNull(probe);
assertEquals(probe.getHttpGet().getPath(), "/health");
assertThat(probe.getHttpGet().getPort().getIntVal()).isEqualTo(8080);
assertThat(probe.getHttpGet().getHttpHeaders()).hasSize(2)
.contains(new HTTPHeader("X-Header", "X"), new HTTPHeader("Y-Header", "Y"));
}
@Test
public void testReadinessDisabledUsingConfig() {
final Map<String, Object> config = createFakeConfig(
"{\"readiness\":{\"path\":\"\"},\"path\":\"/ping\"}");
setupExpectations(config);
VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context);
Probe probe = enricher.getLivenessProbe();
assertNotNull(probe);
assertEquals(probe.getHttpGet().getPath(), "/ping");
probe = enricher.getReadinessProbe();
assertNull(probe);
}
@Test
public void testTCPSocketUsingUserProperties() {
VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context);
final Properties props = new Properties();
props.put("vertx.health.type", "tcp");
props.put("vertx.health.port", "1234");
props.put("vertx.health.readiness.port", "1235");
setupExpectations(props);
Probe probe = enricher.getLivenessProbe();
assertNotNull(probe);
assertEquals(probe.getTcpSocket().getPort().getIntVal().intValue(), 1234);
probe = enricher.getReadinessProbe();
assertNotNull(probe);
assertEquals(probe.getTcpSocket().getPort().getIntVal().intValue(), 1235);
}
@Test
public void testTCPSocketUsingConfig() {
VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context);
final Map<String, Object> config = createFakeConfig(
"{\"type\":\"tcp\",\"liveness\":{\"port\":\"1234\"},\"readiness\":{\"port\":\"1235\"}}");
setupExpectations(config);
Probe probe = enricher.getLivenessProbe();
assertNotNull(probe);
assertEquals(probe.getTcpSocket().getPort().getIntVal().intValue(), 1234);
probe = enricher.getReadinessProbe();
assertNotNull(probe);
assertEquals(probe.getTcpSocket().getPort().getIntVal().intValue(), 1235);
}
@Test
public void testTCPSocketUsingUserPropertiesAndPortName() {
VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context);
final Properties props = new Properties();
props.put("vertx.health.type", "tcp");
props.put("vertx.health.port-name", "health");
props.put("vertx.health.readiness.port-name", "ready");
setupExpectations(props);
Probe probe = enricher.getLivenessProbe();
assertNotNull(probe);
assertEquals(probe.getTcpSocket().getPort().getStrVal(), "health");
probe = enricher.getReadinessProbe();
assertNotNull(probe);
assertEquals(probe.getTcpSocket().getPort().getStrVal(), "ready");
}
private static void applyDesiredProbeSettings(Probe actualProbe, Probe desiredProbe) {
if (actualProbe != null && desiredProbe != null) {
if (desiredProbe.getInitialDelaySeconds() != null) {
actualProbe.setInitialDelaySeconds(desiredProbe.getInitialDelaySeconds());
}
if (desiredProbe.getPeriodSeconds() != null) {
actualProbe.setPeriodSeconds(desiredProbe.getPeriodSeconds());
}
if (desiredProbe.getTimeoutSeconds() != null) {
actualProbe.setTimeoutSeconds(desiredProbe.getTimeoutSeconds());
}
if (desiredProbe.getSuccessThreshold() != null) {
actualProbe.setSuccessThreshold(desiredProbe.getSuccessThreshold());
}
if (desiredProbe.getFailureThreshold() != null) {
actualProbe.setFailureThreshold(desiredProbe.getFailureThreshold());
}
}
}
@Test
public void testExecUsingConfig() {
VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context);
final Map<String, Object> config = createFakeConfig(
"{\"type\":\"exec\"," +
"\"command\": {\"arg\":[\"/bin/sh\", \"-c\",\"touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600\"]}" +
"}");
setupExpectations(config);
Probe probe = enricher.getLivenessProbe();
assertNotNull(probe);
assertThat(probe.getExec().getCommand()).hasSize(3);
probe = enricher.getReadinessProbe();
assertNotNull(probe);
assertThat(probe.getExec().getCommand()).hasSize(3);
}
@Test
public void testExecUsingConfigLivenessDisabled() {
VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context);
final Map<String, Object> config = createFakeConfig(
"{\"type\":\"exec\"," +
"\"readiness\":{" +
"\"command\": {\"arg\":[\"/bin/sh\", \"-c\",\"touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600\"]}}," +
"\"liveness\":{}" +
"}");
setupExpectations(config);
Probe probe = enricher.getLivenessProbe();
assertNull(probe);
probe = enricher.getReadinessProbe();
assertNotNull(probe);
assertThat(probe.getExec().getCommand()).hasSize(3);
}
@Test
public void testExecUsingConfigReadinessDisabled() {
VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context);
final Map<String, Object> config = createFakeConfig(
"{\"type\":\"exec\"," +
"\"liveness\":{" +
"\"command\": {\"arg\":[\"/bin/sh\", \"-c\",\"touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600\"]}}," +
"\"readiness\":{}" +
"}");
setupExpectations(config);
Probe probe = enricher.getLivenessProbe();
assertThat(probe.getExec().getCommand()).hasSize(3);
assertNotNull(probe);
probe = enricher.getReadinessProbe();
assertNull(probe);
}
@Test
public void testWithServerPortAndManagementPortAndManagementContextPathAndActuatorDefaultBasePathSlash() {
SpringBootHealthCheckEnricher enricher = new SpringBootHealthCheckEnricher(context);
Properties props = new Properties();
props.put(propertyHelper.getServerPortPropertyKey(), "8282");
props.put(propertyHelper.getManagementPortPropertyKey(), "8383");
props.put(propertyHelper.getServerContextPathPropertyKey(), "/p1");
props.put(propertyHelper.getManagementContextPathPropertyKey(), "/");
props.put(propertyHelper.getActuatorBasePathPropertyKey(), "/");
Probe probe = enricher.buildProbe(props, 10, null, null, 3, 1);
assertNotNull(probe);
assertNotNull(probe.getHttpGet());
assertEquals("/health", probe.getHttpGet().getPath());
assertEquals(8383, probe.getHttpGet().getPort().getIntVal().intValue());
}
@Test
public void testThatWeCanUSeDifferentTypesForLivenessAndReadiness() {
VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context);
final Map<String, Object> config = createFakeConfig(
"{\"liveness\":{" +
"\"type\":\"exec\",\"command\":{\"arg\":\"ls\"}" +
"},\"readiness\":{\"path\":\"/ping\"}}");
setupExpectations(config);
Probe probe = enricher.getLivenessProbe();
assertNotNull(probe);
assertNotNull(probe.getExec());
probe = enricher.getReadinessProbe();
assertNotNull(probe);
assertNotNull(probe.getHttpGet());
}
@Test
public void testThatGenericUserPropertiesOverrideGenericConfig() {
VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context);
final Map<String, Object> config = createFakeConfig(
"{\"type\":\"exec\",\"command\":{\"arg\":\"ls\"}}"
);
Properties properties = new Properties();
properties.put("vertx.health.type", "tcp");
properties.put("vertx.health.port", "1234");
setupExpectations(properties,config);
Probe probe = enricher.getReadinessProbe();
assertThat(probe).isNotNull();
assertThat(probe.getTcpSocket()).isNotNull();
assertThat(probe.getExec()).isNull();
assertThat(probe.getTcpSocket().getPort().getIntVal()).isEqualTo(1234);
probe = enricher.getLivenessProbe();
assertThat(probe).isNotNull();
assertThat(probe.getTcpSocket()).isNotNull();
assertThat(probe.getExec()).isNull();
assertThat(probe.getTcpSocket().getPort().getIntVal()).isEqualTo(1234);
}
@Test
public void testThatSpecificConfigOverrideGenericConfig() {
VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context);
final Map<String, Object> config = createFakeConfig(
"{\"liveness\":{\"path\":\"/live\"}," +
"\"readiness\":{\"path\":\"/ping\",\"port-name\":\"ready\"}," +
"\"path\":\"/health\",\"port-name\":\"health\"}");
setupExpectations(config);
Probe probe = enricher.getReadinessProbe();
assertThat(probe).isNotNull();
assertThat(probe.getHttpGet()).isNotNull();
assertThat(probe.getHttpGet().getPort().getStrVal()).isEqualTo("ready");
assertThat(probe.getHttpGet().getPath()).isEqualTo("/ping");
probe = enricher.getLivenessProbe();
assertThat(probe).isNotNull();
assertThat(probe.getHttpGet()).isNotNull();
assertThat(probe.getHttpGet().getPort().getStrVal()).isEqualTo("health");
assertThat(probe.getHttpGet().getPath()).isEqualTo("/live");
}
@Test
public void testThatSpecificUserPropertiesOverrideGenericUserProperties() {
VertxHealthCheckEnricher enricher = new VertxHealthCheckEnricher(context);
final Map<String, Object> config = createFakeConfig(
"{\"path\":\"/ping\",\"type\":\"http\"}");
Properties properties = new Properties();
properties.put("vertx.health.readiness.type", "tcp");
properties.put("vertx.health.readiness.port", "1234");
properties.put("vertx.health.port", "1235");
properties.put("vertx.health.liveness.type", "tcp");
properties.put("vertx.health.liveness.port", "1236");
setupExpectations(properties, config);
Probe probe = enricher.getReadinessProbe();
assertThat(probe).isNotNull();
assertThat(probe.getTcpSocket()).isNotNull();
assertThat(probe.getHttpGet()).isNull();
assertThat(probe.getTcpSocket().getPort().getIntVal()).isEqualTo(1234);
probe = enricher.getLivenessProbe();
assertThat(probe).isNotNull();
assertThat(probe.getHttpGet()).isNull();
assertThat(probe.getTcpSocket()).isNotNull();
assertThat(probe.getTcpSocket().getPort().getIntVal()).isEqualTo(1236);
}
private Probe discoverThorntailHealthCheck(int initialDelay) {
if (getContext().hasDependency(IO_THORNTAIL, "thorntail-kernel")) {
// if there's thorntail-kernel, it's Thorntail v4
return null;
}
if (getContext().hasDependency(IO_THORNTAIL, "monitor")
|| getContext().hasDependency(IO_THORNTAIL, "microprofile-health")) {
Integer port = getPort();
// scheme must be in upper case in k8s
String scheme = getScheme().toUpperCase();
String path = getPath();
return new ProbeBuilder()
.withNewHttpGet().withNewPort(port).withPath(path).withScheme(scheme).endHttpGet()
.withFailureThreshold(getFailureThreshold())
.withSuccessThreshold(getSuccessThreshold())
.withInitialDelaySeconds(initialDelay).build();
}
return null;
}
@Test
public void testCustomPropertiesForLivenessAndReadiness() {
Map<String, TreeMap> globalConfig = new HashMap<>();
TreeMap<String, String> enricherConfig = new TreeMap<>();
globalConfig.put(SpringBootHealthCheckEnricher.ENRICHER_NAME, enricherConfig);
enricherConfig.put("readinessProbeInitialDelaySeconds", "30");
enricherConfig.put("readinessProbePeriodSeconds", "40");
enricherConfig.put("livenessProbeInitialDelaySeconds", "460");
enricherConfig.put("livenessProbePeriodSeconds", "50");
final ProcessorConfig config = new ProcessorConfig(null,null, globalConfig);
new Expectations() {{
context.getConfiguration(); result = Configuration.builder().processorConfig(config).build();
context.getProjectClassLoaders();
result = new ProjectClassLoaders(
new URLClassLoader(new URL[0], AbstractSpringBootHealthCheckEnricherTestSupport.class.getClassLoader())) {
@Override
public boolean isClassInCompileClasspath(boolean all, String... clazz) {
return true;
}
};
}};
withProjectProperties(new Properties());
SpringBootHealthCheckEnricher enricher = new SpringBootHealthCheckEnricher(context);
Probe probe = enricher.getReadinessProbe();
assertNotNull(probe);
assertEquals(30, probe.getInitialDelaySeconds().intValue());
assertEquals(40, probe.getPeriodSeconds().intValue());
probe = enricher.getLivenessProbe();
assertNotNull(probe);
assertEquals(460, probe.getInitialDelaySeconds().intValue());
assertEquals(50, probe.getPeriodSeconds().intValue());
}
static Probe createExecProbe(List<String> command, io.strimzi.api.kafka.model.Probe userProbe) {
Probe probe = newProbeBuilder(userProbe).withNewExec()
.withCommand(command)
.endExec()
.build();
log.trace("Created exec probe {}", probe);
return probe;
}
private Probe generateProbe(ProbeModel probeModel) {
if (null == probeModel) {
return null;
}
TCPSocketAction tcpSocketAction = new TCPSocketActionBuilder()
.withNewPort(probeModel.getPort())
.build();
return new ProbeBuilder()
.withInitialDelaySeconds(probeModel.getInitialDelaySeconds())
.withPeriodSeconds(probeModel.getPeriodSeconds())
.withTcpSocket(tcpSocketAction)
.build();
}
@Test
public void testSchemeWithServerPortAndManagementKeystore() {
SpringBootHealthCheckEnricher enricher = new SpringBootHealthCheckEnricher(context);
Properties props = new Properties();
props.put(propertyHelper.getServerPortPropertyKey(), "8080");
props.put(propertyHelper.getManagementKeystorePropertyKey(), "classpath:keystore.p12");
Probe probe = enricher.buildProbe(props, 10, null, null, 3, 1);
assertNotNull(probe);
assertNotNull(probe.getHttpGet());
assertEquals("HTTP", probe.getHttpGet().getScheme());
assertEquals(8080, probe.getHttpGet().getPort().getIntVal().intValue());
}