-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
judge-d-agent/src/main/java/com/hltech/contracts/judged/agent/hltech/HLTechBeanFactory.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,25 @@ | ||
package com.hltech.contracts.judged.agent.hltech; | ||
|
||
import com.hltech.contracts.judged.agent.ServiceLocator; | ||
import feign.Feign; | ||
import io.fabric8.kubernetes.client.DefaultKubernetesClient; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
|
||
@Configuration | ||
@Profile("hltech") | ||
public class HLTechBeanFactory { | ||
|
||
@Bean | ||
public KubernetesClient kubernetesClient() { | ||
return new DefaultKubernetesClient(); | ||
} | ||
|
||
@Bean | ||
public ServiceLocator serviceLocator(KubernetesClient kubernetesClient, Feign feign) { | ||
return new HLTechServiceLocator(kubernetesClient, feign); | ||
} | ||
|
||
} |
104 changes: 104 additions & 0 deletions
104
...-d-agent/src/main/java/com/hltech/contracts/judged/agent/hltech/HLTechServiceLocator.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,104 @@ | ||
package com.hltech.contracts.judged.agent.hltech; | ||
|
||
import com.hltech.contracts.judged.agent.ServiceLocator; | ||
import feign.Feign; | ||
import feign.Target; | ||
import io.fabric8.kubernetes.api.model.Container; | ||
import io.fabric8.kubernetes.api.model.ContainerPort; | ||
import io.fabric8.kubernetes.api.model.Pod; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Collection; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class HLTechServiceLocator implements ServiceLocator { | ||
|
||
private static final Integer DEFAULT_CONTAINER_VERSION_PORT = 9999; | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(HLTechServiceLocator.class); | ||
|
||
private final KubernetesClient kubernetesClient; | ||
private Feign feign; | ||
|
||
public HLTechServiceLocator(KubernetesClient kubernetesClient, Feign feign) { | ||
this.kubernetesClient = kubernetesClient; | ||
this.feign = feign; | ||
} | ||
|
||
@Override | ||
public Set<Service> locateServices() { | ||
return getPods().stream() | ||
.map(pod -> { | ||
try{ | ||
return createService(pod); | ||
} | ||
catch(KubernetesEnvironmentException exception) { | ||
return null; | ||
} | ||
}) | ||
.filter(Objects::nonNull) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
private Collection<Pod> getPods() { | ||
return kubernetesClient.pods().inAnyNamespace().list().getItems(); | ||
} | ||
|
||
private Service createService(Pod pod) { | ||
try { | ||
String podName = getPodName(pod); | ||
String podIP = getPodIP(pod); | ||
Integer podVersionPort = getPodVersionPort(pod).orElse(DEFAULT_CONTAINER_VERSION_PORT); | ||
String podVersion = getPodVersion(podIP, podVersionPort); | ||
|
||
return new Service(podName, podVersion); | ||
} | ||
catch(Exception exception) { | ||
LOGGER.debug("Exception during service resolution for pod: {}", pod.getMetadata().getName()); | ||
throw new KubernetesEnvironmentException("Exception during service resolution", exception); | ||
} | ||
} | ||
|
||
private String getPodIP(Pod pod) { | ||
return pod.getStatus().getPodIP(); | ||
} | ||
|
||
private Optional<Integer> getPodVersionPort(Pod pod) { | ||
Optional<Container> container = pod.getSpec().getContainers().stream() | ||
.filter(cont -> cont.getName().equals(getPodName(pod))) | ||
.findFirst(); | ||
|
||
if (!container.isPresent()) { | ||
return Optional.empty(); | ||
} | ||
|
||
Optional<ContainerPort> port = container.get().getPorts().stream() | ||
.filter(containerPort -> "monitoring".equals(containerPort.getName())) | ||
.findFirst(); | ||
|
||
if (!port.isPresent()) { | ||
return Optional.empty(); | ||
} | ||
|
||
return Optional.ofNullable(port.get().getContainerPort()); | ||
} | ||
|
||
private String getPodName(Pod pod) { | ||
return pod | ||
.getMetadata() | ||
.getLabels() | ||
.get("app"); | ||
} | ||
|
||
private String getPodVersion(String podIP, Integer podVersionPort) { | ||
PodClient podClient = feign.newInstance( | ||
new Target.HardCodedTarget<>(PodClient.class, "http://" + podIP + ":" + podVersionPort)); | ||
|
||
return podClient.getInfo().getBody().get("build").get("version").asText(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...rc/main/java/com/hltech/contracts/judged/agent/hltech/KubernetesEnvironmentException.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,8 @@ | ||
package com.hltech.contracts.judged.agent.hltech; | ||
|
||
public class KubernetesEnvironmentException extends RuntimeException { | ||
|
||
public KubernetesEnvironmentException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
judge-d-agent/src/main/java/com/hltech/contracts/judged/agent/hltech/PodClient.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,12 @@ | ||
package com.hltech.contracts.judged.agent.hltech; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
|
||
public interface PodClient { | ||
|
||
@RequestMapping(value = "/info", method = RequestMethod.GET) | ||
ResponseEntity<JsonNode> getInfo(); | ||
} |
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