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 21, 2024
1 parent c850332 commit e953f56
Show file tree
Hide file tree
Showing 15 changed files with 247 additions and 94 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);
return imageConfigurationList;
}

protected File getManifest(KubernetesClient kc) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public static BuildServiceConfig.BuildServiceConfigBuilder buildServiceConfigBui
.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 @@ -51,7 +51,6 @@ void buildServiceConfigBuilder_shouldInitializeBuildServiceConfigWithDefaults()
assertThat(buildServiceConfig)
.hasFieldOrPropertyWithValue("buildRecreateMode", BuildRecreateMode.none)
.hasFieldOrPropertyWithValue("jKubeBuildStrategy", JKubeBuildStrategy.docker)
.hasFieldOrPropertyWithValue("forcePull", false)
.hasFieldOrPropertyWithValue("buildDirectory", null);
}

Expand All @@ -70,7 +69,6 @@ void buildServiceConfigBuilder_shouldInitializeBuildServiceConfigWithConfiguredV
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());
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,45 @@ 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) {
List<ImageConfiguration> imageConfigs = new ArrayList<>();
for (ImageConfiguration ic : imageConfigurationList) {
if (ic.getBuild() != null) {
BuildConfiguration updatedBuild = mergeGlobalConfigParamsWithSingleImageBuildConfig(ic.getBuild(), forcePull, s2iBuildNameSuffix, s2iImageStreamLookupPolicyLocal, openshiftPullSecret, openshiftPushSecret, buildOutputKind);
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) {
BuildConfiguration.BuildConfigurationBuilder buildConfigBuilder = build.toBuilder();
if (!build.isForcePull() && forcePull) {
buildConfigBuilder.forcePull(true);
}
if (StringUtils.isBlank(build.getS2iBuildNameSuffix()) &&
StringUtils.isNotBlank(s2iBuildNameSuffix)) {
buildConfigBuilder.s2iBuildNameSuffix(s2iBuildNameSuffix);
}
if (!build.isS2iImageStreamLookupPolicyLocal() && s2iImageStreamLookupPolicyLocal) {
buildConfigBuilder.s2iImageStreamLookupPolicyLocal(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.getBuildOutputKind()) && StringUtils.isNotBlank(buildOutputKind)) {
buildConfigBuilder.buildOutputKind(buildOutputKind);
}
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,46 @@ 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);

// Then
assertThat(result)
.singleElement()
.extracting(ImageConfiguration::getBuild)
.hasFieldOrPropertyWithValue("forcePull", false)
.hasFieldOrPropertyWithValue("s2iBuildNameSuffix", null)
.hasFieldOrPropertyWithValue("s2iImageStreamLookupPolicyLocal", false)
.hasFieldOrPropertyWithValue("openshiftPullSecret", null)
.hasFieldOrPropertyWithValue("openshiftPushSecret", null)
.hasFieldOrPropertyWithValue("buildOutputKind", 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");

// Then
assertThat(result)
.singleElement()
.extracting(ImageConfiguration::getBuild)
.hasFieldOrPropertyWithValue("forcePull", true)
.hasFieldOrPropertyWithValue("s2iBuildNameSuffix", "-custom")
.hasFieldOrPropertyWithValue("s2iImageStreamLookupPolicyLocal", true)
.hasFieldOrPropertyWithValue("openshiftPullSecret", "pull-secret")
.hasFieldOrPropertyWithValue("openshiftPushSecret", "push-secret")
.hasFieldOrPropertyWithValue("buildOutputKind", "ImageStreamTag");
}

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,52 @@ 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>docker</code> and <code>s2i</code> build strategy
*/
private boolean forcePull;

Check warning on line 339 in jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/BuildConfiguration.java

View check run for this annotation

Codecov / codecov/patch

jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/BuildConfiguration.java#L339

Added line #L339 was not covered by tests
/**
* 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> and <code>docker</code> build strategy
*/
private String s2iBuildNameSuffix;

Check warning on line 346 in jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/BuildConfiguration.java

View check run for this annotation

Codecov / codecov/patch

jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/BuildConfiguration.java#L346

Added line #L346 was not covered by tests
/**
* 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 s2iImageStreamLookupPolicyLocal;

Check warning on line 353 in jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/BuildConfiguration.java

View check run for this annotation

Codecov / codecov/patch

jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/BuildConfiguration.java#L353

Added line #L353 was not covered by tests
/**
* 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> and <code>docker</code> build strategy
*/
private String openshiftPullSecret;

Check warning on line 360 in jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/BuildConfiguration.java

View check run for this annotation

Codecov / codecov/patch

jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/BuildConfiguration.java#L360

Added line #L360 was not covered by tests
/**
* 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> and <code>docker</code> build strategy
*/
private String openshiftPushSecret;

Check warning on line 367 in jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/BuildConfiguration.java

View check run for this annotation

Codecov / codecov/patch

jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/BuildConfiguration.java#L367

Added line #L367 was not covered by tests
/**
* 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> and <code>docker</code> build strategy
*/
private String buildOutputKind;

Check warning on line 375 in jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/BuildConfiguration.java

View check run for this annotation

Codecov / codecov/patch

jkube-kit/config/image/src/main/java/org/eclipse/jkube/kit/config/image/build/BuildConfiguration.java#L375

Added line #L375 was not covered by tests

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,14 @@
@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().getS2iBuildNameSuffix())) {
s2IBuildName.append(imageConfiguration.getBuild().getS2iBuildNameSuffix());
} 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().isForcePull())
.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().getBuildOutputKind()).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 e953f56

Please sign in to comment.