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

JENKINS-74995 Add rootless support #325

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -31,6 +31,7 @@
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.Node;
import hudson.model.TaskListener;
import hudson.util.ArgumentListBuilder;
import hudson.util.VersionNumber;
import java.io.BufferedReader;
Expand Down Expand Up @@ -318,6 +319,12 @@
return result;
}

private String executeCommand(String... command) throws IOException, InterruptedException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
launcher.launch().cmds(command).quiet(true).stdout(output).start().joinWithTimeout(CLIENT_TIMEOUT, TimeUnit.SECONDS, launcher.getListener());
return output.toString(Charset.defaultCharset()).trim();
}

/**
* Who is executing this {@link DockerClient} instance.
*
Expand All @@ -328,15 +335,28 @@
// Windows does not support username
return "";
}
ByteArrayOutputStream userId = new ByteArrayOutputStream();
launcher.launch().cmds("id", "-u").quiet(true).stdout(userId).start().joinWithTimeout(CLIENT_TIMEOUT, TimeUnit.SECONDS, launcher.getListener());

ByteArrayOutputStream groupId = new ByteArrayOutputStream();
launcher.launch().cmds("id", "-g").quiet(true).stdout(groupId).start().joinWithTimeout(CLIENT_TIMEOUT, TimeUnit.SECONDS, launcher.getListener());
TaskListener listener = launcher.getListener();
final String rootlessId = "0:0";
// First, check if under the hood it's Podman or Docker
String engine = executeCommand("docker", "--version");
if (engine.toLowerCase().contains("podman")) {

Check warning on line 343 in src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 343 is only partially covered, one branch is missing
listener.getLogger().println("Container engine is Podman with build in rootless mode");
return rootlessId;

Check warning on line 345 in src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 344-345 are not covered by tests
}
else {
String rootless = executeCommand("docker", "info", "-f", "{{.SecurityOptions}}" );
if (rootless.toLowerCase().contains("rootless")) {

Check warning on line 349 in src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 349 is only partially covered, one branch is missing
listener.getLogger().println("Container engine is Docker with rootless mode");
return rootlessId;

Check warning on line 351 in src/main/java/org/jenkinsci/plugins/docker/workflow/client/DockerClient.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 350-351 are not covered by tests
}
}

final String charsetName = Charset.defaultCharset().name();
return String.format("%s:%s", userId.toString(charsetName).trim(), groupId.toString(charsetName).trim());
// Else not rootless, return the current user/group ids
String userId = executeCommand("id", "-u");
String groupId = executeCommand("id", "-g");

return String.format("%s:%s", userId, groupId);
}

private static final Pattern hostnameMount = Pattern.compile("/containers/([a-z0-9]{64})/hostname");
Expand Down
Loading