From 55e337ead6e6f3309d1465a943b28c3f3ef59045 Mon Sep 17 00:00:00 2001 From: Andrei Laptsinski Date: Tue, 19 Jun 2018 18:21:01 +0300 Subject: [PATCH] Add docker label option (#234) * Add custom docker label * Add Custom docker Labels * Move Docker label settings to Create Container Settings * Revert setLabelString method * Rename Docker labels methods * Update method annotation * Update getDockerLabels and method annotation --- .../yad/commons/DockerCreateContainer.java | 37 +++++++++++++++++++ .../DockerCreateContainer/config.groovy | 4 ++ .../help-dockerLabelString.html | 3 ++ .../github/kostyasha/yad/DockerCloudTest.java | 1 + 4 files changed, 45 insertions(+) create mode 100644 yet-another-docker-plugin/src/main/resources/com/github/kostyasha/yad/commons/DockerCreateContainer/help-dockerLabelString.html diff --git a/yet-another-docker-plugin/src/main/java/com/github/kostyasha/yad/commons/DockerCreateContainer.java b/yet-another-docker-plugin/src/main/java/com/github/kostyasha/yad/commons/DockerCreateContainer.java index 856d3c58..bc356d95 100644 --- a/yet-another-docker-plugin/src/main/java/com/github/kostyasha/yad/commons/DockerCreateContainer.java +++ b/yet-another-docker-plugin/src/main/java/com/github/kostyasha/yad/commons/DockerCreateContainer.java @@ -45,6 +45,8 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.HashMap; +import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -139,6 +141,9 @@ public class DockerCreateContainer extends AbstractDescribableImpl links; + @CheckForNull + private List dockerLabels; + @CheckForNull private Long shmSize; @@ -369,6 +374,25 @@ public void setEnvironmentString(String environmentString) { setEnvironment(splitAndFilterEmpty(environmentString)); } + @Nonnull + public String getDockerLabelsString() { + return joinToStr(dockerLabels); + } + + @DataBoundSetter + public void setDockerLabelsString(String dockerLabelsString) { + setDockerLabels(splitAndFilterEmpty(dockerLabelsString)); + } + + public void setDockerLabels(List dockerLabels) { + this.dockerLabels = dockerLabels; + } + + @Nonnull + public List getDockerLabels() { + return isNull(dockerLabels) ? Collections.EMPTY_LIST : dockerLabels; + } + // extrahosts @CheckForNull public List getExtraHosts() { @@ -542,6 +566,19 @@ public CreateContainerCmd fillContainerConfig(CreateContainerCmd containerConfig containerConfig.withDns(getDnsHosts().toArray(new String[getDnsHosts().size()])); } + if (CollectionUtils.isNotEmpty(getDockerLabels())) { + Map labels = containerConfig.getLabels(); + if (labels == null) labels = new HashMap<>(); + + for (String s : getDockerLabels()) { + String[] l = s.split("="); + if (l.length > 1) { + labels.put(l[0], l[1]); + } + } + containerConfig.withLabels(labels); + } + // https://github.com/docker/docker/blob/ed257420025772acc38c51b0f018de3ee5564d0f/runconfig/parse.go#L182-L196 if (CollectionUtils.isNotEmpty(getVolumes())) { ArrayList vols = new ArrayList<>(); diff --git a/yet-another-docker-plugin/src/main/resources/com/github/kostyasha/yad/commons/DockerCreateContainer/config.groovy b/yet-another-docker-plugin/src/main/resources/com/github/kostyasha/yad/commons/DockerCreateContainer/config.groovy index 69aebc28..ce17ef4e 100644 --- a/yet-another-docker-plugin/src/main/resources/com/github/kostyasha/yad/commons/DockerCreateContainer/config.groovy +++ b/yet-another-docker-plugin/src/main/resources/com/github/kostyasha/yad/commons/DockerCreateContainer/config.groovy @@ -47,6 +47,10 @@ f.advanced(title: _("Create Container settings"), align: "left") { f.textarea() } + f.entry(title: _("Docker Labels"), field: "dockerLabelsString") { + f.textarea() + } + f.entry(title: _("Port bindings"), field: "bindPorts") { f.textbox() } diff --git a/yet-another-docker-plugin/src/main/resources/com/github/kostyasha/yad/commons/DockerCreateContainer/help-dockerLabelString.html b/yet-another-docker-plugin/src/main/resources/com/github/kostyasha/yad/commons/DockerCreateContainer/help-dockerLabelString.html new file mode 100644 index 00000000..19f7c39c --- /dev/null +++ b/yet-another-docker-plugin/src/main/resources/com/github/kostyasha/yad/commons/DockerCreateContainer/help-dockerLabelString.html @@ -0,0 +1,3 @@ +
+ Docker container labels, key=value. +
diff --git a/yet-another-docker-plugin/src/test/java/com/github/kostyasha/yad/DockerCloudTest.java b/yet-another-docker-plugin/src/test/java/com/github/kostyasha/yad/DockerCloudTest.java index 7af9e929..8021618e 100644 --- a/yet-another-docker-plugin/src/test/java/com/github/kostyasha/yad/DockerCloudTest.java +++ b/yet-another-docker-plugin/src/test/java/com/github/kostyasha/yad/DockerCloudTest.java @@ -115,6 +115,7 @@ public void before() throws Exception { createContainer.setRestartPolicy(new DockerContainerRestartPolicy(NO, 0)); createContainer.setWorkdir("workdir"); createContainer.setUser("user"); + createContainer.setDockerLabels(singletonList("testlabel=testvalue")); final DockerStopContainer stopContainer = new DockerStopContainer(); stopContainer.setTimeout(100);