-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OSFUSE-391: f-m-p annotates services with prometheus.io annotations b…
…y default
- Loading branch information
1 parent
5bb4b73
commit c694974
Showing
6 changed files
with
254 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
enricher/fabric8/src/main/java/io/fabric8/maven/enricher/fabric8/PrometheusEnricher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
enricher/fabric8/src/test/java/io/fabric8/maven/enricher/fabric8/PrometheusEnricherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |