Skip to content

Commit

Permalink
refactor (jkube-kit/config) : Move primitive fields from BuildService…
Browse files Browse the repository at this point in the history
…Config to Image's BuildConfiguration

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Feb 22, 2024
1 parent c850332 commit 2d228ad
Show file tree
Hide file tree
Showing 15 changed files with 272 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.TaskAction;

import static org.eclipse.jkube.kit.build.service.docker.helper.ConfigHelper.mergeGlobalConfigParamsInImageConfigurations;
import static org.eclipse.jkube.kit.build.service.docker.helper.ConfigHelper.initImageConfiguration;
import static org.eclipse.jkube.kit.common.JKubeFileInterpolator.interpolate;
import static org.eclipse.jkube.kit.common.util.BuildReferenceDateUtil.getBuildTimestamp;
Expand Down Expand Up @@ -199,13 +200,15 @@ protected ProcessorConfig extractGeneratorConfig() {
}

protected List<ImageConfiguration> resolveImages(ImageConfigResolver imageConfigResolver) throws IOException {
return initImageConfiguration(
List<ImageConfiguration> imageConfigurationList = initImageConfiguration(
getBuildTimestamp(null, null, kubernetesExtension.javaProject.getBuildDirectory().getAbsolutePath(),
DOCKER_BUILD_TIMESTAMP),
kubernetesExtension.images, imageConfigResolver, kitLogger,
kubernetesExtension.getFilter().getOrNull(),
this::customizeConfig,
jKubeServiceHub.getConfiguration());
imageConfigurationList = mergeGlobalConfigParamsInImageConfigurations(imageConfigurationList, kubernetesExtension.getForcePullOrDefault(), null, false, null, null, null, null);
return imageConfigurationList;
}

protected File getManifest(KubernetesClient kc) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,16 @@
import javax.inject.Inject;

import org.eclipse.jkube.gradle.plugin.OpenShiftExtension;
import org.eclipse.jkube.kit.build.service.docker.config.handler.ImageConfigResolver;
import org.eclipse.jkube.kit.config.image.ImageConfiguration;
import org.eclipse.jkube.kit.config.resource.PlatformMode;
import org.eclipse.jkube.kit.config.service.BuildServiceConfig;

import java.io.IOException;
import java.util.List;

import static org.eclipse.jkube.kit.build.service.docker.helper.ConfigHelper.mergeGlobalConfigParamsInImageConfigurations;

public class OpenShiftBuildTask extends KubernetesBuildTask implements OpenShiftJKubeTask {

@Inject
Expand All @@ -31,15 +38,18 @@ public OpenShiftBuildTask(Class<? extends OpenShiftExtension> 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 List<ImageConfiguration> resolveImages(ImageConfigResolver imageConfigResolver) throws IOException {
List<ImageConfiguration> imageConfigurationList = super.resolveImages(imageConfigResolver);
imageConfigurationList = mergeGlobalConfigParamsInImageConfigurations(imageConfigurationList, getOpenShiftExtension().getForcePullOrDefault(),
getOpenShiftExtension().getS2iBuildNameSuffixOrDefault(), getOpenShiftExtension().getS2iImageStreamLookupPolicyLocalOrDefault(), getOpenShiftExtension().getOpenshiftPullSecretOrDefault(), getOpenShiftExtension().getOpenshiftPushSecretOrDefault(), getOpenShiftExtension().getBuildOutputKindOrDefault(), getOpenShiftExtension().getBuildRecreateOrDefault());
return imageConfigurationList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package org.eclipse.jkube.kit.build.service.docker.helper;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.jkube.kit.build.service.docker.config.handler.ImageConfigResolver;
import org.eclipse.jkube.kit.common.JKubeConfiguration;
import org.eclipse.jkube.kit.config.image.ImageConfiguration;
Expand Down Expand Up @@ -127,6 +128,48 @@ public static boolean matchesConfiguredImages(String imageList, ImageConfigurati
return imagesAllowed.contains(imageConfig.getName()) || imagesAllowed.contains(imageConfig.getAlias());
}

public static List<ImageConfiguration> mergeGlobalConfigParamsInImageConfigurations(List<ImageConfiguration> imageConfigurationList,
boolean forcePull, String s2iBuildNameSuffix, boolean s2iImageStreamLookupPolicyLocal,
String openshiftPullSecret, String openshiftPushSecret, String buildOutputKind, String buildRecreateMode) {
List<ImageConfiguration> imageConfigs = new ArrayList<>();
for (ImageConfiguration ic : imageConfigurationList) {
if (ic.getBuild() != null) {
BuildConfiguration updatedBuild = mergeGlobalConfigParamsWithSingleImageBuildConfig(ic.getBuild(), forcePull, s2iBuildNameSuffix, s2iImageStreamLookupPolicyLocal, openshiftPullSecret, openshiftPushSecret, buildOutputKind, buildRecreateMode);
ic.setBuild(updatedBuild);
imageConfigs.add(ic);
}
}
return imageConfigs;
}

private static BuildConfiguration mergeGlobalConfigParamsWithSingleImageBuildConfig(BuildConfiguration build, boolean forcePull, String s2iBuildNameSuffix, boolean s2iImageStreamLookupPolicyLocal,
String openshiftPullSecret, String openshiftPushSecret, String buildOutputKind, String buildRecreateMode) {
BuildConfiguration.BuildConfigurationBuilder buildConfigBuilder = build.toBuilder();
if (!build.isOpenshiftForcePull() && forcePull) {
buildConfigBuilder.openshiftForcePull(true);
}
if (StringUtils.isBlank(build.getOpenshiftS2iBuildNameSuffix()) &&
StringUtils.isNotBlank(s2iBuildNameSuffix)) {
buildConfigBuilder.openshiftS2iBuildNameSuffix(s2iBuildNameSuffix);
}
if (!build.isOpenshiftS2iImageStreamLookupPolicyLocal() && s2iImageStreamLookupPolicyLocal) {
buildConfigBuilder.openshiftS2iImageStreamLookupPolicyLocal(true);
}
if (StringUtils.isBlank(build.getOpenshiftPullSecret()) && StringUtils.isNotBlank(openshiftPullSecret)) {
buildConfigBuilder.openshiftPullSecret(openshiftPullSecret);
}
if (StringUtils.isBlank(build.getOpenshiftPushSecret()) && StringUtils.isNotBlank(openshiftPushSecret)) {
buildConfigBuilder.openshiftPushSecret(openshiftPushSecret);
}
if (StringUtils.isBlank(build.getOpenshiftBuildOutputKind()) && StringUtils.isNotBlank(buildOutputKind)) {
buildConfigBuilder.openshiftBuildOutputKind(buildOutputKind);
}
if (StringUtils.isBlank(build.getOpenshiftBuildRecreateMode()) && StringUtils.isNotBlank(buildRecreateMode)) {
buildConfigBuilder.openshiftBuildRecreateMode(buildRecreateMode);
}
return buildConfigBuilder.build();
}

// ===========================================================================================================

// Filter image configuration on name. Given filter should be either null (no filter) or a comma separated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,48 @@ void validateExternalPropertyActivation_withMultipleImagesWithoutExplicitExterna
.withMessage("Configuration error: Cannot use property docker.imagePropertyConfiguration on projects with multiple images without explicit image external configuration.");
}

@Test
void initAdditionalBuildConfigParameters_whenNoFieldsProvided_thenDoNotSetInBuildConfig() {
// Given
List<ImageConfiguration> imageConfigurations = Collections.singletonList(createNewDummyImageConfiguration());

// When
List<ImageConfiguration> result = ConfigHelper.mergeGlobalConfigParamsInImageConfigurations(imageConfigurations, false, null, false, null, null, null, null);

// 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
void initAdditionalBuildConfigParameters_whenFieldsProvided_thenSetInBuildConfig() {
// Given
List<ImageConfiguration> imageConfigurations = Collections.singletonList(createNewDummyImageConfiguration());

// When
List<ImageConfiguration> result = ConfigHelper.mergeGlobalConfigParamsInImageConfigurations(imageConfigurations, true, "-custom", true, "pull-secret", "push-secret", "ImageStreamTag", "buildConfig");

// 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", "buildConfig");
}

private ImageConfiguration createNewDummyImageConfiguration() {
return ImageConfiguration.builder()
.name("foo/bar:latest")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,66 @@ public class BuildConfiguration implements Serializable {
@Singular("addCacheFrom")
private List<String> cacheFrom;

/**
* 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.
* <p>
* ForcePull to override the local image and refresh it from the registry to which the image stream points.
* <p>
* This field is applicable in case of <code>s2i</code> 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
* <p>
* This field is applicable in case of OpenShift <code>s2i</code> 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.
* <p>
* This field is only applicable in case of OpenShift <code>s2i</code> 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.
* <p>
* This field is applicable in case of OpenShift <code>s2i</code> 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.
* <p>
* This field is applicable in case of OpenShift <code>s2i</code> 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.
* <p>
* This field is applicable in case of OpenShift <code>s2i</code> build strategy
*/
private String openshiftBuildOutputKind;

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

public boolean isDockerFileMode() {
return dockerFile != null || contextDir != null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,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<KubernetesListBuilder> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,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);
}
Expand Down Expand Up @@ -149,7 +149,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) {
Expand All @@ -163,16 +163,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();
}
Expand Down
Loading

0 comments on commit 2d228ad

Please sign in to comment.