Skip to content

Commit

Permalink
refactor (jkube-kit) : Move Dockerfile opinionated ImageConfig logic …
Browse files Browse the repository at this point in the history
…from `ConfigHelper.initImageConfiguration` to a dedicated generator (#2802)

* refactor (jkube-kit) : Move Dockerfile opinionated ImageConfig logic from `ConfigHelper.initImageConfiguration` to a dedicated generator

+ ConfigHelper modifies the provided set of ImageConfigurations when it
  detects a `Dockerfile` in project root directory. This is actually a
  feature called Simple Dockerfile Mode. However, this mutation of
  ImageConfiguration from within `initImageConfiguration` does not look
  appropriate. It can be moved to a dedicated generator that will get
  activated when `Dockerfile` is present in project base directory.
+ Add a new generator SimpleDockerfileGenerator, it adds a simple
  ImageConfiguration with Dockerfile configured in build and adds
  it to existing set of ImageConfigurations.
+ Remove check from BaseGenerator to skip generation if it's simple Dockerfile mode
  Move it to SimpleDockerfileGenerator.isApplicable instead

* review: IT for gradle and maven plugin precedence
  • Loading branch information
rohanKanojia authored Apr 11, 2024
1 parent 8c5e126 commit 5931629
Show file tree
Hide file tree
Showing 46 changed files with 1,131 additions and 270 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
# Copyright (c) 2019 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at:
#
# https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
#

FROM busybox:latest
EXPOSE 9080
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
plugins {
id 'org.eclipse.jkube.kubernetes' version "${jKubeVersion}"
id 'org.springframework.boot' version '2.7.17'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
id 'java'
}

group = 'org.eclipse.jkube.integration.tests.gradle'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
mavenCentral()
}

def extensionConfig = {
offline = true
}

kubernetes(extensionConfig)
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
# Copyright (c) 2019 Red Hat, Inc.
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at:
#
# https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Red Hat, Inc. - initial API and implementation
#

FROM busybox:latest
EXPOSE 9080
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
plugins {
id 'org.eclipse.jkube.openshift' version "${jKubeVersion}"
id 'org.springframework.boot' version '2.7.17'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
id 'java'
}

group = 'org.eclipse.jkube.integration.tests.gradle'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
mavenCentral()
}

def extensionConfig = {
offline = true
}

openshift(extensionConfig)
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.gradle.plugin.tests;

import org.gradle.testkit.runner.BuildResult;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.io.File;
import java.util.stream.Collectors;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

