forked from eclipse-jkube/jkube
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Enricher defined Container env vars get merged with vars defined…
… in Image Build Configuration Signed-off-by: Marc Nuri <marc@marcnuri.com>
- Loading branch information
Showing
22 changed files
with
588 additions
and
15 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
105 changes: 105 additions & 0 deletions
105
...rc/main/java/org/eclipse/jkube/enricher/generic/ContainerEnvJavaOptionsMergeEnricher.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,105 @@ | ||
/** | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at: | ||
* | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.jkube.enricher.generic; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.eclipse.jkube.kit.common.Configs; | ||
import org.eclipse.jkube.kit.config.image.ImageConfiguration; | ||
import org.eclipse.jkube.kit.config.image.build.BuildConfiguration; | ||
import org.eclipse.jkube.kit.config.resource.PlatformMode; | ||
import org.eclipse.jkube.kit.enricher.api.BaseEnricher; | ||
import org.eclipse.jkube.kit.enricher.api.JKubeEnricherContext; | ||
|
||
import io.fabric8.kubernetes.api.builder.TypedVisitor; | ||
import io.fabric8.kubernetes.api.model.ContainerBuilder; | ||
import io.fabric8.kubernetes.api.model.EnvVar; | ||
import io.fabric8.kubernetes.api.model.KubernetesListBuilder; | ||
|
||
import static org.eclipse.jkube.kit.common.Configs.asBoolean; | ||
|
||
/** | ||
* Enricher to merge <code>JAVA_OPTIONS</code> environment variables defined in {@link BuildConfiguration#getEnv()} | ||
* with those added to {@link io.fabric8.kubernetes.api.model.Container} by other enrichers. | ||
* | ||
* <p> This prevents Container environment variables from overriding and hiding the initial JAVA_OPTIONS value | ||
* possibly added by some <code>Generator</code>. | ||
*/ | ||
public class ContainerEnvJavaOptionsMergeEnricher extends BaseEnricher { | ||
|
||
@AllArgsConstructor | ||
private enum Config implements Configs.Config { | ||
// What pull policy to use when fetching images | ||
DISABLE("disable", "false"); | ||
|
||
@Getter | ||
protected String key; | ||
@Getter | ||
protected String defaultValue; | ||
} | ||
|
||
public ContainerEnvJavaOptionsMergeEnricher(JKubeEnricherContext enricherContext) { | ||
super(enricherContext, "jkube-container-env-java-options"); | ||
} | ||
|
||
@Override | ||
public void enrich(PlatformMode platformMode, KubernetesListBuilder builder) { | ||
if (!asBoolean(getConfig(Config.DISABLE)) && hasImageConfiguration()) { | ||
builder.accept(new ContainerEnvJavaOptionsMergeVisitor(getImages())); | ||
} | ||
} | ||
|
||
static final class ContainerEnvJavaOptionsMergeVisitor extends TypedVisitor<ContainerBuilder> { | ||
|
||
private static final String ENV_KEY = "JAVA_OPTIONS"; | ||
|
||
private final List<ImageConfiguration> imageConfigurations; | ||
|
||
public ContainerEnvJavaOptionsMergeVisitor(List<ImageConfiguration> imageConfigurations) { | ||
this.imageConfigurations = imageConfigurations; | ||
} | ||
|
||
@Override | ||
public void visit(ContainerBuilder containerBuilder) { | ||
imageConfigurations.stream() | ||
.filter(ic -> ImageEnricher.containerImageName(ic).equals(containerBuilder.getImage())) | ||
.filter(ic -> !ic.getBuild().getEnv().isEmpty()) | ||
.filter(ic -> ic.getBuild().getEnv().containsKey(ENV_KEY)) | ||
.findFirst() | ||
.ifPresent(ic -> containerBuilder.withEnv(mergeEnv(containerBuilder.buildEnv(), ic))); | ||
} | ||
|
||
private List<EnvVar> mergeEnv(List<EnvVar> envVars, ImageConfiguration imageConfiguration) { | ||
final List<EnvVar> ret = new ArrayList<>(); | ||
for (EnvVar env : envVars) { | ||
if (env.getName().equalsIgnoreCase(ENV_KEY)) { | ||
final EnvVar merged = new EnvVar(); | ||
merged.setName(env.getName()); | ||
merged.setValueFrom(env.getValueFrom()); | ||
merged.setValue(String.format("%s %s", | ||
imageConfiguration.getBuild().getEnv().getOrDefault(ENV_KEY, ""), | ||
env.getValue() | ||
)); | ||
ret.add(merged); | ||
} else { | ||
ret.add(env); | ||
} | ||
} | ||
return ret; | ||
} | ||
} | ||
} |
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
114 changes: 114 additions & 0 deletions
114
...ic/src/test/java/org/eclipse/jkube/enricher/generic/ContainerEnvJavaOptionsMergeTest.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,114 @@ | ||
/** | ||
* Copyright (c) 2019 Red Hat, Inc. | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at: | ||
* | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
package org.eclipse.jkube.enricher.generic; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Properties; | ||
import java.util.stream.Collectors; | ||
|
||
import io.fabric8.kubernetes.api.model.EnvVar; | ||
import io.fabric8.kubernetes.api.model.apps.Deployment; | ||
import org.eclipse.jkube.kit.config.image.ImageConfiguration; | ||
|
||
import io.fabric8.kubernetes.api.model.Container; | ||
import io.fabric8.kubernetes.api.model.ContainerBuilder; | ||
import io.fabric8.kubernetes.api.model.KubernetesListBuilder; | ||
import io.fabric8.kubernetes.api.model.apps.DeploymentBuilder; | ||
import mockit.Expectations; | ||
import mockit.Mocked; | ||
import org.eclipse.jkube.kit.config.resource.PlatformMode; | ||
import org.eclipse.jkube.kit.enricher.api.JKubeEnricherContext; | ||
import org.eclipse.jkube.kit.enricher.api.model.Configuration; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@SuppressWarnings("ResultOfMethodCallIgnored") | ||
public class ContainerEnvJavaOptionsMergeTest { | ||
|
||
@SuppressWarnings("unused") | ||
@Mocked | ||
private ImageConfiguration imageConfiguration; | ||
|
||
@Mocked | ||
private JKubeEnricherContext context; | ||
|
||
private ContainerEnvJavaOptionsMergeEnricher containerEnvJavaOptionsMergeEnricher; | ||
private KubernetesListBuilder kubernetesListBuilder; | ||
private Properties properties; | ||
|
||
@Before | ||
public void setUp() { | ||
containerEnvJavaOptionsMergeEnricher = new ContainerEnvJavaOptionsMergeEnricher(context); | ||
kubernetesListBuilder = new KubernetesListBuilder(); | ||
properties = new Properties(); | ||
// @formatter:off | ||
kubernetesListBuilder.addToItems(new DeploymentBuilder().withNewSpec() | ||
.withNewTemplate() | ||
.withNewSpec() | ||
.addToContainers(new ContainerBuilder() | ||
.withImage("the-image:latest") | ||
.addToEnv(new EnvVar("JAVA_OPTIONS", "val-from-container", null)) | ||
.build()) | ||
.endSpec() | ||
.endTemplate() | ||
.endSpec().build()); | ||
new Expectations() {{ | ||
context.getConfiguration(); result = Configuration.builder().image(imageConfiguration).build(); | ||
context.getProperties(); result = properties; | ||
}}; | ||
// @formatter:on | ||
} | ||
|
||
@Test | ||
public void enrichWithDefaultsShouldMergeValues() { | ||
// Given | ||
// @formatter:off | ||
new Expectations() {{ | ||
imageConfiguration.getName(); result = "the-image:latest"; minTimes = 0; | ||
imageConfiguration.getBuild().getEnv(); result = Collections.singletonMap("JAVA_OPTIONS", "val-from-ic"); minTimes = 0; | ||
}}; | ||
// @formatter:on | ||
// When | ||
containerEnvJavaOptionsMergeEnricher.enrich(PlatformMode.kubernetes, kubernetesListBuilder); | ||
// Then | ||
assertThat(containerList(kubernetesListBuilder)) | ||
.flatExtracting("env") | ||
.hasSize(1) | ||
.containsOnly(new EnvVar("JAVA_OPTIONS", "val-from-ic val-from-container", null)); | ||
} | ||
|
||
@Test | ||
public void enrichWithDisabledShouldDoNothing() { | ||
// Given | ||
properties.put("jkube.enricher.jkube-container-env-java-options.disable", "true"); | ||
// When | ||
containerEnvJavaOptionsMergeEnricher.enrich(PlatformMode.kubernetes, kubernetesListBuilder); | ||
// Then | ||
assertThat(containerList(kubernetesListBuilder)) | ||
.flatExtracting("env") | ||
.hasSize(1) | ||
.containsOnly(new EnvVar("JAVA_OPTIONS", "val-from-container", null)); | ||
} | ||
|
||
static List<Container> containerList(KubernetesListBuilder kubernetesListBuilder) { | ||
return kubernetesListBuilder.build().getItems().stream() | ||
.map(Deployment.class::cast) | ||
.flatMap(d -> d.getSpec().getTemplate().getSpec().getContainers().stream()) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
} |
Oops, something went wrong.