Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KubernetesWatchTask Prerequisite refactor in JKube Kit #1127

Merged
merged 1 commit into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ protected GeneratorContext.GeneratorContextBuilder initGeneratorContextBuilder()
.project(kubernetesExtension.javaProject)
.logger(kitLogger)
.runtimeMode(kubernetesExtension.getRuntimeMode())
.useProjectClasspath(kubernetesExtension.getUseProjectClassPathOrDefault())
.artifactResolver(jKubeServiceHub.getArtifactResolverService());
.useProjectClasspath(kubernetesExtension.getUseProjectClassPathOrDefault());
}

protected ClusterConfiguration initClusterConfiguration() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public static List<Dependency> getDependencies(MavenProject project) {
return project.getDependencies().stream()
.map(d -> Dependency.builder()
.groupId(d.getGroupId()).artifactId(d.getArtifactId()).version(d.getVersion()).type(d.getType())
.scope(d.getScope()).build())
.scope(d.getScope()).file(getArtifactFileFromArtifactMap(project, d)).build())
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -418,5 +418,13 @@ public static File getRootProjectFolder(MavenProject project) {
}
return answer;
}

private static File getArtifactFileFromArtifactMap(MavenProject mavenProject, org.apache.maven.model.Dependency dependency) {
Artifact artifact = mavenProject.getArtifactMap().get(dependency.getGroupId() + ":" + dependency.getArtifactId());
if (artifact != null) {
return artifact.getFile();
}
return null;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@
*/
package org.eclipse.jkube.kit.common.util;

import java.util.Collections;
import java.util.Map;
import java.util.Properties;

import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.Plugin;
import org.junit.Test;

import static junit.framework.TestCase.assertNull;
Expand Down Expand Up @@ -74,4 +78,42 @@ public void testMultipleProfilesParsing() {
assertNull(props.get("spring.application.name"));
}

@Test
public void getSpringBootPluginConfiguration_whenSpringBootMavenPluginPresent_thenReturnsPluginConfiguration() {
// Given
JavaProject javaProject = JavaProject.builder()
.plugin(Plugin.builder()
.groupId("org.springframework.boot")
.artifactId("spring-boot-maven-plugin")
.configuration(Collections.singletonMap("layout", "ZIP"))
.build())
.build();

// When
Map<String, Object> configuration = SpringBootUtil.getSpringBootPluginConfiguration(javaProject);

// Then
assertNotNull(configuration);
assertEquals("ZIP", configuration.get("layout").toString());
}

@Test
public void getSpringBootPluginConfiguration_whenSpringBootGradlePluginPresent_thenReturnsPluginConfiguration() {
// Given
JavaProject javaProject = JavaProject.builder()
.plugin(Plugin.builder()
.groupId("org.springframework.boot")
.artifactId("org.springframework.boot.gradle.plugin")
.configuration(Collections.singletonMap("mainClass", "com.example.ExampleApplication"))
.build())
.build();

// When
Map<String, Object> configuration = SpringBootUtil.getSpringBootPluginConfiguration(javaProject);

// Then
assertNotNull(configuration);
assertEquals("com.example.ExampleApplication", configuration.get("mainClass").toString());
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,19 @@ public static String getProperty(String key, JavaProject project) {
}
return value;
}

public static File resolveArtifact(JavaProject project, String groupId, String artifactId, String version, String type) {
File artifact = project.getDependencies().stream()
.filter(d -> d.getGroupId().equals(groupId)
&& d.getArtifactId().equals(artifactId)
&& d.getVersion().equals(version)
&& d.getType().equals(type))
.findFirst()
.map(Dependency::getFile)
.orElse(null);
if (artifact == null) {
throw new IllegalStateException("Cannot find artifact " + String.format("%s-%s.%s", artifactId, version, type) + " within the resolved resources");
}
return artifact;
}
manusa marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@

import java.net.URL;
import java.net.URLClassLoader;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;

import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.Plugin;

import static org.eclipse.jkube.kit.common.util.PropertiesUtil.getPropertiesFromResource;

Expand Down Expand Up @@ -88,5 +91,17 @@ public static String getSpringBootActiveProfile(JavaProject project) {
}
return null;
}

