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 27, 2024
1 parent 0040930 commit 688191f
Show file tree
Hide file tree
Showing 17 changed files with 436 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ private void init() {
ImageConfigResolver imageConfigResolver = new ImageConfigResolver();
try {
resolvedImages = resolveImages(imageConfigResolver);
postProcessResolvedImages();
final JKubeEnricherContext context = JKubeEnricherContext.builder()
.project(kubernetesExtension.javaProject)
.processorConfig(ProfileUtil.blendProfileWithConfiguration(ProfileUtil.ENRICHER_CONFIG,
Expand Down Expand Up @@ -220,6 +221,8 @@ protected File getManifest(KubernetesClient kc) {
return manifest;
}

protected void postProcessResolvedImages() { }

private File[] gradleFilterFiles(File[] resourceFiles) throws IOException {
if (resourceFiles == null) {
return new File[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}

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 @@ -19,6 +19,8 @@
import org.eclipse.jkube.kit.config.resource.PlatformMode;
import org.eclipse.jkube.kit.config.service.BuildServiceConfig;

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

public class OpenShiftBuildTask extends KubernetesBuildTask implements OpenShiftJKubeTask {

@Inject
Expand All @@ -31,15 +33,16 @@ 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 void postProcessResolvedImages() {
resolvedImages = mergeGlobalConfigParamsInImageConfigurations(resolvedImages, getOpenShiftExtension().getForcePullOrDefault(),
getOpenShiftExtension().getS2iBuildNameSuffixOrDefault(), getOpenShiftExtension().getS2iImageStreamLookupPolicyLocalOrDefault(), getOpenShiftExtension().getOpenshiftPullSecretOrDefault(), getOpenShiftExtension().getOpenshiftPushSecretOrDefault(), getOpenShiftExtension().getBuildOutputKindOrDefault(), getOpenShiftExtension().getBuildRecreateOrDefault());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@
package org.eclipse.jkube.gradle.plugin.task;

import java.util.Collections;
import java.util.List;

import org.assertj.core.api.AssertionsForInterfaceTypes;
import org.eclipse.jkube.gradle.plugin.OpenShiftExtension;
import org.eclipse.jkube.gradle.plugin.TestOpenShiftExtension;
import org.eclipse.jkube.kit.config.image.ImageConfiguration;
import org.eclipse.jkube.kit.config.image.build.BuildConfiguration;
import org.eclipse.jkube.kit.config.service.JKubeServiceException;
import org.eclipse.jkube.kit.config.service.openshift.OpenshiftBuildService;

import org.gradle.api.provider.Property;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -42,18 +45,15 @@ class OpenShiftBuildTaskTest {

private MockedConstruction<OpenshiftBuildService> openshiftBuildServiceMockedConstruction;

private OpenShiftExtension extension;

@BeforeEach
void setUp() {
openshiftBuildServiceMockedConstruction = mockConstruction(OpenshiftBuildService.class,
(mock, ctx) -> when(mock.isApplicable()).thenReturn(true));
final OpenShiftExtension extension = new TestOpenShiftExtension();
extension = new TestOpenShiftExtension();
when(taskEnvironment.project.getExtensions().getByType(OpenShiftExtension.class)).thenReturn(extension);
extension.images = Collections.singletonList(ImageConfiguration.builder()
.name("foo/bar:latest")
.build(BuildConfiguration.builder()
.dockerFile("Dockerfile")
.build())
.build());
extension.images = createImageConfigurationList();
}

@AfterEach
Expand All @@ -78,4 +78,94 @@ void runTask_withImageConfiguration_shouldRunBuild() throws JKubeServiceExceptio
verify(buildTask.jKubeServiceHub.getBuildService(), times(1)).build(any());
}

@Test
void postProcessImages_whenNoS2IBuildConfigProvidedViaPluginParams_thenImageConfigurationIsMergedWithDefaultValues() {
// Given
final OpenShiftBuildTask buildTask = new OpenShiftBuildTask(OpenShiftExtension.class);
buildTask.resolvedImages = createImageConfigurationList();

// When
buildTask.postProcessResolvedImages();

// Then
AssertionsForInterfaceTypes.assertThat(buildTask.resolvedImages)
.singleElement()
.extracting(ImageConfiguration::getBuild)
.hasFieldOrPropertyWithValue("openshiftForcePull", false)
.hasFieldOrPropertyWithValue("openshiftS2iBuildNameSuffix", "-s2i")
.hasFieldOrPropertyWithValue("openshiftS2iImageStreamLookupPolicyLocal", true)
.hasFieldOrPropertyWithValue("openshiftPullSecret", "pullsecret-jkube")
.hasFieldOrPropertyWithValue("openshiftPushSecret", null)
.hasFieldOrPropertyWithValue("openshiftBuildOutputKind", "ImageStreamTag")
.hasFieldOrPropertyWithValue("openshiftBuildRecreateMode", "none");
}

@Test
void postProcessImages_whenS2IBuildConfigProvidedViaPluginParams_thenReturnMergedImageConfigWithOpenShiftS2IBuildParams() {
// Given
extension = new TestOpenShiftExtension() {
@Override
public Property<String> getBuildRecreate() {
return property(String.class).value("bc");
}

@Override
public Property<String> getOpenshiftPullSecret() {
return property(String.class).value("pull-secret");
}

@Override
public Property<String> getS2iBuildNameSuffix() {
return property(String.class).value("-custom");
}

@Override
public Property<Boolean> getS2iImageStreamLookupPolicyLocal() {
return property(Boolean.class).value(true);
}

@Override
public Property<String> getBuildOutputKind() {
return property(String.class).value("DockerImage");
}

@Override
public Property<String> getOpenshiftPushSecret() {
return property(String.class).value("push-secret");
}

@Override
public Property<Boolean> getForcePull() {
return property(Boolean.class).value(true);
}
};

when(taskEnvironment.project.getExtensions().getByType(OpenShiftExtension.class)).thenReturn(extension);
final OpenShiftBuildTask buildTask = new OpenShiftBuildTask(OpenShiftExtension.class);
buildTask.resolvedImages = createImageConfigurationList();

// When
buildTask.postProcessResolvedImages();

// Then
AssertionsForInterfaceTypes.assertThat(buildTask.resolvedImages)
.singleElement()
.extracting(ImageConfiguration::getBuild)
.hasFieldOrPropertyWithValue("openshiftForcePull", true)
.hasFieldOrPropertyWithValue("openshiftS2iBuildNameSuffix", "-custom")
.hasFieldOrPropertyWithValue("openshiftS2iImageStreamLookupPolicyLocal", true)
.hasFieldOrPropertyWithValue("openshiftPullSecret", "pull-secret")
.hasFieldOrPropertyWithValue("openshiftPushSecret", "push-secret")
.hasFieldOrPropertyWithValue("openshiftBuildOutputKind", "DockerImage")
.hasFieldOrPropertyWithValue("openshiftBuildRecreateMode", "bc");
}

private static List<ImageConfiguration> createImageConfigurationList() {
return Collections.singletonList(ImageConfiguration.builder()
.name("foo/bar:latest")
.build(BuildConfiguration.builder()
.dockerFile("Dockerfile")
.build())
.build());
}
}
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
Loading

0 comments on commit 688191f

Please sign in to comment.