This repository was archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 199
feat(#937): Overrides environment variables when defined in resources #1434
Open
lordofthejars
wants to merge
7
commits into
fabric8io:master
Choose a base branch
from
lordofthejars:issue-937
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b19c9a4
feat(#937): Overrides environment variables when defined in resources
lordofthejars 3fc7467
feat(#937): Use ifPresent method in Optionals
lordofthejars d20f854
Fixes test names
lordofthejars 6728e8a
Merge branch 'master' into issue-937
lordofthejars 54ff956
Merge branch 'master' into issue-937
rhuss 12c90ae
Merge branch 'master' into issue-937
rhuss 74cffbf
Merge branch 'master' into issue-937
rhuss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
151 changes: 151 additions & 0 deletions
151
enricher/api/src/test/java/io/fabric8/maven/enricher/api/BaseEnricherTest.java
This file contains hidden or 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,151 @@ | ||
| /** | ||
| * 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; | ||
| 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.KubernetesListBuilder; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import mockit.Mocked; | ||
| import mockit.integration.junit4.JMockit; | ||
| 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 setEnvironmentVariablesFromResources() { | ||
|
|
||
| // Given | ||
| final DummyBaseEnricher dummyBaseEnricher = new DummyBaseEnricher(enricherContext, "dummy"); | ||
|
|
||
| // When | ||
| final KubernetesListBuilder kubernetesListBuilder = createKubernetesList(); | ||
| final HashMap<String, String> resourceEnv = new HashMap<>(); | ||
| resourceEnv.put("A", "B"); | ||
|
|
||
| dummyBaseEnricher.overrideEnvironmentVariables(kubernetesListBuilder, resourceEnv); | ||
|
|
||
| // Then | ||
| kubernetesListBuilder.accept(new TypedVisitor<ContainerBuilder>() { | ||
| @Override | ||
| public void visit(ContainerBuilder element) { | ||
| final List<EnvVar> envVars = element.buildEnv(); | ||
|
|
||
| assertThat(envVars) | ||
| .containsExactly(createEnvVar("A", "B")); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| public void addResourceEnvironmentVariablesToContainerEnvironmentVariables() { | ||
|
|
||
| // Given | ||
| final DummyBaseEnricher dummyBaseEnricher = new DummyBaseEnricher(enricherContext, "dummy"); | ||
|
|
||
| // When | ||
| final KubernetesListBuilder kubernetesListBuilder = createKubernetesList(createEnvVar("C", "D")); | ||
| final HashMap<String, String> resourceEnv = new HashMap<>(); | ||
| resourceEnv.put("A", "B"); | ||
|
|
||
|
|
||
| dummyBaseEnricher.overrideEnvironmentVariables(kubernetesListBuilder, resourceEnv); | ||
|
|
||
| // Then | ||
| kubernetesListBuilder.accept(new TypedVisitor<ContainerBuilder>() { | ||
| @Override | ||
| public void visit(ContainerBuilder element) { | ||
| final List<EnvVar> envVars = element.buildEnv(); | ||
|
|
||
| assertThat(envVars) | ||
| .containsExactlyInAnyOrder(createEnvVar("A", "B"), createEnvVar("C", "D")); | ||
| } | ||
| }); | ||
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void overrideResourceEnvironmentVariablesToContainerEnvironmentVariables() { | ||
|
|
||
| // Given | ||
| final DummyBaseEnricher dummyBaseEnricher = new DummyBaseEnricher(enricherContext, "dummy"); | ||
|
|
||
| // When | ||
| final KubernetesListBuilder kubernetesListBuilder = createKubernetesList(createEnvVar("A", "B")); | ||
| final HashMap<String, String> resourceEnv = new HashMap<>(); | ||
| resourceEnv.put("A", "C"); | ||
|
|
||
| dummyBaseEnricher.overrideEnvironmentVariables(kubernetesListBuilder, resourceEnv); | ||
|
|
||
| // Then | ||
| kubernetesListBuilder.accept(new TypedVisitor<ContainerBuilder>() { | ||
| @Override | ||
| public void visit(ContainerBuilder element) { | ||
| final List<EnvVar> 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); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -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,12 @@ private void addArtifactsWithYaml(Set<URL> artifactSet, String dependencyYaml) { | |
|
|
||
| @Override | ||
| public void adapt(final KubernetesListBuilder builder) { | ||
|
|
||
| getConfiguration().getResource().ifPresent(resourceConfig -> { | ||
| overrideEnvironmentVariables(builder, resourceConfig.getEnv() | ||
| .orElse(new HashMap<>())); | ||
| }); | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really not sure why we need to duplicate that piece of code in every enricher. For me that looks like a smell. Whats about a post processing step outside of all enricher, which just overrides the environment variables added by the enricher chain ? |
||
| final List<HasMetadata> kubernetesItems = new ArrayList<>(); | ||
| processArtifactSetResources(this.kubernetesDependencyArtifacts, items -> { | ||
| kubernetesItems.addAll(Arrays.asList(items.toArray(new HasMetadata[items.size()]))); | ||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.