Skip to content

Commit

Permalink
Fix eclipse-jkube#1706: Add support for configuration of prometheus.i…
Browse files Browse the repository at this point in the history
…o/path annotation

Ported PR fabric8io/fabric8-maven-plugin#1707
  • Loading branch information
rohanKanojia authored and manusa committed Nov 27, 2019
1 parent e6661ab commit 68c8b7a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import io.fabric8.kubernetes.api.builder.TypedVisitor;
import io.fabric8.kubernetes.api.model.KubernetesListBuilder;
Expand All @@ -35,12 +36,14 @@ public class PrometheusEnricher extends BaseEnricher {

static final String ANNOTATION_PROMETHEUS_PORT = "prometheus.io/port";
static final String ANNOTATION_PROMETHEUS_SCRAPE = "prometheus.io/scrape";
static final String ANNOTATION_PROMETHEUS_PATH = "prometheus.io/path";

static final String ENRICHER_NAME = "jkube-prometheus";
static final String PROMETHEUS_PORT = "9779";

private enum Config implements Configs.Key {
prometheusPort;
prometheusPort,
prometheusPath;

public String def() { return d; } protected String d;
}
Expand All @@ -56,13 +59,20 @@ public void create(PlatformMode platformMode, KubernetesListBuilder builder) {
public void visit(ServiceBuilder serviceBuilder) {
String prometheusPort = findPrometheusPort();
if (StringUtils.isNotBlank(prometheusPort)) {
log.verbose("Add prometheus.io annotations: %s=%s, %s=%s",
ANNOTATION_PROMETHEUS_SCRAPE, "true",
ANNOTATION_PROMETHEUS_PORT, prometheusPort);

Map<String, String> annotations = new HashMap<>();
MapUtil.putIfAbsent(annotations, ANNOTATION_PROMETHEUS_PORT, prometheusPort);
MapUtil.putIfAbsent(annotations, ANNOTATION_PROMETHEUS_SCRAPE, "true");
String prometheusPath = getConfig(Config.prometheusPath);
if (StringUtils.isNotBlank(prometheusPath)) {
MapUtil.putIfAbsent(annotations, ANNOTATION_PROMETHEUS_PATH, prometheusPath);
}

log.verbose("Adding prometheus.io annotations: %s",
annotations.entrySet()
.stream()
.map(Object::toString)
.collect(Collectors.joining(", ")));
serviceBuilder.editMetadata().addToAnnotations(annotations).endMetadata();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public class PrometheusEnricherTest {
ImageConfiguration imageConfiguration;

private enum Config implements Configs.Key {
prometheusPort;
prometheusPort,
prometheusPath;
public String def() { return d; } protected String d;
}

Expand Down Expand Up @@ -148,4 +149,45 @@ public void testNoDefinedPrometheusPort() throws Exception {

assertEquals(Collections.emptyMap(), annotations);
}

@Test
public void testCustomPrometheusPath() throws Exception {
final ProcessorConfig config = new ProcessorConfig(
null,
null,
Collections.singletonMap(
PrometheusEnricher.ENRICHER_NAME,
new TreeMap(Collections.singletonMap(
Config.prometheusPath.name(),
"/prometheus")
)
)
);

final BuildConfiguration imageConfig = new BuildConfiguration.Builder()
.ports(Arrays.asList(PrometheusEnricher.PROMETHEUS_PORT))
.build();


// Setup mock behaviour
new Expectations() {{
context.getConfiguration();
result = new Configuration.Builder()
.processorConfig(config)
.images(Arrays.asList(imageConfiguration))
.build();

imageConfiguration.getBuildConfiguration(); result = imageConfig;
}};

KubernetesListBuilder builder = new KubernetesListBuilder().withItems(new ServiceBuilder().withNewMetadata().withName("foo").endMetadata().build());
PrometheusEnricher enricher = new PrometheusEnricher(context);
enricher.create(PlatformMode.kubernetes, builder);
Map<String, String> annotations = builder.buildFirstItem().getMetadata().getAnnotations();

assertEquals(3, annotations.size());
assertEquals("9779", annotations.get(PrometheusEnricher.ANNOTATION_PROMETHEUS_PORT));
assertEquals("true", annotations.get(PrometheusEnricher.ANNOTATION_PROMETHEUS_SCRAPE));
assertEquals("/prometheus", annotations.get(PrometheusEnricher.ANNOTATION_PROMETHEUS_PATH));
}
}

0 comments on commit 68c8b7a

Please sign in to comment.