Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Fix #1706: Add support for configuration of prometheus.io/path annota… #1707

Merged
merged 1 commit into from
Sep 19, 2019
Merged
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ After this we will switch probably to real [Semantic Versioning 2.0.0](http://se
* Fix #1697: NullpointerException when trying to apply custom resources
* Fix #1676: Support for latest kubernetes client
* Feature #1536: Java Image Builder Support
* Fix #1704: fabric8-build failing on openshift
* Fix #1704: fabric8-build failing on openshift
* Feature #1706: Prometheus Enricher; Configuration support for Prometheus path

### 4.2.0 (01-08-2019)
* Fix #1638: Remove enrichAll parameter from ImageChangeTriggerEnricher
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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 @@ -38,12 +39,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 = "f8-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 @@ -59,13 +62,19 @@ public void create(PlatformMode platformMode, KubernetesListBuilder builder) {
public void visit(ServiceBuilder serviceBuilder) {
String prometheusPort = findPrometheusPort();
if (StringUtils.isNotBlank(prometheusPort)) {
log.verbose(Logger.LogVerboseCategory.BUILD, "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(Logger.LogVerboseCategory.BUILD, "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 @@ -44,7 +44,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 @@ -150,4 +151,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 BuildImageConfiguration imageConfig = new BuildImageConfiguration.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));
}
}