Skip to content

Commit

Permalink
OSFUSE-391: f-m-p annotates services with prometheus.io annotations b…
Browse files Browse the repository at this point in the history
…y default
  • Loading branch information
lburgazzoli committed Oct 31, 2016
1 parent 5bb4b73 commit c694974
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@

package io.fabric8.maven.core.handler;

import io.fabric8.kubernetes.api.Annotations;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.fabric8.kubernetes.api.model.IntOrString;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServiceBuilder;
Expand All @@ -27,12 +32,6 @@
import io.fabric8.maven.core.util.MapUtil;
import io.fabric8.utils.Strings;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author roland
* @since 08/04/16
Expand Down Expand Up @@ -102,13 +101,6 @@ public List<Service> getServices(List<ServiceConfig> services) {

private Map<String, String> getAnnotations(ServiceConfig service) {
Map<String, String> serviceAnnotations = new HashMap<>();
// TODO: Use a prometheus enricher
// lets add the prometheus annotations if required
String prometheusPort = findPrometheusPort(service.getPorts());
if (Strings.isNotBlank(prometheusPort)) {
MapUtil.putIfAbsent(serviceAnnotations, Annotations.Management.PROMETHEUS_PORT, prometheusPort);
MapUtil.putIfAbsent(serviceAnnotations, Annotations.Management.PROMETHEUS_SCRAPE, "true");
}
return serviceAnnotations;
}

Expand All @@ -119,20 +111,4 @@ private Map<String, String> getLabels(ServiceConfig service) {
}
return labels;
}

private String findPrometheusPort(List<ServiceConfig.Port> ports) {
for (ServiceConfig.Port port : ports) {
int number = port.getPort();
boolean valid = number == 9779;
String name = port.getName();
if (name != null && name.toLowerCase().equals("prometheus")) {
valid = true;
}
if (valid) {
return "" + number;
}
}
return null;
}

}
24 changes: 24 additions & 0 deletions doc/src/main/asciidoc/inc/_enricher.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ fabric8-maven-plugin comes with a set of enrichers which are enabled by default.
| <<f8-icon>>
| Add an URL to icons for well known application types.

| <<f8-prometheus>>
| Add Prometheus annotations.

| <<fmp-controller>>
| Create default controller (replication controller, replica set or deployment) if missing.

Expand Down Expand Up @@ -119,6 +122,27 @@ Fabric8 enrichers are used for providing the connection to other components of t
[[f8-icon]]
==== f8-icon

[[f8-prometheus]]
==== f8-prometheus

This enricher adds Prometheus annotation like:

[source,yaml]
----
apiVersion: v1
kind: List
items:
- apiVersion: v1
kind: Service
metadata:
annotations:
prometheus.io/scrape: "true"
prometheus.io/port: 9779
----

By default the enricher inspects the images' BuildConfiguration and add the annotations if the port 9779 is listed.
You can force the plugin to add annotations by setting enricher's config ```prometheusPort```

== Enricher API

_howto write your own enricher and install them_
5 changes: 5 additions & 0 deletions enricher/fabric8/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
<artifactId>org.eclipse.jgit</artifactId>
</dependency>

<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2016 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package io.fabric8.maven.enricher.fabric8;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.fabric8.kubernetes.api.Annotations;
import io.fabric8.maven.core.util.Configs;
import io.fabric8.maven.core.util.MapUtil;
import io.fabric8.maven.docker.config.ImageConfiguration;
import io.fabric8.maven.enricher.api.BaseEnricher;
import io.fabric8.maven.enricher.api.EnricherContext;
import io.fabric8.maven.enricher.api.Kind;
import io.fabric8.utils.Strings;

