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

JENKINS-47376: Fix global env variables #245

Merged
merged 5 commits into from
Feb 3, 2018

Conversation

MattLud
Copy link
Contributor

@MattLud MattLud commented Nov 7, 2017

https://issues.jenkins-ci.org/browse/JENKINS-47376

This fixes the missing global environment variables that are missing when a container runs.

As an alternative fix, this could instead be included as part of the POD level environment variables rather than injected and I can move it there if desired.

@MattLud
Copy link
Contributor Author

MattLud commented Nov 7, 2017

The alternate spot to inject these would be here -

final PodTemplate unwrappedTemplate = slave.getTemplate();

As part of the final setup before scheduling the Pod and as part of the Pod EnvVars.

Copy link
Contributor

@carlossg carlossg left a comment

Choose a reason for hiding this comment

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

some minor comments, but otherwise looks good


public ContainerExecDecorator(KubernetesClient client, String podName, String containerName, String namespace, EnvironmentExpander environmentExpander) {
public ContainerExecDecorator(KubernetesClient client, String podName, String containerName, String namespace, EnvironmentExpander environmentExpander, EnvVars globalVars) {
Copy link
Contributor

Choose a reason for hiding this comment

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

can we keep binary compatibility ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should be done in the latest iteration.

DescribableList<NodeProperty<?>, NodePropertyDescriptor>globalNodeProperties = instance.getGlobalNodeProperties();
List<EnvironmentVariablesNodeProperty> envVarsNodePropertyList = globalNodeProperties.getAll(EnvironmentVariablesNodeProperty.class);
if ( envVarsNodePropertyList != null && envVarsNodePropertyList.size() != 0 ) {
globalVars = envVarsNodePropertyList.get(0).getEnvVars();
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume you need to iterate through envVarsNodePropertyList not just take the first item

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You shouldn't unless there are multiple GlobalNodeProperties in a config.

@MattLud MattLud force-pushed the JENKINS-47376 branch 2 times, most recently from 5d3587c to c0ee807 Compare December 22, 2017 19:42
Copy link
Contributor

@carlossg carlossg left a comment

Choose a reason for hiding this comment

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

Sorry for the delay, I've had the time to look into detail and found some issues, wrote the tests and commented

boolean javaHome_detected = false;
for(String env: procStarter)
{
if(env.contains("JAVA_HOME"))
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand what does it mean if JAVA_HOME is there, what does that mean?

Copy link
Contributor

Choose a reason for hiding this comment

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

and due to the break any variable after JAVA_HOME is ignored, so the order of vars matter

@@ -96,6 +102,14 @@ public void configureCloud() throws Exception {
JenkinsLocationConfiguration.get().setUrl(nonLocalhostUrl.toString());

r.jenkins.clouds.add(cloud);

DescribableList<NodeProperty<?>, NodePropertyDescriptor> list = r.jenkins.getGlobalNodeProperties();
list.getAll(hudson.slaves.EnvironmentVariablesNodeProperty.class);
Copy link
Contributor

Choose a reason for hiding this comment

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

unused?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.

// built-in ones.
boolean javaHome_detected = false;
for (String env : procStarter) {
if (env.contains("JAVA_HOME")) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand what does it mean if JAVA_HOME is there?

and due to the break any variable after JAVA_HOME is ignored, so the order of vars matter

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed that break...The JAVA_HOME comes from not being able to tell if the execProc was dispatched by another plugin, thus necessitating the entire provided environment variables to be injected or from the master, thus overwriting whatever local environment variables there were.

At least, that the best I can understand from the various ways Jenkins envVars are provided.

@carlossg carlossg merged commit f46193a into jenkinsci:master Feb 3, 2018
@pauxus
Copy link

pauxus commented Feb 4, 2018

I implemented a similiar fix (before seeing your PR, unfortunately), however, I took a slightly different approach (taking a cue from the pipeline plugin), which might be somewhat more generic.

When injecting the variables from the proc starter, I simply removed all variables from computer.environment (key AND value). So anything that is not already there must have come from another plugin (like globalVar, configFile provider, Maven Pipeline Integration etc.). Makes for shorter code and works.

The only caveat (which should be documented) is the workings of PATH+X like environment expansions, which must only be done inside container statements to work. If needed, I can adapt my changes to the current state, but this would mostly overshadow this PR.

@MattLud
Copy link
Contributor Author

MattLud commented Feb 5, 2018

@pauxus - Feel free to open it and rework/remove items contributed by this PR. This code makes some naive assumptions about envvars and there is probably a better means of handling proc-injected variables though this is mostly for globals.

👍

@MattLud MattLud deleted the JENKINS-47376 branch February 5, 2018 16:50
@MattLud
Copy link
Contributor Author

MattLud commented Feb 8, 2018

@pauxus - I've opened #283 to address a related issue. Feel free to weigh in on that as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants