Skip to content

Commit

Permalink
Merge pull request #96 from lgranie/lgranie/issue95
Browse files Browse the repository at this point in the history
Lgranie/issue95
  • Loading branch information
lexemmens committed Apr 16, 2024
2 parents b90af5e + b7d9c63 commit 06a7be4
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 127 deletions.
20 changes: 5 additions & 15 deletions docs/modules/ROOT/pages/goals/build.adoc
Expand Up @@ -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 `<pull>false</pull>`) 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 `<pull>never</pull>`) 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.
Expand Down Expand Up @@ -152,8 +143,7 @@ Supported values are:
<image>
<name>your-image-name</name>
<build>
<pull>true</pull>
<pullAlways>false</pullAlways>
<pull>always</pull>
<tags>
<sampleTag>sampleTagValue</sampleTag>
</tags>
Expand Down
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
}

Expand All @@ -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
*
Expand Down
@@ -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;
Expand Down Expand Up @@ -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.
* <p>
* 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 <a href="https://docs.podman.io/en/latest/markdown/podman-build.1.html#pull-always">--pull-always on docs.podman.io</a>
*/
@Parameter
protected Boolean pullAlways;
protected PullPolicy pullPolicy;

/**
* Array consisting of one or more tags to attach to a container image.
Expand Down Expand Up @@ -194,17 +184,8 @@ public boolean isNoCache() {
*
* @return When set to true, podman will build with --pull
*/
public Optional<Boolean> 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<Boolean> getPullAlways() {
return Optional.ofNullable(pullAlways);
public Optional<PullPolicy> getPullPolicy() {
return Optional.ofNullable(pullPolicy);
}

public void validate(MavenProject project) {
Expand Down Expand Up @@ -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;
}

/**
Expand Down
Expand Up @@ -76,17 +76,13 @@ private List<SingleImageConfiguration> 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);
Expand Down
48 changes: 48 additions & 0 deletions 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 <a href="www.mankier.com/1/podman-build">Manual of podman build</a>
*/
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;
}
}
Expand Up @@ -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;
Expand Down Expand Up @@ -66,14 +67,9 @@ public List<String> build(SingleImageConfiguration image) throws MojoExecutionEx
builder = builder.setLayers(image.getBuild().getLayers());
}

Optional<Boolean> pullOptional = image.getBuild().getPull();
if (pullOptional.isPresent()) {
builder = builder.setPull(pullOptional.get());
}

Optional<Boolean> pullAlwaysOptional = image.getBuild().getPullAlways();
if (pullAlwaysOptional.isPresent()) {
builder = builder.setPullAllways(pullAlwaysOptional.get());
Optional<PullPolicy> pullPolicyOptional = image.getBuild().getPullPolicy();
if (pullPolicyOptional.isPresent()) {
builder = builder.setPullPolicy(pullPolicyOptional.get().getValue());
}

Optional<String> platform = image.getBuild().getPlatform();
Expand Down
10 changes: 4 additions & 6 deletions src/test/java/nl/lexemmens/podman/BatchBuildMojoTest.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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"})
Expand Down Expand Up @@ -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());
}

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 06a7be4

Please sign in to comment.