diff --git a/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/TaskUtil.java b/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/TaskUtil.java index d0cb5eb93e..506c9464c0 100644 --- a/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/TaskUtil.java +++ b/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/TaskUtil.java @@ -18,7 +18,6 @@ import org.eclipse.jkube.kit.build.service.docker.ImagePullManager; import org.eclipse.jkube.kit.build.service.docker.access.DockerAccess; import org.eclipse.jkube.kit.common.KitLogger; -import org.eclipse.jkube.kit.config.resource.BuildRecreateMode; import org.eclipse.jkube.kit.config.service.BuildServiceConfig; import static org.eclipse.jkube.kit.build.service.docker.ImagePullManager.createImagePullManager; @@ -36,9 +35,7 @@ public static BuildServiceConfig.BuildServiceConfigBuilder buildServiceConfigBui kubernetesExtension.javaProject.getProperties()); return BuildServiceConfig.builder() .imagePullManager(imagePullManager) - .buildRecreateMode(BuildRecreateMode.fromParameter(kubernetesExtension.getBuildRecreateOrDefault())) .jKubeBuildStrategy(kubernetesExtension.getBuildStrategyOrDefault()) - .forcePull(kubernetesExtension.getForcePullOrDefault()) .buildDirectory(kubernetesExtension.javaProject.getBuildDirectory().getAbsolutePath()); } diff --git a/gradle-plugin/kubernetes/src/test/java/org/eclipse/jkube/gradle/plugin/task/TaskUtilTest.java b/gradle-plugin/kubernetes/src/test/java/org/eclipse/jkube/gradle/plugin/task/TaskUtilTest.java index a7d9e1349d..7b49e3022b 100644 --- a/gradle-plugin/kubernetes/src/test/java/org/eclipse/jkube/gradle/plugin/task/TaskUtilTest.java +++ b/gradle-plugin/kubernetes/src/test/java/org/eclipse/jkube/gradle/plugin/task/TaskUtilTest.java @@ -17,7 +17,6 @@ import org.eclipse.jkube.kit.build.service.docker.DockerAccessFactory; import org.eclipse.jkube.kit.common.KitLogger; import org.eclipse.jkube.kit.config.image.build.JKubeBuildStrategy; -import org.eclipse.jkube.kit.config.resource.BuildRecreateMode; import org.eclipse.jkube.kit.config.service.BuildServiceConfig; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -49,16 +48,13 @@ void buildServiceConfigBuilder_shouldInitializeBuildServiceConfigWithDefaults() // Then assertThat(buildServiceConfig) - .hasFieldOrPropertyWithValue("buildRecreateMode", BuildRecreateMode.none) .hasFieldOrPropertyWithValue("jKubeBuildStrategy", JKubeBuildStrategy.docker) - .hasFieldOrPropertyWithValue("forcePull", false) .hasFieldOrPropertyWithValue("buildDirectory", null); } @Test void buildServiceConfigBuilder_shouldInitializeBuildServiceConfigWithConfiguredValues() { // Given - extension.buildRecreate = "true"; extension.isForcePull = true; extension.buildStrategy = JKubeBuildStrategy.jib; when(extension.javaProject.getBuildDirectory().getAbsolutePath()).thenReturn("/tmp/foo"); @@ -68,9 +64,7 @@ void buildServiceConfigBuilder_shouldInitializeBuildServiceConfigWithConfiguredV // Then assertThat(buildServiceConfig) - .hasFieldOrPropertyWithValue("buildRecreateMode", BuildRecreateMode.all) .hasFieldOrPropertyWithValue("jKubeBuildStrategy", JKubeBuildStrategy.jib) - .hasFieldOrPropertyWithValue("forcePull", true) .hasFieldOrPropertyWithValue("buildDirectory", "/tmp/foo"); } diff --git a/gradle-plugin/openshift/src/main/java/org/eclipse/jkube/gradle/plugin/task/OpenShiftBuildTask.java b/gradle-plugin/openshift/src/main/java/org/eclipse/jkube/gradle/plugin/task/OpenShiftBuildTask.java index 8dd0d967c3..f83d0d1baf 100644 --- a/gradle-plugin/openshift/src/main/java/org/eclipse/jkube/gradle/plugin/task/OpenShiftBuildTask.java +++ b/gradle-plugin/openshift/src/main/java/org/eclipse/jkube/gradle/plugin/task/OpenShiftBuildTask.java @@ -15,7 +15,9 @@ import javax.inject.Inject; +import org.eclipse.jkube.generator.api.GeneratorContext; import org.eclipse.jkube.gradle.plugin.OpenShiftExtension; +import org.eclipse.jkube.kit.common.BuildRecreateMode; import org.eclipse.jkube.kit.config.resource.PlatformMode; import org.eclipse.jkube.kit.config.service.BuildServiceConfig; @@ -31,15 +33,22 @@ public OpenShiftBuildTask(Class extensionClass) { @Override protected BuildServiceConfig.BuildServiceConfigBuilder buildServiceConfigBuilder() { return super.buildServiceConfigBuilder() - .openshiftPullSecret(getOpenShiftExtension().getOpenshiftPullSecretOrDefault()) - .s2iBuildNameSuffix(getOpenShiftExtension().getS2iBuildNameSuffixOrDefault()) - .s2iImageStreamLookupPolicyLocal(getOpenShiftExtension().getS2iImageStreamLookupPolicyLocalOrDefault()) - .openshiftPushSecret(getOpenShiftExtension().getOpenshiftPushSecretOrDefault()) .resourceConfig(getOpenShiftExtension().resources) - .buildOutputKind(getOpenShiftExtension().getBuildOutputKindOrDefault()) .enricherTask(e -> { enricherManager.enrich(PlatformMode.kubernetes, e); enricherManager.enrich(PlatformMode.openshift, e); }); } + + @Override + protected GeneratorContext.GeneratorContextBuilder initGeneratorContextBuilder() { + return super.initGeneratorContextBuilder() + .openshiftForcePull(getOpenShiftExtension().getForcePullOrDefault()) + .openshiftS2iBuildNameSuffix(getOpenShiftExtension().getS2iBuildNameSuffixOrDefault()) + .openshiftS2iImageStreamLookupPolicyLocal(getOpenShiftExtension().getS2iImageStreamLookupPolicyLocalOrDefault()) + .openshiftPullSecret(getOpenShiftExtension().getOpenshiftPullSecretOrDefault()) + .openshiftPushSecret(getOpenShiftExtension().getOpenshiftPushSecretOrDefault()) + .openshiftBuildOutputKind(getOpenShiftExtension().getBuildOutputKindOrDefault()) + .openshiftBuildRecreate(BuildRecreateMode.fromParameter(kubernetesExtension.getBuildRecreateOrDefault())); + } } diff --git a/jkube-kit/build/api/src/main/java/org/eclipse/jkube/kit/build/api/helper/ConfigHelper.java b/jkube-kit/build/api/src/main/java/org/eclipse/jkube/kit/build/api/helper/ConfigHelper.java index fed66899db..63cea69f00 100644 --- a/jkube-kit/build/api/src/main/java/org/eclipse/jkube/kit/build/api/helper/ConfigHelper.java +++ b/jkube-kit/build/api/src/main/java/org/eclipse/jkube/kit/build/api/helper/ConfigHelper.java @@ -77,8 +77,6 @@ public static String getExternalConfigActivationProperty(JavaProject project) { // =========================================================================================================== - // ========================================================================= - /** * Format an image name by replacing certain placeholders */ diff --git a/jkube-kit/config/resource/src/main/java/org/eclipse/jkube/kit/config/resource/BuildRecreateMode.java b/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/BuildRecreateMode.java similarity index 97% rename from jkube-kit/config/resource/src/main/java/org/eclipse/jkube/kit/config/resource/BuildRecreateMode.java rename to jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/BuildRecreateMode.java index ea68812a93..c32f662f78 100644 --- a/jkube-kit/config/resource/src/main/java/org/eclipse/jkube/kit/config/resource/BuildRecreateMode.java +++ b/jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/BuildRecreateMode.java @@ -11,7 +11,7 @@ * Contributors: * Red Hat, Inc. - initial API and implementation */ -package org.eclipse.jkube.kit.config.resource; +package org.eclipse.jkube.kit.common; import org.apache.commons.lang3.StringUtils; diff --git a/jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/BuildConfiguration.java b/jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/BuildConfiguration.java index 330df4fc2e..42dd6a5909 100644 --- a/jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/BuildConfiguration.java +++ b/jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/BuildConfiguration.java @@ -32,6 +32,7 @@ import lombok.NoArgsConstructor; import lombok.Setter; import lombok.Singular; +import org.eclipse.jkube.kit.common.BuildRecreateMode; import org.eclipse.jkube.kit.common.Arguments; import org.eclipse.jkube.kit.common.AssemblyConfiguration; import org.eclipse.jkube.kit.common.archive.ArchiveCompression; @@ -56,7 +57,9 @@ public class BuildConfiguration implements Serializable { public static final String DEFAULT_FILTER = "${*}"; public static final String DEFAULT_CLEANUP = "try"; + /////////////////////////////////////////////////////////////////////////// // Generic fields applicable to all build strategies + /////////////////////////////////////////////////////////////////////////// /** * The base image which should be used for this image. * @@ -328,6 +331,9 @@ public class BuildConfiguration implements Serializable { @Singular("addCacheFrom") private List cacheFrom; + /////////////////////////////////////////////////////////////////////////// + // Fields applicable to buildpacks build strategy + /////////////////////////////////////////////////////////////////////////// /** * Configure BuildPack builder OCI image for BuildPack Build. *