class DockerfileSimpleExtensionPrecedenceIT {


@RegisterExtension
private final ITGradleRunnerExtension gradleRunner = new ITGradleRunnerExtension();

@Test
void k8sResource_whenRun_generatesK8sManifests() {
// Remove kubernetes-gradle-plugin from classpath to avoid using its profiles-default.yml
gradleRunner.withPluginClassPath(gradleRunner.pluginClassPath().stream()
.filter(f -> !f.getAbsolutePath().contains("gradle-plugin" + File.separator + "openshift"))
.collect(Collectors.toList())
);
// When
final BuildResult result = gradleRunner
.withITProject("dockerfile-simple-generator-precedence-kubernetes")
.withArguments("k8sResource", "--stacktrace")
.build();
// Then
assertThat(result).extracting(BuildResult::getOutput).asString()
.contains("Running generator dockerfile-simple");
}

@Test
void ocResource_whenRun_generatesOpenShiftManifests() {
// When
final BuildResult result = gradleRunner
.withITProject("dockerfile-simple-generator-precedence-openshift")
.withArguments("ocResource")
.build();
// Then
assertThat(result).extracting(BuildResult::getOutput).asString()
.contains("Running generator dockerfile-simple");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.net.URI;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ITGradleRunnerExtension implements BeforeEachCallback, AfterEachCallback {
Expand Down Expand Up @@ -58,6 +59,15 @@ public ITGradleRunnerExtension withArguments(String... originalArguments) {
return this;
}

public List<? extends File> pluginClassPath() {
return gradleRunner.getPluginClasspath();
}

public ITGradleRunnerExtension withPluginClassPath(Iterable<? extends File> pluginClassPath) {
gradleRunner = gradleRunner.withPluginClasspath(pluginClassPath);
return this;
}

public File resolveFile(String... relativePaths) {
Path path = gradleRunner.getProjectDir().toPath();
for (String rp : relativePaths) {
Expand Down
7 changes: 7 additions & 0 deletions gradle-plugin/kubernetes/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@
<artifactId>jkube-kit-generator-java-exec</artifactId>
<version>${jkube.kit.version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.jkube</groupId>
<artifactId>jkube-kit-generator-dockerfile-simple</artifactId>
<version>${jkube.kit.version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.jkube</groupId>
<artifactId>jkube-kit-generator-karaf</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
generator:
# The order given in "includes" is the order in which generators are called
includes:
- dockerfile-simple
- quarkus
- spring-boot
- thorntail-v2
Expand Down Expand Up @@ -118,6 +119,7 @@
generator:
# The order given in "includes" is the order in which generators are called
includes:
- dockerfile-simple
- spring-boot
- thorntail-v2
- wildfly-jar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class TestKubernetesExtension extends KubernetesExtension {
public String buildRecreate;
public Boolean isForcePull;
public Boolean isFailOnNoKubernetesJson;
public Boolean isSkipPush;

public TestKubernetesExtension() {
javaProject = mock(JavaProject.class, RETURNS_DEEP_STUBS);
Expand Down Expand Up @@ -300,7 +301,7 @@ public Property<Integer> getServiceUrlWaitTimeSeconds() {

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

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,5 @@ public Property<Boolean> getSkipBuild() {
assertThat(dockerBuildServiceMockedConstruction.constructed()).isEmpty();
verify(buildTask.jKubeServiceHub.getBuildService(), times(0)).build(any());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
generator:
# The order given in "includes" is the order in which generators are called
includes:
- dockerfile-simple
- quarkus
- spring-boot
- thorntail-v2
Expand Down Expand Up @@ -119,6 +120,7 @@
generator:
# The order given in "includes" is the order in which generators are called
includes:
- dockerfile-simple
- spring-boot
- thorntail-v2
- wildfly-jar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.common.util.JKubeProjectUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
Expand All @@ -33,12 +32,6 @@
import java.util.Set;
import java.util.stream.Collectors;

import static org.eclipse.jkube.kit.build.api.helper.DockerFileUtil.addSimpleDockerfileConfig;
import static org.eclipse.jkube.kit.build.api.helper.DockerFileUtil.createSimpleDockerfileConfig;
import static org.eclipse.jkube.kit.build.api.helper.DockerFileUtil.getTopLevelDockerfile;
import static org.eclipse.jkube.kit.build.api.helper.DockerFileUtil.isSimpleDockerFileMode;
import static org.eclipse.jkube.kit.common.util.PropertiesUtil.getValueFromProperties;

/**
* Utility class which helps in resolving, customizing, initializing and validating
* image configuration.
Expand Down Expand Up @@ -174,16 +167,6 @@ public static List<ImageConfiguration> initImageConfiguration(Date buildTimeStam
filter, // A filter which image to process
generatorManager); // GeneratorManager which will invoke generator

// Check for simple Dockerfile mode
ImageConfiguration dockerFileImageConfig = createImageConfigurationForSimpleDockerfile(resolvedImages, jKubeConfiguration, imageNameFormatter);
if (dockerFileImageConfig != null) {
if (resolvedImages.isEmpty()) {
resolvedImages.add(dockerFileImageConfig);
} else {
resolvedImages.set(0, dockerFileImageConfig);
}
}

// Init and validate Image configurations. After this step, getResolvedImages() contains the valid configuration.
for (ImageConfiguration imageConfiguration : resolvedImages) {
imageConfiguration.setName(imageNameFormatter.format(imageConfiguration.getName()));
Expand All @@ -196,20 +179,6 @@ public static List<ImageConfiguration> initImageConfiguration(Date buildTimeStam
return resolvedImages;
}

private static ImageConfiguration createImageConfigurationForSimpleDockerfile(List<ImageConfiguration> resolvedImages, JKubeConfiguration jKubeConfiguration, ImageNameFormatter imageNameFormatter) {
if (isSimpleDockerFileMode(jKubeConfiguration.getBasedir())) {
File topDockerfile = getTopLevelDockerfile(jKubeConfiguration.getBasedir());
String defaultImageName = imageNameFormatter.format(getValueFromProperties(jKubeConfiguration.getProject().getProperties(),
"jkube.image.name", "jkube.generator.name"));
if (resolvedImages.isEmpty()) {
return createSimpleDockerfileConfig(topDockerfile, defaultImageName);
} else if (resolvedImages.size() == 1 && resolvedImages.get(0).getBuildConfiguration() == null) {
return addSimpleDockerfileConfig(resolvedImages.get(0), topDockerfile);
}
}
return null;
}

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

private static void printDockerfileInfoIfDockerfileMode(ImageConfiguration imageConfiguration, KitLogger log, JKubeConfiguration jKubeConfiguration) {
Expand Down
Loading

0 comments on commit 5931629

Please sign in to comment.