diff --git a/docs/modules/ROOT/pages/goals/build.adoc b/docs/modules/ROOT/pages/goals/build.adoc index 64b23a9c..0ae0c712 100644 --- a/docs/modules/ROOT/pages/goals/build.adoc +++ b/docs/modules/ROOT/pages/goals/build.adoc @@ -54,24 +54,15 @@ Note: You can also override the default value of layers by setting the BUILDAH_L **Default value is**: `false` |pull -|When the option is specified or set to “true”, pull the image. Raise an error if the image could not be pulled, even if the image is present locally. +|When the option is specified or set, pull the image. Raise an error if the image could not be pulled, even if the image is present locally. -If the option is disabled (with `false`) or not specified, pull the image from the registry only if the image is not present locally. Raise an error if the image is not found in the registries and is not present locally. - -When this option is specified _and_ `pullAlways` is also specified, builds will fail as Podman does not support both options to be enabled at the same time. +If the option is disabled (with `never`) or not specified, pull the image from the registry only if the image is not present locally. Raise an error if the image is not found in the registries and is not present locally. **Default value is**: `null` (not specified). -**See**: https://docs.podman.io/en/latest/markdown/podman-build.1.html - -|pullAlways -|Pull the image from the first registry it is found in as listed in registries.conf. Raise an error if not found in the registries, even if the image is present locally. - -When this option is specified _and_ `pullAlways` is also specified, builds will fail as Podman does not support both options to be enabled at the same time. +**Supported values are:** ALWAYS, TRUE, MISSING, NEVER, FALSE, NEWER -**Default value is**: `null` (not specified). - -**See**: https://docs.podman.io/en/latest/markdown/podman-build.1.html +**See**: https://docs.podman.io/en/latest/markdown/podman-build.1.html#pull-policy |squash |Squash all of the image’s new layers into a single new layer; any preexisting layers are not squashed. @@ -152,8 +143,7 @@ Supported values are: your-image-name - true - false + always sampleTagValue diff --git a/src/main/java/nl/lexemmens/podman/command/podman/PodmanBuildCommand.java b/src/main/java/nl/lexemmens/podman/command/podman/PodmanBuildCommand.java index 8811e431..df584b3d 100644 --- a/src/main/java/nl/lexemmens/podman/command/podman/PodmanBuildCommand.java +++ b/src/main/java/nl/lexemmens/podman/command/podman/PodmanBuildCommand.java @@ -23,8 +23,7 @@ public class PodmanBuildCommand extends AbstractPodmanCommand { private static final String LAYERS_CMD = "--layers"; private static final String BUILD_FORMAT_CMD = "--format"; private static final String CONTAINERFILE_CMD = "--file"; - private static final String PULL_CMD = "--pull"; - private static final String PULL_ALWAYS_CMD = "--pull-always"; + private static final String PULL_POLICY_CMD = "--pull"; private static final String NO_CACHE_CMD = "--no-cache"; private static final String BUILD_ARG_CMD = "--build-arg"; private static final String PLATFORM_CMD = "--platform"; @@ -109,11 +108,11 @@ public Builder setContainerFile(Path containerFile) { /** * Sets whether the base image should be pulled * - * @param pull Sets whether to pull the image + * @param pullPolicy Sets whether to pull the image * @return This builder instance */ - public Builder setPull(Boolean pull) { - command.withOption(PULL_CMD, pull.toString()); + public Builder setPullPolicy(String pullPolicy) { + command.withOption(PULL_POLICY_CMD, pullPolicy); return this; } @@ -128,17 +127,6 @@ public Builder setNoCache(boolean noCache) { return this; } - /** - * Sets whether base images should always be pushed - * - * @param pullAlways Sets the value of the pullAlways property - * @return This builder instance - */ - public Builder setPullAllways(Boolean pullAlways) { - command.withOption(PULL_ALWAYS_CMD, pullAlways.toString()); - return this; - } - /** * Sets the platform for the resulting image rather using the default of the build system * diff --git a/src/main/java/nl/lexemmens/podman/config/image/AbstractImageBuildConfiguration.java b/src/main/java/nl/lexemmens/podman/config/image/AbstractImageBuildConfiguration.java index 43c31206..e0d3f43f 100644 --- a/src/main/java/nl/lexemmens/podman/config/image/AbstractImageBuildConfiguration.java +++ b/src/main/java/nl/lexemmens/podman/config/image/AbstractImageBuildConfiguration.java @@ -1,6 +1,8 @@ package nl.lexemmens.podman.config.image; import nl.lexemmens.podman.enumeration.ContainerFormat; +import nl.lexemmens.podman.enumeration.PullPolicy; + import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugins.annotations.Parameter; @@ -55,19 +57,7 @@ public abstract class AbstractImageBuildConfiguration { * always be build on the latest base. */ @Parameter - protected Boolean pull; - - /** - * Configures whether from-images should always be pulled from the first registry it is found in. - *