public class PrometheusEnricher extends BaseEnricher {
static final String ENRICHER_NAME = "f8-prometheus";
static final String PROMETHEUS_PORT = "9779";

private enum Config implements Configs.Key {
prometheusPort;

public String def() { return d; } protected String d;
}

public PrometheusEnricher(EnricherContext buildContext) {
super(buildContext, ENRICHER_NAME);
}

@Override
public Map<String, String> getAnnotations(Kind kind) {
if (kind.isService()) {
String prometheusPort = findPrometheusPort();
if (Strings.isNotBlank(prometheusPort)) {
log.info("Add prometheus.io annotations: %s=%s, %s=%S",
Annotations.Management.PROMETHEUS_SCRAPE, "true",
Annotations.Management.PROMETHEUS_PORT, prometheusPort);

Map<String, String> annotations = new HashMap<>();
MapUtil.putIfAbsent(annotations, Annotations.Management.PROMETHEUS_PORT, prometheusPort);
MapUtil.putIfAbsent(annotations, Annotations.Management.PROMETHEUS_SCRAPE, "true");
return annotations;
}
}

return super.getAnnotations(kind);
}

private String findPrometheusPort() {
String prometheusPort = getConfig(Config.prometheusPort);
if (!Strings.isNotBlank(prometheusPort)) {
for (ImageConfiguration configuration : getContext().getImages()) {
List<String> ports = configuration.getBuildConfiguration().getPorts();
if (ports != null && ports.contains(PROMETHEUS_PORT)) {
prometheusPort = PROMETHEUS_PORT;
break;
}
}
}

return prometheusPort;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ io.fabric8.maven.enricher.fabric8.IconEnricher
io.fabric8.maven.enricher.fabric8.ExposeEnricher,1000
io.fabric8.maven.enricher.fabric8.SpringBootHealthCheckEnricher
io.fabric8.maven.enricher.fabric8.KarafHealthCheckEnricher
io.fabric8.maven.enricher.fabric8.PrometheusEnricher
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Copyright 2016 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package io.fabric8.maven.enricher.fabric8;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;

import io.fabric8.kubernetes.api.Annotations;
import io.fabric8.maven.core.config.ProcessorConfig;
import io.fabric8.maven.core.util.Configs;
import io.fabric8.maven.docker.config.BuildImageConfiguration;
import io.fabric8.maven.docker.config.ImageConfiguration;
import io.fabric8.maven.enricher.api.EnricherContext;
import io.fabric8.maven.enricher.api.Kind;
import mockit.Expectations;
import mockit.Mocked;
import mockit.integration.junit4.JMockit;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

@RunWith(JMockit.class)
public class PrometheusEnricherTest {

@Mocked
private EnricherContext context;
@Mocked
ImageConfiguration imageConfiguration;

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

// *******************************
// Tests
// *******************************

@Test
public void testCustomPrometheusPort() throws Exception {
final ProcessorConfig config = new ProcessorConfig(
null,
null,
Collections.singletonMap(
PrometheusEnricher.ENRICHER_NAME,
new TreeMap(Collections.singletonMap(
Config.prometheusPort.name(),
"1234")
)
)
);

// Setup mock behaviour
new Expectations() {{
context.getConfig(); result = config;
}};

PrometheusEnricher enricher = new PrometheusEnricher(context);
Map<String, String> annotations = enricher.getAnnotations(Kind.SERVICE);

assertEquals(2, annotations.size());
assertEquals("1234", annotations.get(Annotations.Management.PROMETHEUS_PORT));
assertEquals("true", annotations.get(Annotations.Management.PROMETHEUS_SCRAPE));
}

@Test
public void testDetectPrometheusPort() throws Exception {
final ProcessorConfig config = new ProcessorConfig(
null,
null,
Collections.singletonMap(
PrometheusEnricher.ENRICHER_NAME,
new TreeMap()
)
);

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


// Setup mock behaviour
new Expectations() {{
context.getConfig(); result = config;
imageConfiguration.getBuildConfiguration(); result = imageConfig;
context.getImages(); result = Arrays.asList(imageConfiguration);
}};

PrometheusEnricher enricher = new PrometheusEnricher(context);
Map<String, String> annotations = enricher.getAnnotations(Kind.SERVICE);

assertEquals(2, annotations.size());
assertEquals("9779", annotations.get(Annotations.Management.PROMETHEUS_PORT));
assertEquals("true", annotations.get(Annotations.Management.PROMETHEUS_SCRAPE));
}

@Test
public void testNoDefinedPrometheusPort() throws Exception {
final ProcessorConfig config = new ProcessorConfig(
null,
null,
Collections.singletonMap(
PrometheusEnricher.ENRICHER_NAME,
new TreeMap()
)
);

final BuildImageConfiguration imageConfig = new BuildImageConfiguration.Builder()
.build();

// Setup mock behaviour
new Expectations() {{
context.getConfig(); result = config;
imageConfiguration.getBuildConfiguration(); result = imageConfig;
context.getImages(); result = Arrays.asList(imageConfiguration);
}};

PrometheusEnricher enricher = new PrometheusEnricher(context);
Map<String, String> annotations = enricher.getAnnotations(Kind.SERVICE);

assertNull(annotations);
}
}

0 comments on commit c694974

Please sign in to comment.