From b19c9a40427421f50058062e7c8ba2f3323a6f78 Mon Sep 17 00:00:00 2001 From: Alex Soto Date: Wed, 14 Nov 2018 11:52:19 +0100 Subject: [PATCH 1/3] feat(#937): Overrides environment variables when defined in resources --- .../maven/enricher/api/BaseEnricher.java | 57 ++++++++ .../maven/enricher/api/BaseEnricherTest.java | 138 ++++++++++++++++++ .../fabric8/AbstractHealthCheckEnricher.java | 9 ++ .../enricher/standard/ConfigMapEnricher.java | 9 ++ .../enricher/standard/DebugEnricher.java | 10 ++ .../standard/DefaultControllerEnricher.java | 9 ++ .../standard/DefaultServiceEnricher.java | 9 ++ .../enricher/standard/DependencyEnricher.java | 9 ++ .../enricher/standard/ImageEnricher.java | 9 ++ .../maven/enricher/standard/NameEnricher.java | 9 ++ .../standard/PodAnnotationEnricher.java | 9 ++ .../enricher/standard/PortNameEnricher.java | 9 ++ .../enricher/standard/ProjectEnricher.java | 9 ++ .../RemoveBuildAnnotationsEnricher.java | 10 ++ .../standard/RevisionHistoryEnricher.java | 10 ++ .../enricher/standard/SecretEnricher.java | 10 ++ .../standard/TriggersAnnotationEnricher.java | 9 ++ .../standard/VolumePermissionEnricher.java | 8 + .../standard/openshift/AutoTLSEnricher.java | 9 ++ .../standard/openshift/ExposeEnricher.java | 10 ++ .../standard/openshift/RouteEnricher.java | 10 ++ 21 files changed, 371 insertions(+) create mode 100644 enricher/api/src/test/java/io/fabric8/maven/enricher/api/BaseEnricherTest.java diff --git a/enricher/api/src/main/java/io/fabric8/maven/enricher/api/BaseEnricher.java b/enricher/api/src/main/java/io/fabric8/maven/enricher/api/BaseEnricher.java index 5f33a8f736..6623762609 100644 --- a/enricher/api/src/main/java/io/fabric8/maven/enricher/api/BaseEnricher.java +++ b/enricher/api/src/main/java/io/fabric8/maven/enricher/api/BaseEnricher.java @@ -15,6 +15,11 @@ */ package io.fabric8.maven.enricher.api; +import io.fabric8.kubernetes.api.builder.TypedVisitor; +import io.fabric8.kubernetes.api.model.ConfigMapBuilder; +import io.fabric8.kubernetes.api.model.ContainerBuilder; +import io.fabric8.kubernetes.api.model.EnvVar; +import io.fabric8.kubernetes.api.model.EnvVarBuilder; import io.fabric8.kubernetes.api.model.KubernetesListBuilder; import io.fabric8.maven.core.config.PlatformMode; import io.fabric8.maven.core.model.Configuration; @@ -24,6 +29,8 @@ import io.fabric8.maven.docker.util.Logger; import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Optional; @@ -105,6 +112,56 @@ protected EnricherContext getContext() { return enricherContext; } + protected void overrideEnvironmentVariables(KubernetesListBuilder builder, final Map resourceEnv) { + builder.accept(new TypedVisitor() { + @Override + public void visit(ContainerBuilder containerBuilder) { + List envVars = containerBuilder.buildEnv(); + + if (envVars == null) { + envVars = new LinkedList<>(); + containerBuilder.withEnv(envVars); + } + + if (resourceEnv != null) { + for (Map.Entry resourceEnvEntry : resourceEnv.entrySet()) { + EnvVar newEnvVar = + new EnvVarBuilder() + .withName(resourceEnvEntry.getKey()) + .withValue(resourceEnvEntry.getValue()) + .build(); + if (hasEnvWithName(envVars, resourceEnvEntry.getKey())) { + final String oldValue = removeEnvWithName(envVars, resourceEnvEntry.getKey()); + log.warn( + "Environment variable %s is overridden: Setting the value %s, which replaces %s", + newEnvVar.getName(), newEnvVar.getValue(), oldValue); + } + envVars.add(newEnvVar); + } + containerBuilder.withEnv(envVars); + } + } + }); + + } + + private String removeEnvWithName(List envVars, String name) { + final Iterator iterator = envVars.iterator(); + while(iterator.hasNext()) { + final EnvVar envVar = iterator.next(); + if (envVar.getName().equals(name)) { + iterator.remove(); + return envVar.getValue(); + } + } + + return ""; + } + + private boolean hasEnvWithName(List envVars, String name) { + return envVars.stream().anyMatch(e -> e.getName().equals(name)); + } + /** * Returns true if we are in OpenShift S2I binary building mode */ diff --git a/enricher/api/src/test/java/io/fabric8/maven/enricher/api/BaseEnricherTest.java b/enricher/api/src/test/java/io/fabric8/maven/enricher/api/BaseEnricherTest.java new file mode 100644 index 0000000000..213394a99e --- /dev/null +++ b/enricher/api/src/test/java/io/fabric8/maven/enricher/api/BaseEnricherTest.java @@ -0,0 +1,138 @@ +package io.fabric8.maven.enricher.api; + +import io.fabric8.kubernetes.api.builder.TypedVisitor; +import io.fabric8.kubernetes.api.model.Container; +import io.fabric8.kubernetes.api.model.ContainerBuilder; +import io.fabric8.kubernetes.api.model.ContainerPortBuilder; +import io.fabric8.kubernetes.api.model.EnvVar; +import io.fabric8.kubernetes.api.model.EnvVarBuilder; +import io.fabric8.kubernetes.api.model.KubernetesList; +import io.fabric8.kubernetes.api.model.KubernetesListBuilder; +import java.util.HashMap; +import java.util.List; +import mockit.Mocked; +import mockit.integration.junit4.JMockit; +import org.assertj.core.api.Assertions; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(JMockit.class) +public class BaseEnricherTest { + + @Mocked + private EnricherContext enricherContext; + + @Test + public void should_set_environment_variables_from_resources() { + + // Given + final DummyBaseEnricher dummyBaseEnricher = new DummyBaseEnricher(enricherContext, "dummy"); + + // When + final KubernetesListBuilder kubernetesListBuilder = createKubernetesList(); + final HashMap resourceEnv = new HashMap<>(); + resourceEnv.put("A", "B"); + + dummyBaseEnricher.overrideEnvironmentVariables(kubernetesListBuilder, resourceEnv); + + // Then + kubernetesListBuilder.accept(new TypedVisitor() { + @Override + public void visit(ContainerBuilder element) { + final List envVars = element.buildEnv(); + + assertThat(envVars) + .containsExactly(createEnvVar("A", "B")); + } + }); + } + + @Test + public void should_add_resource_environment_variables_to_container_environment_variables() { + + // Given + final DummyBaseEnricher dummyBaseEnricher = new DummyBaseEnricher(enricherContext, "dummy"); + + // When + final KubernetesListBuilder kubernetesListBuilder = createKubernetesList(createEnvVar("C", "D")); + final HashMap resourceEnv = new HashMap<>(); + resourceEnv.put("A", "B"); + + + dummyBaseEnricher.overrideEnvironmentVariables(kubernetesListBuilder, resourceEnv); + + // Then + kubernetesListBuilder.accept(new TypedVisitor() { + @Override + public void visit(ContainerBuilder element) { + final List envVars = element.buildEnv(); + + assertThat(envVars) + .containsExactlyInAnyOrder(createEnvVar("A", "B"), createEnvVar("C", "D")); + } + }); + + } + + @Test + public void should_override_resource_environment_variables_to_container_environment_variables() { + + // Given + final DummyBaseEnricher dummyBaseEnricher = new DummyBaseEnricher(enricherContext, "dummy"); + + // When + final KubernetesListBuilder kubernetesListBuilder = createKubernetesList(createEnvVar("A", "B")); + final HashMap resourceEnv = new HashMap<>(); + resourceEnv.put("A", "C"); + + dummyBaseEnricher.overrideEnvironmentVariables(kubernetesListBuilder, resourceEnv); + + // Then + kubernetesListBuilder.accept(new TypedVisitor() { + @Override + public void visit(ContainerBuilder element) { + final List envVars = element.buildEnv(); + + assertThat(envVars) + .containsExactlyInAnyOrder(createEnvVar("A", "C")); + } + }); + + } + + + private KubernetesListBuilder createKubernetesList(EnvVar... envVars) { + final Container container = new ContainerBuilder() + .withName("test-port-enricher") + .withImage("test-image") + .withEnv(envVars) + .withPorts(new ContainerPortBuilder().withContainerPort(80).withProtocol("TCP").build()) + .build(); + return new KubernetesListBuilder() + .addNewReplicaSetItem() + .withNewSpec() + .withNewTemplate() + .withNewSpec() + .withContainers(container) + .endSpec() + .endTemplate() + .endSpec() + .endReplicaSetItem(); + } + + private EnvVar createEnvVar(String key, String value) { + return + new EnvVarBuilder() + .withName(key) + .withValue(value) + .build(); + } + + class DummyBaseEnricher extends BaseEnricher { + public DummyBaseEnricher(EnricherContext enricherContext, String name) { + super(enricherContext, name); + } + } +} diff --git a/enricher/fabric8/src/main/java/io/fabric8/maven/enricher/fabric8/AbstractHealthCheckEnricher.java b/enricher/fabric8/src/main/java/io/fabric8/maven/enricher/fabric8/AbstractHealthCheckEnricher.java index 291f6aab37..56854a4b48 100644 --- a/enricher/fabric8/src/main/java/io/fabric8/maven/enricher/fabric8/AbstractHealthCheckEnricher.java +++ b/enricher/fabric8/src/main/java/io/fabric8/maven/enricher/fabric8/AbstractHealthCheckEnricher.java @@ -20,8 +20,10 @@ import io.fabric8.kubernetes.api.model.ContainerBuilder; import io.fabric8.kubernetes.api.model.KubernetesListBuilder; import io.fabric8.kubernetes.api.model.Probe; +import io.fabric8.maven.core.config.ResourceConfig; import io.fabric8.maven.enricher.api.BaseEnricher; import io.fabric8.maven.enricher.api.MavenEnricherContext; +import java.util.HashMap; /** * Enriches containers with health check probes. @@ -35,6 +37,13 @@ public AbstractHealthCheckEnricher(MavenEnricherContext buildContext, String nam @Override public void addMissingResources(KubernetesListBuilder builder) { + final ResourceConfig resourceConfig = getConfiguration().getResource().get(); + + if (resourceConfig != null) { + overrideEnvironmentVariables(builder, resourceConfig.getEnv() + .orElse(new HashMap<>())); + } + builder.accept(new TypedVisitor() { @Override public void visit(ContainerBuilder container) { diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ConfigMapEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ConfigMapEnricher.java index 31bf6a8523..2dfa1dbe8b 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ConfigMapEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ConfigMapEnricher.java @@ -18,6 +18,7 @@ import io.fabric8.kubernetes.api.builder.TypedVisitor; import io.fabric8.kubernetes.api.model.ConfigMapBuilder; import io.fabric8.kubernetes.api.model.KubernetesListBuilder; +import io.fabric8.maven.core.config.ResourceConfig; import io.fabric8.maven.enricher.api.BaseEnricher; import io.fabric8.maven.enricher.api.MavenEnricherContext; import java.io.IOException; @@ -38,6 +39,14 @@ public ConfigMapEnricher(MavenEnricherContext enricherContext) { @Override public void addMissingResources(KubernetesListBuilder builder) { + + final ResourceConfig resourceConfig = getConfiguration().getResource().get(); + + if (resourceConfig != null) { + overrideEnvironmentVariables(builder, resourceConfig.getEnv() + .orElse(new HashMap<>())); + } + addAnnotations(builder); } diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DebugEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DebugEnricher.java index 70d77a528d..829b25375e 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DebugEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DebugEnricher.java @@ -28,6 +28,7 @@ import io.fabric8.kubernetes.api.model.apps.DeploymentSpec; import io.fabric8.kubernetes.api.model.apps.ReplicaSet; import io.fabric8.kubernetes.api.model.apps.ReplicaSetSpec; +import io.fabric8.maven.core.config.ResourceConfig; import io.fabric8.maven.core.util.kubernetes.KubernetesHelper; import io.fabric8.maven.core.util.kubernetes.KubernetesResourceUtil; import io.fabric8.maven.enricher.api.BaseEnricher; @@ -35,6 +36,7 @@ import io.fabric8.openshift.api.model.DeploymentConfig; import io.fabric8.openshift.api.model.DeploymentConfigSpec; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import org.apache.commons.lang3.StringUtils; @@ -55,6 +57,14 @@ public DebugEnricher(MavenEnricherContext buildContext) { @Override public void addMissingResources(KubernetesListBuilder builder) { + + final ResourceConfig resourceConfig = getConfiguration().getResource().get(); + + if (resourceConfig != null) { + overrideEnvironmentVariables(builder, resourceConfig.getEnv() + .orElse(new HashMap<>())); + } + if (debugEnabled()) { int count = 0; List items = builder.getItems(); diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DefaultControllerEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DefaultControllerEnricher.java index c627ae3762..78e8107e71 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DefaultControllerEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DefaultControllerEnricher.java @@ -41,6 +41,7 @@ import io.fabric8.maven.enricher.api.MavenEnricherContext; import java.util.Collections; +import java.util.HashMap; import java.util.List; /** @@ -96,6 +97,14 @@ public DefaultControllerEnricher(MavenEnricherContext buildContext) { @Override public void addMissingResources(KubernetesListBuilder builder) { + + final ResourceConfig resourceConfig = getConfiguration().getResource().get(); + + if (resourceConfig != null) { + overrideEnvironmentVariables(builder, resourceConfig.getEnv() + .orElse(new HashMap<>())); + } + final String name = getConfig(Config.name, MavenUtil.createDefaultResourceName(getContext().getGav().getSanitizedArtifactId())); final ResourceConfig config = new ResourceConfig.Builder() .controllerName(name) diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DefaultServiceEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DefaultServiceEnricher.java index ffb354c576..c8a0fec881 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DefaultServiceEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DefaultServiceEnricher.java @@ -26,6 +26,7 @@ import io.fabric8.kubernetes.api.model.ServicePort; import io.fabric8.kubernetes.api.model.ServicePortBuilder; import io.fabric8.kubernetes.api.model.ServiceSpec; +import io.fabric8.maven.core.config.ResourceConfig; import io.fabric8.maven.core.util.Configs; import io.fabric8.maven.core.util.MavenUtil; import io.fabric8.maven.core.util.kubernetes.KubernetesHelper; @@ -97,6 +98,14 @@ public DefaultServiceEnricher(MavenEnricherContext buildContext) { @Override public void addMissingResources(KubernetesListBuilder builder) { + + final ResourceConfig resourceConfig = getConfiguration().getResource().get(); + + if (resourceConfig != null) { + overrideEnvironmentVariables(builder, resourceConfig.getEnv() + .orElse(new HashMap<>())); + } + final Service defaultService = getDefaultService(); if (hasServices(builder)) { diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DependencyEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DependencyEnricher.java index ca1a6eedf9..cd8cb788b7 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DependencyEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DependencyEnricher.java @@ -15,6 +15,7 @@ */ package io.fabric8.maven.enricher.standard; +import io.fabric8.maven.core.config.ResourceConfig; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -119,6 +120,14 @@ private void addArtifactsWithYaml(Set artifactSet, String dependencyYaml) { @Override public void adapt(final KubernetesListBuilder builder) { + + final ResourceConfig resourceConfig = getConfiguration().getResource().get(); + + if (resourceConfig != null) { + overrideEnvironmentVariables(builder, resourceConfig.getEnv() + .orElse(new HashMap<>())); + } + final List kubernetesItems = new ArrayList<>(); processArtifactSetResources(this.kubernetesDependencyArtifacts, items -> { kubernetesItems.addAll(Arrays.asList(items.toArray(new HasMetadata[items.size()]))); diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ImageEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ImageEnricher.java index fbf45c4092..6d1708d033 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ImageEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ImageEnricher.java @@ -16,6 +16,7 @@ package io.fabric8.maven.enricher.standard; import java.util.ArrayList; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -86,6 +87,14 @@ private enum Config implements Configs.Key { @Override public void addMissingResources(KubernetesListBuilder builder) { + + final ResourceConfig resourceConfig = getConfiguration().getResource().get(); + + if (resourceConfig != null) { + overrideEnvironmentVariables(builder, resourceConfig.getEnv() + .orElse(new HashMap<>())); + } + if (!hasImageConfiguration()) { log.verbose("No images resolved. Skipping ..."); return; diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/NameEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/NameEnricher.java index 68f39dfdbe..7939092b6d 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/NameEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/NameEnricher.java @@ -25,10 +25,12 @@ import io.fabric8.kubernetes.api.model.apps.DeploymentFluent; import io.fabric8.kubernetes.api.model.apps.ReplicaSetBuilder; import io.fabric8.kubernetes.api.model.apps.ReplicaSetFluent; +import io.fabric8.maven.core.config.ResourceConfig; import io.fabric8.maven.core.util.Configs; import io.fabric8.maven.core.util.MavenUtil; import io.fabric8.maven.enricher.api.BaseEnricher; import io.fabric8.maven.enricher.api.MavenEnricherContext; +import java.util.HashMap; import org.apache.commons.lang3.StringUtils; /** @@ -53,6 +55,13 @@ private enum Config implements Configs.Key { @Override public void addMissingResources(KubernetesListBuilder builder) { + final ResourceConfig resourceConfig = getConfiguration().getResource().get(); + + if (resourceConfig != null) { + overrideEnvironmentVariables(builder, resourceConfig.getEnv() + .orElse(new HashMap<>())); + } + final String defaultName = getConfig(Config.name, MavenUtil.createDefaultResourceName(getContext().getGav().getSanitizedArtifactId())); builder.accept(new TypedVisitor() { diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/PodAnnotationEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/PodAnnotationEnricher.java index 76f35e9f63..6201642706 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/PodAnnotationEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/PodAnnotationEnricher.java @@ -15,6 +15,8 @@ */ package io.fabric8.maven.enricher.standard; +import io.fabric8.maven.core.config.ResourceConfig; +import java.util.HashMap; import java.util.List; import io.fabric8.kubernetes.api.model.HasMetadata; @@ -38,6 +40,13 @@ public PodAnnotationEnricher(MavenEnricherContext buildContext) { @Override public void adapt(KubernetesListBuilder builder) { + final ResourceConfig resourceConfig = getConfiguration().getResource().get(); + + if (resourceConfig != null) { + overrideEnvironmentVariables(builder, resourceConfig.getEnv() + .orElse(new HashMap<>())); + } + super.adapt(builder); List items = builder.getItems(); diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/PortNameEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/PortNameEnricher.java index 11f4ebf48e..51fa226663 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/PortNameEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/PortNameEnricher.java @@ -15,6 +15,7 @@ */ package io.fabric8.maven.enricher.standard; +import io.fabric8.maven.core.config.ResourceConfig; import java.io.IOException; import java.util.Collections; import java.util.HashMap; @@ -52,6 +53,14 @@ public PortNameEnricher(MavenEnricherContext buildContext) { @Override public void addMissingResources(KubernetesListBuilder builder) { + + final ResourceConfig resourceConfig = getConfiguration().getResource().get(); + + if (resourceConfig != null) { + overrideEnvironmentVariables(builder, resourceConfig.getEnv() + .orElse(new HashMap<>())); + } + builder.accept(new TypedVisitor() { @Override public void visit(ContainerPortBuilder portBuilder) { diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ProjectEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ProjectEnricher.java index 95f7d3262b..1935c8ca92 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ProjectEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ProjectEnricher.java @@ -18,6 +18,7 @@ import io.fabric8.kubernetes.api.builder.TypedVisitor; import io.fabric8.kubernetes.api.model.KubernetesListBuilder; import io.fabric8.kubernetes.api.model.ObjectMetaBuilder; +import io.fabric8.maven.core.config.ResourceConfig; import io.fabric8.maven.core.util.Configs; import io.fabric8.maven.core.util.MapUtil; import io.fabric8.maven.core.model.GroupArtifactVersion; @@ -67,6 +68,14 @@ public Map getSelector(Kind kind) { @Override public void adapt(KubernetesListBuilder builder) { + + final ResourceConfig resourceConfig = getConfiguration().getResource().get(); + + if (resourceConfig != null) { + overrideEnvironmentVariables(builder, resourceConfig.getEnv() + .orElse(new HashMap<>())); + } + // Add to all objects in the builder builder.accept(new TypedVisitor() { @Override diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/RemoveBuildAnnotationsEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/RemoveBuildAnnotationsEnricher.java index 612be3cb29..0654e02bea 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/RemoveBuildAnnotationsEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/RemoveBuildAnnotationsEnricher.java @@ -15,6 +15,8 @@ */ package io.fabric8.maven.enricher.standard; +import io.fabric8.maven.core.config.ResourceConfig; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -37,6 +39,14 @@ public RemoveBuildAnnotationsEnricher(MavenEnricherContext buildContext) { @Override public void adapt(KubernetesListBuilder builder) { + + final ResourceConfig resourceConfig = getConfiguration().getResource().get(); + + if (resourceConfig != null) { + overrideEnvironmentVariables(builder, resourceConfig.getEnv() + .orElse(new HashMap<>())); + } + List items = builder.buildItems(); for (HasMetadata item : items) { diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/RevisionHistoryEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/RevisionHistoryEnricher.java index 17e92460d2..2bd06e49d2 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/RevisionHistoryEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/RevisionHistoryEnricher.java @@ -18,9 +18,11 @@ import io.fabric8.kubernetes.api.builder.TypedVisitor; import io.fabric8.kubernetes.api.model.KubernetesListBuilder; import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder; +import io.fabric8.maven.core.config.ResourceConfig; import io.fabric8.maven.core.util.Configs; import io.fabric8.maven.enricher.api.BaseEnricher; import io.fabric8.maven.enricher.api.MavenEnricherContext; +import java.util.HashMap; /** * This enricher adds the 'revisionHistoryLimit' property to deployment spec of RCs / RSs for KuberNetes/OpenShift resource descriptors. @@ -46,6 +48,14 @@ public RevisionHistoryEnricher(MavenEnricherContext buildContext) { @Override public void addMissingResources(KubernetesListBuilder builder) { + + final ResourceConfig resourceConfig = getConfiguration().getResource().get(); + + if (resourceConfig != null) { + overrideEnvironmentVariables(builder, resourceConfig.getEnv() + .orElse(new HashMap<>())); + } + final Integer maxRevisionHistories = Configs.asInt(getConfig(Config.limit)); log.info("Adding revision history limit to %s", maxRevisionHistories); diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/SecretEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/SecretEnricher.java index 4137061691..b3847f8593 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/SecretEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/SecretEnricher.java @@ -15,6 +15,8 @@ */ package io.fabric8.maven.enricher.standard; +import io.fabric8.maven.core.config.ResourceConfig; +import java.util.HashMap; import java.util.Map; import io.fabric8.kubernetes.api.builder.TypedVisitor; @@ -36,6 +38,14 @@ protected String encode(String raw) { @Override public void addMissingResources(KubernetesListBuilder builder) { + + final ResourceConfig resourceConfig = getConfiguration().getResource().get(); + + if (resourceConfig != null) { + overrideEnvironmentVariables(builder, resourceConfig.getEnv() + .orElse(new HashMap<>())); + } + // update builder // use a selector to choose all secret builder in kubernetes list builders. // try to find the target annotations diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/TriggersAnnotationEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/TriggersAnnotationEnricher.java index b176bbd80c..bd76f91d1a 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/TriggersAnnotationEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/TriggersAnnotationEnricher.java @@ -15,7 +15,9 @@ */ package io.fabric8.maven.enricher.standard; +import io.fabric8.maven.core.config.ResourceConfig; import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -70,6 +72,13 @@ public TriggersAnnotationEnricher(MavenEnricherContext buildContext) { @Override public void adapt(KubernetesListBuilder builder) { + final ResourceConfig resourceConfig = getConfiguration().getResource().get(); + + if (resourceConfig != null) { + overrideEnvironmentVariables(builder, resourceConfig.getEnv() + .orElse(new HashMap<>())); + } + builder.accept(new TypedVisitor() { @Override public void visit(StatefulSetBuilder o) { diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/VolumePermissionEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/VolumePermissionEnricher.java index 2000189ea3..347490d614 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/VolumePermissionEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/VolumePermissionEnricher.java @@ -15,6 +15,7 @@ */ package io.fabric8.maven.enricher.standard; +import io.fabric8.maven.core.config.ResourceConfig; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; @@ -59,6 +60,13 @@ public VolumePermissionEnricher(MavenEnricherContext buildContext) { @Override public void adapt(KubernetesListBuilder builder) { + final ResourceConfig resourceConfig = getConfiguration().getResource().get(); + + if (resourceConfig != null) { + overrideEnvironmentVariables(builder, resourceConfig.getEnv() + .orElse(new HashMap<>())); + } + builder.accept(new TypedVisitor() { @Override public void visit(PodTemplateSpecBuilder builder) { diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/AutoTLSEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/AutoTLSEnricher.java index 77a1d833c9..ca7526c1e0 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/AutoTLSEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/AutoTLSEnricher.java @@ -15,6 +15,7 @@ */ package io.fabric8.maven.enricher.standard.openshift; +import io.fabric8.maven.core.config.ResourceConfig; import java.util.Collections; import java.util.List; import java.util.Map; @@ -85,6 +86,14 @@ public Map getAnnotations(Kind kind) { @Override public void addMissingResources(KubernetesListBuilder builder) { + + final ResourceConfig resourceConfig = getConfiguration().getResource().get(); + + if (resourceConfig != null) { + overrideEnvironmentVariables(builder, resourceConfig.getEnv() + .orElse(new HashMap<>())); + } + if (!isOpenShiftMode()) { return; } diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/ExposeEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/ExposeEnricher.java index bd25c95702..91b6cf864b 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/ExposeEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/ExposeEnricher.java @@ -16,7 +16,9 @@ package io.fabric8.maven.enricher.standard.openshift; +import io.fabric8.maven.core.config.ResourceConfig; import java.util.Arrays; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; @@ -47,6 +49,14 @@ public ExposeEnricher(MavenEnricherContext buildContext) { @Override public void addMissingResources(KubernetesListBuilder builder) { + + final ResourceConfig resourceConfig = getConfiguration().getResource().get(); + + if (resourceConfig != null) { + overrideEnvironmentVariables(builder, resourceConfig.getEnv() + .orElse(new HashMap<>())); + } + List items = builder.getItems(); if (items != null) { for (HasMetadata item : items) { diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/RouteEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/RouteEnricher.java index 6f3aa14918..3386a32453 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/RouteEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/RouteEnricher.java @@ -16,7 +16,9 @@ package io.fabric8.maven.enricher.standard.openshift; +import io.fabric8.maven.core.config.ResourceConfig; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; @@ -47,6 +49,14 @@ public RouteEnricher(MavenEnricherContext buildContext) { @Override public void addMissingResources(final KubernetesListBuilder listBuilder) { + + final ResourceConfig resourceConfig = getConfiguration().getResource().get(); + + if (resourceConfig != null) { + overrideEnvironmentVariables(listBuilder, resourceConfig.getEnv() + .orElse(new HashMap<>())); + } + final List routes = new ArrayList<>(); listBuilder.accept(new TypedVisitor() { From 3fc74670af2da2c2e2e11b684d52922a71e7c936 Mon Sep 17 00:00:00 2001 From: Alex Soto Date: Mon, 26 Nov 2018 16:13:36 +0100 Subject: [PATCH 2/3] feat(#937): Use ifPresent method in Optionals --- .../maven/enricher/api/BaseEnricherTest.java | 17 +++++++++++++++-- .../fabric8/AbstractHealthCheckEnricher.java | 6 ++---- .../enricher/standard/ConfigMapEnricher.java | 6 ++---- .../maven/enricher/standard/DebugEnricher.java | 6 ++---- .../standard/DefaultControllerEnricher.java | 10 ++++------ .../standard/DefaultServiceEnricher.java | 6 ++---- .../enricher/standard/DependencyEnricher.java | 6 ++---- .../maven/enricher/standard/ImageEnricher.java | 6 ++---- .../maven/enricher/standard/NameEnricher.java | 6 ++---- .../standard/PodAnnotationEnricher.java | 6 ++---- .../enricher/standard/PortNameEnricher.java | 6 ++---- .../enricher/standard/ProjectEnricher.java | 6 ++---- .../RemoveBuildAnnotationsEnricher.java | 6 ++---- .../standard/RevisionHistoryEnricher.java | 6 ++---- .../maven/enricher/standard/SecretEnricher.java | 6 ++---- .../standard/TriggersAnnotationEnricher.java | 6 ++---- .../standard/VolumePermissionEnricher.java | 6 ++---- .../standard/openshift/AutoTLSEnricher.java | 6 ++---- .../standard/openshift/ExposeEnricher.java | 6 ++---- .../standard/openshift/RouteEnricher.java | 6 ++---- .../standard/DefaultControllerEnricherTest.java | 3 --- 21 files changed, 55 insertions(+), 83 deletions(-) diff --git a/enricher/api/src/test/java/io/fabric8/maven/enricher/api/BaseEnricherTest.java b/enricher/api/src/test/java/io/fabric8/maven/enricher/api/BaseEnricherTest.java index 213394a99e..a09ffbf44e 100644 --- a/enricher/api/src/test/java/io/fabric8/maven/enricher/api/BaseEnricherTest.java +++ b/enricher/api/src/test/java/io/fabric8/maven/enricher/api/BaseEnricherTest.java @@ -1,3 +1,18 @@ +/** + * Copyright 2018 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.api; import io.fabric8.kubernetes.api.builder.TypedVisitor; @@ -6,13 +21,11 @@ import io.fabric8.kubernetes.api.model.ContainerPortBuilder; import io.fabric8.kubernetes.api.model.EnvVar; import io.fabric8.kubernetes.api.model.EnvVarBuilder; -import io.fabric8.kubernetes.api.model.KubernetesList; import io.fabric8.kubernetes.api.model.KubernetesListBuilder; import java.util.HashMap; import java.util.List; import mockit.Mocked; import mockit.integration.junit4.JMockit; -import org.assertj.core.api.Assertions; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/enricher/fabric8/src/main/java/io/fabric8/maven/enricher/fabric8/AbstractHealthCheckEnricher.java b/enricher/fabric8/src/main/java/io/fabric8/maven/enricher/fabric8/AbstractHealthCheckEnricher.java index 56854a4b48..410f87c182 100644 --- a/enricher/fabric8/src/main/java/io/fabric8/maven/enricher/fabric8/AbstractHealthCheckEnricher.java +++ b/enricher/fabric8/src/main/java/io/fabric8/maven/enricher/fabric8/AbstractHealthCheckEnricher.java @@ -37,12 +37,10 @@ public AbstractHealthCheckEnricher(MavenEnricherContext buildContext, String nam @Override public void addMissingResources(KubernetesListBuilder builder) { - final ResourceConfig resourceConfig = getConfiguration().getResource().get(); - - if (resourceConfig != null) { + getConfiguration().getResource().ifPresent(resourceConfig -> { overrideEnvironmentVariables(builder, resourceConfig.getEnv() .orElse(new HashMap<>())); - } + }); builder.accept(new TypedVisitor() { @Override diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ConfigMapEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ConfigMapEnricher.java index 2dfa1dbe8b..eb3ab303dc 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ConfigMapEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ConfigMapEnricher.java @@ -40,12 +40,10 @@ public ConfigMapEnricher(MavenEnricherContext enricherContext) { @Override public void addMissingResources(KubernetesListBuilder builder) { - final ResourceConfig resourceConfig = getConfiguration().getResource().get(); - - if (resourceConfig != null) { + getConfiguration().getResource().ifPresent(resourceConfig -> { overrideEnvironmentVariables(builder, resourceConfig.getEnv() .orElse(new HashMap<>())); - } + }); addAnnotations(builder); } diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DebugEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DebugEnricher.java index 829b25375e..da1cf5eae4 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DebugEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DebugEnricher.java @@ -58,12 +58,10 @@ public DebugEnricher(MavenEnricherContext buildContext) { @Override public void addMissingResources(KubernetesListBuilder builder) { - final ResourceConfig resourceConfig = getConfiguration().getResource().get(); - - if (resourceConfig != null) { + getConfiguration().getResource().ifPresent(resourceConfig -> { overrideEnvironmentVariables(builder, resourceConfig.getEnv() .orElse(new HashMap<>())); - } + }); if (debugEnabled()) { int count = 0; diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DefaultControllerEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DefaultControllerEnricher.java index 78e8107e71..6b4d80158e 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DefaultControllerEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DefaultControllerEnricher.java @@ -98,12 +98,10 @@ public DefaultControllerEnricher(MavenEnricherContext buildContext) { @Override public void addMissingResources(KubernetesListBuilder builder) { - final ResourceConfig resourceConfig = getConfiguration().getResource().get(); - - if (resourceConfig != null) { - overrideEnvironmentVariables(builder, resourceConfig.getEnv() - .orElse(new HashMap<>())); - } + getConfiguration().getResource().ifPresent(resourceConfig -> { + overrideEnvironmentVariables(builder, resourceConfig.getEnv() + .orElse(new HashMap<>())); + }); final String name = getConfig(Config.name, MavenUtil.createDefaultResourceName(getContext().getGav().getSanitizedArtifactId())); final ResourceConfig config = new ResourceConfig.Builder() diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DefaultServiceEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DefaultServiceEnricher.java index c8a0fec881..75ca6c0b30 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DefaultServiceEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DefaultServiceEnricher.java @@ -99,12 +99,10 @@ public DefaultServiceEnricher(MavenEnricherContext buildContext) { @Override public void addMissingResources(KubernetesListBuilder builder) { - final ResourceConfig resourceConfig = getConfiguration().getResource().get(); - - if (resourceConfig != null) { + getConfiguration().getResource().ifPresent(resourceConfig -> { overrideEnvironmentVariables(builder, resourceConfig.getEnv() .orElse(new HashMap<>())); - } + }); final Service defaultService = getDefaultService(); diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DependencyEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DependencyEnricher.java index cd8cb788b7..252da7d3a6 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DependencyEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/DependencyEnricher.java @@ -121,12 +121,10 @@ private void addArtifactsWithYaml(Set artifactSet, String dependencyYaml) { @Override public void adapt(final KubernetesListBuilder builder) { - final ResourceConfig resourceConfig = getConfiguration().getResource().get(); - - if (resourceConfig != null) { + getConfiguration().getResource().ifPresent(resourceConfig -> { overrideEnvironmentVariables(builder, resourceConfig.getEnv() .orElse(new HashMap<>())); - } + }); final List kubernetesItems = new ArrayList<>(); processArtifactSetResources(this.kubernetesDependencyArtifacts, items -> { diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ImageEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ImageEnricher.java index 6d1708d033..ff932ece64 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ImageEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ImageEnricher.java @@ -88,12 +88,10 @@ private enum Config implements Configs.Key { @Override public void addMissingResources(KubernetesListBuilder builder) { - final ResourceConfig resourceConfig = getConfiguration().getResource().get(); - - if (resourceConfig != null) { + getConfiguration().getResource().ifPresent(resourceConfig -> { overrideEnvironmentVariables(builder, resourceConfig.getEnv() .orElse(new HashMap<>())); - } + }); if (!hasImageConfiguration()) { log.verbose("No images resolved. Skipping ..."); diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/NameEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/NameEnricher.java index 7939092b6d..6999acd77c 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/NameEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/NameEnricher.java @@ -55,12 +55,10 @@ private enum Config implements Configs.Key { @Override public void addMissingResources(KubernetesListBuilder builder) { - final ResourceConfig resourceConfig = getConfiguration().getResource().get(); - - if (resourceConfig != null) { + getConfiguration().getResource().ifPresent(resourceConfig -> { overrideEnvironmentVariables(builder, resourceConfig.getEnv() .orElse(new HashMap<>())); - } + }); final String defaultName = getConfig(Config.name, MavenUtil.createDefaultResourceName(getContext().getGav().getSanitizedArtifactId())); diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/PodAnnotationEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/PodAnnotationEnricher.java index 6201642706..74ed115dc4 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/PodAnnotationEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/PodAnnotationEnricher.java @@ -40,12 +40,10 @@ public PodAnnotationEnricher(MavenEnricherContext buildContext) { @Override public void adapt(KubernetesListBuilder builder) { - final ResourceConfig resourceConfig = getConfiguration().getResource().get(); - - if (resourceConfig != null) { + getConfiguration().getResource().ifPresent(resourceConfig -> { overrideEnvironmentVariables(builder, resourceConfig.getEnv() .orElse(new HashMap<>())); - } + }); super.adapt(builder); diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/PortNameEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/PortNameEnricher.java index 51fa226663..e30e28dcda 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/PortNameEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/PortNameEnricher.java @@ -54,12 +54,10 @@ public PortNameEnricher(MavenEnricherContext buildContext) { @Override public void addMissingResources(KubernetesListBuilder builder) { - final ResourceConfig resourceConfig = getConfiguration().getResource().get(); - - if (resourceConfig != null) { + getConfiguration().getResource().ifPresent(resourceConfig -> { overrideEnvironmentVariables(builder, resourceConfig.getEnv() .orElse(new HashMap<>())); - } + }); builder.accept(new TypedVisitor() { @Override diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ProjectEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ProjectEnricher.java index 1935c8ca92..0c12724826 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ProjectEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/ProjectEnricher.java @@ -69,12 +69,10 @@ public Map getSelector(Kind kind) { @Override public void adapt(KubernetesListBuilder builder) { - final ResourceConfig resourceConfig = getConfiguration().getResource().get(); - - if (resourceConfig != null) { + getConfiguration().getResource().ifPresent(resourceConfig -> { overrideEnvironmentVariables(builder, resourceConfig.getEnv() .orElse(new HashMap<>())); - } + }); // Add to all objects in the builder builder.accept(new TypedVisitor() { diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/RemoveBuildAnnotationsEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/RemoveBuildAnnotationsEnricher.java index 0654e02bea..32bd4a7d26 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/RemoveBuildAnnotationsEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/RemoveBuildAnnotationsEnricher.java @@ -40,12 +40,10 @@ public RemoveBuildAnnotationsEnricher(MavenEnricherContext buildContext) { @Override public void adapt(KubernetesListBuilder builder) { - final ResourceConfig resourceConfig = getConfiguration().getResource().get(); - - if (resourceConfig != null) { + getConfiguration().getResource().ifPresent(resourceConfig -> { overrideEnvironmentVariables(builder, resourceConfig.getEnv() .orElse(new HashMap<>())); - } + }); List items = builder.buildItems(); diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/RevisionHistoryEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/RevisionHistoryEnricher.java index 2bd06e49d2..4e3d3f8d0e 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/RevisionHistoryEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/RevisionHistoryEnricher.java @@ -49,12 +49,10 @@ public RevisionHistoryEnricher(MavenEnricherContext buildContext) { @Override public void addMissingResources(KubernetesListBuilder builder) { - final ResourceConfig resourceConfig = getConfiguration().getResource().get(); - - if (resourceConfig != null) { + getConfiguration().getResource().ifPresent(resourceConfig -> { overrideEnvironmentVariables(builder, resourceConfig.getEnv() .orElse(new HashMap<>())); - } + }); final Integer maxRevisionHistories = Configs.asInt(getConfig(Config.limit)); diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/SecretEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/SecretEnricher.java index b3847f8593..041b332cf6 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/SecretEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/SecretEnricher.java @@ -39,12 +39,10 @@ protected String encode(String raw) { @Override public void addMissingResources(KubernetesListBuilder builder) { - final ResourceConfig resourceConfig = getConfiguration().getResource().get(); - - if (resourceConfig != null) { + getConfiguration().getResource().ifPresent(resourceConfig -> { overrideEnvironmentVariables(builder, resourceConfig.getEnv() .orElse(new HashMap<>())); - } + }); // update builder // use a selector to choose all secret builder in kubernetes list builders. diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/TriggersAnnotationEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/TriggersAnnotationEnricher.java index bd76f91d1a..bb7e4ce84c 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/TriggersAnnotationEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/TriggersAnnotationEnricher.java @@ -72,12 +72,10 @@ public TriggersAnnotationEnricher(MavenEnricherContext buildContext) { @Override public void adapt(KubernetesListBuilder builder) { - final ResourceConfig resourceConfig = getConfiguration().getResource().get(); - - if (resourceConfig != null) { + getConfiguration().getResource().ifPresent(resourceConfig -> { overrideEnvironmentVariables(builder, resourceConfig.getEnv() .orElse(new HashMap<>())); - } + }); builder.accept(new TypedVisitor() { @Override diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/VolumePermissionEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/VolumePermissionEnricher.java index 347490d614..486c3f4735 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/VolumePermissionEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/VolumePermissionEnricher.java @@ -60,12 +60,10 @@ public VolumePermissionEnricher(MavenEnricherContext buildContext) { @Override public void adapt(KubernetesListBuilder builder) { - final ResourceConfig resourceConfig = getConfiguration().getResource().get(); - - if (resourceConfig != null) { + getConfiguration().getResource().ifPresent(resourceConfig -> { overrideEnvironmentVariables(builder, resourceConfig.getEnv() .orElse(new HashMap<>())); - } + }); builder.accept(new TypedVisitor() { @Override diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/AutoTLSEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/AutoTLSEnricher.java index ca7526c1e0..df9fe614d0 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/AutoTLSEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/AutoTLSEnricher.java @@ -87,12 +87,10 @@ public Map getAnnotations(Kind kind) { @Override public void addMissingResources(KubernetesListBuilder builder) { - final ResourceConfig resourceConfig = getConfiguration().getResource().get(); - - if (resourceConfig != null) { + getConfiguration().getResource().ifPresent(resourceConfig -> { overrideEnvironmentVariables(builder, resourceConfig.getEnv() .orElse(new HashMap<>())); - } + }); if (!isOpenShiftMode()) { return; diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/ExposeEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/ExposeEnricher.java index 91b6cf864b..a22f064458 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/ExposeEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/ExposeEnricher.java @@ -50,12 +50,10 @@ public ExposeEnricher(MavenEnricherContext buildContext) { @Override public void addMissingResources(KubernetesListBuilder builder) { - final ResourceConfig resourceConfig = getConfiguration().getResource().get(); - - if (resourceConfig != null) { + getConfiguration().getResource().ifPresent(resourceConfig -> { overrideEnvironmentVariables(builder, resourceConfig.getEnv() .orElse(new HashMap<>())); - } + }); List items = builder.getItems(); if (items != null) { diff --git a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/RouteEnricher.java b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/RouteEnricher.java index 3386a32453..004798577c 100644 --- a/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/RouteEnricher.java +++ b/enricher/standard/src/main/java/io/fabric8/maven/enricher/standard/openshift/RouteEnricher.java @@ -50,12 +50,10 @@ public RouteEnricher(MavenEnricherContext buildContext) { @Override public void addMissingResources(final KubernetesListBuilder listBuilder) { - final ResourceConfig resourceConfig = getConfiguration().getResource().get(); - - if (resourceConfig != null) { + getConfiguration().getResource().ifPresent(resourceConfig -> { overrideEnvironmentVariables(listBuilder, resourceConfig.getEnv() .orElse(new HashMap<>())); - } + }); final List routes = new ArrayList<>(); listBuilder.accept(new TypedVisitor() { diff --git a/enricher/standard/src/test/java/io/fabric8/maven/enricher/standard/DefaultControllerEnricherTest.java b/enricher/standard/src/test/java/io/fabric8/maven/enricher/standard/DefaultControllerEnricherTest.java index c4bc767204..84a83d5507 100644 --- a/enricher/standard/src/test/java/io/fabric8/maven/enricher/standard/DefaultControllerEnricherTest.java +++ b/enricher/standard/src/test/java/io/fabric8/maven/enricher/standard/DefaultControllerEnricherTest.java @@ -15,7 +15,6 @@ */ package io.fabric8.maven.enricher.standard; -import com.google.common.io.Files; import com.jayway.jsonpath.matchers.JsonPathMatchers; import io.fabric8.kubernetes.api.model.KubernetesList; import io.fabric8.kubernetes.api.model.KubernetesListBuilder; @@ -26,8 +25,6 @@ import io.fabric8.maven.docker.config.BuildImageConfiguration; import io.fabric8.maven.docker.config.ImageConfiguration; import io.fabric8.maven.enricher.api.MavenEnricherContext; -import io.fabric8.maven.enricher.api.util.ProjectClassLoaders; -import java.net.URLClassLoader; import java.util.Arrays; import java.util.Collections; import java.util.TreeMap; From d20f854718a939302408bfcfc1e99b008f8c54c4 Mon Sep 17 00:00:00 2001 From: Alex Soto Date: Wed, 12 Dec 2018 12:49:32 +0100 Subject: [PATCH 3/3] Fixes test names --- .../io/fabric8/maven/enricher/api/BaseEnricherTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/enricher/api/src/test/java/io/fabric8/maven/enricher/api/BaseEnricherTest.java b/enricher/api/src/test/java/io/fabric8/maven/enricher/api/BaseEnricherTest.java index a09ffbf44e..f24b448ca6 100644 --- a/enricher/api/src/test/java/io/fabric8/maven/enricher/api/BaseEnricherTest.java +++ b/enricher/api/src/test/java/io/fabric8/maven/enricher/api/BaseEnricherTest.java @@ -38,7 +38,7 @@ public class BaseEnricherTest { private EnricherContext enricherContext; @Test - public void should_set_environment_variables_from_resources() { + public void setEnvironmentVariablesFromResources() { // Given final DummyBaseEnricher dummyBaseEnricher = new DummyBaseEnricher(enricherContext, "dummy"); @@ -63,7 +63,7 @@ public void visit(ContainerBuilder element) { } @Test - public void should_add_resource_environment_variables_to_container_environment_variables() { + public void addResourceEnvironmentVariablesToContainerEnvironmentVariables() { // Given final DummyBaseEnricher dummyBaseEnricher = new DummyBaseEnricher(enricherContext, "dummy"); @@ -90,7 +90,7 @@ public void visit(ContainerBuilder element) { } @Test - public void should_override_resource_environment_variables_to_container_environment_variables() { + public void overrideResourceEnvironmentVariablesToContainerEnvironmentVariables() { // Given final DummyBaseEnricher dummyBaseEnricher = new DummyBaseEnricher(enricherContext, "dummy");