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 Apr 25, 2024
1 parent a54a40c commit d7c6aec
Show file tree
Hide file tree
Showing 16 changed files with 344 additions and 115 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,6 +15,7 @@

import javax.inject.Inject;

import org.eclipse.jkube.generator.api.GeneratorContext;
import org.eclipse.jkube.gradle.plugin.OpenShiftExtension;
import org.eclipse.jkube.kit.config.resource.PlatformMode;
import org.eclipse.jkube.kit.config.service.BuildServiceConfig;
Expand All @@ -31,15 +32,22 @@ 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 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(getOpenShiftExtension().getBuildRecreateOrDefault());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ public static String getExternalConfigActivationProperty(JavaProject project) {

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

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

/**
* Format an image name by replacing certain placeholders
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,71 @@ public class BuildConfiguration implements Serializable {
*/
private String buildpacksBuilderImage;

/**
* 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 @@ -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;
Expand All @@ -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<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 @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.config.resource.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;
Expand Down Expand Up @@ -138,16 +139,16 @@ 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);
} else {
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);

Expand Down Expand Up @@ -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();
Expand All @@ -213,7 +214,7 @@ protected String updateOrCreateBuildConfig(BuildServiceConfig config, OpenShiftC
BuildConfigSpec spec = OpenShiftBuildServiceUtils.getBuildConfigSpec(buildConfig);
validateSourceType(buildName, spec);

if (config.getBuildRecreateMode().isBuildConfig()) {
if (BuildRecreateMode.fromParameter(imageConfig.getBuild().getOpenshiftBuildRecreateMode()).isBuildConfig()) {
// Delete and recreate afresh
client.buildConfigs().inNamespace(applicableOpenShiftNamespace).withName(buildName).delete();
return createBuildConfig(builder, buildName, buildStrategyResource, buildOutput);
Expand All @@ -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));
}
Expand Down Expand Up @@ -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 && BuildRecreateMode.fromParameter(applicableImageConfig.getBuild().getOpenshiftBuildRecreateMode()).isImageStream()) {
client.imageStreams().inNamespace(applicableOpenShiftNamespace).withName(imageStreamName).delete();
hasImageStream = false;
}
Expand All @@ -416,7 +417,7 @@ private void checkOrCreateImageStream(BuildServiceConfig config, OpenShiftClient
.endMetadata()
.withNewSpec()
.withNewLookupPolicy()
.withLocal(config.isS2iImageStreamLookupPolicyLocal())
.withLocal(applicableImageConfig.getBuild().isOpenshiftS2iImageStreamLookupPolicyLocal())
.endLookupPolicy()
.endSpec()
.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit d7c6aec

Please sign in to comment.