Skip to content

Commit

Permalink
Get the actuator endpoint from the liveness probe
Browse files Browse the repository at this point in the history
  • Loading branch information
dturanski committed Jan 19, 2022
1 parent d16ea64 commit 333f35f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -123,13 +123,14 @@ private DeploymentState mapState() {

@Override
public Map<String, String> getAttributes() {
Map<String, String> result = new HashMap<>();
ConcurrentHashMap<String, String> 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('-', '.'),
Expand Down Expand Up @@ -179,14 +180,22 @@ public Map<String, String> 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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ void setUp() {
Map<String, String> 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);
Expand Down

0 comments on commit 333f35f

Please sign in to comment.