Skip to content

Commit

Permalink
JENKINS-47376: Fix global env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
MattLud committed Nov 7, 2017
1 parent 4303ec3 commit 8d44527
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,34 +82,35 @@ public class ContainerExecDecorator extends LauncherDecorator implements Seriali
private final String namespace;
private final String containerName;
private final EnvironmentExpander environmentExpander;
private final EnvVars globalVars;

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) {
this.client = client;
this.podName = podName;
this.namespace = namespace;
this.containerName = containerName;
this.environmentExpander = environmentExpander;
this.globalVars = globalVars;
}

public ContainerExecDecorator(KubernetesClient client, String podName, String containerName, String namespace) {
this(client, podName, containerName, namespace, null);
this(client, podName, containerName, namespace, null, null);
}

@Deprecated
public ContainerExecDecorator(KubernetesClient client, String podName, String containerName, AtomicBoolean alive, CountDownLatch started, CountDownLatch finished, String namespace) {
this(client, podName, containerName, namespace, null);
this(client, podName, containerName, namespace, null, null);
}

@Deprecated
public ContainerExecDecorator(KubernetesClient client, String podName, String containerName, AtomicBoolean alive, CountDownLatch started, CountDownLatch finished) {
this(client, podName, containerName, null, null);
this(client, podName, containerName, null);
}

@Deprecated
public ContainerExecDecorator(KubernetesClient client, String podName, String containerName, String path, AtomicBoolean alive, CountDownLatch started, CountDownLatch finished) {
this(client, podName, containerName, null, null);
this(client, podName, containerName, null);
}

