Skip to content

Commit

Permalink
Add docker label option (KostyaSha#234)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
denami authored and KostyaSha committed Jun 19, 2018
1 parent 4a70367 commit 55e337e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -139,6 +141,9 @@ public class DockerCreateContainer extends AbstractDescribableImpl<DockerCreateC
@CheckForNull
private List<String> links;

@CheckForNull
private List<String> dockerLabels;

@CheckForNull
private Long shmSize;

Expand Down Expand Up @@ -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<String> dockerLabels) {
this.dockerLabels = dockerLabels;
}

@Nonnull
public List<String> getDockerLabels() {
return isNull(dockerLabels) ? Collections.EMPTY_LIST : dockerLabels;
}

// extrahosts
@CheckForNull
public List<String> getExtraHosts() {
Expand Down Expand Up @@ -542,6 +566,19 @@ public CreateContainerCmd fillContainerConfig(CreateContainerCmd containerConfig
containerConfig.withDns(getDnsHosts().toArray(new String[getDnsHosts().size()]));
}

if (CollectionUtils.isNotEmpty(getDockerLabels())) {
Map<String, String> 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<Volume> vols = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
Docker container labels, key=value.
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 55e337e

Please sign in to comment.