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

Enable Additional Docker Run Arguments #308

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.jenkinsci.plugins.docker.commons.tools.DockerTool;
import org.jenkinsci.plugins.docker.workflow.client.DockerClient;
import org.jenkinsci.plugins.docker.workflow.client.WindowsDockerClient;
import org.jenkinsci.plugins.docker.workflow.declarative.DeclarativeDockerUtils;
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepExecutionImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
Expand Down Expand Up @@ -197,7 +198,8 @@ public Execution() {
}

String command = launcher.isUnix() ? "cat" : "cmd.exe";
container = dockerClient.run(env, step.image, step.args, ws, volumes, volumesFromContainers, envReduced, dockerClient.whoAmI(), /* expected to hang until killed */ command);
String args = (DeclarativeDockerUtils.getAdditionalRunArgs() + " " + Util.fixNull(step.args)).trim();
container = dockerClient.run(env, step.image, args, ws, volumes, volumesFromContainers, envReduced, dockerClient.whoAmI(), /* expected to hang until killed */ command);
final List<String> ps = dockerClient.listProcess(env, container);
if (!ps.contains(command)) {
listener.error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@
return null;
}

@Whitelisted
public static String getAdditionalRunArgs() {
return getAdditionalRunArgs(null);
}

@Whitelisted
public static String getAdditionalRunArgs(@Nullable String override) {
if (!StringUtils.isBlank(override)) {

Check warning on line 95 in src/main/java/org/jenkinsci/plugins/docker/workflow/declarative/DeclarativeDockerUtils.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 95 is only partially covered, one branch is missing
return override;

Check warning on line 96 in src/main/java/org/jenkinsci/plugins/docker/workflow/declarative/DeclarativeDockerUtils.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 96 is not covered by tests
} else {
Run<?,?> r = currentRun();
for (DockerPropertiesProvider provider : DockerPropertiesProvider.all()) {
String additionalRunArgs = provider.getAdditionalRunArgs(r);
if (!StringUtils.isBlank(additionalRunArgs)) {
return additionalRunArgs;
}
}
}
return "";
}