@Override
public Launcher decorate(final Launcher launcher, final Node node) {
return new Launcher.DecoratedLauncher(launcher) {
Expand Down Expand Up @@ -223,6 +224,10 @@ public void onClose(int i, String s) {
String.format("cd \"%s\"%s", pwd, NEWLINE).getBytes(StandardCharsets.UTF_8));

}
//get global vars here, run the export first as they'll get overwritten.
if (globalVars != null) {
this.setupEnvironmentVariable(globalVars, watch);
}

EnvVars envVars = new EnvVars();
if (environmentExpander != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@

import hudson.EnvVars;
import hudson.LauncherDecorator;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.slaves.NodeProperty;
import hudson.slaves.NodePropertyDescriptor;
import hudson.util.DescribableList;
import io.fabric8.kubernetes.client.KubernetesClient;
import java.util.List;

import javax.annotation.Nonnull;
import jenkins.model.Jenkins;

import static org.csanchez.jenkins.plugins.kubernetes.pipeline.Resources.closeQuietly;

Expand Down Expand Up @@ -43,7 +49,14 @@ public boolean start() throws Exception {
client = nodeContext.connectToCloud();

EnvironmentExpander env = getContext().get(EnvironmentExpander.class);
decorator = new ContainerExecDecorator(client, nodeContext.getPodName(), containerName, nodeContext.getNamespace(), env);
EnvVars globalVars = null;
Jenkins instance = Jenkins.getInstance();
DescribableList<NodeProperty<?>, NodePropertyDescriptor>globalNodeProperties = instance.getGlobalNodeProperties();
List<EnvironmentVariablesNodeProperty> envVarsNodePropertyList = globalNodeProperties.getAll(EnvironmentVariablesNodeProperty.class);
if ( envVarsNodePropertyList != null && envVarsNodePropertyList.size() != 0 ) {
globalVars = envVarsNodePropertyList.get(0).getEnvVars();
}
decorator = new ContainerExecDecorator(client, nodeContext.getPodName(), containerName, nodeContext.getNamespace(), env, globalVars);
getContext().newBodyInvoker()
.withContext(BodyInvoker
.mergeLauncherDecorators(getContext().get(LauncherDecorator.class), decorator))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
import org.jvnet.hudson.test.LoggerRule;

import com.google.common.collect.ImmutableMap;
import hudson.EnvVars;
import hudson.slaves.EnvironmentVariablesNodeProperty;
import hudson.slaves.NodeProperty;
import hudson.slaves.NodePropertyDescriptor;
import hudson.util.DescribableList;

import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.api.model.SecretBuilder;
Expand All @@ -61,6 +66,7 @@ public class AbstractKubernetesPipelineTest {
protected static final String SECRET_KEY = "password";
protected static final String CONTAINER_ENV_VAR_FROM_SECRET_VALUE = "container-pa55w0rd";
protected static final String POD_ENV_VAR_FROM_SECRET_VALUE = "pod-pa55w0rd";
protected static final String GLOBAL = "GLOBAL";

@ClassRule
public static BuildWatcher buildWatcher = new BuildWatcher();
Expand Down Expand Up @@ -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);
EnvironmentVariablesNodeProperty newEnvVarsNodeProperty = new hudson.slaves.EnvironmentVariablesNodeProperty();
list.add(newEnvVarsNodeProperty);
EnvVars envVars = newEnvVarsNodeProperty.getEnvVars();
envVars.put("GLOBAL", "GLOBAL");
r.jenkins.save();
}

private PodTemplate buildBusyboxTemplate(String label) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,14 @@ private void assertEnvVars(JenkinsRuleNonLocalhost r2, WorkflowRun b) throws Exc
r.assertLogContains("INSIDE_CONTAINER_ENV_VAR_FROM_SECRET = " + CONTAINER_ENV_VAR_FROM_SECRET_VALUE + "\n", b);
r.assertLogContains("INSIDE_POD_ENV_VAR = " + POD_ENV_VAR_VALUE + "\n", b);
r.assertLogContains("INSIDE_POD_ENV_VAR_FROM_SECRET = " + POD_ENV_VAR_FROM_SECRET_VALUE + "\n", b);
r.assertLogContains("INSIDE_GLOBAL = " + GLOBAL + "\n", b);


r.assertLogContains("OUTSIDE_CONTAINER_ENV_VAR =\n", b);
r.assertLogContains("OUTSIDE_CONTAINER_ENV_VAR_LEGACY =\n", b);
r.assertLogContains("OUTSIDE_CONTAINER_ENV_VAR_FROM_SECRET =\n", b);
r.assertLogContains("OUTSIDE_POD_ENV_VAR = " + POD_ENV_VAR_VALUE + "\n", b);
r.assertLogContains("OUTSIDE_POD_ENV_VAR_FROM_SECRET = " + POD_ENV_VAR_FROM_SECRET_VALUE + "\n", b);
r.assertLogContains("OUTSIDE_GLOBAL = " + GLOBAL + "\n", b);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ node ('busybox') {
echo OUTSIDE_CONTAINER_ENV_VAR_FROM_SECRET = \$CONTAINER_ENV_VAR_FROM_SECRET
echo OUTSIDE_POD_ENV_VAR = \$POD_ENV_VAR
echo OUTSIDE_POD_ENV_VAR_FROM_SECRET = \$POD_ENV_VAR_FROM_SECRET
echo OUTSIDE_GLOBAL = \$GLOBAL
"""

stage('Run busybox') {
Expand All @@ -18,6 +19,7 @@ node ('busybox') {
echo INSIDE_CONTAINER_ENV_VAR_FROM_SECRET = \$CONTAINER_ENV_VAR_FROM_SECRET
echo INSIDE_POD_ENV_VAR = \$POD_ENV_VAR
echo INSIDE_POD_ENV_VAR_FROM_SECRET = \$POD_ENV_VAR_FROM_SECRET
echo INSIDE_GLOBAL = \$GLOBAL
"""
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ podTemplate(label: 'mypod',
echo OUTSIDE_CONTAINER_ENV_VAR_FROM_SECRET = \$CONTAINER_ENV_VAR_FROM_SECRET
echo OUTSIDE_POD_ENV_VAR = \$POD_ENV_VAR
echo OUTSIDE_POD_ENV_VAR_FROM_SECRET = \$POD_ENV_VAR_FROM_SECRET
echo OUTSIDE_GLOBAL = \$GLOBAL
"""
stage('Run busybox') {
container('busybox') {
Expand All @@ -30,6 +31,7 @@ podTemplate(label: 'mypod',
echo INSIDE_CONTAINER_ENV_VAR_FROM_SECRET = \$CONTAINER_ENV_VAR_FROM_SECRET
echo INSIDE_POD_ENV_VAR = \$POD_ENV_VAR
echo INSIDE_POD_ENV_VAR_FROM_SECRET = \$POD_ENV_VAR_FROM_SECRET
echo INSIDE_GLOBAL = \$GLOBAL
"""
}
}
Expand Down

0 comments on commit 8d44527

Please sign in to comment.