Skip to content

Commit

Permalink
Add docker run --dns-search flag to agent template (#1105)
Browse files Browse the repository at this point in the history
* Add dns-search

* Add test
  • Loading branch information
kirk-fitz authored Oct 12, 2024
1 parent d5453c5 commit 6d223a3
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/attachments/docker-plugin-configuration-script.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def templateBaseParameters = [
// cpuShares: (Integer)null,
// devicesString: '',
// dnsString: '',
// dnsSearchString: '',
// dockerCommand: '',
// environmentsString: '',
// extraDockerLabelsString: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public DockerSimpleTemplate(
String image,
String pullCredentialsId,
String dnsString,
String dnsSearchString,
String network,
String dockerCommand,
String mountsString,
Expand All @@ -35,6 +36,7 @@ public DockerSimpleTemplate(
image,
pullCredentialsId,
dnsString,
dnsSearchString,
network,
dockerCommand,
mountsString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ public String getDnsString() {
return dockerTemplateBase.getDnsString();
}

public String getDnsSearchString() {
return dockerTemplateBase.getDnsSearchString();

Check warning on line 165 in src/main/java/com/nirima/jenkins/plugins/docker/DockerTemplate.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 165 is not covered by tests
}

@CheckForNull
public String[] getMounts() {
return dockerTemplateBase.getMounts();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ public class DockerTemplateBase implements Describable<DockerTemplateBase>, Seri

public @CheckForNull String[] dnsHosts;

public @CheckForNull String[] dnsSearch;

public @CheckForNull String network;

/**
Expand Down Expand Up @@ -158,6 +160,7 @@ public DockerTemplateBase(String image) {
* @param image See {@link #DockerTemplateBase(String)}
* @param pullCredentialsId See {@link #setPullCredentialsId(String)}
* @param dnsString See {@link #setDnsString(String)}
* @param dnsSearchString See {@link #setDnsSearchString(String)}
* @param network See {@link #setNetwork(String)}
* @param dockerCommand See {@link #setDockerCommand(String)}
* @param mountsString See {@link #setMountsString(String)}
Expand All @@ -184,6 +187,7 @@ public DockerTemplateBase(
String image,
String pullCredentialsId,
String dnsString,
String dnsSearchString,
String network,
String dockerCommand,
String mountsString,
Expand All @@ -207,6 +211,7 @@ public DockerTemplateBase(
this(image);
setPullCredentialsId(pullCredentialsId);
setDnsString(dnsString);
setDnsSearchString(dnsSearchString);
setNetwork(network);
setDockerCommand(dockerCommand);
setMountsString(mountsString);
Expand Down Expand Up @@ -335,6 +340,28 @@ public void setDnsString(String dnsString) {
setDnsHosts(splitAndFilterEmpty(dnsString, " "));
}

@CheckForNull
public String[] getDnsSearch() {
return fixEmpty(dnsSearch);
}

@NonNull
public String getDnsSearchString() {
if (dnsSearch == null) {

Check warning on line 350 in src/main/java/com/nirima/jenkins/plugins/docker/DockerTemplateBase.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 350 is only partially covered, one branch is missing
return "";
}
return Joiner.on(" ").join(dnsSearch);

Check warning on line 353 in src/main/java/com/nirima/jenkins/plugins/docker/DockerTemplateBase.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 353 is not covered by tests
}

public void setDnsSearch(String[] dnsSearch) {
this.dnsSearch = fixEmpty(dnsSearch);
}

@DataBoundSetter
public void setDnsSearchString(String dnsSearchString) {
setDnsSearch(splitAndFilterEmpty(dnsSearchString, " "));
}

@CheckForNull
public String getNetwork() {
return Util.fixEmpty(network);
Expand Down Expand Up @@ -838,6 +865,11 @@ public CreateContainerCmd fillContainerConfig(CreateContainerCmd containerConfig
hostConfig(containerConfig).withDns(dnsHostsOrNull);
}

final String[] dnsSearchOrNull = getDnsSearch();
if (dnsSearchOrNull != null && dnsSearchOrNull.length > 0) {

Check warning on line 869 in src/main/java/com/nirima/jenkins/plugins/docker/DockerTemplateBase.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 869 is only partially covered, 3 branches are missing
hostConfig(containerConfig).withDnsSearch(dnsSearchOrNull);
}

final String networkOrNull = getNetwork();
if (networkOrNull != null && networkOrNull.length() > 0) {
containerConfig.withNetworkDisabled(false);
Expand Down Expand Up @@ -1141,6 +1173,9 @@ public boolean equals(Object o) {
if (!Arrays.equals(dnsHosts, that.dnsHosts)) {
return false;
}
if (!Arrays.equals(dnsSearch, that.dnsSearch)) {
return false;

Check warning on line 1177 in src/main/java/com/nirima/jenkins/plugins/docker/DockerTemplateBase.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 870-1177 are not covered by tests
}
if (!Objects.equals(network, that.network)) {
return false;
}
Expand Down Expand Up @@ -1216,6 +1251,7 @@ public int hashCode() {
result = 31 * result + (user != null ? user.hashCode() : 0);
result = 31 * result + (extraGroups != null ? extraGroups.hashCode() : 0);
result = 31 * result + Arrays.hashCode(dnsHosts);
result = 31 * result + Arrays.hashCode(dnsSearch);
result = 31 * result + (network != null ? network.hashCode() : 0);
result = 31 * result + Arrays.hashCode(mounts);
result = 31 * result + Arrays.hashCode(volumesFrom2);
Expand Down Expand Up @@ -1255,6 +1291,7 @@ public String toString() {
bldToString(sb, "user", user);
bldToString(sb, "extraGroups", extraGroups);
bldToString(sb, "dnsHosts", dnsHosts);
bldToString(sb, "dnsSearch", dnsSearch);

Check warning on line 1294 in src/main/java/com/nirima/jenkins/plugins/docker/DockerTemplateBase.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 1294 is not covered by tests
bldToString(sb, "network'", network);
bldToString(sb, "mounts", mounts);
bldToString(sb, "volumesFrom2", volumesFrom2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class DockerBuilderControlOptionRun extends DockerBuilderControlCloudOpti
private String pullCredentialsId;
private transient DockerRegistryEndpoint registry;
public final String dnsString;
public final String dnsSearchString;
public final String network;
public final String dockerCommand;
public final String mountsString;
Expand Down Expand Up @@ -66,6 +67,7 @@ public DockerBuilderControlOptionRun(
String image,
String pullCredentialsId,
String dnsString,
String dnsSearchString,
String network,
String dockerCommand,
String mountsString,
Expand All @@ -91,6 +93,7 @@ public DockerBuilderControlOptionRun(
this.image = image;
this.pullCredentialsId = pullCredentialsId;
this.dnsString = dnsString;
this.dnsSearchString = dnsSearchString;

Check warning on line 96 in src/main/java/com/nirima/jenkins/plugins/docker/builder/DockerBuilderControlOptionRun.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 96 is not covered by tests
this.network = network;
this.dockerCommand = dockerCommand;
this.mountsString = mountsString;
Expand Down Expand Up @@ -188,6 +191,7 @@ private void executeOnDocker(
xImage,
pullCredentialsId,
dnsString,
dnsSearchString,
network,
xCommand,
mountsString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
<f:textbox />
</f:entry>

<f:entry title="${%DNS Search}" field="dnsSearchString">
<f:textbox />
</f:entry>

<f:entry title="${%Network}" field="network">
<f:textbox />
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
Set the DNS search domains to use within your images, if not set Docker will use DNS settings of the host
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public void globalConfigRoundtrip() throws Exception {
"image",
"pullCredentialsId",
"dnsString",
"dnsSearchString",
"network",
"dockerCommand",
"mountsString",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class DockerTemplateTest {
String suffixStartAgentCmd = " suffixStartAgentCmd";
String instanceCapStr = "";
String network = "";
String dnsSearchString = "docker.com";

String dockerCommand = "dockerCommand";
String mountsString = "mounts";
Expand All @@ -50,6 +51,7 @@ private DockerTemplate getDockerTemplateInstanceWithDNSHost(String dnsString) {
image,
null,
dnsString,
dnsSearchString,
network,
dockerCommand,
mountsString,
Expand Down Expand Up @@ -98,6 +100,16 @@ public void testDnsHosts() {
assertArrayEquals(expected, instance.getDockerTemplateBase().dnsHosts);
}

@Test
public void testDnsSearch() {
DockerTemplate instance;
String[] expected;

instance = getDockerTemplateInstanceWithDNSHost("");
expected = new String[] {"docker.com"};
assertArrayEquals(expected, instance.getDockerTemplateBase().dnsSearch);
}

@Test
public void testLimits() {
DockerTemplate instance;
Expand Down

0 comments on commit 6d223a3

Please sign in to comment.