@@ -335,6 +341,74 @@ public class BuildConfiguration implements Serializable { */ private String buildpacksBuilderImage; + /////////////////////////////////////////////////////////////////////////// + // Fields applicable to openshift build strategy + /////////////////////////////////////////////////////////////////////////// + /** + * While creating a BuildConfig, By default, if the builder image specified in the + * build configuration is available locally on the node, that image will be used. + *

+ * ForcePull to override the local image and refresh it from the registry to which the image stream points. + *

+ * This field is applicable in case of s2i build strategy + */ + private boolean openshiftForcePull; + + /** + * The S2I binary builder BuildConfig name suffix appended to the image name to avoid + * clashing with the underlying BuildConfig for the Jenkins pipeline + *

+ * This field is applicable in case of OpenShift s2i build strategy + */ + private String openshiftS2iBuildNameSuffix; + + /** + * Allow the ImageStream used in the S2I binary build to be used in standard + * Kubernetes resources such as Deployment or StatefulSet. + *

+ * This field is only applicable in case of OpenShift s2i build strategy + */ + private boolean openshiftS2iImageStreamLookupPolicyLocal; + + /** + * The name of pullSecret to be used to pull the base image in case pulling from a protected + * registry which requires authentication. + *

+ * This field is applicable in case of OpenShift s2i build strategy + */ + private String openshiftPullSecret; + + /** + * The name of pushSecret to be used to push the final image in case pushing from a protected + * registry which requires authentication. + *

+ * This field is applicable in case of OpenShift s2i build strategy + */ + private String openshiftPushSecret; + + /** + * Allow specifying in which registry to push the container image at the end of the build. + * If the output kind is ImageStreamTag, then the image will be pushed to the internal OpenShift registry. + * If the output is of type DockerImage, then the name of the output reference will be used as a Docker push specification. + *

+ * This field is applicable in case of OpenShift s2i build strategy + */ + private String openshiftBuildOutputKind; + + /** + * How the OpenShift resource objects associated with the build should be treated when they already exist + + * buildConfig or bc : Only the BuildConfig is recreated + * all or is : Only the ImageStream is recreated + * all : Both, BuildConfig and ImageStream are recreated + * none : Neither BuildConfig nor ImageStream is recreated + * The default is none. If you provide the property without value then all is assumed, so everything gets recreated. + * + *

+ * This field is applicable in case of OpenShift s2i build strategy + */ + private BuildRecreateMode openshiftBuildRecreateMode; + public boolean isDockerFileMode() { return dockerFile != null || contextDir != null; } diff --git a/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/BuildServiceConfig.java b/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/BuildServiceConfig.java index 4517a0777d..0c4b7bf80f 100644 --- a/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/BuildServiceConfig.java +++ b/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/BuildServiceConfig.java @@ -22,7 +22,6 @@ import org.eclipse.jkube.kit.build.service.docker.ImagePullManager; import org.eclipse.jkube.kit.build.service.docker.helper.Task; import org.eclipse.jkube.kit.config.image.build.JKubeBuildStrategy; -import org.eclipse.jkube.kit.config.resource.BuildRecreateMode; import org.eclipse.jkube.kit.config.resource.ResourceConfig; import java.io.File; @@ -36,21 +35,13 @@ @Getter @EqualsAndHashCode public class BuildServiceConfig { - - private BuildRecreateMode buildRecreateMode; private JKubeBuildStrategy jKubeBuildStrategy; - private boolean forcePull; - private String s2iBuildNameSuffix; - private String openshiftPullSecret; - private String openshiftPushSecret; private Task enricherTask; private String buildDirectory; private Attacher attacher; private ImagePullManager imagePullManager; - private boolean s2iImageStreamLookupPolicyLocal; private ResourceConfig resourceConfig; private File resourceDir; - private String buildOutputKind; public void attachArtifact(String classifier, File destFile) { if (attacher != null) { diff --git a/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/openshift/OpenShiftBuildServiceUtils.java b/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/openshift/OpenShiftBuildServiceUtils.java index 199993a0da..e2144dcfb1 100644 --- a/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/openshift/OpenShiftBuildServiceUtils.java +++ b/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/openshift/OpenShiftBuildServiceUtils.java @@ -73,10 +73,10 @@ protected static File createBuildArchive(JKubeServiceHub jKubeServiceHub, ImageC * Returns the applicable name for the S2I Build resource considering the provided {@link ImageName} and * {@link BuildServiceConfig}. */ - static String computeS2IBuildName(BuildServiceConfig config, ImageName imageName) { + static String computeS2IBuildName(ImageConfiguration imageConfiguration, BuildServiceConfig config, ImageName imageName) { final StringBuilder s2IBuildName = new StringBuilder(resolveImageStreamName(imageName)); - if (!StringUtils.isEmpty(config.getS2iBuildNameSuffix())) { - s2IBuildName.append(config.getS2iBuildNameSuffix()); + if (!StringUtils.isEmpty(imageConfiguration.getBuild().getOpenshiftS2iBuildNameSuffix())) { + s2IBuildName.append(imageConfiguration.getBuild().getOpenshiftS2iBuildNameSuffix()); } else if (config.getJKubeBuildStrategy() == JKubeBuildStrategy.s2i) { s2IBuildName.append(DEFAULT_S2I_BUILD_SUFFIX); } @@ -155,7 +155,7 @@ protected static BuildStrategy createBuildStrategy( .withName(fromName) .withNamespace(StringUtils.isEmpty(fromNamespace) ? null : fromNamespace) .endFrom() - .withForcePull(config.isForcePull()) + .withForcePull(imageConfig.getBuild().isOpenshiftForcePull()) .endSourceStrategy() .build(); if (openshiftPullSecret != null) { @@ -169,16 +169,16 @@ protected static BuildStrategy createBuildStrategy( } } - protected static BuildOutput createBuildOutput(BuildServiceConfig config, ImageName imageName) { - final String buildOutputKind = Optional.ofNullable(config.getBuildOutputKind()).orElse(DEFAULT_BUILD_OUTPUT_KIND); + protected static BuildOutput createBuildOutput(ImageConfiguration imageConfiguration, ImageName imageName) { + final String buildOutputKind = Optional.ofNullable(imageConfiguration.getBuild().getOpenshiftBuildOutputKind()).orElse(DEFAULT_BUILD_OUTPUT_KIND); final String outputImageStreamTag = resolveImageStreamName(imageName) + ":" + (imageName.getTag() != null ? imageName.getTag() : "latest"); final BuildOutputBuilder buildOutputBuilder = new BuildOutputBuilder(); buildOutputBuilder.withNewTo().withKind(buildOutputKind).withName(outputImageStreamTag).endTo(); if (DOCKER_IMAGE.equals(buildOutputKind)) { buildOutputBuilder.editTo().withName(imageName.getFullName()).endTo(); } - if(StringUtils.isNotBlank(config.getOpenshiftPushSecret())) { - buildOutputBuilder.withNewPushSecret().withName(config.getOpenshiftPushSecret()).endPushSecret(); + if(StringUtils.isNotBlank(imageConfiguration.getBuild().getOpenshiftPushSecret())) { + buildOutputBuilder.withNewPushSecret().withName(imageConfiguration.getBuild().getOpenshiftPushSecret()).endPushSecret(); } return buildOutputBuilder.build(); } diff --git a/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildService.java b/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildService.java index b6842cb34d..dcc3282df6 100644 --- a/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildService.java +++ b/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildService.java @@ -39,6 +39,7 @@ import org.eclipse.jkube.kit.config.image.build.BuildConfiguration; import org.eclipse.jkube.kit.common.JKubeConfiguration; import org.eclipse.jkube.kit.config.image.build.JKubeBuildStrategy; +import org.eclipse.jkube.kit.common.BuildRecreateMode; import org.eclipse.jkube.kit.config.resource.RuntimeMode; import org.eclipse.jkube.kit.config.service.AbstractImageBuildService; import org.eclipse.jkube.kit.config.service.BuildServiceConfig; @@ -138,7 +139,7 @@ public void buildSingleImage(ImageConfiguration imageConfig) throws JKubeService KubernetesListBuilder builder = new KubernetesListBuilder(); // Check for buildconfig / imagestream / pullSecret and create them if necessary - String openshiftPullSecret = buildServiceConfig.getOpenshiftPullSecret(); + String openshiftPullSecret = imageConfig.getBuild().getOpenshiftPullSecret(); final boolean usePullSecret = checkOrCreatePullSecret(client, builder, openshiftPullSecret, applicableImageConfig); if (usePullSecret) { buildName = updateOrCreateBuildConfig(buildServiceConfig, client, builder, applicableImageConfig, openshiftPullSecret); @@ -146,8 +147,8 @@ public void buildSingleImage(ImageConfiguration imageConfig) throws JKubeService buildName = updateOrCreateBuildConfig(buildServiceConfig, client, builder, applicableImageConfig, null); } - if (buildServiceConfig.getBuildOutputKind() == null || IMAGE_STREAM_TAG.equals(buildServiceConfig.getBuildOutputKind())) { - checkOrCreateImageStream(buildServiceConfig, client, builder, resolveImageStreamName(imageName)); + if (imageConfig.getBuild().getOpenshiftBuildOutputKind() == null || IMAGE_STREAM_TAG.equals(imageConfig.getBuild().getOpenshiftBuildOutputKind())) { + checkOrCreateImageStream(applicableImageConfig, client, builder, resolveImageStreamName(imageName)); applyBuild(buildName, dockerTar, builder); @@ -201,10 +202,10 @@ public void postProcess() { protected String updateOrCreateBuildConfig(BuildServiceConfig config, OpenShiftClient client, KubernetesListBuilder builder, ImageConfiguration imageConfig, String openshiftPullSecret) { ImageName imageName = new ImageName(imageConfig.getName()); - String buildName = computeS2IBuildName(config, imageName); + String buildName = computeS2IBuildName(imageConfig, config, imageName); BuildStrategy buildStrategyResource = createBuildStrategy(jKubeServiceHub, imageConfig, openshiftPullSecret); - BuildOutput buildOutput = createBuildOutput(config, imageName); + BuildOutput buildOutput = createBuildOutput(imageConfig, imageName); // Fetch existing build config BuildConfig buildConfig = client.buildConfigs().inNamespace(applicableOpenShiftNamespace).withName(buildName).get(); @@ -213,7 +214,7 @@ protected String updateOrCreateBuildConfig(BuildServiceConfig config, OpenShiftC BuildConfigSpec spec = OpenShiftBuildServiceUtils.getBuildConfigSpec(buildConfig); validateSourceType(buildName, spec); - if (config.getBuildRecreateMode().isBuildConfig()) { + if (imageConfig.getBuild().getOpenshiftBuildRecreateMode().isBuildConfig()) { // Delete and recreate afresh client.buildConfigs().inNamespace(applicableOpenShiftNamespace).withName(buildName).delete(); return createBuildConfig(builder, buildName, buildStrategyResource, buildOutput); @@ -235,7 +236,7 @@ ImageConfiguration getApplicableImageConfiguration(ImageConfiguration imageConfi imageConfig.getBuildConfiguration().getAssembly().getFlattenedClone(jKubeServiceHub.getConfiguration())) .build()); } - if (buildServiceConfig.getBuildOutputKind() != null && buildServiceConfig.getBuildOutputKind().equals(DOCKER_IMAGE)) { + if (imageConfig.getBuild().getOpenshiftBuildOutputKind() != null && imageConfig.getBuild().getOpenshiftBuildOutputKind().equals(DOCKER_IMAGE)) { String applicableRegistry = getApplicablePushRegistryFrom(imageConfig, registryConfig); applicableImageConfigBuilder.name(new ImageName(imageConfig.getName()).getFullName(applicableRegistry)); } @@ -402,9 +403,9 @@ private boolean updateSecret(OpenShiftClient client, String pullSecretName, Map< return true; } - private void checkOrCreateImageStream(BuildServiceConfig config, OpenShiftClient client, KubernetesListBuilder builder, String imageStreamName) { + private void checkOrCreateImageStream(ImageConfiguration applicableImageConfig, OpenShiftClient client, KubernetesListBuilder builder, String imageStreamName) { boolean hasImageStream = client.imageStreams().inNamespace(applicableOpenShiftNamespace).withName(imageStreamName).get() != null; - if (hasImageStream && config.getBuildRecreateMode().isImageStream()) { + if (hasImageStream && applicableImageConfig.getBuild().getOpenshiftBuildRecreateMode().isImageStream()) { client.imageStreams().inNamespace(applicableOpenShiftNamespace).withName(imageStreamName).delete(); hasImageStream = false; } @@ -416,7 +417,7 @@ private void checkOrCreateImageStream(BuildServiceConfig config, OpenShiftClient .endMetadata() .withNewSpec() .withNewLookupPolicy() - .withLocal(config.isS2iImageStreamLookupPolicyLocal()) + .withLocal(applicableImageConfig.getBuild().isOpenshiftS2iImageStreamLookupPolicyLocal()) .endLookupPolicy() .endSpec() .build() diff --git a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenShiftBuildServiceTest.java b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenShiftBuildServiceTest.java index 550f2590f2..d7b82f204c 100644 --- a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenShiftBuildServiceTest.java +++ b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenShiftBuildServiceTest.java @@ -136,9 +136,9 @@ void build_withImageBuildConfigurationSkipEnabled_shouldNotBuildImage() throws J @Test void getApplicableImageConfiguration_withRegistryInImageConfigurationAndDockerImageBuildOutput_shouldAppendRegistryToImageName() { // Given - when(jKubeServiceHub.getBuildServiceConfig()).thenReturn(BuildServiceConfig.builder() - .buildOutputKind("DockerImage") - .build()); + imageConfiguration = imageConfiguration.toBuilder() + .build(imageConfiguration.getBuild().toBuilder().openshiftBuildOutputKind("DockerImage").build()) + .build(); OpenshiftBuildService openshiftBuildService = new OpenshiftBuildService(jKubeServiceHub); // When @@ -152,9 +152,9 @@ void getApplicableImageConfiguration_withRegistryInImageConfigurationAndDockerIm @Test void getApplicableImageConfiguration_withRegistryInImageConfiguration_shouldNotAppendRegistryToImageName() { // Given - when(jKubeServiceHub.getBuildServiceConfig()).thenReturn(BuildServiceConfig.builder() - .buildOutputKind("ImageStreamTag") - .build()); + imageConfiguration = imageConfiguration.toBuilder() + .build(imageConfiguration.getBuild().toBuilder().openshiftBuildOutputKind("ImageStreamTag").build()) + .build(); OpenshiftBuildService openshiftBuildService = new OpenshiftBuildService(jKubeServiceHub); // When diff --git a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenShiftBuildServiceUtilsTest.java b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenShiftBuildServiceUtilsTest.java index c242827b22..02e2c6c980 100644 --- a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenShiftBuildServiceUtilsTest.java +++ b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenShiftBuildServiceUtilsTest.java @@ -109,7 +109,7 @@ void computeS2IBuildName_withImageNameAndEmptyBuildServiceConfig_shouldReturnNam // Given final ImageName imageName = new ImageName("registry/name:tag"); // When - final String result = computeS2IBuildName(new BuildServiceConfig(), imageName); + final String result = computeS2IBuildName(imageConfiguration, new BuildServiceConfig(), imageName); // Then assertThat(result).isEqualTo("name"); } @@ -123,7 +123,7 @@ void computeS2IBuildName_withImageNameAndBuildServiceWithS2I_shouldReturnNameWit .build(); final ImageName imageName = new ImageName("registry/name:tag"); // When - final String result = computeS2IBuildName(buildServiceConfig, imageName); + final String result = computeS2IBuildName(imageConfiguration, buildServiceConfig, imageName); // Then assertThat(result).isEqualTo("name-s2i"); } @@ -133,12 +133,14 @@ void computeS2IBuildName_withImageNameAndBuildServiceWithCustomSuffix_shouldRetu // Given final BuildServiceConfig buildServiceConfig = BuildServiceConfig.builder() .jKubeBuildStrategy(JKubeBuildStrategy.s2i) - .s2iBuildNameSuffix("-custom") .buildDirectory(temporaryFolder.getAbsolutePath()) .build(); + imageConfiguration = imageConfiguration.toBuilder() + .build(imageConfiguration.getBuild().toBuilder().openshiftS2iBuildNameSuffix("-custom").build()) + .build(); final ImageName imageName = new ImageName("registry/name:tag"); // When - final String result = computeS2IBuildName(buildServiceConfig, imageName); + final String result = computeS2IBuildName(imageConfiguration, buildServiceConfig, imageName); // Then assertThat(result).isEqualTo("name-custom"); } @@ -245,7 +247,7 @@ void createBuildStrategy_withDockerBuildStrategyAndPullSecret_shouldReturnValidB @Test void createBuildOutput_withDefaults_shouldReturnImageStreamTag() { // When - final BuildOutput result = createBuildOutput(new BuildServiceConfig(), new ImageName("my-app-image")); + final BuildOutput result = createBuildOutput(imageConfiguration, new ImageName("my-app-image")); // Then assertThat(result) .extracting(BuildOutput::getTo) @@ -256,13 +258,14 @@ void createBuildOutput_withDefaults_shouldReturnImageStreamTag() { @Test void createBuildOutput_withOutputKindDockerAndPushSecret_shouldReturnDocker() { // Given - final BuildServiceConfig buildServiceConfig = BuildServiceConfig.builder() - .buildOutputKind("DockerImage") - .openshiftPushSecret("my-push-secret") - .buildDirectory(temporaryFolder.getAbsolutePath()) + imageConfiguration = imageConfiguration.toBuilder() + .build(imageConfiguration.getBuild().toBuilder() + .openshiftBuildOutputKind("DockerImage") + .openshiftPushSecret("my-push-secret") + .build()) .build(); // When - final BuildOutput result = createBuildOutput(buildServiceConfig, new ImageName("my-app-image")); + final BuildOutput result = createBuildOutput(imageConfiguration, new ImageName("my-app-image")); // Then assertThat(result) .hasFieldOrPropertyWithValue("pushSecret.name", "my-push-secret") diff --git a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildServiceIntegrationTest.java b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildServiceIntegrationTest.java index b7e8fcd7c8..aad97335fa 100644 --- a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildServiceIntegrationTest.java +++ b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildServiceIntegrationTest.java @@ -32,6 +32,7 @@ import org.eclipse.jkube.kit.build.service.docker.ArchiveService; import org.eclipse.jkube.kit.common.Assembly; import org.eclipse.jkube.kit.common.AssemblyConfiguration; +import org.eclipse.jkube.kit.common.BuildRecreateMode; import org.eclipse.jkube.kit.common.JKubeConfiguration; import org.eclipse.jkube.kit.common.JavaProject; import org.eclipse.jkube.kit.common.KitLogger; @@ -41,7 +42,6 @@ import org.eclipse.jkube.kit.config.image.ImageConfiguration; import org.eclipse.jkube.kit.config.image.build.BuildConfiguration; import org.eclipse.jkube.kit.config.image.build.JKubeBuildStrategy; -import org.eclipse.jkube.kit.config.resource.BuildRecreateMode; import org.eclipse.jkube.kit.config.resource.ContainerResourcesConfig; import org.eclipse.jkube.kit.config.resource.ResourceConfig; import org.eclipse.jkube.kit.config.service.BuildServiceConfig; @@ -102,12 +102,6 @@ class OpenshiftBuildServiceIntegrationTest { private BuildServiceConfig.BuildServiceConfigBuilder defaultConfig; - private BuildServiceConfig.BuildServiceConfigBuilder defaultConfigSecret; - - private BuildServiceConfig.BuildServiceConfigBuilder dockerImageConfigSecret; - - private BuildServiceConfig.BuildServiceConfigBuilder dockerImageConfig; - private ResourceConfig resourceConfig; @BeforeEach @@ -148,24 +142,15 @@ void init(@TempDir Path temporaryFolder) throws Exception { .name(projectName) .build(BuildConfiguration.builder() .from(projectName) + .openshiftS2iBuildNameSuffix("-s2i-suffix2") + .openshiftBuildRecreateMode(BuildRecreateMode.none) .build() ).build(); defaultConfig = BuildServiceConfig.builder() .buildDirectory(baseDir) - .buildRecreateMode(BuildRecreateMode.none) - .s2iBuildNameSuffix("-s2i-suffix2") .resourceConfig(resourceConfig) .jKubeBuildStrategy(JKubeBuildStrategy.s2i); - - defaultConfigSecret = defaultConfig.build().toBuilder().openshiftPullSecret("pullsecret-fabric8"); - - dockerImageConfig = defaultConfig.build().toBuilder().buildOutputKind("DockerImage"); - - dockerImageConfigSecret = defaultConfig.build().toBuilder() - .openshiftPullSecret("pullsecret-fabric8") - .openshiftPushSecret("pushsecret-fabric8") - .buildOutputKind("DockerImage"); } @AfterEach @@ -205,7 +190,7 @@ void build_withAssembly_shouldSucceed() throws Exception { // Given final BuildServiceConfig config = withBuildServiceConfig(defaultConfig.build()); final WebServerEventCollector collector = prepareMockServer(config, true, false, false, false); - image.setBuild(BuildConfiguration.builder() + image.setBuild(image.getBuild().toBuilder() .from(projectName) .assembly(AssemblyConfiguration.builder() .layer(Assembly.builder().id("one").build()) @@ -258,9 +243,12 @@ void build_withDockerfileModeAndAssembly_shouldSucceed() throws Exception { @Test void successfulBuildNoS2iSuffix() throws Exception { - final BuildServiceConfig config = withBuildServiceConfig(defaultConfig.s2iBuildNameSuffix(null).build()); + image = image.toBuilder() + .build(image.getBuild().toBuilder().openshiftS2iBuildNameSuffix(null).build()) + .build(); + BuildServiceConfig buildServiceConfig = withBuildServiceConfig(defaultConfig.build()); final WebServerEventCollector collector = prepareMockServer( - config, true, false, false, false); + buildServiceConfig, true, false, false, false); new OpenshiftBuildService(jKubeServiceHub).build(image); @@ -274,10 +262,13 @@ void successfulBuildNoS2iSuffix() throws Exception { @Test void dockerBuild() throws Exception { + image = image.toBuilder() + .build(image.getBuild().toBuilder() + .openshiftS2iBuildNameSuffix("-docker") + .build()) + .build(); final BuildServiceConfig dockerConfig = withBuildServiceConfig(BuildServiceConfig.builder() .buildDirectory(baseDir) - .buildRecreateMode(BuildRecreateMode.none) - .s2iBuildNameSuffix("-docker") .jKubeBuildStrategy(JKubeBuildStrategy.docker) .resourceConfig(resourceConfig).build()); final WebServerEventCollector collector = prepareMockServer(dockerConfig, true, false, false, false); @@ -295,11 +286,12 @@ void dockerBuild() throws Exception { void dockerBuildWithMultiComponentImageName() throws Exception { final BuildServiceConfig dockerConfig = withBuildServiceConfig(BuildServiceConfig.builder() .buildDirectory(baseDir) - .buildRecreateMode(BuildRecreateMode.none) - .s2iBuildNameSuffix("-docker") .jKubeBuildStrategy(JKubeBuildStrategy.docker) .resourceConfig(resourceConfig).build()); - image.setName("docker.io/registry/component1/component2/name:tag"); + image = image.toBuilder() + .name("docker.io/registry/component1/component2/name:tag") + .build(image.getBuild().toBuilder().openshiftS2iBuildNameSuffix("-docker").build()) + .build(); final WebServerEventCollector collector = prepareMockServer("component1-component2-name", dockerConfig, true, false, false, false); @@ -316,10 +308,12 @@ void dockerBuildWithMultiComponentImageName() throws Exception { void dockerBuildNoS2iSuffix() throws Exception { final BuildServiceConfig dockerConfig = withBuildServiceConfig(BuildServiceConfig.builder() .buildDirectory(baseDir) - .buildRecreateMode(BuildRecreateMode.none) .jKubeBuildStrategy(JKubeBuildStrategy.docker) .resourceConfig(resourceConfig) .build()); + image = image.toBuilder() + .build(image.getBuild().toBuilder().openshiftS2iBuildNameSuffix(null).build()) + .build(); final WebServerEventCollector collector = prepareMockServer(dockerConfig, true, false, false, false); new OpenshiftBuildService(jKubeServiceHub).build(image); @@ -335,26 +329,25 @@ void dockerBuildNoS2iSuffix() throws Exception { void dockerBuildFromExt() throws Exception { final BuildServiceConfig dockerConfig = withBuildServiceConfig(BuildServiceConfig.builder() .buildDirectory(baseDir) - .buildRecreateMode(BuildRecreateMode.none) - .s2iBuildNameSuffix("-docker") .jKubeBuildStrategy(JKubeBuildStrategy.docker) .resourceConfig(resourceConfig) .build()); - final WebServerEventCollector collector = prepareMockServer(dockerConfig, true, false, false, false); - - OpenshiftBuildService service = new OpenshiftBuildService(jKubeServiceHub); Map fromExt = ImmutableMap.of("name", "app:1.2-1", "kind", "ImageStreamTag", "namespace", "my-project"); - ImageConfiguration fromExtImage = ImageConfiguration.builder() + image = ImageConfiguration.builder() .name(projectName) .build(BuildConfiguration.builder() .fromExt(fromExt) .nocache(Boolean.TRUE) + .openshiftS2iBuildNameSuffix("-docker") .build() ).build(); + final WebServerEventCollector collector = prepareMockServer(dockerConfig, true, false, false, false); - service.build(fromExtImage); + OpenshiftBuildService service = new OpenshiftBuildService(jKubeServiceHub); + + service.build(image); assertThat(mockServer.getRequestCount()).isGreaterThan(8); collector.assertEventsRecordedInOrder("build-config-check", "new-build-config", "pushed"); @@ -365,7 +358,12 @@ void dockerBuildFromExt() throws Exception { @Test void successfulBuildSecret() throws Exception { - final BuildServiceConfig config = withBuildServiceConfig(defaultConfigSecret.build()); + final BuildServiceConfig config = withBuildServiceConfig(defaultConfig.build()); + image = image.toBuilder() + .build(image.getBuild().toBuilder() + .openshiftPullSecret("pullsecret-fabric8") + .build()) + .build(); final WebServerEventCollector collector = prepareMockServer(config, true, false, false, false); new OpenshiftBuildService(jKubeServiceHub).build(image); @@ -429,7 +427,13 @@ void successfulBuildWithResourceConfig() throws Exception { @Test void successfulDockerImageOutputBuild() throws Exception { - final BuildServiceConfig config = withBuildServiceConfig(dockerImageConfig.build()); + final BuildServiceConfig config = withBuildServiceConfig(defaultConfig.build()); + image = image.toBuilder() + .build(image.getBuild() + .toBuilder() + .openshiftBuildOutputKind("DockerImage") + .build()) + .build(); final WebServerEventCollector collector = prepareMockServer(config, true, false, false, false); new OpenshiftBuildService(jKubeServiceHub).build(image); @@ -445,7 +449,14 @@ void successfulDockerImageOutputBuild() throws Exception { @Test void successfulDockerImageOutputBuildSecret() throws Exception { - final BuildServiceConfig config = withBuildServiceConfig(dockerImageConfigSecret.build()); + final BuildServiceConfig config = withBuildServiceConfig(defaultConfig.build()); + image = image.toBuilder() + .build(image.getBuild().toBuilder() + .openshiftPullSecret("pullsecret-fabric8") + .openshiftPushSecret("pushsecret-fabric8") + .openshiftBuildOutputKind("DockerImage") + .build()) + .build(); final WebServerEventCollector collector = prepareMockServer(config, true, false, false, false); new OpenshiftBuildService(jKubeServiceHub).build(image); @@ -484,11 +495,11 @@ private WebServerEventCollector prepareMockServer( final long buildDelay = 50L; final String s2iBuildNameSuffix = Optional - .ofNullable(config.getS2iBuildNameSuffix()) + .ofNullable(image.getBuild().getOpenshiftS2iBuildNameSuffix()) .orElseGet(() -> config.getJKubeBuildStrategy() == JKubeBuildStrategy.s2i ? "-s2i" : ""); - final String buildOutputKind = Optional.ofNullable(config.getBuildOutputKind()).orElse("ImageStreamTag"); + final String buildOutputKind = Optional.ofNullable(image.getBuild().getOpenshiftBuildOutputKind()).orElse("ImageStreamTag"); BuildConfig bc = new BuildConfigBuilder() .withNewMetadata() @@ -504,7 +515,7 @@ private WebServerEventCollector prepareMockServer( .build(); BuildConfig bcSecret = null; - if (config.getOpenshiftPullSecret() != null) { + if (image.getBuild().getOpenshiftPullSecret() != null) { bcSecret = new BuildConfigBuilder() .withNewMetadata() .withName(resourceName + s2iBuildNameSuffix + "pullSecret") @@ -512,10 +523,10 @@ private WebServerEventCollector prepareMockServer( .withNewSpec() .withStrategy(new BuildStrategyBuilder().withType("Docker") .withNewDockerStrategy() - .withNewPullSecret(config.getOpenshiftPullSecret()) + .withNewPullSecret(image.getBuild().getOpenshiftPullSecret()) .endDockerStrategy().build()) .withNewOutput() - .withNewPushSecret().withName(config.getOpenshiftPushSecret()).endPushSecret() + .withNewPushSecret().withName(image.getBuild().getOpenshiftPushSecret()).endPushSecret() .withNewTo().withKind(buildOutputKind).endTo() .endOutput() .endSpec() diff --git a/jkube-kit/generator/api/src/main/java/org/eclipse/jkube/generator/api/DefaultGeneratorManager.java b/jkube-kit/generator/api/src/main/java/org/eclipse/jkube/generator/api/DefaultGeneratorManager.java index 28302bdced..66b5af8ebc 100644 --- a/jkube-kit/generator/api/src/main/java/org/eclipse/jkube/generator/api/DefaultGeneratorManager.java +++ b/jkube-kit/generator/api/src/main/java/org/eclipse/jkube/generator/api/DefaultGeneratorManager.java @@ -20,6 +20,7 @@ import java.util.Set; import java.util.stream.Collectors; +import org.apache.commons.lang3.StringUtils; import org.eclipse.jkube.kit.build.api.helper.ImageConfigResolver; import org.eclipse.jkube.kit.build.api.helper.ImageNameFormatter; import org.eclipse.jkube.kit.common.JKubeException; @@ -59,6 +60,8 @@ public List generateAndMerge(List unreso for (ImageConfiguration imageConfiguration : filteredImages) { imageConfiguration.setName(imageNameFormatter.format(imageConfiguration.getName())); if (imageConfiguration.getBuild() != null) { + BuildConfiguration updatedBuildConfig = mergeGlobalConfigParamsWithSingleImageBuildConfig(imageConfiguration.getBuild()); + imageConfiguration.setBuild(updatedBuildConfig); imageConfiguration.getBuild().initAndValidate(); } final BuildConfiguration buildConfiguration = imageConfiguration.getBuildConfiguration(); @@ -132,4 +135,31 @@ private List createUsableGeneratorList() { final List generators = pluginFactory.createServiceObjects(SERVICE_PATHS); return genCtx.getConfig().prepareProcessors(generators, "generator"); } + + private BuildConfiguration mergeGlobalConfigParamsWithSingleImageBuildConfig(BuildConfiguration build) { + BuildConfiguration.BuildConfigurationBuilder buildConfigBuilder = build.toBuilder(); + if (!build.isOpenshiftForcePull() && genCtx.isOpenshiftForcePull()) { + buildConfigBuilder.openshiftForcePull(true); + } + if (StringUtils.isBlank(build.getOpenshiftS2iBuildNameSuffix()) && + StringUtils.isNotBlank(genCtx.getOpenshiftS2iBuildNameSuffix())) { + buildConfigBuilder.openshiftS2iBuildNameSuffix(genCtx.getOpenshiftS2iBuildNameSuffix()); + } + if (!build.isOpenshiftS2iImageStreamLookupPolicyLocal() && genCtx.isOpenshiftS2iImageStreamLookupPolicyLocal()) { + buildConfigBuilder.openshiftS2iImageStreamLookupPolicyLocal(true); + } + if (StringUtils.isBlank(build.getOpenshiftPullSecret()) && StringUtils.isNotBlank(genCtx.getOpenshiftPullSecret())) { + buildConfigBuilder.openshiftPullSecret(genCtx.getOpenshiftPullSecret()); + } + if (StringUtils.isBlank(build.getOpenshiftPushSecret()) && StringUtils.isNotBlank(genCtx.getOpenshiftPushSecret())) { + buildConfigBuilder.openshiftPushSecret(genCtx.getOpenshiftPushSecret()); + } + if (StringUtils.isBlank(build.getOpenshiftBuildOutputKind()) && StringUtils.isNotBlank(genCtx.getOpenshiftBuildOutputKind())) { + buildConfigBuilder.openshiftBuildOutputKind(genCtx.getOpenshiftBuildOutputKind()); + } + if (build.getOpenshiftBuildRecreateMode() == null) { + buildConfigBuilder.openshiftBuildRecreateMode(genCtx.getOpenshiftBuildRecreate()); + } + return buildConfigBuilder.build(); + } } diff --git a/jkube-kit/generator/api/src/main/java/org/eclipse/jkube/generator/api/GeneratorContext.java b/jkube-kit/generator/api/src/main/java/org/eclipse/jkube/generator/api/GeneratorContext.java index b45c1530ce..f7890eabd0 100644 --- a/jkube-kit/generator/api/src/main/java/org/eclipse/jkube/generator/api/GeneratorContext.java +++ b/jkube-kit/generator/api/src/main/java/org/eclipse/jkube/generator/api/GeneratorContext.java @@ -19,6 +19,7 @@ import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.NoArgsConstructor; +import org.eclipse.jkube.kit.common.BuildRecreateMode; import org.eclipse.jkube.kit.common.JavaProject; import org.eclipse.jkube.kit.common.KitLogger; import org.eclipse.jkube.kit.config.image.build.JKubeBuildStrategy; @@ -50,6 +51,13 @@ public class GeneratorContext { private Date buildTimestamp; private String sourceDirectory; private String filter; + private boolean openshiftForcePull; + private String openshiftS2iBuildNameSuffix; + private boolean openshiftS2iImageStreamLookupPolicyLocal; + private String openshiftPullSecret; + private String openshiftPushSecret; + private String openshiftBuildOutputKind; + private BuildRecreateMode openshiftBuildRecreate; public GeneratorMode getGeneratorMode() { diff --git a/jkube-kit/generator/api/src/test/java/org/eclipse/jkube/generator/api/DefaultGeneratorManagerTest.java b/jkube-kit/generator/api/src/test/java/org/eclipse/jkube/generator/api/DefaultGeneratorManagerTest.java index 4a9803566c..c426840e9d 100644 --- a/jkube-kit/generator/api/src/test/java/org/eclipse/jkube/generator/api/DefaultGeneratorManagerTest.java +++ b/jkube-kit/generator/api/src/test/java/org/eclipse/jkube/generator/api/DefaultGeneratorManagerTest.java @@ -21,6 +21,7 @@ import java.util.stream.Collectors; import org.assertj.core.api.AssertionsForInterfaceTypes; +import org.eclipse.jkube.kit.common.BuildRecreateMode; import org.eclipse.jkube.kit.common.JKubeException; import org.eclipse.jkube.kit.common.JavaProject; import org.eclipse.jkube.kit.common.KitLogger; @@ -170,6 +171,136 @@ void whenDockerfileUsed_thenLogDockerfilePathAndContextDir(@TempDir File tempora verify(logger).info(eq("Using Dockerfile: %s"), anyString()); verify(logger).info(eq("Using Docker Context Directory: %s"), any(File.class)); } + + + @Nested + @DisplayName("merge configuration parameters for OpenShift S2I build into Image Build configuration") + class MergeOpenShiftS2IImageConfigParams { + @Test + @DisplayName("zero configuration, do not set anything in image build configuration") + void whenNoConfigurationProvided_thenDoNotSetInBuildConfig() { + // When + List result = generatorManager.generateAndMerge(Collections.singletonList(imageConfig)); + + // Then + assertThat(result) + .singleElement() + .extracting(ImageConfiguration::getBuild) + .hasFieldOrPropertyWithValue("openshiftForcePull", false) + .hasFieldOrPropertyWithValue("openshiftS2iBuildNameSuffix", null) + .hasFieldOrPropertyWithValue("openshiftS2iImageStreamLookupPolicyLocal", false) + .hasFieldOrPropertyWithValue("openshiftPullSecret", null) + .hasFieldOrPropertyWithValue("openshiftPushSecret", null) + .hasFieldOrPropertyWithValue("openshiftBuildOutputKind", null) + .hasFieldOrPropertyWithValue("openshiftBuildRecreateMode", null); + } + + @Test + @DisplayName("image Configuration, then fields retained in Image Configuration") + void whenProvidedInImageConfiguration_thenDoNotUpdateBuildConfig() { + // Given + imageConfig = imageConfig.toBuilder() + .build(BuildConfiguration.builder() + .openshiftForcePull(true) + .openshiftS2iBuildNameSuffix("-custom") + .openshiftS2iImageStreamLookupPolicyLocal(true) + .openshiftPushSecret("push-secret") + .openshiftPullSecret("pull-secret") + .openshiftBuildOutputKind("ImageStreamTag") + .openshiftBuildRecreateMode(BuildRecreateMode.buildConfig) + .build()) + .build(); + List images = Collections.singletonList(imageConfig); + + // When + List result = generatorManager.generateAndMerge(images); + + // Then + assertThat(result) + .singleElement() + .extracting(ImageConfiguration::getBuild) + .hasFieldOrPropertyWithValue("openshiftForcePull", true) + .hasFieldOrPropertyWithValue("openshiftS2iBuildNameSuffix", "-custom") + .hasFieldOrPropertyWithValue("openshiftS2iImageStreamLookupPolicyLocal", true) + .hasFieldOrPropertyWithValue("openshiftPullSecret", "pull-secret") + .hasFieldOrPropertyWithValue("openshiftPushSecret", "push-secret") + .hasFieldOrPropertyWithValue("openshiftBuildOutputKind", "ImageStreamTag") + .hasFieldOrPropertyWithValue("openshiftBuildRecreateMode", BuildRecreateMode.buildConfig); + } + + @Test + @DisplayName("plugin configuration, then fields merged in final Image Configuration") + void whenProvidedViaPluginConfiguration_thenSetInBuildConfig() { + // Given + generatorContext = generatorContext.toBuilder() + .openshiftForcePull(true) + .openshiftS2iBuildNameSuffix("-custom") + .openshiftS2iImageStreamLookupPolicyLocal(true) + .openshiftPullSecret("pull-secret") + .openshiftPushSecret("push-secret") + .openshiftBuildOutputKind("ImageStreamTag") + .openshiftBuildRecreate(BuildRecreateMode.buildConfig) + .build(); + List images = Collections.singletonList(imageConfig); + + // When + List result = new DefaultGeneratorManager(generatorContext).generateAndMerge(images); + + // Then + assertThat(result) + .singleElement() + .extracting(ImageConfiguration::getBuild) + .hasFieldOrPropertyWithValue("openshiftForcePull", true) + .hasFieldOrPropertyWithValue("openshiftS2iBuildNameSuffix", "-custom") + .hasFieldOrPropertyWithValue("openshiftS2iImageStreamLookupPolicyLocal", true) + .hasFieldOrPropertyWithValue("openshiftPullSecret", "pull-secret") + .hasFieldOrPropertyWithValue("openshiftPushSecret", "push-secret") + .hasFieldOrPropertyWithValue("openshiftBuildOutputKind", "ImageStreamTag") + .hasFieldOrPropertyWithValue("openshiftBuildRecreateMode", BuildRecreateMode.buildConfig); + } + + @Test + @DisplayName("plugin configuration and image configuration, then fields retained in Image Configuration") + void whenProvidedViaBothPluginAndImageConfiguration_thenDoNotModifyConfigurationSetInBuildConfig() { + // Given + imageConfig = imageConfig.toBuilder() + .build(BuildConfiguration.builder() + .openshiftForcePull(true) + .openshiftS2iBuildNameSuffix("-custom-via-image-config") + .openshiftS2iImageStreamLookupPolicyLocal(true) + .openshiftPushSecret("push-secret-via-image-config") + .openshiftPullSecret("pull-secret-via-image-config") + .openshiftBuildOutputKind("ImageStreamTag-via-image-config") + .openshiftBuildRecreateMode(BuildRecreateMode.buildConfig) + .build()) + .build(); + generatorContext = generatorContext.toBuilder() + .openshiftForcePull(true) + .openshiftS2iBuildNameSuffix("-custom") + .openshiftS2iImageStreamLookupPolicyLocal(true) + .openshiftPullSecret("pull-secret") + .openshiftPushSecret("push-secret") + .openshiftBuildOutputKind("ImageStreamTag") + .openshiftBuildRecreate(BuildRecreateMode.buildConfig) + .build(); + List images = Collections.singletonList(imageConfig); + + // When + List result = new DefaultGeneratorManager(generatorContext).generateAndMerge(images); + + // Then + assertThat(result) + .singleElement() + .extracting(ImageConfiguration::getBuild) + .hasFieldOrPropertyWithValue("openshiftForcePull", true) + .hasFieldOrPropertyWithValue("openshiftS2iBuildNameSuffix", "-custom-via-image-config") + .hasFieldOrPropertyWithValue("openshiftS2iImageStreamLookupPolicyLocal", true) + .hasFieldOrPropertyWithValue("openshiftPullSecret", "pull-secret-via-image-config") + .hasFieldOrPropertyWithValue("openshiftPushSecret", "push-secret-via-image-config") + .hasFieldOrPropertyWithValue("openshiftBuildOutputKind", "ImageStreamTag-via-image-config") + .hasFieldOrPropertyWithValue("openshiftBuildRecreateMode", BuildRecreateMode.buildConfig); + } + } } // Loaded from META-INF/jkube/generator-default diff --git a/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractDockerMojo.java b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractDockerMojo.java index 897f493532..20c0143892 100644 --- a/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractDockerMojo.java +++ b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractDockerMojo.java @@ -46,7 +46,6 @@ import org.eclipse.jkube.kit.config.access.ClusterAccess; import org.eclipse.jkube.kit.config.access.ClusterConfiguration; import org.eclipse.jkube.kit.config.image.build.RegistryAuthConfiguration; -import org.eclipse.jkube.kit.config.resource.BuildRecreateMode; import org.eclipse.jkube.kit.config.resource.PlatformMode; import org.eclipse.jkube.kit.config.resource.ProcessorConfig; import org.eclipse.jkube.kit.config.resource.ResourceConfig; @@ -544,9 +543,7 @@ private void buildAndTag(List imageConfigs) protected BuildServiceConfig.BuildServiceConfigBuilder buildServiceConfigBuilder() { return BuildServiceConfig.builder() - .buildRecreateMode(BuildRecreateMode.fromParameter(buildRecreate)) .jKubeBuildStrategy(getJKubeBuildStrategy()) - .forcePull(forcePull) .imagePullManager(ImagePullManager.createImagePullManager(imagePullPolicy, autoPull, project.getProperties())) .buildDirectory(project.getBuild().getDirectory()) .resourceConfig(resources) diff --git a/openshift-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/OpenshiftBuildMojo.java b/openshift-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/OpenshiftBuildMojo.java index 93e802476e..0eb377bbea 100644 --- a/openshift-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/OpenshiftBuildMojo.java +++ b/openshift-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/OpenshiftBuildMojo.java @@ -19,9 +19,9 @@ import org.apache.maven.plugins.annotations.Parameter; import org.apache.maven.plugins.annotations.ResolutionScope; import org.eclipse.jkube.generator.api.GeneratorContext; +import org.eclipse.jkube.kit.common.BuildRecreateMode; import org.eclipse.jkube.kit.config.image.build.JKubeBuildStrategy; import org.eclipse.jkube.kit.config.resource.RuntimeMode; -import org.eclipse.jkube.kit.config.service.BuildServiceConfig; import org.eclipse.jkube.maven.plugin.mojo.OpenShift; import static org.eclipse.jkube.kit.config.resource.RuntimeMode.KUBERNETES; @@ -88,20 +88,17 @@ protected void doExecute() throws MojoExecutionException { super.doExecute(); } - @Override - protected BuildServiceConfig.BuildServiceConfigBuilder buildServiceConfigBuilder() { - return super.buildServiceConfigBuilder() - .openshiftPullSecret(openshiftPullSecret) - .s2iBuildNameSuffix(s2iBuildNameSuffix) - .s2iImageStreamLookupPolicyLocal(s2iImageStreamLookupPolicyLocal) - .openshiftPushSecret(openshiftPushSecret) - .buildOutputKind(buildOutputKind); - } - @Override protected GeneratorContext.GeneratorContextBuilder generatorContextBuilder() { return super.generatorContextBuilder() - .strategy(getJKubeBuildStrategy()); + .strategy(getJKubeBuildStrategy()) + .openshiftForcePull(forcePull) + .openshiftS2iBuildNameSuffix(s2iBuildNameSuffix) + .openshiftS2iImageStreamLookupPolicyLocal(s2iImageStreamLookupPolicyLocal) + .openshiftPullSecret(openshiftPullSecret) + .openshiftPushSecret(openshiftPushSecret) + .openshiftBuildOutputKind(buildOutputKind) + .openshiftBuildRecreate(BuildRecreateMode.fromParameter(buildRecreate)); } @Override @@ -116,5 +113,4 @@ protected JKubeBuildStrategy getJKubeBuildStrategy() { } return JKubeBuildStrategy.s2i; } - }