public static Map<String, Object> getSpringBootPluginConfiguration(JavaProject javaProject) {
Plugin mavenPlugin = JKubeProjectUtil.getPlugin(javaProject, SpringBootConfigurationHelper.SPRING_BOOT_MAVEN_PLUGIN_ARTIFACT_ID);
if (mavenPlugin != null) {
return mavenPlugin.getConfiguration();
}
Plugin gradlePlugin = JKubeProjectUtil.getPlugin(javaProject, SpringBootConfigurationHelper.SPRING_BOOT_GRADLE_PLUGIN_ARTIFACT_ID);
if (gradlePlugin != null) {
return gradlePlugin.getConfiguration();
}
return Collections.emptyMap();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.eclipse.jkube.kit.common.Dependency;
import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.SystemMock;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand Down Expand Up @@ -199,4 +200,38 @@ public void getProperty_whenProjectPropertyPresent_returnsProjectProperty() {
// Then
assertThat(result).isEqualTo("true");
}

@Test
public void resolveArtifact_whenArtifactPresent_shouldReturnArtifact() {
// Given
File actualArtifact = new File(temporaryFolder.getRoot(), "test-artifact-0.0.1.jar");
JavaProject javaProject = JavaProject.builder()
.dependency(Dependency.builder()
.groupId("org.example")
.artifactId("test-artifact")
.version("0.0.1")
.type("jar")
.file(actualArtifact)
.build())
.build();

// When
File resolvedArtifact = JKubeProjectUtil.resolveArtifact(javaProject, "org.example", "test-artifact", "0.0.1", "jar");

// Then
assertThat(resolvedArtifact).isNotNull().isEqualTo(actualArtifact);
}

@Test
public void resolveArtifact_whenNoArtifactPresent_shouldThrowException() {
// Given
JavaProject javaProject = JavaProject.builder().build();

// When
IllegalStateException illegalStateException = Assert.assertThrows(IllegalStateException.class,
() -> JKubeProjectUtil.resolveArtifact(javaProject, "org.example", "test-artifact", "0.0.1", "jar"));

// Then
assertThat(illegalStateException).hasMessage("Cannot find artifact test-artifact-0.0.1.jar within the resolved resources");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.eclipse.jkube.kit.common.service.MigrateService;
import org.eclipse.jkube.kit.build.service.docker.ServiceHub;
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.common.service.ArtifactResolverService;
import org.eclipse.jkube.kit.common.util.LazyBuilder;
import org.eclipse.jkube.kit.config.access.ClusterAccess;
import org.eclipse.jkube.kit.config.access.ClusterConfiguration;
Expand Down Expand Up @@ -58,7 +57,6 @@ public class JKubeServiceHub implements Closeable {
private BuildServiceConfig buildServiceConfig;
@Getter
private KubernetesClient client;
private LazyBuilder<ArtifactResolverService> artifactResolverService;
private LazyBuilder<BuildServiceManager> buildServiceManager;
private LazyBuilder<ResourceService> resourceService;
private LazyBuilder<PortForwardService> portForwardService;
Expand Down Expand Up @@ -104,10 +102,6 @@ public RuntimeMode getRuntimeMode() {
return platformMode;
}

public ArtifactResolverService getArtifactResolverService() {
return artifactResolverService.get();
}

public BuildService getBuildService() {
return buildServiceManager.get().resolveBuildService();
}
Expand Down Expand Up @@ -148,7 +142,6 @@ private void initClusterAccessAndLazyBuilders() {
}
this.client = clusterAccess.createDefaultClient();
}
artifactResolverService = new LazyBuilder<>(() -> new JKubeArtifactResolverService(configuration.getProject()));
buildServiceManager = new LazyBuilder<>(() -> new BuildServiceManager(this));
applyService = new LazyBuilder<>(() -> {
validateIfConnectedToCluster();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,6 @@ public void testGetBuildServiceInOpenShift() {
.isInstanceOf(OpenshiftBuildService.class);
}

@Test
public void testGetArtifactResolverService() {
JKubeServiceHub hub = commonInit()
.platformMode(RuntimeMode.KUBERNETES)
.build();

assertThat(hub.getArtifactResolverService()).isNotNull();
}

@Test
public void testGetJibBuildServiceInKubernetes() {
// Given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import lombok.NoArgsConstructor;
import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.common.service.ArtifactResolverService;
import org.eclipse.jkube.kit.config.image.build.JKubeBuildStrategy;
import org.eclipse.jkube.kit.config.resource.RuntimeMode;
import org.eclipse.jkube.kit.config.resource.ProcessorConfig;
Expand All @@ -45,7 +44,6 @@ public class GeneratorContext {

private boolean useProjectClasspath;
private boolean prePackagePhase;
private ArtifactResolverService artifactResolver;

private GeneratorMode generatorMode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,7 @@ private boolean isSpringBootRepackage() {
private File getSpringBootDevToolsJar() {
String version = SpringBootUtil.getSpringBootDevToolsVersion(getProject())
.orElseThrow(() -> new IllegalStateException("Unable to find the spring-boot version"));
final File devToolsJar = getContext().getArtifactResolver()
.resolveArtifact(SPRING_BOOT_GROUP_ID, SPRING_BOOT_DEVTOOLS_ARTIFACT_ID, version, "jar");
final File devToolsJar = JKubeProjectUtil.resolveArtifact(getProject(), SPRING_BOOT_GROUP_ID, SPRING_BOOT_DEVTOOLS_ARTIFACT_ID, version, "jar");
if (!devToolsJar.exists()) {
throw new IllegalArgumentException("devtools need to be included in repacked archive, please set <excludeDevtools> to false in plugin configuration");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.apache.commons.lang3.StringUtils;

import static org.eclipse.jkube.kit.common.util.SpringBootConfigurationHelper.DEV_TOOLS_REMOTE_SECRET;
import static org.eclipse.jkube.kit.common.util.SpringBootUtil.getSpringBootPluginConfiguration;

public class SpringBootWatcher extends BaseWatcher {

Expand All @@ -79,8 +80,8 @@ public SpringBootWatcher(WatcherContext watcherContext) {

@Override
public boolean isApplicable(List<ImageConfiguration> configs, Collection<HasMetadata> resources, PlatformMode mode) {
return JKubeProjectUtil.hasPluginOfAnyArtifactId(getContext().getBuildContext().getProject(),
SpringBootConfigurationHelper.SPRING_BOOT_MAVEN_PLUGIN_ARTIFACT_ID);
return JKubeProjectUtil.hasPluginOfAnyArtifactId(getContext().getBuildContext().getProject(), SpringBootConfigurationHelper.SPRING_BOOT_MAVEN_PLUGIN_ARTIFACT_ID) ||
JKubeProjectUtil.hasPluginOfAnyArtifactId(getContext().getBuildContext().getProject(), SpringBootConfigurationHelper.SPRING_BOOT_GRADLE_PLUGIN_ARTIFACT_ID);
}

@Override
Expand Down Expand Up @@ -259,14 +260,12 @@ public void run() {

private File getSpringBootDevToolsJar(JavaProject project) {
String version = SpringBootUtil.getSpringBootDevToolsVersion(project).orElseThrow(() -> new IllegalStateException("Unable to find the spring-boot version"));
return getContext().getJKubeServiceHub().getArtifactResolverService().resolveArtifact(SpringBootConfigurationHelper.SPRING_BOOT_GROUP_ID, SpringBootConfigurationHelper.SPRING_BOOT_DEVTOOLS_ARTIFACT_ID, version, "jar");
return JKubeProjectUtil.resolveArtifact(getContext().getBuildContext().getProject(), SpringBootConfigurationHelper.SPRING_BOOT_GROUP_ID, SpringBootConfigurationHelper.SPRING_BOOT_DEVTOOLS_ARTIFACT_ID, version, "jar");
}

private String validateSpringBootDevtoolsSettings() {
final Map<String, Object> configuration = JKubeProjectUtil
.getPlugin(getContext().getBuildContext().getProject(), SpringBootConfigurationHelper.SPRING_BOOT_MAVEN_PLUGIN_ARTIFACT_ID)
.getConfiguration();
if(!configuration.containsKey("excludeDevtools") || !configuration.get("excludeDevtools").equals("false")) {
final Map<String, Object> configuration = getSpringBootPluginConfiguration(getContext().getBuildContext().getProject());
if(configuration != null && (!configuration.containsKey("excludeDevtools") || !configuration.get("excludeDevtools").equals("false"))) {
log.warn("devtools need to be included in repacked archive, please set <excludeDevtools> to false in plugin configuration");
throw new IllegalStateException("devtools needs to be included in fat jar");
}
Expand Down
Loading