Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the "app" label to change the name derived from pod generate-name #50

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class KubernetesInstanceDiscovery implements InstanceDiscovery {

public static final String PORT_ANNOTATION_KEY = "breakerbox-port";
public static final String POD_HASH_LABEL_KEY = "pod-template-hash";
public static final String APP_LABEL_KEY = "app";

private final KubernetesClient client;

Expand All @@ -42,7 +43,7 @@ public KubernetesInstanceDiscovery(KubernetesClient client) {
@Override
public Collection<Instance> getInstanceList() throws Exception {
LOGGER.info("Starting Kubernetes instance discovery using master URL: {}", client.getMasterUrl());
return client.pods().inAnyNamespace()
return client.pods()
.list()
.getItems().stream()
.filter(pod -> pod.getMetadata().getAnnotations() != null) // Ignore pods without annotations
Expand All @@ -66,12 +67,17 @@ public Collection<Instance> getInstanceList() throws Exception {
private static String extractClusterNameFor(Pod pod) {
String podBaseName = pod.getMetadata().getGenerateName();
// Remove auto-generated hashes, if there are any
if (pod.getMetadata().getLabels() != null && pod.getMetadata().getLabels().containsKey(POD_HASH_LABEL_KEY)) {
String hash = pod.getMetadata().getLabels().get(POD_HASH_LABEL_KEY);
podBaseName = podBaseName.replace(hash + "-", "");
if (pod.getMetadata().getLabels() != null) {
if (pod.getMetadata().getLabels().containsKey(APP_LABEL_KEY)) {
return pod.getMetadata().getLabels().get(APP_LABEL_KEY);
}
if (pod.getMetadata().getLabels().containsKey(POD_HASH_LABEL_KEY)) {
String hash = pod.getMetadata().getLabels().get(POD_HASH_LABEL_KEY);
podBaseName = podBaseName.replace(hash + "-", "");
}
}
// Pod's base names always end with a '-', remove it
podBaseName = podBaseName.substring(0, podBaseName.length()-1);
podBaseName = podBaseName.substring(0, podBaseName.length() - 1);
return String.format("%s-%s", pod.getMetadata().getNamespace(), podBaseName);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.yammer.breakerbox.turbine.tests;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.netflix.turbine.discovery.Instance;
import com.yammer.breakerbox.turbine.KubernetesInstanceDiscovery;
Expand Down Expand Up @@ -28,7 +29,6 @@ public class KubernetesInstanceDiscoveryTest {

@Mock DefaultKubernetesClient client;
@Mock ClientMixedOperation<Pod, PodList, DoneablePod, ClientPodResource<Pod, DoneablePod>> podsOperation;
@Mock ClientMixedOperation<Pod, PodList, DoneablePod, ClientPodResource<Pod, DoneablePod>> podsAnyNsOperation;
@Mock PodList podList;

private KubernetesInstanceDiscovery discovery;
Expand All @@ -37,8 +37,7 @@ public class KubernetesInstanceDiscoveryTest {
@Before
public void setupDiscovery() {
stub(client.pods()).toReturn(podsOperation);
stub(podsOperation.inAnyNamespace()).toReturn(podsAnyNsOperation);
stub(podsAnyNsOperation.list()).toReturn(podList);
stub(podsOperation.list()).toReturn(podList);

Pod serviceA1 = new Pod();
serviceA1.setMetadata(new ObjectMeta());
Expand Down Expand Up @@ -176,4 +175,28 @@ public void removesPodTemplateHashFromClusterName() throws Exception {
.findAny();
assertThat(instanceOptional.isPresent()).isTrue();
}

@Test
public void prefersAppLabelToExtractedName() throws Exception {
Pod deploymentPod = new Pod();
deploymentPod.setMetadata(new ObjectMeta());
deploymentPod.setStatus(new PodStatus());
deploymentPod.getStatus().setPodIP("10.116.0.8");
deploymentPod.getStatus().setPhase("Running");
deploymentPod.getMetadata().setAnnotations(
Maps.newHashMap(KubernetesInstanceDiscovery.PORT_ANNOTATION_KEY, "8080"));
deploymentPod.getMetadata().setLabels(ImmutableMap.of(
KubernetesInstanceDiscovery.POD_HASH_LABEL_KEY, "5432543253",
KubernetesInstanceDiscovery.APP_LABEL_KEY, "test-service")
);
deploymentPod.getMetadata().setName("service-depl-5432543253-097fsd");
deploymentPod.getMetadata().setGenerateName("service-depl-5432543253-");
deploymentPod.getMetadata().setNamespace("production");
pods.add(deploymentPod);
Optional<Instance> instanceOptional = discovery.getInstanceList().stream()
.filter(instance -> instance.getCluster().equals("test-service"))
.findAny();
assertThat(instanceOptional.isPresent()).isTrue();

}
}