Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add plugin configuration for environment variables #930

Merged
merged 7 commits into from
Sep 5, 2018
Merged

Conversation

TadCordle
Copy link
Contributor

@TadCordle TadCordle commented Sep 4, 2018

Fixes #890

This adds a new environment configuration parameter to the maven and gradle plugins to set environment variables on the container image.

Maven:

<configuration>
  <container>
    <environment>
      <key1>value1</key1>
      <key2>value2</key2>
    </environment>
  </container>
</configuration>

Gradle:

jib {
  container {
    environment = [key1:"value1", key2:"value2"]
  }
}

This PR also adds the corresponding "ENV" command to the docker context generator.

Also slipped skip into the changelog to fix #929

@@ -267,14 +282,24 @@ String makeDockerfile() throws JsonProcessingException {
dockerfile.append("\nEXPOSE ").append(port);
}

boolean firstLabel = true;
boolean firstElement = true;
for (Entry<String, String> env : environment.entrySet()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reusing booleans is a error prone. Maybe this code should use java.util.StringJoiner instead — it supports a prefix.

@@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file.

### Added

- `<skip>` configuration parameter to skip Jib execution in multi-module projects ([#865](https://github.com/GoogleContainerTools/jib/issues/865))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and add also settable via property jib.skip

Assert.assertThat(
dockerContainerConfig,
CoreMatchers.containsString(
" \"var1=value1\",\n \"var2=value2\"\n"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to match the environment part ("Env": [) too, if possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is that there are other environment variables already in there, like PATH and SSL_CERT_FILE. I guess I could use regex to skip those or just match those variables in the string as well (I'm not sure if they change though)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I can just use docker inspect -f {{.Config.Env}} <image>.

@@ -226,4 +197,31 @@ private BuildConfiguration getBuildConfiguration(
.setCreatedBy("jib-integration-test")
.build();
}

private void assertDockerInspect(String imageReference) throws IOException, InterruptedException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it can be static?

/**
* Sets the environment variables
*
* @param environment the environment variables
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: map from environment variable name to value?

for (Entry<String, String> env : environment.entrySet()) {
joiner.add(env.getKey() + "=" + objectMapper.writeValueAsString(env.getValue()));
}
if (environment.entrySet().size() > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can just be environment.size()

dockerfile.append(joiner.toString());
}

joiner = new StringJoiner(" \\\n ", "\nLABEL ", "");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic for labels seems to be the same as the logic for environment - maybe condense to a static method?

@@ -34,6 +34,7 @@ jib {
args = ['An argument.']
mainClass = 'com.test.HelloWorld'
jvmFlags = ['-Xms512m', '-Xdebug']
environment = [var1:'value1', var2:'value2']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: it might be clearer if the values were like env1/env2?

@@ -34,7 +34,7 @@ jib {
args = ['An argument.']
mainClass = 'com.test.HelloWorld'
jvmFlags = ['-Xms512m', '-Xdebug']
environment = [var1:'value1', var2:'value2']
environment = [env1:'value1', env2:'value2']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I was referring to value1 and value2 since it would make the assertEquals be something like \nenv1value\nenv2value\n rather than \nvalue1\nvalue2\n where value1 and value2 are not immediately clear where they should have come from.

* @throws JsonProcessingException if getting the json string of a map value fails
*/
private static String mapToDockerfileString(
Map<String, String> map, String command, ObjectMapper objectMapper)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ObjectMapper can prob just be eliminated and made into a constant.

*
* <pre>{@code
* command key1="value1" \
* key2="value2" \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should we line up key1 and key2 since we're adding spaces anyways?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command names are different lengths, and adding a variable number of spaces here would have harmed readability or required a weird extra parameter, so I don't think it's worth it, unless there's a function we can use to generate whitespace that I don't know about (that doesn't require another import). I think it's fine as is.

@patflynn
Copy link
Contributor

patflynn commented Sep 5, 2018

It'd be really nice if these PRs had a bit more content in the description so it's really easy for reviewers to understand what the changes are without looking at code.

For instance if this PR actually changes plugin config params it should include examples of what it enables.

@TadCordle TadCordle merged commit 499c3d6 into master Sep 5, 2018
@TadCordle TadCordle deleted the env-config branch September 5, 2018 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add CHANGELOG for skip parameter How to set environment variables in the container
5 participants