Skip to content

Commit

Permalink
Merge 79f84e5 into 47f2d6f
Browse files Browse the repository at this point in the history
  • Loading branch information
krzyzy committed Jul 20, 2018
2 parents 47f2d6f + 79f84e5 commit 54363bb
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 1 deletion.
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);
}

}
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();
}
}
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);
}
}
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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class K8sLabelBasedServiceLocator implements ServiceLocator {

private final KubernetesClient kubernetesClient;
private String requiredLabel;
private final String requiredLabel;

public K8sLabelBasedServiceLocator(KubernetesClient kubernetesClient, String requiredLabel) {
this.kubernetesClient = kubernetesClient;
Expand Down

0 comments on commit 54363bb

Please sign in to comment.