Skip to content

Commit

Permalink
Make labels a general container-image setting instead of Jib specific
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Sep 6, 2021
1 parent 06f9f63 commit 056706e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<String> dockerArgs = new ArrayList<>(6 + dockerConfig.buildArgs.size());
dockerArgs.addAll(Arrays.asList("build", "-f", dockerfilePaths.getDockerfilePath().toAbsolutePath().toString()));
for (Map.Entry<String, String> entry : dockerConfig.buildArgs.entrySet()) {
dockerArgs.addAll(Arrays.asList("--build-arg", entry.getKey() + "=" + entry.getValue()));
}
for (Map.Entry<String, String> entry : containerImageConfig.labels.entrySet()) {
dockerArgs.addAll(Arrays.asList("--label", String.format("%s=%s", entry.getKey(), entry.getValue())));
}
if (dockerConfig.cacheFrom.isPresent()) {
List<String> cacheFrom = dockerConfig.cacheFrom.get();
if (!cacheFrom.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> labels;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -280,6 +281,7 @@ private Logger.Level toJBossLoggingLevel(LogEvent.Level level) {
* </ul>
*/
private JibContainerBuilder createContainerBuilderFromFastJar(JibConfig jibConfig,
ContainerImageConfig containerImageConfig,
JarBuildItem sourceJarBuildItem,
CurateOutcomeBuildItem curateOutcome, List<ContainerImageLabelBuildItem> containerImageLabels,
Optional<AppCDSResultBuildItem> appCDSResult) {
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -468,6 +470,7 @@ private void setPlatforms(JibConfig jibConfig, JibContainerBuilder jibContainerB
}

private JibContainerBuilder createContainerBuilderFromLegacyJar(JibConfig jibConfig,
ContainerImageConfig containerImageConfig,
JarBuildItem sourceJarBuildItem,
OutputTargetBuildItem outputTargetBuildItem,
MainClassBuildItem mainClassBuildItem,
Expand Down Expand Up @@ -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()) {
Expand All @@ -517,7 +520,7 @@ private JibContainerBuilder createContainerBuilderFromLegacyJar(JibConfig jibCon
}
}

private JibContainerBuilder createContainerBuilderFromNative(JibConfig jibConfig,
private JibContainerBuilder createContainerBuilderFromNative(JibConfig jibConfig, ContainerImageConfig containerImageConfig,
NativeImageBuildItem nativeImageBuildItem, List<ContainerImageLabelBuildItem> containerImageLabels) {

List<String> entrypoint;
Expand All @@ -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));
Expand Down Expand Up @@ -604,12 +607,14 @@ private void handleExtraFiles(OutputTargetBuildItem outputTarget, JibContainerBu
}
}

private Map<String, String> allLabels(JibConfig jibConfig, List<ContainerImageLabelBuildItem> containerImageLabels) {
private Map<String, String> allLabels(JibConfig jibConfig, ContainerImageConfig containerImageConfig,
List<ContainerImageLabelBuildItem> containerImageLabels) {
if (jibConfig.labels.isEmpty() && containerImageLabels.isEmpty()) {
return Collections.emptyMap();
}

final Map<String, String> 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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -33,6 +34,12 @@ public class ContainerImageConfig {
@ConfigItem
public Optional<List<String>> additionalTags;

/**
* Custom labels to add to the generated image.
*/
@ConfigItem
public Map<String, String> labels;

/**
* The container registry to use
*/
Expand Down

0 comments on commit 056706e

Please sign in to comment.