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

Remove unnecessary ImageID resolution in DockerProcessor #34361

Merged
merged 2 commits into from
Jun 29, 2023
Merged
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 @@ -17,11 +17,11 @@ public class ExecUtil {

private static final Logger LOG = Logger.getLogger(ExecUtil.class);

private static final Function<InputStream, Runnable> INFO_LOGGING = i -> new HandleOutput(i);
private static final Function<InputStream, Runnable> DEBUG_LOGGING = i -> new HandleOutput(i, Logger.Level.DEBUG);
private static final Function<InputStream, Runnable> SYSTEM_LOGGING = i -> new HandleOutput(i);
public static final Function<InputStream, Runnable> INFO_LOGGING = i -> new HandleOutput(i, Logger.Level.INFO);
public static final Function<InputStream, Runnable> DEBUG_LOGGING = i -> new HandleOutput(i, Logger.Level.DEBUG);
public static final Function<InputStream, Runnable> SYSTEM_LOGGING = HandleOutput::new;

private static Function<InputStream, Runnable> SELECTED_LOGGING = INFO_LOGGING;
private static final Function<InputStream, Runnable> DEFAULT_LOGGING = INFO_LOGGING;

private static final int PROCESS_CHECK_INTERVAL = 500;

Expand Down Expand Up @@ -52,23 +52,6 @@ public void run() {
}
}

public static void useInfoLogging() {
ExecUtil.SELECTED_LOGGING = INFO_LOGGING;
}

public static void useDebugLogging() {
ExecUtil.SELECTED_LOGGING = DEBUG_LOGGING;
}

/**
* There are cases where its preferable to just write to System.out.
* For example from maven-invoker verify scripts, logging can trigger Stack Overflow.
* For such cases its preferable to use this method.
*/
public static void useSystemLogging() {
ExecUtil.SELECTED_LOGGING = SYSTEM_LOGGING;
}

/**
* Execute the specified command from within the current directory.
*
Expand Down Expand Up @@ -101,7 +84,7 @@ public static boolean execWithTimeout(Duration timeout, String command, String..
* @return true if commands where executed successfully
*/
public static boolean exec(File directory, String command, String... args) {
return exec(directory, SELECTED_LOGGING, command, args);
return exec(directory, DEFAULT_LOGGING, command, args);
}

/**
Expand All @@ -114,7 +97,7 @@ public static boolean exec(File directory, String command, String... args) {
* @return true if commands where executed successfully
*/
public static boolean execWithTimeout(File directory, Duration timeout, String command, String... args) {
return execWithTimeout(directory, SELECTED_LOGGING, timeout, command, args);
return execWithTimeout(directory, DEFAULT_LOGGING, timeout, command, args);
}

