From f0ae12a58ffc0ec3874241169c419a25c7b886a7 Mon Sep 17 00:00:00 2001 From: Rohan Kumar Date: Tue, 19 Mar 2024 15:27:27 +0530 Subject: [PATCH] fix (jkube-kit): `k8s:watch`/`k8sWatch` should throw error in `buildpacks` build strategy Signed-off-by: Rohan Kumar --- CHANGELOG.md | 1 + .../gradle/plugin/task/KubernetesWatchTask.java | 1 + .../eclipse/jkube/watcher/api/WatcherContext.java | 2 ++ .../eclipse/jkube/watcher/api/WatcherManager.java | 5 +++++ .../jkube/watcher/api/WatcherManagerTest.java | 14 +++++++++++++- .../jkube/maven/plugin/mojo/develop/WatchMojo.java | 1 + 6 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index abccd5a162..f41b380213 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ Usage: ./scripts/extract-changelog-for-version.sh 1.3.37 5 ``` ### 1.17-SNAPSHOT +* Fix #2461: `k8s:watch`/`k8sWatch` should throw error in `buildpacks` build strategy ### 1.16.1 (2024-02-27) * Fix #2582: Moved PortMapping class from package `org.eclipse.jkube.kit.build.api.model` to `org.eclipse.jkube.kit.common` diff --git a/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/KubernetesWatchTask.java b/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/KubernetesWatchTask.java index b7c53428f4..d25d482509 100644 --- a/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/KubernetesWatchTask.java +++ b/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/KubernetesWatchTask.java @@ -83,6 +83,7 @@ private WatcherContext createWatcherContext() throws IOException { .oldPodLogger(createLogger("[[R]][OLD][[R]] ")) .useProjectClasspath(kubernetesExtension.getUseProjectClassPathOrDefault()) .jKubeServiceHub(jKubeServiceHub) + .jKubeBuildStrategy(kubernetesExtension.getBuildStrategyOrDefault()) .build(); } diff --git a/jkube-kit/watcher/api/src/main/java/org/eclipse/jkube/watcher/api/WatcherContext.java b/jkube-kit/watcher/api/src/main/java/org/eclipse/jkube/watcher/api/WatcherContext.java index a9c3b9d93c..8f9955279b 100644 --- a/jkube-kit/watcher/api/src/main/java/org/eclipse/jkube/watcher/api/WatcherContext.java +++ b/jkube-kit/watcher/api/src/main/java/org/eclipse/jkube/watcher/api/WatcherContext.java @@ -22,6 +22,7 @@ import org.eclipse.jkube.kit.build.service.docker.watch.WatchContext; import org.eclipse.jkube.kit.common.KitLogger; import org.eclipse.jkube.kit.common.JKubeConfiguration; +import org.eclipse.jkube.kit.config.image.build.JKubeBuildStrategy; import org.eclipse.jkube.kit.config.resource.ProcessorConfig; import org.eclipse.jkube.kit.config.service.JKubeServiceHub; @@ -43,6 +44,7 @@ public class WatcherContext { private WatchContext watchContext; private JKubeConfiguration buildContext; private JKubeServiceHub jKubeServiceHub; + private JKubeBuildStrategy jKubeBuildStrategy; public String getNamespace() { return getJKubeServiceHub().getClusterAccess().getNamespace(); diff --git a/jkube-kit/watcher/api/src/main/java/org/eclipse/jkube/watcher/api/WatcherManager.java b/jkube-kit/watcher/api/src/main/java/org/eclipse/jkube/watcher/api/WatcherManager.java index 01a244715e..510069e070 100644 --- a/jkube-kit/watcher/api/src/main/java/org/eclipse/jkube/watcher/api/WatcherManager.java +++ b/jkube-kit/watcher/api/src/main/java/org/eclipse/jkube/watcher/api/WatcherManager.java @@ -14,11 +14,13 @@ package org.eclipse.jkube.watcher.api; import io.fabric8.kubernetes.api.model.HasMetadata; +import org.eclipse.jkube.kit.common.JKubeException; import org.eclipse.jkube.kit.common.KitLogger; import org.eclipse.jkube.kit.common.util.ClassUtil; import org.eclipse.jkube.kit.common.util.OpenshiftHelper; import org.eclipse.jkube.kit.common.util.PluginServiceFactory; import org.eclipse.jkube.kit.config.image.ImageConfiguration; +import org.eclipse.jkube.kit.config.image.build.JKubeBuildStrategy; import org.eclipse.jkube.kit.config.resource.PlatformMode; import java.util.Collection; @@ -42,6 +44,9 @@ private WatcherManager() { public static void watch(List ret, String namespace, Collection resources, WatcherContext watcherCtx) throws Exception { + if (watcherCtx.getJKubeBuildStrategy() != null && watcherCtx.getJKubeBuildStrategy().equals(JKubeBuildStrategy.buildpacks)) { + throw new JKubeException("Watch is not supported in BuildPacks build strategy"); + } final PluginServiceFactory pluginFactory = new PluginServiceFactory<>(watcherCtx); if (watcherCtx.isUseProjectClasspath()) { pluginFactory.addAdditionalClassLoader(ClassUtil.createProjectClassLoader( diff --git a/jkube-kit/watcher/api/src/test/java/org/eclipse/jkube/watcher/api/WatcherManagerTest.java b/jkube-kit/watcher/api/src/test/java/org/eclipse/jkube/watcher/api/WatcherManagerTest.java index 5f742e2722..ebd746c74a 100644 --- a/jkube-kit/watcher/api/src/test/java/org/eclipse/jkube/watcher/api/WatcherManagerTest.java +++ b/jkube-kit/watcher/api/src/test/java/org/eclipse/jkube/watcher/api/WatcherManagerTest.java @@ -17,8 +17,10 @@ import java.util.Collections; import java.util.List; +import org.eclipse.jkube.kit.common.JKubeException; import org.eclipse.jkube.kit.common.KitLogger; import org.eclipse.jkube.kit.config.image.ImageConfiguration; +import org.eclipse.jkube.kit.config.image.build.JKubeBuildStrategy; import org.eclipse.jkube.kit.config.resource.PlatformMode; import org.eclipse.jkube.kit.config.resource.ProcessorConfig; import org.eclipse.jkube.kit.config.service.JKubeServiceHub; @@ -28,11 +30,11 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; class WatcherManagerTest { @@ -67,6 +69,16 @@ void watch_withTestWatcher_shouldMutateImages() throws Exception { verify(logger,times(1)).info("Running watcher %s", "fake-watcher"); } + @Test + void watch_whenBuildPacksBuildStrategy_thenThrowException() { + // Given + watcherContext = watcherContext.toBuilder().jKubeBuildStrategy(JKubeBuildStrategy.buildpacks).build(); + // When + assertThatExceptionOfType(JKubeException.class) + .isThrownBy(() -> WatcherManager.watch(null, null, null, watcherContext)) + .withMessage("Watch is not supported in BuildPacks build strategy"); + } + // Loaded from META-INF/jkube/watcher-default public static final class TestWatcher implements Watcher { diff --git a/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/develop/WatchMojo.java b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/develop/WatchMojo.java index 11f57dcfb4..81a5bfba7f 100644 --- a/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/develop/WatchMojo.java +++ b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/develop/WatchMojo.java @@ -121,6 +121,7 @@ private WatcherContext getWatcherContext() throws MojoExecutionException { .oldPodLogger(createLogger("[[R]][OLD][[R]] ")) .useProjectClasspath(useProjectClasspath) .jKubeServiceHub(jkubeServiceHub) + .jKubeBuildStrategy(getJKubeBuildStrategy()) .build(); } catch (DependencyResolutionRequiredException dependencyException) { throw new MojoExecutionException("Instructed to use project classpath, but cannot. Continuing build if we can: " + dependencyException.getMessage());