@Whitelisted
public static WithScriptScript<?> getLabelScript(AbstractDockerAgent<?> describable, CpsScript script) throws Exception {
String targetLabel = getLabel(describable.getLabel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public abstract class DockerPropertiesProvider implements ExtensionPoint {
@CheckForNull
public abstract String getLabel(@Nullable Run run);

@CheckForNull
public abstract String getAdditionalRunArgs(@Nullable Run run);

public static ExtensionList<DockerPropertiesProvider> all() {
return ExtensionList.lookup(DockerPropertiesProvider.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import hudson.Extension;
import hudson.Util;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.model.Items;
Expand All @@ -47,6 +48,7 @@
*/
public class FolderConfig extends AbstractFolderProperty<AbstractFolder<?>> {
private String dockerLabel;
private String additionalRunArgs;
private DockerRegistryEndpoint registry;

@DataBoundConstructor
Expand All @@ -60,20 +62,30 @@
* @param url The registry URL
* @param creds the registry credentials ID
*/
public FolderConfig(String dockerLabel, String url, String creds) {
public FolderConfig(String dockerLabel, String additionalRunArgs, String url, String creds) {
this.dockerLabel = dockerLabel;
this.additionalRunArgs = additionalRunArgs;
this.registry = new DockerRegistryEndpoint(url, creds);
}

public String getDockerLabel() {
return dockerLabel;
}

public String getAdditionalRunArgs() {
return Util.fixNull(additionalRunArgs);
}

@DataBoundSetter
public void setDockerLabel(String dockerLabel) {
this.dockerLabel = dockerLabel;
}

@DataBoundSetter
public void setAdditionalRunArgs(String additionalArgs) {
this.additionalRunArgs = additionalArgs;
}

Check warning on line 87 in src/main/java/org/jenkinsci/plugins/docker/workflow/declarative/FolderConfig.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 86-87 are not covered by tests

public DockerRegistryEndpoint getRegistry() {
return registry;
}
Expand Down Expand Up @@ -128,6 +140,34 @@
return null;
}

@Override
public String getAdditionalRunArgs(@Nullable Run run) {
if (run != null) {

Check warning on line 145 in src/main/java/org/jenkinsci/plugins/docker/workflow/declarative/FolderConfig.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 145 is only partially covered, one branch is missing
Job job = run.getParent();
ItemGroup parent = job.getParent();
while (parent != null) {

if (parent instanceof AbstractFolder) {
AbstractFolder folder = (AbstractFolder) parent;
FolderConfig config = (FolderConfig) folder.getProperties().get(FolderConfig.class);
if (config != null) {
String additionalRunArgs = config.getAdditionalRunArgs();
if (!StringUtils.isBlank(additionalRunArgs)) {
return additionalRunArgs;
}
}
}

if (parent instanceof Item) {
parent = ((Item) parent).getParent();
} else {
parent = null;
}
}
}
return "";
}

@Override
public String getRegistryUrl(@Nullable Run run) {
if (run != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class GlobalConfig extends GlobalConfiguration {
private static final Logger LOGGER = Logger.getLogger(GlobalConfig.class.getName());

private String dockerLabel;
private String additionalRunArgs;
private DockerRegistryEndpoint registry;

public GlobalConfig() {
Expand All @@ -79,11 +80,20 @@ public String getDockerLabel() {
return Util.fixEmpty(dockerLabel);
}

public String getAdditionalRunArgs() {
return Util.fixNull(additionalRunArgs);
}

@DataBoundSetter
public void setDockerLabel(String dockerLabel) {
this.dockerLabel = dockerLabel;
}

@DataBoundSetter
public void setAdditionalRunArgs(String additionalRunArgs) {
this.additionalRunArgs = additionalRunArgs;
}

public DockerRegistryEndpoint getRegistry() {
return registry;
}
Expand Down Expand Up @@ -114,6 +124,11 @@ public String getLabel(@Nullable Run run) {
return config.getDockerLabel();
}

@Override
public String getAdditionalRunArgs(@Nullable Run run) {
return config.getAdditionalRunArgs();
}

@Override
public String getRegistryUrl(@Nullable Run run) {
if (config.getRegistry() != null && !StringUtils.isBlank(config.getRegistry().getUrl())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ def f = namespace(lib.FormTagLib)
f.entry(field: "dockerLabel", title: _("Docker Label")) {
f.textbox()
}
f.entry(field: "additionalRunArgs", title:_("Docker Additional Run Arguments")) {
f.textbox()
}
f.property(field: "registry")
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@ f.section(title:_("Declarative Pipeline (Docker)")) {
f.entry(field: "dockerLabel", title:_("Docker Label")) {
f.textbox()
}
f.entry(field: "additionalRunArgs", title:_("Docker Additional Run Arguments")) {
f.textbox()
}
f.property(field: "registry")
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ public void plainSystemConfig() throws Exception {
Assume.assumeFalse("Fails using the version of Git installed on the Windows ACI agents on ci.jenkins.io", Functions.isWindows());
GlobalConfig.get().setDockerLabel("config_docker");
GlobalConfig.get().setRegistry(new DockerRegistryEndpoint("https://docker.registry", globalCred.getId()));
GlobalConfig.get().setAdditionalRunArgs("-v /tmp:/tmptest:ro,z");
expect("org/jenkinsci/plugins/docker/workflow/declarative/declarativeDockerConfig")
.logContains("Docker Label is: config_docker",
"Docker Run Additional Arguments are: -v /tmp:/tmptest:ro,z",
"Registry URL is: https://docker.registry",
"Registry Creds ID is: " + globalCred.getId()).go();
}
Expand All @@ -88,11 +90,12 @@ public void testExtensionOrdinal() {
public void directParent() throws Exception {
Folder folder = j.createProject(Folder.class);
getFolderStore(folder).addCredentials(Domain.global(), folderCred);
folder.addProperty(new FolderConfig("folder_docker", "https://folder.registry", folderCred.getId()));
folder.addProperty(new FolderConfig("folder_docker", "", "https://folder.registry", folderCred.getId()));
expect("org/jenkinsci/plugins/docker/workflow/declarative/declarativeDockerConfig")
.inFolder(folder)
.runFromRepo(false)
.logContains("Docker Label is: folder_docker",
"Docker Run Additional Arguments are: ",
"Registry URL is: https://folder.registry",
"Registry Creds ID is: " + folderCred.getId()).go();
}
Expand All @@ -102,29 +105,33 @@ public void withDefaults() throws Exception {
Folder folder = j.createProject(Folder.class);
getFolderStore(folder).addCredentials(Domain.global(), folderCred);
getFolderStore(folder).addCredentials(Domain.global(), grandParentCred);
folder.addProperty(new FolderConfig("folder_docker", "https://folder.registry", folderCred.getId()));
folder.addProperty(new FolderConfig("folder_docker", "", "https://folder.registry", folderCred.getId()));
expect("org/jenkinsci/plugins/docker/workflow/declarative/declarativeDockerConfigWithOverride")
.inFolder(folder)
.runFromRepo(false)
.logContains("Docker Label is: other-label",
"Docker Run Additional Arguments are: ",
"Registry URL is: https://other.registry",
"Registry Creds ID is: " + grandParentCred.getId()).go();
}

@Test
public void directParentNotSystem() throws Exception {
GlobalConfig.get().setDockerLabel("config_docker");
GlobalConfig.get().setAdditionalRunArgs("-v /tmp:/tmp1:ro,z");
GlobalConfig.get().setRegistry(new DockerRegistryEndpoint("https://docker.registry", globalCred.getId()));
Folder folder = j.createProject(Folder.class);
getFolderStore(folder).addCredentials(Domain.global(), folderCred);
folder.addProperty(new FolderConfig("folder_docker", "https://folder.registry", folderCred.getId()));
folder.addProperty(new FolderConfig("folder_docker", "-v /tmp:/tmp2:ro,z", "https://folder.registry", folderCred.getId()));
expect("org/jenkinsci/plugins/docker/workflow/declarative/declarativeDockerConfig")
.inFolder(folder)
.runFromRepo(false)
.logContains("Docker Label is: folder_docker",
"Docker Run Additional Arguments are: -v /tmp:/tmp2:ro,z",
"Registry URL is: https://folder.registry",
"Registry Creds ID is: " + folderCred.getId())
.logNotContains("Docker Label is: config_docker",
"Docker Run Additional Arguments are: -v /tmp:/tmp1:ro,z",
"Registry URL is: https://docker.registry",
"Registry Creds ID is: " + globalCred.getId()).go();
}
Expand All @@ -133,12 +140,13 @@ public void directParentNotSystem() throws Exception {
public void grandParent() throws Exception {
Folder grandParent = j.createProject(Folder.class);
getFolderStore(grandParent).addCredentials(Domain.global(), grandParentCred);
grandParent.addProperty(new FolderConfig("parent_docker", "https://parent.registry", grandParentCred.getId()));
grandParent.addProperty(new FolderConfig("parent_docker", "", "https://parent.registry", grandParentCred.getId()));
Folder parent = grandParent.createProject(Folder.class, "testParent"); //Can be static since grandParent should be unique
expect("org/jenkinsci/plugins/docker/workflow/declarative/declarativeDockerConfig")
.inFolder(parent)
.runFromRepo(false)
.logContains("Docker Label is: parent_docker",
"Docker Run Additional Arguments are: ",
"Registry URL is: https://parent.registry",
"Registry Creds ID is: " + grandParentCred.getId()).go();
}
Expand All @@ -147,18 +155,20 @@ public void grandParent() throws Exception {
public void grandParentOverride() throws Exception {
Folder grandParent = j.createProject(Folder.class);
getFolderStore(grandParent).addCredentials(Domain.global(), grandParentCred);
grandParent.addProperty(new FolderConfig("parent_docker", "https://parent.registry", grandParentCred.getId()));
grandParent.addProperty(new FolderConfig("parent_docker", "-v /tmp:/tmp1:ro,z", "https://parent.registry", grandParentCred.getId()));
Folder parent = grandParent.createProject(Folder.class, "testParent"); //Can be static since grandParent should be unique
getFolderStore(parent).addCredentials(Domain.global(), folderCred);
parent.addProperty(new FolderConfig("folder_docker", "https://folder.registry", folderCred.getId()));
parent.addProperty(new FolderConfig("folder_docker", "-v /tmp:/tmp2:ro,z", "https://folder.registry", folderCred.getId()));

expect("org/jenkinsci/plugins/docker/workflow/declarative/declarativeDockerConfig")
.inFolder(parent)
.runFromRepo(false)
.logContains("Docker Label is: folder_docker",
"Docker Run Additional Arguments are: -v /tmp:/tmp2:ro,z",
"Registry URL is: https://folder.registry",
"Registry Creds ID is: " + folderCred.getId())
.logNotContains("Docker Label is: parent_docker",
"Docker Run Additional Arguments are: -v /tmp:/tmp1:ro,z",
"Registry URL is: https://parent.registry",
"Registry Creds ID is: " + grandParentCred.getId()).go();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ import org.jenkinsci.plugins.docker.workflow.declarative.DeclarativeDockerUtils


echo "Docker Label is: ${DeclarativeDockerUtils.getLabel()}"
echo "Docker Run Additional Arguments are: ${DeclarativeDockerUtils.getAdditionalRunArgs()}"
echo "Registry URL is: ${DeclarativeDockerUtils.getRegistryUrl()}"
echo "Registry Creds ID is: ${DeclarativeDockerUtils.getRegistryCredentialsId()}"
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
import org.jenkinsci.plugins.docker.workflow.declarative.DeclarativeDockerUtils

echo "Docker Label is: ${DeclarativeDockerUtils.getLabel('other-label')}"
echo "Docker Run Additional Arguments are: ${DeclarativeDockerUtils.getAdditionalRunArgs()}"
echo "Registry URL is: ${DeclarativeDockerUtils.getRegistryUrl('https://other.registry')}"
echo "Registry Creds ID is: ${DeclarativeDockerUtils.getRegistryCredentialsId('grandParentCreds')}"
Loading