Skip to content

Commit

Permalink
fix (jkube-kit): k8s:watch/k8sWatch should throw error in `buildp…
Browse files Browse the repository at this point in the history
…acks` build strategy

Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia authored and manusa committed Mar 25, 2024
1 parent e76fe55 commit f0ae12a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ private WatcherContext createWatcherContext() throws IOException {
.oldPodLogger(createLogger("[[R]][OLD][[R]] "))
.useProjectClasspath(kubernetesExtension.getUseProjectClassPathOrDefault())
.jKubeServiceHub(jKubeServiceHub)
.jKubeBuildStrategy(kubernetesExtension.getBuildStrategyOrDefault())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,6 +44,9 @@ private WatcherManager() {
public static void watch(List<ImageConfiguration> ret, String namespace, Collection<HasMetadata> 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<WatcherContext> pluginFactory = new PluginServiceFactory<>(watcherCtx);
if (watcherCtx.isUseProjectClasspath()) {
pluginFactory.addAdditionalClassLoader(ClassUtil.createProjectClassLoader(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit f0ae12a

Please sign in to comment.