- * From Podman docs: - * Pull the image from the first registry it is found in as listed in registries.conf. - * Raise an error if not found in the registries, even if the image is present locally. - * - * @see --pull-always on docs.podman.io - */ - @Parameter - protected Boolean pullAlways; + protected PullPolicy pullPolicy; /** * Array consisting of one or more tags to attach to a container image. @@ -194,17 +184,8 @@ public boolean isNoCache() { * * @return When set to true, podman will build with --pull */ - public Optional getPull() { - return Optional.ofNullable(pull); - } - - /** - * Returns if the --pull-always property should be used - * - * @return When set to true, podman will build with --pull-always - */ - public Optional getPullAlways() { - return Optional.ofNullable(pullAlways); + public Optional getPullPolicy() { + return Optional.ofNullable(pullPolicy); } public void validate(MavenProject project) { @@ -448,19 +429,10 @@ public void setNoCache(boolean noCache) { * Configures whether from-images should be pulled so that the image will * always be build on the latest base. * - * @param pull The value to set - */ - public void setPull(Boolean pull) { - this.pull = pull; - } - - /** - * Configures whether from-images should always be pulled from the first registry it is found in. - * - * @param pullAlways The value to set + * @param pullPolicy The value to set */ - public void setPullAlways(Boolean pullAlways) { - this.pullAlways = pullAlways; + public void setPullPolicy(PullPolicy pullPolicy) { + this.pullPolicy = pullPolicy; } /** diff --git a/src/main/java/nl/lexemmens/podman/config/image/batch/BatchImageConfiguration.java b/src/main/java/nl/lexemmens/podman/config/image/batch/BatchImageConfiguration.java index e879dc6f..6abea26c 100644 --- a/src/main/java/nl/lexemmens/podman/config/image/batch/BatchImageConfiguration.java +++ b/src/main/java/nl/lexemmens/podman/config/image/batch/BatchImageConfiguration.java @@ -76,17 +76,13 @@ private List convertToSingleImageConfigurations(Log lo buildConfiguration.setCreateLatestTag(getBuild().isCreateLatestTag()); buildConfiguration.setLabels(getBuild().getLabels()); - if(getBuild().getPull().isPresent()) { - buildConfiguration.setPull(getBuild().getPull().get()); + if(getBuild().getPullPolicy().isPresent()) { + buildConfiguration.setPullPolicy(getBuild().getPullPolicy().get()); } buildConfiguration.setNoCache(getBuild().isNoCache()); buildConfiguration.setTags(getBuild().getTags()); - if(getBuild().getPullAlways().isPresent()) { - buildConfiguration.setPullAlways(getBuild().getPullAlways().get()); - } - buildConfiguration.setTagWithMavenProjectVersion(getBuild().isTagWithMavenProjectVersion()); imageConfiguration.setBuild(buildConfiguration); diff --git a/src/main/java/nl/lexemmens/podman/enumeration/PullPolicy.java b/src/main/java/nl/lexemmens/podman/enumeration/PullPolicy.java new file mode 100644 index 00000000..161ab5ff --- /dev/null +++ b/src/main/java/nl/lexemmens/podman/enumeration/PullPolicy.java @@ -0,0 +1,48 @@ +package nl.lexemmens.podman.enumeration; + +/** + * Defines the pullpolicy of the built image's manifest and configuration data. Recognised values include oci and docker. + * @see Manual of podman build + */ +public enum PullPolicy { + + /** + * Always pull the image and throw an error if the pull fails. + */ + ALWAYS("always"), + TRUE("true"), + + /** + * Only pull the image when it does not exist in the local containers storage. Throw an error if no image is found and the pull fails. + */ + MISSING("missing"), + + /** + * Never pull the image but use the one from the local containers storage. Throw an error when no image is found. + */ + NEVER("never"), + FALSE("false"), + + /** + * Pull if the image on the registry is newer than the one in the local containers storage. An image is considered to be newer when the digests are different. Comparing the time stamps is prone to errors. Pull errors are suppressed if a local image was found. + */ + NEWER("newer"); + + private final String value; + + /** + * Constructor + * @param pullPolicy The pullpolicy to set + */ + PullPolicy(String value) { + this.value = value; + } + + /** + * Returns the selected pullpolicy + * @return the selected pullpolicy + */ + public String getValue() { + return value; + } +} diff --git a/src/main/java/nl/lexemmens/podman/service/PodmanExecutorService.java b/src/main/java/nl/lexemmens/podman/service/PodmanExecutorService.java index 8328c806..80100dd2 100644 --- a/src/main/java/nl/lexemmens/podman/service/PodmanExecutorService.java +++ b/src/main/java/nl/lexemmens/podman/service/PodmanExecutorService.java @@ -3,6 +3,7 @@ import nl.lexemmens.podman.command.podman.*; import nl.lexemmens.podman.config.image.single.SingleImageConfiguration; import nl.lexemmens.podman.config.podman.PodmanConfiguration; +import nl.lexemmens.podman.enumeration.PullPolicy; import nl.lexemmens.podman.executor.CommandExecutorDelegate; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.logging.Log; @@ -66,14 +67,9 @@ public List build(SingleImageConfiguration image) throws MojoExecutionEx builder = builder.setLayers(image.getBuild().getLayers()); } - Optional pullOptional = image.getBuild().getPull(); - if (pullOptional.isPresent()) { - builder = builder.setPull(pullOptional.get()); - } - - Optional pullAlwaysOptional = image.getBuild().getPullAlways(); - if (pullAlwaysOptional.isPresent()) { - builder = builder.setPullAllways(pullAlwaysOptional.get()); + Optional pullPolicyOptional = image.getBuild().getPullPolicy(); + if (pullPolicyOptional.isPresent()) { + builder = builder.setPullPolicy(pullPolicyOptional.get().getValue()); } Optional platform = image.getBuild().getPlatform(); diff --git a/src/test/java/nl/lexemmens/podman/BatchBuildMojoTest.java b/src/test/java/nl/lexemmens/podman/BatchBuildMojoTest.java index 569e3eee..32b0f145 100644 --- a/src/test/java/nl/lexemmens/podman/BatchBuildMojoTest.java +++ b/src/test/java/nl/lexemmens/podman/BatchBuildMojoTest.java @@ -8,6 +8,7 @@ import nl.lexemmens.podman.config.podman.TestPodmanConfigurationBuilder; import nl.lexemmens.podman.config.skopeo.SkopeoConfiguration; import nl.lexemmens.podman.enumeration.ContainerFormat; +import nl.lexemmens.podman.enumeration.PullPolicy; import nl.lexemmens.podman.enumeration.TlsVerify; import nl.lexemmens.podman.service.ContainerfileDecorator; import org.apache.maven.model.Build; @@ -186,8 +187,7 @@ public void testConversionWithCustomValues() throws MojoExecutionException { BatchImageConfiguration image = new TestBatchImageConfigurationBuilder("sample-image-%d") .setContainerfile("Containerfile") - .setPull(true) - .setPullAlways(true) + .setPullPolicy(PullPolicy.ALWAYS) .setStages(new StageConfiguration[]{stageCfg}) .setLabels(Collections.singletonMap("KEY", "VALUE")) .setTags(new String[]{"latest"}) @@ -218,10 +218,8 @@ public void testConversionWithCustomValues() throws MojoExecutionException { assertNotNull(image1.getBuild()); assertEquals(1, image1.getBuild().getAllTags().size()); assertEquals("latest", image1.getBuild().getAllTags().get(0)); - assertTrue(image1.getBuild().getPull().isPresent()); - assertTrue(image1.getBuild().getPull().get()); - assertTrue(image1.getBuild().getPullAlways().isPresent()); - assertTrue(image1.getBuild().getPullAlways().get()); + assertTrue(image1.getBuild().getPullPolicy().isPresent()); + assertEquals(PullPolicy.ALWAYS, image1.getBuild().getPullPolicy().get()); assertEquals(ContainerFormat.DOCKER, image1.getBuild().getFormat()); } diff --git a/src/test/java/nl/lexemmens/podman/config/image/batch/TestBatchImageConfigurationBuilder.java b/src/test/java/nl/lexemmens/podman/config/image/batch/TestBatchImageConfigurationBuilder.java index 76191457..f8cb3115 100644 --- a/src/test/java/nl/lexemmens/podman/config/image/batch/TestBatchImageConfigurationBuilder.java +++ b/src/test/java/nl/lexemmens/podman/config/image/batch/TestBatchImageConfigurationBuilder.java @@ -2,6 +2,8 @@ import nl.lexemmens.podman.config.image.StageConfiguration; import nl.lexemmens.podman.enumeration.ContainerFormat; +import nl.lexemmens.podman.enumeration.PullPolicy; + import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; @@ -38,8 +40,8 @@ public TestBatchImageConfigurationBuilder setNoCache(boolean noCache) { return this; } - public TestBatchImageConfigurationBuilder setPull(boolean pull) { - image.getBuild().setPull(pull); + public TestBatchImageConfigurationBuilder setPullPolicy(PullPolicy pullPolicy) { + image.getBuild().setPullPolicy(pullPolicy); return this; } @@ -104,11 +106,6 @@ public TestBatchImageConfigurationBuilder addCustomImageNameForBuildStage(String return this; } - public TestBatchImageConfigurationBuilder setPullAlways(boolean pullAlways) { - image.getBuild().setPullAlways(pullAlways); - return this; - } - public BatchImageConfiguration build() { return image; } diff --git a/src/test/java/nl/lexemmens/podman/config/image/single/TestSingleImageConfigurationBuilder.java b/src/test/java/nl/lexemmens/podman/config/image/single/TestSingleImageConfigurationBuilder.java index 59099276..8abe84b6 100644 --- a/src/test/java/nl/lexemmens/podman/config/image/single/TestSingleImageConfigurationBuilder.java +++ b/src/test/java/nl/lexemmens/podman/config/image/single/TestSingleImageConfigurationBuilder.java @@ -2,6 +2,8 @@ import nl.lexemmens.podman.config.image.StageConfiguration; import nl.lexemmens.podman.enumeration.ContainerFormat; +import nl.lexemmens.podman.enumeration.PullPolicy; + import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.logging.Log; import org.apache.maven.project.MavenProject; @@ -37,8 +39,8 @@ public TestSingleImageConfigurationBuilder setNoCache(boolean noCache) { return this; } - public TestSingleImageConfigurationBuilder setPull(boolean pull) { - image.getBuild().setPull(pull); + public TestSingleImageConfigurationBuilder setPullPolicy(PullPolicy pullPolicy) { + image.getBuild().setPullPolicy(pullPolicy); return this; } @@ -118,11 +120,6 @@ public TestSingleImageConfigurationBuilder addCustomImageNameForBuildStage(Strin return this; } - public TestSingleImageConfigurationBuilder setPullAlways(boolean pullAlways) { - image.getBuild().setPullAlways(pullAlways); - return this; - } - public TestSingleImageConfigurationBuilder setTargetStage(String targetStage){ image.getBuild().setTargetStage(targetStage); return this; diff --git a/src/test/java/nl/lexemmens/podman/service/PodmanExecutorServiceTest.java b/src/test/java/nl/lexemmens/podman/service/PodmanExecutorServiceTest.java index 193aad24..cb99bafa 100644 --- a/src/test/java/nl/lexemmens/podman/service/PodmanExecutorServiceTest.java +++ b/src/test/java/nl/lexemmens/podman/service/PodmanExecutorServiceTest.java @@ -5,6 +5,7 @@ import nl.lexemmens.podman.config.podman.PodmanConfiguration; import nl.lexemmens.podman.config.podman.TestPodmanConfigurationBuilder; import nl.lexemmens.podman.enumeration.CGroupManager; +import nl.lexemmens.podman.enumeration.PullPolicy; import nl.lexemmens.podman.executor.CommandExecutorDelegate; import org.apache.commons.lang3.StringUtils; import org.apache.maven.model.Build; @@ -453,14 +454,14 @@ public void testBuildPlatform() throws MojoExecutionException { } @Test - public void testBuildPullAlways() throws MojoExecutionException { + public void testBuildPullPolicy() throws MojoExecutionException { when(mavenProject.getBuild()).thenReturn(build); when(build.getDirectory()).thenReturn("target"); PodmanConfiguration podmanConfig = new TestPodmanConfigurationBuilder().setTlsVerify(TRUE).initAndValidate(mavenProject, log).build(); SingleImageConfiguration image = new TestSingleImageConfigurationBuilder("test_image") .setFormat(OCI) - .setPullAlways(true) + .setPullPolicy(PullPolicy.ALWAYS) .setContainerfileDir("src/test/resources") .initAndValidate(mavenProject, log, true) .build(); @@ -472,30 +473,7 @@ public void testBuildPullAlways() throws MojoExecutionException { podmanExecutorService.build(image); Assertions.assertEquals("podman build --tls-verify=true --format=oci --file=" + image.getBuild().getTargetContainerFile() + " --no-cache=false " + - "--pull-always=true .", delegate.getCommandAsString()); - } - - @Test - public void testBuildPull() throws MojoExecutionException { - when(mavenProject.getBuild()).thenReturn(build); - when(build.getDirectory()).thenReturn("target"); - - PodmanConfiguration podmanConfig = new TestPodmanConfigurationBuilder().setTlsVerify(TRUE).initAndValidate(mavenProject, log).build(); - SingleImageConfiguration image = new TestSingleImageConfigurationBuilder("test_image") - .setFormat(OCI) - .setPull(true) - .setContainerfileDir("src/test/resources") - .initAndValidate(mavenProject, log, true) - .build(); - - String sampleImageHash = "this_would_normally_be_an_image_hash"; - InterceptorCommandExecutorDelegate delegate = new InterceptorCommandExecutorDelegate(Collections.singletonList(sampleImageHash)); - podmanExecutorService = new PodmanExecutorService(log, podmanConfig, delegate); - - podmanExecutorService.build(image); - - Assertions.assertEquals("podman build --tls-verify=true --format=oci --file=" + image.getBuild().getTargetContainerFile() + " --no-cache=false " + - "--pull=true .", delegate.getCommandAsString()); + "--pull=always .", delegate.getCommandAsString()); } @Test