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 Mar 8, 2024
1 parent 60edfc8 commit 4f2416f
Show file tree
Hide file tree
Showing 16 changed files with 571 additions and 198 deletions.
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 @@ -15,10 +15,19 @@

import javax.inject.Inject;

import org.eclipse.jkube.generator.api.DefaultGeneratorManager;
import org.eclipse.jkube.gradle.plugin.OpenShiftExtension;
import org.eclipse.jkube.kit.build.api.helper.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.api.helper.ConfigHelper.initImageConfiguration;
import static org.eclipse.jkube.kit.common.util.BuildReferenceDateUtil.getBuildTimestamp;

public class OpenShiftBuildTask extends KubernetesBuildTask implements OpenShiftJKubeTask {

@Inject
Expand All @@ -31,15 +40,29 @@ 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 {
return initImageConfiguration(
getBuildTimestamp(null, null, getOpenShiftExtension().javaProject.getBuildDirectory().getAbsolutePath(),
DOCKER_BUILD_TIMESTAMP),
getOpenShiftExtension().images, imageConfigResolver, kitLogger,
getOpenShiftExtension().getFilter().getOrNull(),
new DefaultGeneratorManager(initGeneratorContextBuilder().build()),
false,
jKubeServiceHub.getConfiguration(),
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 @@ -15,6 +15,7 @@

import org.eclipse.jkube.kit.build.api.config.handler.property.PropertyConfigHandler;
import org.eclipse.jkube.kit.build.api.config.handler.property.PropertyMode;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jkube.kit.common.JKubeConfiguration;
import org.eclipse.jkube.kit.config.image.GeneratorManager;
import org.eclipse.jkube.kit.config.image.ImageConfiguration;
Expand Down Expand Up @@ -129,6 +130,35 @@ public static boolean matchesConfiguredImages(String imageList, ImageConfigurati
return imagesAllowed.contains(imageConfig.getName()) || imagesAllowed.contains(imageConfig.getAlias());
}


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 Expand Up @@ -167,6 +197,15 @@ private static void verifyImageNames(List<ImageConfiguration> ret) {
}

public static List<ImageConfiguration> initImageConfiguration(Date buildTimeStamp, List<ImageConfiguration> unresolvedImages, ImageConfigResolver imageConfigResolver, KitLogger log, String filter, GeneratorManager generatorManager, boolean isPrePackagePhase, JKubeConfiguration jKubeConfiguration) {
return initImageConfiguration(buildTimeStamp, unresolvedImages, imageConfigResolver, log, filter, generatorManager, isPrePackagePhase, jKubeConfiguration, false, null, false, null, null, null, null);
}

public static List<ImageConfiguration> initImageConfiguration(Date buildTimeStamp, List<ImageConfiguration> unresolvedImages,
ImageConfigResolver imageConfigResolver, KitLogger log, String filter,
GeneratorManager generatorManager, boolean isPrePackagePhase,
JKubeConfiguration jKubeConfiguration, boolean isForcePull,
String s2iBuildNameSuffix, boolean s2iImageStreamLookupPolicyLocal,
String openshiftPullSecret, String openshiftPushSecret, String buildOutputKind, String buildRecreateMode) {
final ImageNameFormatter imageNameFormatter = new ImageNameFormatter(jKubeConfiguration.getProject(), buildTimeStamp);
// Resolve images
final List<ImageConfiguration> resolvedImages = ConfigHelper.resolveImages(
Expand All @@ -191,6 +230,8 @@ public static List<ImageConfiguration> initImageConfiguration(Date buildTimeStam
for (ImageConfiguration imageConfiguration : resolvedImages) {
imageConfiguration.setName(imageNameFormatter.format(imageConfiguration.getName()));
if (imageConfiguration.getBuild() != null) {
BuildConfiguration updatedBuild = mergeGlobalConfigParamsWithSingleImageBuildConfig(imageConfiguration.getBuild(), isForcePull, s2iBuildNameSuffix, s2iImageStreamLookupPolicyLocal, openshiftPullSecret, openshiftPushSecret, buildOutputKind, buildRecreateMode);
imageConfiguration.setBuild(updatedBuild);
imageConfiguration.getBuild().initAndValidate();
}
printDockerfileInfoIfDockerfileMode(imageConfiguration, log, jKubeConfiguration);
Expand Down
Loading

0 comments on commit 4f2416f

Please sign in to comment.