From 333f35f2421c354ebf460d8e96cdfcc78b6ec0e0 Mon Sep 17 00:00:00 2001 From: David Turanski Date: Wed, 19 Jan 2022 14:34:19 -0500 Subject: [PATCH] Get the actuator endpoint from the liveness probe --- .../KubernetesActuatorTemplate.java | 5 +++-- .../KubernetesAppInstanceStatus.java | 21 +++++++++++++------ .../KubernetesAutoConfiguration.java | 4 +++- .../KubernetesActuatorTemplateTests.java | 1 + 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/springframework/cloud/deployer/spi/kubernetes/KubernetesActuatorTemplate.java b/src/main/java/org/springframework/cloud/deployer/spi/kubernetes/KubernetesActuatorTemplate.java index e0522d17..1557dc8a 100644 --- a/src/main/java/org/springframework/cloud/deployer/spi/kubernetes/KubernetesActuatorTemplate.java +++ b/src/main/java/org/springframework/cloud/deployer/spi/kubernetes/KubernetesActuatorTemplate.java @@ -32,7 +32,8 @@ public KubernetesActuatorTemplate(RestTemplate restTemplate, AppDeployer appDepl } protected String actuatorUrlForInstance(AppInstanceStatus appInstanceStatus) { - return String.format("http://%s:%d/actuator", appInstanceStatus.getAttributes().get("pod.ip"), - Integer.valueOf(appInstanceStatus.getAttributes().get("actuator.port"))); + return String.format("http://%s:%d/%s", appInstanceStatus.getAttributes().get("pod.ip"), + Integer.valueOf(appInstanceStatus.getAttributes().get("actuator.port")), + appInstanceStatus.getAttributes().get("actuator.path")); } } diff --git a/src/main/java/org/springframework/cloud/deployer/spi/kubernetes/KubernetesAppInstanceStatus.java b/src/main/java/org/springframework/cloud/deployer/spi/kubernetes/KubernetesAppInstanceStatus.java index 205add6e..fa31d9a8 100644 --- a/src/main/java/org/springframework/cloud/deployer/spi/kubernetes/KubernetesAppInstanceStatus.java +++ b/src/main/java/org/springframework/cloud/deployer/spi/kubernetes/KubernetesAppInstanceStatus.java @@ -16,9 +16,9 @@ package org.springframework.cloud.deployer.spi.kubernetes; -import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import io.fabric8.kubernetes.api.model.Container; import io.fabric8.kubernetes.api.model.ContainerStatus; @@ -123,13 +123,14 @@ private DeploymentState mapState() { @Override public Map getAttributes() { - Map result = new HashMap<>(); + ConcurrentHashMap result = new ConcurrentHashMap<>(); if (pod != null) { result.put("pod.name", pod.getMetadata().getName()); result.put("pod.startTime", pod.getStatus().getStartTime()); result.put("pod.ip", pod.getStatus().getPodIP()); - result.put("actuator.port", determineActuatorPort(pod)); + result.put("actuator.path", determineActuatorPathFromLivenessProbe(pod)); + result.put("actuator.port", determineActuatorPortFromLivenessProbe(pod, result.get("actuator.path"))); result.put("host.ip", pod.getStatus().getHostIP()); result.put("phase", pod.getStatus().getPhase()); result.put(AbstractKubernetesDeployer.SPRING_APP_KEY.replace('-', '.'), @@ -179,14 +180,22 @@ public Map getAttributes() { return result; } - private String determineActuatorPort(Pod pod) { + private String determineActuatorPathFromLivenessProbe(Pod pod) { + return pod.getSpec().getContainers().stream().filter( (Container container) -> + container.getLivenessProbe() != null && + container.getLivenessProbe().getHttpGet() != null) + .findFirst() + .map(container -> container.getLivenessProbe().getHttpGet().getPath()) + .orElse("/actuator"); + } + private String determineActuatorPortFromLivenessProbe(Pod pod, String path) { return pod.getSpec().getContainers().stream().filter( (Container container) -> container.getLivenessProbe() != null && container.getLivenessProbe().getHttpGet() != null && - container.getLivenessProbe().getHttpGet().getPath().startsWith("/actuator")) + container.getLivenessProbe().getHttpGet().getPath().equals(path)) .findFirst() .map(container -> container.getLivenessProbe().getHttpGet().getPort().getStrVal()) - .orElse(new IntOrString("unknown").getStrVal()); + .orElse(new IntOrString(8080).getStrVal()); } } diff --git a/src/main/java/org/springframework/cloud/deployer/spi/kubernetes/KubernetesAutoConfiguration.java b/src/main/java/org/springframework/cloud/deployer/spi/kubernetes/KubernetesAutoConfiguration.java index 1e1b312c..2bb8947f 100644 --- a/src/main/java/org/springframework/cloud/deployer/spi/kubernetes/KubernetesAutoConfiguration.java +++ b/src/main/java/org/springframework/cloud/deployer/spi/kubernetes/KubernetesAutoConfiguration.java @@ -75,11 +75,13 @@ public ContainerFactory containerFactory() { } @Bean - ActuatorOperations actuatorSupport(RestTemplate actuatorRestTemplate, AppDeployer appDeployer) { + @ConditionalOnMissingBean(ActuatorOperations.class) + ActuatorOperations actuatorOperations(RestTemplate actuatorRestTemplate, AppDeployer appDeployer) { return new KubernetesActuatorTemplate(actuatorRestTemplate, appDeployer); } @Bean + @ConditionalOnMissingBean RestTemplate actuatorRestTemplate() { //TODO: Configure security return new RestTemplate(); diff --git a/src/test/java/org/springframework/cloud/deployer/spi/kubernetes/KubernetesActuatorTemplateTests.java b/src/test/java/org/springframework/cloud/deployer/spi/kubernetes/KubernetesActuatorTemplateTests.java index 079d505c..be6e8b1c 100644 --- a/src/test/java/org/springframework/cloud/deployer/spi/kubernetes/KubernetesActuatorTemplateTests.java +++ b/src/test/java/org/springframework/cloud/deployer/spi/kubernetes/KubernetesActuatorTemplateTests.java @@ -111,6 +111,7 @@ void setUp() { Map attributes = new HashMap<>(); attributes.put("pod.ip", "127.0.0.1"); attributes.put("actuator.port", String.valueOf(mockActuator.getPort())); + attributes.put("actuator.path", "/actuator"); attributes.put("guid", "test-application-0"); when(appInstanceStatus.getAttributes()).thenReturn(attributes); when(appInstanceStatus.getState()).thenReturn(DeploymentState.deployed);