/**
Expand All @@ -131,7 +114,7 @@ public static boolean exec(File directory, Function<InputStream, Runnable> outpu
String... args) {
try {
Function<InputStream, Runnable> loggingFunction = outputFilterFunction != null ? outputFilterFunction
: INFO_LOGGING;
: DEFAULT_LOGGING;
Process process = startProcess(directory, command, args);
Thread t = new Thread(loggingFunction.apply(process.getInputStream()));
t.setName("Process stdout");
Expand Down Expand Up @@ -160,7 +143,7 @@ public static boolean execWithTimeout(File directory, Function<InputStream, Runn
Duration timeout, String command, String... args) {
try {
Function<InputStream, Runnable> loggingFunction = outputFilterFunction != null ? outputFilterFunction
: INFO_LOGGING;
: DEFAULT_LOGGING;
Process process = startProcess(directory, command, args);
Thread t = new Thread(loggingFunction.apply(process.getInputStream()));
t.setName("Process stdout");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
import static io.quarkus.container.util.PathsUtil.findMainSourcesRoot;
import static io.quarkus.runtime.util.ContainerRuntimeUtil.detectContainerRuntime;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand All @@ -19,8 +15,6 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.stream.Stream;

import org.jboss.logging.Logger;
Expand Down Expand Up @@ -106,8 +100,7 @@ public void dockerBuildFromJar(DockerConfig dockerConfig,
log.info("Starting (local) container image build for jar using docker.");
}

ImageIdReader reader = new ImageIdReader();
String builtContainerImage = createContainerImage(containerImageConfig, dockerConfig, containerImageInfo, out, reader,
String builtContainerImage = createContainerImage(containerImageConfig, dockerConfig, containerImageInfo, out,
false,
buildContainerImage,
pushContainerImage, packageConfig);
Expand Down Expand Up @@ -151,8 +144,7 @@ public void dockerBuildFromNativeImage(DockerConfig dockerConfig,

log.info("Starting (local) container image build for native binary using docker.");

ImageIdReader reader = new ImageIdReader();
String builtContainerImage = createContainerImage(containerImageConfig, dockerConfig, containerImage, out, reader, true,
String builtContainerImage = createContainerImage(containerImageConfig, dockerConfig, containerImage, out, true,
buildContainerImage,
pushContainerImage, packageConfig);

Expand All @@ -166,7 +158,7 @@ public void dockerBuildFromNativeImage(DockerConfig dockerConfig,

private String createContainerImage(ContainerImageConfig containerImageConfig, DockerConfig dockerConfig,
ContainerImageInfoBuildItem containerImageInfo,
OutputTargetBuildItem out, ImageIdReader reader, boolean forNative, boolean buildContainerImage,
OutputTargetBuildItem out, boolean forNative, boolean buildContainerImage,
boolean pushContainerImage,
PackageConfig packageConfig) {

Expand Down Expand Up @@ -201,8 +193,7 @@ private String createContainerImage(ContainerImageConfig containerImageConfig, D
final String executableName = dockerConfig.executableName.orElse(detectContainerRuntime(true).getExecutableName());
log.infof("Executing the following command to build docker image: '%s %s'", executableName,
String.join(" ", dockerArgs));
boolean buildSuccessful = ExecUtil.exec(out.getOutputDirectory().toFile(), reader, executableName,
dockerArgs);
boolean buildSuccessful = ExecUtil.exec(out.getOutputDirectory().toFile(), executableName, dockerArgs);
if (!buildSuccessful) {
throw dockerException(executableName, dockerArgs);
}
Expand All @@ -212,8 +203,7 @@ private String createContainerImage(ContainerImageConfig containerImageConfig, D
.ifPresentOrElse(
platform -> log.infof("Built container image %s (%s platform(s))\n", containerImageInfo.getImage(),
String.join(",", platform)),
() -> log.infof("Built container image %s (%s)\n", containerImageInfo.getImage(),
reader.getImageId()));
() -> log.infof("Built container image %s\n", containerImageInfo.getImage()));

}

Expand Down Expand Up @@ -371,41 +361,6 @@ private DockerfilePaths getDockerfilePaths(DockerConfig dockerConfig, boolean fo
}
}

/**
* A function that creates a command output reader, that reads and holds the image id from the docker build output.
*/
private static class ImageIdReader implements Function<InputStream, Runnable> {

private final AtomicReference<String> id = new AtomicReference<>();

public String getImageId() {
return id.get();
}

@Override
public Runnable apply(InputStream t) {
return new Runnable() {
@Override
public void run() {
try (InputStreamReader isr = new InputStreamReader(t);
BufferedReader reader = new BufferedReader(isr)) {

for (String line = reader.readLine(); line != null; line = reader.readLine()) {
if (line.startsWith("Successfully built")) {
String[] parts = line.split(" ");
if (parts.length == 3)
id.set(parts[2]);
}
log.info(line);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
};
}
}

private interface DockerfilePaths {
Path getDockerfilePath();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import io.quarkus.deployment.util.ExecUtil

ExecUtil.useSystemLogging() //prevents stack overflow issues

try {
ExecUtil.exec("docker", "version", "--format", "'{{.Server.Version}}'")
ExecUtil.execWithSystemLogging("docker", "version", "--format", "'{{.Server.Version}}'")
} catch (Exception ignored) {
println "Docker not found"
return
}

String group = System.getProperty("user.name")
assert ExecUtil.exec("docker", "images", group + "/container-build-docker")
assert ExecUtil.exec("docker", "rmi", group + "/container-build-docker:0.1-SNAPSHOT")
assert ExecUtil.execWithSystemLogging("docker", "images", group + "/container-build-docker")
assert ExecUtil.execWithSystemLogging("docker", "rmi", group + "/container-build-docker:0.1-SNAPSHOT")
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ import io.quarkus.deployment.util.ExecUtil

import java.util.concurrent.ThreadLocalRandom

ExecUtil.useSystemLogging() //prevents stack overflow issues
try {
ExecUtil.exec("docker", "version", "--format", "'{{.Server.Version}}'")
ExecUtil.execWithSystemLogging("docker", "version", "--format", "'{{.Server.Version}}'")
} catch (Exception ignored) {
return
}

String image = "${System.getProperty("user.name")}/container-build-jib-appcds:0.1-SNAPSHOT"
assert ExecUtil.exec("docker", "images", image)
assert ExecUtil.execWithSystemLogging("docker", "images", image)

String containerName = "container-build-jib-appcds-" + ThreadLocalRandom.current().nextInt(10000)
int maxTimesToCheck = 10
int i = 0
int hostPort = 12345
assert ExecUtil.exec("docker", "run", "-d", "-p", "$hostPort:8080", "--name", containerName, image)
assert ExecUtil.execWithSystemLogging("docker", "run", "-d", "-p", "$hostPort:8080", "--name", containerName, image)

while (true) {
try {
Expand All @@ -33,6 +32,6 @@ while (true) {
}
}
}
assert ExecUtil.exec("docker", "stop", containerName)
assert ExecUtil.exec("docker", "rm", containerName)
assert ExecUtil.exec("docker", "rmi", image)
assert ExecUtil.execWithSystemLogging("docker", "stop", containerName)
assert ExecUtil.execWithSystemLogging("docker", "rm", containerName)
assert ExecUtil.execWithSystemLogging("docker", "rmi", image)
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@ import io.quarkus.deployment.util.ExecUtil

import java.util.concurrent.ThreadLocalRandom

ExecUtil.useSystemLogging() //prevents stack overflow issues

try {
ExecUtil.exec("docker", "version", "--format", "'{{.Server.Version}}'")
ExecUtil.execWithSystemLogging("docker", "version", "--format", "'{{.Server.Version}}'")
} catch (Exception ignored) {
return
}

String image = "${System.getProperty("user.name")}/container-build-jib-inherit:0.1-SNAPSHOT"
assert ExecUtil.exec("docker", "images", image)
assert ExecUtil.execWithSystemLogging("docker", "images", image)

String containerName = "container-build-jib-inherit-" + ThreadLocalRandom.current().nextInt(10000)
int maxTimesToCheck = 10
int i = 0
int hostPort = 12345
assert ExecUtil.exec("docker", "run", "-d", "-p", "$hostPort:8080", "--name", containerName, image)
assert ExecUtil.execWithSystemLogging("docker", "run", "-d", "-p", "$hostPort:8080", "--name", containerName, image)

while (true) {
try {
Expand All @@ -34,6 +33,6 @@ while (true) {
}
}
}
assert ExecUtil.exec("docker", "stop", containerName)
assert ExecUtil.exec("docker", "rm", containerName)
assert ExecUtil.exec("docker", "rmi", image)
assert ExecUtil.execWithSystemLogging("docker", "stop", containerName)
assert ExecUtil.execWithSystemLogging("docker", "rm", containerName)
assert ExecUtil.execWithSystemLogging("docker", "rmi", image)
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths

ExecUtil.useSystemLogging() //prevents stack overflow issues
try {
ExecUtil.exec("docker", "version", "--format", "'{{.Server.Version}}'")
ExecUtil.execWithSystemLogging("docker", "version", "--format", "'{{.Server.Version}}'")
} catch (Exception ignored) {
println "Docker not found"
return
}

assert ExecUtil.exec("docker", "images", "container-build-jib")
assert ExecUtil.exec("docker", "rmi", "container-build-jib:0.1-SNAPSHOT")
assert ExecUtil.execWithSystemLogging("docker", "images", "container-build-jib")
assert ExecUtil.execWithSystemLogging("docker", "rmi", "container-build-jib:0.1-SNAPSHOT")


Path pathInIT = Paths.get("target", "it", "container-build-jib", "target")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import io.quarkus.deployment.util.ExecUtil

ExecUtil.useSystemLogging() //prevents stack overflow issues
try {
ExecUtil.exec("docker", "version", "--format", "'{{.Server.Version}}'")
ExecUtil.execWithSystemLogging("docker", "version", "--format", "'{{.Server.Version}}'")
} catch (Exception ignored) {
println "Docker not found"
return
}

String group = System.getProperty("user.name")
assert ExecUtil.exec("docker", "images", group + "/container-build-multiple-tags-docker")
assert ExecUtil.exec("docker", "rmi", group + "/container-build-multiple-tags-docker:foo")
assert ExecUtil.exec("docker", "rmi", group + "/container-build-multiple-tags-docker:bar")
assert ExecUtil.exec("docker", "rmi", group + "/container-build-multiple-tags-docker:baz")
assert ExecUtil.execWithSystemLogging("docker", "images", group + "/container-build-multiple-tags-docker")
assert ExecUtil.execWithSystemLogging("docker", "rmi", group + "/container-build-multiple-tags-docker:foo")
assert ExecUtil.execWithSystemLogging("docker", "rmi", group + "/container-build-multiple-tags-docker:bar")
assert ExecUtil.execWithSystemLogging("docker", "rmi", group + "/container-build-multiple-tags-docker:baz")
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import io.quarkus.deployment.util.ExecUtil

ExecUtil.useSystemLogging() //prevents stack overflow issues
try {
ExecUtil.exec("docker", "version", "--format", "'{{.Server.Version}}'")
ExecUtil.execWithSystemLogging("docker", "version", "--format", "'{{.Server.Version}}'")
} catch (Exception ignored) {
println "Docker not found"
return
}

String group = System.getProperty("user.name")
assert ExecUtil.exec("docker", "images", group + "/container-build-multiple-tags-jib")
assert ExecUtil.exec("docker", "rmi", group + "/container-build-multiple-tags-jib:foo")
assert ExecUtil.exec("docker", "rmi", group + "/container-build-multiple-tags-jib:bar")
assert ExecUtil.exec("docker", "rmi", group + "/container-build-multiple-tags-jib:baz")
assert ExecUtil.execWithSystemLogging("docker", "images", group + "/container-build-multiple-tags-jib")
assert ExecUtil.execWithSystemLogging("docker", "rmi", group + "/container-build-multiple-tags-jib:foo")
assert ExecUtil.execWithSystemLogging("docker", "rmi", group + "/container-build-multiple-tags-jib:bar")
assert ExecUtil.execWithSystemLogging("docker", "rmi", group + "/container-build-multiple-tags-jib:baz")
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import io.quarkus.deployment.util.ExecUtil

ExecUtil.useSystemLogging() //prevents stack overflow issues
try {
ExecUtil.exec("docker", "version", "--format", "'{{.Server.Version}}'")
ExecUtil.execWithSystemLogging("docker", "version", "--format", "'{{.Server.Version}}'")
} catch (Exception ignored) {
println "Docker not found"
return
}

assert ExecUtil.exec("docker", "run", "--rm", "-p", "5000:5000", "-d", "--name", "registry" ,"registry:2");
assert ExecUtil.execWithSystemLogging("docker", "run", "--rm", "-p", "5000:5000", "-d", "--name", "registry" ,"registry:2");
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import io.quarkus.deployment.util.ExecUtil
import static io.restassured.RestAssured.get
import static org.hamcrest.Matchers.containsString

ExecUtil.useSystemLogging() //prevents stack overflow issues
try {
ExecUtil.exec("docker", "version", "--format", "'{{.Server.Version}}'")
ExecUtil.execWithSystemLogging("docker", "version", "--format", "'{{.Server.Version}}'")
} catch (Exception ignored) {
println "Docker not found"
return
Expand All @@ -15,4 +14,4 @@ get("http://localhost:5000/v2/_catalog")
.then()
.body(containsString("container-image-push"))

assert ExecUtil.exec("docker", "stop", "registry")
assert ExecUtil.execWithSystemLogging("docker", "stop", "registry")