Skip to content

Commit

Permalink
KubernetesWatchTask Prerequisite refactor in JKube Kit
Browse files Browse the repository at this point in the history
+ SpringBootWatcher.isApplicable returns true for Spring Boot Gradle Plugin
+ JKubeArtifactResolverService also resolves artifact in Gradle Local cache
+ SpringBootUtil.getSpringBootPluginConfiguration also fetches
  configuration of Spring Boot Gradle Plugin

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia committed Nov 19, 2021
1 parent 1e3b4a7 commit fbe0d52
Show file tree
Hide file tree
Showing 6 changed files with 289 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@
*/
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;
import static junit.framework.TestCase.assertTrue;
import static org.eclipse.jkube.kit.common.util.SpringBootConfigurationHelper.SPRING_BOOT_GROUP_ID;
import static org.eclipse.jkube.kit.common.util.SpringBootConfigurationHelper.SPRING_BOOT_MAVEN_PLUGIN_ARTIFACT_ID;
import static org.eclipse.jkube.kit.common.util.SpringBootConfigurationHelper.SPRING_BOOT_GRADLE_PLUGIN_ARTIFACT_ID;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -74,4 +81,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(SPRING_BOOT_GROUP_ID)
.artifactId(SPRING_BOOT_MAVEN_PLUGIN_ARTIFACT_ID)
.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(SPRING_BOOT_GROUP_ID)
.artifactId(SPRING_BOOT_GRADLE_PLUGIN_ARTIFACT_ID)
.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());
}

}
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 @@ -15,6 +15,7 @@

import org.eclipse.jkube.kit.common.JavaProject;
import org.eclipse.jkube.kit.common.service.ArtifactResolverService;
import org.eclipse.jkube.kit.common.util.FileUtil;

import java.io.File;
import java.util.Objects;
Expand All @@ -24,21 +25,55 @@
*/
class JKubeArtifactResolverService implements ArtifactResolverService {

private JavaProject project;
private final JavaProject project;

JKubeArtifactResolverService(JavaProject project) {
this.project = Objects.requireNonNull(project, "project");
}

@Override
public File resolveArtifact(String groupId, String artifactId, String version, String type) {
String canonicalString = String.join(File.separator, groupId.split("\\.")) + File.separator + artifactId + File.separator +
version + File.separator + artifactId + "-" + version + "." + type;
File artifact = resolveArtifactInMavenCache(groupId, artifactId, version, type);
if (artifact != null && artifact.isFile()) {
return artifact;
}

artifact = resolveArtifactInGradleCache(groupId, artifactId, version, type);
if (artifact != null && artifact.isFile()) {
return artifact;
}
throw new IllegalStateException("Cannot find artifact " + getArtifactName(groupId, artifactId, type) + " within the resolved resources");
}

private File resolveArtifactInMavenCache(String groupId, String artifactId, String version, String type) {
if (project.getLocalRepositoryBaseDirectory() != null) {
return new File(project.getLocalRepositoryBaseDirectory(), canonicalString);
return new File(project.getLocalRepositoryBaseDirectory(), canonicalMavenString(groupId, artifactId, version, type));
}

return null;
}

private File resolveArtifactInGradleCache(String groupId, String artifactId, String version, String type) {
File artifactParentDir = new File(project.getLocalRepositoryBaseDirectory(), canonicalGradleString(groupId, artifactId, version));
if (artifactParentDir.isDirectory()) {
return FileUtil.listFilesAndDirsRecursivelyInDirectory(artifactParentDir).stream()
.filter(f -> f.getName().equals(getArtifactName(artifactId, version, type)))
.findFirst().orElse(null);
}
return null;
}

private String canonicalMavenString(String groupId, String artifactId, String version, String type) {
return String.join(File.separator, groupId.split("\\.")) + File.separator + artifactId + File.separator +
version + File.separator + getArtifactName(artifactId, version, type);
}

private String canonicalGradleString(String groupId, String artifactId, String version) {
return groupId + File.separator + artifactId + File.separator + version;
}

throw new IllegalStateException("Cannot find artifact " + canonicalString + " within the resolved resources");
private String getArtifactName(String artifactId, String version, String type) {
return artifactId + "-" + version + "." + type;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* 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.kit.config.service;

import org.eclipse.jkube.kit.common.JavaProject;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.Assert.assertThrows;

public class JKubeArtifactResolverServiceTest {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

@Test
public void resolveArtifact_whenArtifactPresentInGradleCache_thenReturnsArtifact() throws IOException {
// Given
File artifactParentDir = temporaryFolder.newFolder("org", "example", "test-artifact", "1.0.0");
File artifact = new File(artifactParentDir, "test-artifact-1.0.0.jar");
boolean artifactCreated = artifact.createNewFile();
JavaProject javaProject = JavaProject.builder()
.localRepositoryBaseDirectory(temporaryFolder.getRoot())
.build();
JKubeArtifactResolverService jKubeArtifactResolverService = new JKubeArtifactResolverService(javaProject);

// When
File resolveArtifact = jKubeArtifactResolverService.resolveArtifact("org.example", "test-artifact", "1.0.0", "jar");

// Then
assertThat(artifactCreated).isTrue();
assertThat(artifact).isFile().exists();
assertThat(resolveArtifact).isEqualTo(artifact);
}

@Test
public void resolveArtifact_whenArtifactPresentInMavenCache_thenReturnsArtifact() throws IOException {
// Given
File artifactParentDir = temporaryFolder.newFolder("org.example", "test-artifact", "1.0.0");
File artifact = new File(artifactParentDir, "test-artifact-1.0.0.jar");
boolean artifactCreated = artifact.createNewFile();
JavaProject javaProject = JavaProject.builder()
.localRepositoryBaseDirectory(temporaryFolder.getRoot())
.build();
JKubeArtifactResolverService jKubeArtifactResolverService = new JKubeArtifactResolverService(javaProject);

// When
File resolveArtifact = jKubeArtifactResolverService.resolveArtifact("org.example", "test-artifact", "1.0.0", "jar");

// Then
assertThat(artifactCreated).isTrue();
assertThat(artifact).isFile().exists();
assertThat(resolveArtifact).isEqualTo(artifact);
}

@Test
public void resolveArtifact_whenNoArtifactPresent_thenThrowsException() {
// Given
JavaProject javaProject = JavaProject.builder()
.localRepositoryBaseDirectory(temporaryFolder.getRoot())
.build();
JKubeArtifactResolverService jKubeArtifactResolverService = new JKubeArtifactResolverService(javaProject);

// When
IllegalStateException illegalStateException = assertThrows(IllegalStateException.class,
() -> jKubeArtifactResolverService.resolveArtifact("org.example", "test-artifact", "1.0.0", "jar"));

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

@Test
public void resolveArtifact_whenNoArtifactPresentAndNoLocalRepository_thenThrowsException() {
// Given
JKubeArtifactResolverService jKubeArtifactResolverService = new JKubeArtifactResolverService(JavaProject.builder().build());

// When
IllegalStateException illegalStateException = assertThrows(IllegalStateException.class,
() -> jKubeArtifactResolverService.resolveArtifact("org.example", "test-artifact", "1.0.0", "jar"));

// Then
assertThat(illegalStateException)
.hasMessage("Cannot find artifact org.example-test-artifact.jar within the resolved resources");
}
}
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 @@ -263,10 +264,8 @@ private File getSpringBootDevToolsJar(JavaProject project) {
}

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

0 comments on commit fbe0d52

Please sign in to comment.