diff --git a/extensions/container-image/container-image-docker/deployment/src/main/java/io/quarkus/container/image/docker/deployment/DockerProcessor.java b/extensions/container-image/container-image-docker/deployment/src/main/java/io/quarkus/container/image/docker/deployment/DockerProcessor.java index e2afb940d65e5..dba24339b4903 100644 --- a/extensions/container-image/container-image-docker/deployment/src/main/java/io/quarkus/container/image/docker/deployment/DockerProcessor.java +++ b/extensions/container-image/container-image-docker/deployment/src/main/java/io/quarkus/container/image/docker/deployment/DockerProcessor.java @@ -136,7 +136,7 @@ private String createContainerImage(ContainerImageConfig containerImageConfig, D PackageConfig packageConfig) { DockerfilePaths dockerfilePaths = getDockerfilePaths(dockerConfig, forNative, packageConfig, out); - String[] dockerArgs = getDockerArgs(containerImageInfo.getImage(), dockerfilePaths, dockerConfig); + String[] dockerArgs = getDockerArgs(containerImageInfo.getImage(), dockerfilePaths, containerImageConfig, dockerConfig); log.infof("Executing the following command to build docker image: '%s %s'", dockerConfig.executableName, String.join(" ", dockerArgs)); boolean buildSuccessful = ExecUtil.exec(out.getOutputDirectory().toFile(), reader, dockerConfig.executableName, @@ -178,12 +178,16 @@ private String createContainerImage(ContainerImageConfig containerImageConfig, D return containerImageInfo.getImage(); } - private String[] getDockerArgs(String image, DockerfilePaths dockerfilePaths, DockerConfig dockerConfig) { + private String[] getDockerArgs(String image, DockerfilePaths dockerfilePaths, ContainerImageConfig containerImageConfig, + DockerConfig dockerConfig) { List dockerArgs = new ArrayList<>(6 + dockerConfig.buildArgs.size()); dockerArgs.addAll(Arrays.asList("build", "-f", dockerfilePaths.getDockerfilePath().toAbsolutePath().toString())); for (Map.Entry entry : dockerConfig.buildArgs.entrySet()) { dockerArgs.addAll(Arrays.asList("--build-arg", entry.getKey() + "=" + entry.getValue())); } + for (Map.Entry entry : containerImageConfig.labels.entrySet()) { + dockerArgs.addAll(Arrays.asList("--label", String.format("%s=%s", entry.getKey(), entry.getValue()))); + } if (dockerConfig.cacheFrom.isPresent()) { List cacheFrom = dockerConfig.cacheFrom.get(); if (!cacheFrom.isEmpty()) { diff --git a/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibConfig.java b/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibConfig.java index c1d476e7e52d9..f8f74492a5979 100644 --- a/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibConfig.java +++ b/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibConfig.java @@ -85,8 +85,11 @@ public class JibConfig { /** * Custom labels to add to the generated image + * + * @deprecated Use 'quarkus.container-image.labels' instead */ @ConfigItem + @Deprecated public Map labels; /** diff --git a/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibProcessor.java b/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibProcessor.java index 5b6dd13883442..910bc79d776cc 100644 --- a/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibProcessor.java +++ b/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibProcessor.java @@ -119,10 +119,11 @@ public void buildFromJar(ContainerImageConfig containerImageConfig, JibConfig ji JibContainerBuilder jibContainerBuilder; String packageType = packageConfig.type; if (packageConfig.isLegacyJar() || packageType.equalsIgnoreCase(PackageConfig.UBER_JAR)) { - jibContainerBuilder = createContainerBuilderFromLegacyJar(jibConfig, + jibContainerBuilder = createContainerBuilderFromLegacyJar(jibConfig, containerImageConfig, sourceJar, outputTarget, mainClass, containerImageLabels); } else if (packageConfig.isFastJar()) { - jibContainerBuilder = createContainerBuilderFromFastJar(jibConfig, sourceJar, curateOutcome, containerImageLabels, + jibContainerBuilder = createContainerBuilderFromFastJar(jibConfig, containerImageConfig, sourceJar, curateOutcome, + containerImageLabels, appCDSResult); } else { throw new IllegalArgumentException( @@ -161,7 +162,7 @@ public void buildFromNative(ContainerImageConfig containerImageConfig, JibConfig "The native binary produced by the build is not a Linux binary and therefore cannot be used in a Linux container image. Consider adding \"quarkus.native.container-build=true\" to your configuration"); } - JibContainerBuilder jibContainerBuilder = createContainerBuilderFromNative(jibConfig, + JibContainerBuilder jibContainerBuilder = createContainerBuilderFromNative(jibConfig, containerImageConfig, nativeImage, containerImageLabels); setUser(jibConfig, jibContainerBuilder); setPlatforms(jibConfig, jibContainerBuilder); @@ -280,6 +281,7 @@ private Logger.Level toJBossLoggingLevel(LogEvent.Level level) { * */ private JibContainerBuilder createContainerBuilderFromFastJar(JibConfig jibConfig, + ContainerImageConfig containerImageConfig, JarBuildItem sourceJarBuildItem, CurateOutcomeBuildItem curateOutcome, List containerImageLabels, Optional appCDSResult) { @@ -427,7 +429,7 @@ private JibContainerBuilder createContainerBuilderFromFastJar(JibConfig jibConfi .setWorkingDirectory(workDirInContainer) .setEntrypoint(entrypoint) .setEnvironment(getEnvironmentVariables(jibConfig)) - .setLabels(allLabels(jibConfig, containerImageLabels)) + .setLabels(allLabels(jibConfig, containerImageConfig, containerImageLabels)) .setCreationTime(Instant.now()); for (int port : jibConfig.ports) { jibContainerBuilder.addExposedPort(Port.tcp(port)); @@ -468,6 +470,7 @@ private void setPlatforms(JibConfig jibConfig, JibContainerBuilder jibContainerB } private JibContainerBuilder createContainerBuilderFromLegacyJar(JibConfig jibConfig, + ContainerImageConfig containerImageConfig, JarBuildItem sourceJarBuildItem, OutputTargetBuildItem outputTargetBuildItem, MainClassBuildItem mainClassBuildItem, @@ -502,7 +505,7 @@ private JibContainerBuilder createContainerBuilderFromLegacyJar(JibConfig jibCon JibContainerBuilder jibContainerBuilder = javaContainerBuilder.toContainerBuilder() .setEnvironment(getEnvironmentVariables(jibConfig)) - .setLabels(allLabels(jibConfig, containerImageLabels)) + .setLabels(allLabels(jibConfig, containerImageConfig, containerImageLabels)) .setCreationTime(Instant.now()); if (jibConfig.jvmEntrypoint.isPresent()) { @@ -517,7 +520,7 @@ private JibContainerBuilder createContainerBuilderFromLegacyJar(JibConfig jibCon } } - private JibContainerBuilder createContainerBuilderFromNative(JibConfig jibConfig, + private JibContainerBuilder createContainerBuilderFromNative(JibConfig jibConfig, ContainerImageConfig containerImageConfig, NativeImageBuildItem nativeImageBuildItem, List containerImageLabels) { List entrypoint; @@ -541,7 +544,7 @@ private JibContainerBuilder createContainerBuilderFromNative(JibConfig jibConfig .setWorkingDirectory(workDirInContainer) .setEntrypoint(entrypoint) .setEnvironment(getEnvironmentVariables(jibConfig)) - .setLabels(allLabels(jibConfig, containerImageLabels)) + .setLabels(allLabels(jibConfig, containerImageConfig, containerImageLabels)) .setCreationTime(Instant.now()); for (int port : jibConfig.ports) { jibContainerBuilder.addExposedPort(Port.tcp(port)); @@ -604,12 +607,14 @@ private void handleExtraFiles(OutputTargetBuildItem outputTarget, JibContainerBu } } - private Map allLabels(JibConfig jibConfig, List containerImageLabels) { + private Map allLabels(JibConfig jibConfig, ContainerImageConfig containerImageConfig, + List containerImageLabels) { if (jibConfig.labels.isEmpty() && containerImageLabels.isEmpty()) { return Collections.emptyMap(); } final Map allLabels = new HashMap<>(jibConfig.labels); + allLabels.putAll(containerImageConfig.labels); for (ContainerImageLabelBuildItem containerImageLabel : containerImageLabels) { // we want the user supplied labels to take precedence so the user can override labels generated from other extensions if desired allLabels.putIfAbsent(containerImageLabel.getName(), containerImageLabel.getValue()); diff --git a/extensions/container-image/deployment/src/main/java/io/quarkus/container/image/deployment/ContainerImageConfig.java b/extensions/container-image/deployment/src/main/java/io/quarkus/container/image/deployment/ContainerImageConfig.java index 87547b9f92a7b..457555aad35f1 100644 --- a/extensions/container-image/deployment/src/main/java/io/quarkus/container/image/deployment/ContainerImageConfig.java +++ b/extensions/container-image/deployment/src/main/java/io/quarkus/container/image/deployment/ContainerImageConfig.java @@ -1,6 +1,7 @@ package io.quarkus.container.image.deployment; import java.util.List; +import java.util.Map; import java.util.Optional; import io.quarkus.runtime.annotations.ConfigItem; @@ -33,6 +34,12 @@ public class ContainerImageConfig { @ConfigItem public Optional> additionalTags; + /** + * Custom labels to add to the generated image. + */ + @ConfigItem + public Map labels; + /** * The container registry to use */