From 1868f35aafb872aae337b5b09d81878f2cce655a Mon Sep 17 00:00:00 2001 From: Marc Nuri Date: Wed, 3 Jul 2024 15:45:35 +0200 Subject: [PATCH] review:refactor: remove ClusterAccess wrapper class Signed-off-by: Marc Nuri --- .../gradle/plugin/task/AbstractJKubeTask.java | 8 ++-- .../plugin/task/KubernetesApplyTask.java | 2 +- .../plugin/task/KubernetesWatchTask.java | 2 +- .../plugin/task/KubernetesApplyTaskTest.java | 1 - .../plugin/task/KubernetesWatchTaskTest.java | 1 - .../kubernetes/KubernetesClientUtil.java | 14 +++---- .../kubernetes/KubernetesUndeployService.java | 2 +- .../openshift/OpenshiftBuildService.java | 2 +- .../kubernetes/KubernetesClientUtilTest.java | 10 +++-- .../OpenshiftBuildServiceIntegrationTest.java | 2 + .../service/plugins/PluginServiceTest.java | 9 ----- .../jkube/watcher/api/WatcherContext.java | 2 +- .../plugin/mojo/build/AbstractDockerMojo.java | 6 ++- .../plugin/mojo/build/AbstractJKubeMojo.java | 6 ++- .../maven/plugin/mojo/build/ApplyMojo.java | 3 +- .../maven/plugin/mojo/build/ResourceMojo.java | 3 +- .../maven/plugin/mojo/develop/WatchMojo.java | 2 +- .../plugin/mojo/build/ApplyMojoTest.java | 9 ++++- .../plugin/mojo/develop/WatchMojoTest.java | 38 ++++++------------- 19 files changed, 54 insertions(+), 68 deletions(-) diff --git a/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/AbstractJKubeTask.java b/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/AbstractJKubeTask.java index fa7cc55c4a..4fb3612216 100644 --- a/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/AbstractJKubeTask.java +++ b/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/AbstractJKubeTask.java @@ -22,7 +22,6 @@ import java.util.Optional; import io.fabric8.kubernetes.client.KubernetesClient; -import io.fabric8.kubernetes.client.KubernetesClientBuilder; import org.apache.commons.lang3.StringUtils; import org.eclipse.jkube.generator.api.DefaultGeneratorManager; import org.eclipse.jkube.generator.api.GeneratorContext; @@ -57,6 +56,7 @@ public abstract class AbstractJKubeTask extends DefaultTask implements Kubernete protected final KubernetesExtension kubernetesExtension; protected KitLogger kitLogger; + protected ClusterConfiguration clusterConfiguration; protected JKubeServiceHub jKubeServiceHub; protected static final String DOCKER_BUILD_TIMESTAMP = "docker/build.timestamp"; protected List resolvedImages; @@ -78,8 +78,8 @@ public final void runTask() { protected void init() { kubernetesExtension.javaProject = GradleUtil.convertGradleProject(getProject()); - kubernetesExtension.access = initClusterConfiguration(); kitLogger = createLogger(null); + clusterConfiguration = initClusterConfiguration(); jKubeServiceHub = initJKubeServiceHubBuilder().build(); kubernetesExtension.resources = updateResourceConfigNamespace(kubernetesExtension.getNamespaceOrNull(), kubernetesExtension.resources); resolvedImages = resolveImages(); @@ -141,7 +141,7 @@ protected JKubeServiceHub.JKubeServiceHubBuilder initJKubeServiceHubBuilder() { .registry(kubernetesExtension.getPushRegistryOrNull() != null ? kubernetesExtension.getPushRegistryOrNull() : kubernetesExtension.getRegistryOrDefault()) .build()) - .clusterConfiguration(kubernetesExtension.access) + .clusterConfiguration(clusterConfiguration) .build()) .offline(kubernetesExtension.getOfflineOrDefault()) .platformMode(kubernetesExtension.getRuntimeMode()) @@ -175,7 +175,7 @@ protected GeneratorContext.GeneratorContextBuilder initGeneratorContextBuilder() .prePackagePhase(false) .useProjectClasspath(kubernetesExtension.getUseProjectClassPathOrDefault()) .sourceDirectory(kubernetesExtension.getBuildSourceDirectoryOrDefault()) - .openshiftNamespace(StringUtils.isNotBlank(kubernetesExtension.getNamespaceOrNull()) ? kubernetesExtension.getNamespaceOrNull() : new KubernetesClientBuilder().withConfig(kubernetesExtension.access.getConfig()).build().getNamespace()) + .openshiftNamespace(StringUtils.isNotBlank(kubernetesExtension.getNamespaceOrNull()) ? kubernetesExtension.getNamespaceOrNull() : clusterConfiguration.getNamespace()) .buildTimestamp(getBuildTimestamp(null, null, kubernetesExtension.javaProject.getBuildDirectory().getAbsolutePath(), DOCKER_BUILD_TIMESTAMP)) .filter(kubernetesExtension.getFilterOrNull()); diff --git a/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/KubernetesApplyTask.java b/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/KubernetesApplyTask.java index 98839d4389..4f565a6c50 100644 --- a/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/KubernetesApplyTask.java +++ b/gradle-plugin/kubernetes/src/main/java/org/eclipse/jkube/gradle/plugin/task/KubernetesApplyTask.java @@ -92,6 +92,6 @@ protected void configureApplyService() { applyService.setRollingUpgradePreserveScale(kubernetesExtension.getRollingUpgradePreserveScaleOrDefault()); applyService.setRecreateMode(kubernetesExtension.getRecreateOrDefault()); applyService.setNamespace(kubernetesExtension.getNamespaceOrNull()); - applyService.setFallbackNamespace(resolveFallbackNamespace(kubernetesExtension.resources, jKubeServiceHub.getClient())); + applyService.setFallbackNamespace(resolveFallbackNamespace(kubernetesExtension.resources, clusterConfiguration)); } } 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 20383a32ad..efdd17f437 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 @@ -60,7 +60,7 @@ public void run() { WatcherContext context = createWatcherContext(); WatcherManager.watch(resolvedImages, - applicableNamespace(null, kubernetesExtension.getNamespaceOrNull(), kubernetesExtension.resources, kubernetesClient), + applicableNamespace(null, kubernetesExtension.getNamespaceOrNull(), kubernetesExtension.resources, clusterConfiguration), resources, context); } catch (KubernetesClientException kubernetesClientException) { diff --git a/gradle-plugin/kubernetes/src/test/java/org/eclipse/jkube/gradle/plugin/task/KubernetesApplyTaskTest.java b/gradle-plugin/kubernetes/src/test/java/org/eclipse/jkube/gradle/plugin/task/KubernetesApplyTaskTest.java index 05e7819dbb..9db143c800 100644 --- a/gradle-plugin/kubernetes/src/test/java/org/eclipse/jkube/gradle/plugin/task/KubernetesApplyTaskTest.java +++ b/gradle-plugin/kubernetes/src/test/java/org/eclipse/jkube/gradle/plugin/task/KubernetesApplyTaskTest.java @@ -17,7 +17,6 @@ import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; -import io.fabric8.openshift.client.OpenShiftClient; import org.eclipse.jkube.gradle.plugin.KubernetesExtension; import org.eclipse.jkube.gradle.plugin.TestKubernetesExtension; import org.eclipse.jkube.kit.common.access.ClusterConfiguration; diff --git a/gradle-plugin/kubernetes/src/test/java/org/eclipse/jkube/gradle/plugin/task/KubernetesWatchTaskTest.java b/gradle-plugin/kubernetes/src/test/java/org/eclipse/jkube/gradle/plugin/task/KubernetesWatchTaskTest.java index 25e7cdc829..2890d2dcd8 100644 --- a/gradle-plugin/kubernetes/src/test/java/org/eclipse/jkube/gradle/plugin/task/KubernetesWatchTaskTest.java +++ b/gradle-plugin/kubernetes/src/test/java/org/eclipse/jkube/gradle/plugin/task/KubernetesWatchTaskTest.java @@ -35,7 +35,6 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mockConstruction; import static org.mockito.Mockito.mockStatic; diff --git a/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/kubernetes/KubernetesClientUtil.java b/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/kubernetes/KubernetesClientUtil.java index 3be43efc84..b778fa8fd5 100644 --- a/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/kubernetes/KubernetesClientUtil.java +++ b/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/kubernetes/KubernetesClientUtil.java @@ -22,12 +22,11 @@ import java.util.concurrent.TimeUnit; import io.fabric8.kubernetes.api.model.GenericKubernetesResource; -import io.fabric8.kubernetes.client.Client; -import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.NamespacedKubernetesClient; import io.fabric8.kubernetes.client.dsl.Resource; import io.fabric8.kubernetes.client.dsl.ScalableResource; import org.eclipse.jkube.kit.common.KitLogger; +import org.eclipse.jkube.kit.common.access.ClusterConfiguration; import org.eclipse.jkube.kit.common.util.KubernetesHelper; import org.eclipse.jkube.kit.common.util.OpenshiftHelper; import org.eclipse.jkube.kit.config.image.ImageName; @@ -172,8 +171,8 @@ public static void doDeleteAndWait(KubernetesClient kubernetesClient, GenericKub crClient.waitUntilCondition(Objects::isNull, seconds, TimeUnit.SECONDS); } - public static String applicableNamespace(HasMetadata resource, String namespace, ResourceConfig resourceConfig, KubernetesClient kubernetesClient) { - return applicableNamespace(resource, namespace, resolveFallbackNamespace(resourceConfig, kubernetesClient)); + public static String applicableNamespace(HasMetadata resource, String namespace, ResourceConfig resourceConfig, ClusterConfiguration clusterConfiguration) { + return applicableNamespace(resource, namespace, resolveFallbackNamespace(resourceConfig, clusterConfiguration)); } public static String applicableNamespace(HasMetadata resource, String namespace, String fallbackNamespace) { @@ -186,12 +185,11 @@ public static String applicableNamespace(HasMetadata resource, String namespace, return StringUtils.isNotBlank(fallbackNamespace) ? fallbackNamespace : KubernetesHelper.getDefaultNamespace(); } - public static String resolveFallbackNamespace(ResourceConfig resourceConfig, KubernetesClient kubernetesClient) { + public static String resolveFallbackNamespace(ResourceConfig resourceConfig, ClusterConfiguration clusterConfiguration) { return Optional.ofNullable(resourceConfig) .map(ResourceConfig::getNamespace) - .orElse(Optional.ofNullable(kubernetesClient) - .map(Client::getConfiguration) - .map(Config::getNamespace) + .orElse(Optional.ofNullable(clusterConfiguration) + .map(ClusterConfiguration::getNamespace) .orElse(null)); } diff --git a/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/kubernetes/KubernetesUndeployService.java b/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/kubernetes/KubernetesUndeployService.java index 5aa2fea029..6933eb70da 100644 --- a/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/kubernetes/KubernetesUndeployService.java +++ b/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/kubernetes/KubernetesUndeployService.java @@ -51,7 +51,7 @@ public KubernetesUndeployService(JKubeServiceHub jKubeServiceHub, KitLogger logg @Override public void undeploy(List resourceDirs, ResourceConfig resourceConfig, File... manifestFiles) throws IOException { - final String fallbackNamespace = KubernetesClientUtil.resolveFallbackNamespace(resourceConfig, jKubeServiceHub.getClient()); + final String fallbackNamespace = KubernetesClientUtil.resolveFallbackNamespace(resourceConfig, jKubeServiceHub.getConfiguration().getClusterConfiguration()); final List manifests = Stream.of(manifestFiles) .filter(Objects::nonNull).filter(File::exists).filter(File::isFile) .collect(Collectors.toList()); diff --git a/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildService.java b/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildService.java index 5eb28ee859..ddc5b9e8a6 100644 --- a/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildService.java +++ b/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildService.java @@ -251,7 +251,7 @@ private void initClient() { if (buildServiceConfig.getResourceConfig() != null && buildServiceConfig.getResourceConfig().getNamespace() != null) { applicableOpenShiftNamespace = buildServiceConfig.getResourceConfig().getNamespace(); } else { - applicableOpenShiftNamespace = jKubeServiceHub.getClient().getNamespace(); + applicableOpenShiftNamespace = jKubeServiceHub.getConfiguration().getClusterConfiguration().getNamespace(); } } diff --git a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/kubernetes/KubernetesClientUtilTest.java b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/kubernetes/KubernetesClientUtilTest.java index 6230901dd4..e8c04f9c0f 100644 --- a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/kubernetes/KubernetesClientUtilTest.java +++ b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/kubernetes/KubernetesClientUtilTest.java @@ -22,6 +22,7 @@ import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; import io.fabric8.kubernetes.client.Watcher; +import org.eclipse.jkube.kit.common.access.ClusterConfiguration; import org.eclipse.jkube.kit.config.resource.ResourceConfig; import org.junit.jupiter.api.Test; @@ -89,9 +90,9 @@ void applicableNamespace_whenNamespaceInResourceMetadata_shouldReturnProvidedNam @Test void applicableNamespace_whenNamespaceProvidedViaKubernetesClient_shouldReturnProvidedNamespace() { - // Given // When - String resolvedNamespace = KubernetesClientUtil.applicableNamespace(null, null, null, kubernetesClient); + String resolvedNamespace = KubernetesClientUtil.applicableNamespace(null, null, null, + ClusterConfiguration.from(kubernetesClient.getConfiguration()).build()); // Then assertThat(resolvedNamespace).isEqualTo(kubernetesClient.getNamespace()); @@ -123,13 +124,14 @@ void resolveFallbackNamespace_whenNamespaceProvidedViaResourceConfiguration_shou @Test void resolveFallbackNamespace_whenNamespaceProvidedViaKubernetesClient_shouldReturnProvidedNamespace() { - // Given // When - String resolvedNamespace = KubernetesClientUtil.resolveFallbackNamespace(null, kubernetesClient); + String resolvedNamespace = KubernetesClientUtil + .resolveFallbackNamespace(null, ClusterConfiguration.from(kubernetesClient.getConfiguration()).build()); // Then assertThat(resolvedNamespace).isEqualTo(kubernetesClient.getNamespace()); } + @Test void getPodStatusMessagePostfix_whenActionIsDeleted_shouldReturnPodDeletedMessage() { // Given diff --git a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildServiceIntegrationTest.java b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildServiceIntegrationTest.java index def6bfb197..a7ab792aba 100644 --- a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildServiceIntegrationTest.java +++ b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/openshift/OpenshiftBuildServiceIntegrationTest.java @@ -37,6 +37,7 @@ import org.eclipse.jkube.kit.common.JavaProject; import org.eclipse.jkube.kit.common.KitLogger; import org.eclipse.jkube.kit.common.RegistryConfig; +import org.eclipse.jkube.kit.common.access.ClusterConfiguration; import org.eclipse.jkube.kit.common.archive.ArchiveCompression; import org.eclipse.jkube.kit.common.util.Serialization; import org.eclipse.jkube.kit.config.image.ImageConfiguration; @@ -136,6 +137,7 @@ void init(@TempDir Path temporaryFolder) throws Exception { .buildDirectory(target) .build()) .pullRegistryConfig(RegistryConfig.builder().build()) + .clusterConfiguration(ClusterConfiguration.from(client.getConfiguration()).build()) .build()); image = ImageConfiguration.builder() diff --git a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/plugins/PluginServiceTest.java b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/plugins/PluginServiceTest.java index 5e9c4af7b5..095ba1223d 100644 --- a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/plugins/PluginServiceTest.java +++ b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/plugins/PluginServiceTest.java @@ -13,16 +13,11 @@ */ package org.eclipse.jkube.kit.config.service.plugins; - -import io.fabric8.kubernetes.client.KubernetesClient; -import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; -import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer; import org.apache.commons.io.FileUtils; import org.eclipse.jkube.api.JKubePlugin; import org.eclipse.jkube.kit.common.JKubeConfiguration; import org.eclipse.jkube.kit.common.JavaProject; import org.eclipse.jkube.kit.common.KitLogger; -import org.eclipse.jkube.kit.common.access.ClusterConfiguration; import org.eclipse.jkube.kit.common.util.LazyBuilder; import org.eclipse.jkube.kit.config.resource.ResourceServiceConfig; import org.eclipse.jkube.kit.config.resource.RuntimeMode; @@ -45,7 +40,6 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -@EnableKubernetesMockClient(crud = true) class PluginServiceTest { @TempDir @@ -53,8 +47,6 @@ class PluginServiceTest { private KitLogger logger; private JKubeServiceHub jKubeServiceHub; - private KubernetesMockServer mockServer; - private KubernetesClient kubernetesClient; @BeforeEach void setUp() { @@ -64,7 +56,6 @@ void setUp() { .project(JavaProject.builder() .outputDirectory(temporaryFolder) .build()) - .clusterConfiguration(ClusterConfiguration.from(kubernetesClient.getConfiguration()).build()) .build(), new BuildServiceConfig(), new ResourceServiceConfig(), new LazyBuilder<>(hub -> null), true); } 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 b17555dc66..c74182b93d 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 @@ -47,6 +47,6 @@ public class WatcherContext { private JKubeBuildStrategy jKubeBuildStrategy; public String getNamespace() { - return getJKubeServiceHub().getClient().getNamespace(); + return getJKubeServiceHub().getConfiguration().getClusterConfiguration().getNamespace(); } } diff --git a/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractDockerMojo.java b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractDockerMojo.java index 04dff97aca..b417e0619b 100644 --- a/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractDockerMojo.java +++ b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractDockerMojo.java @@ -310,6 +310,9 @@ public abstract class AbstractDockerMojo extends AbstractMojo protected KitLogger log; + // Resolved Cluster Configuration + protected ClusterConfiguration clusterConfiguration; + // The JKube service hub protected JKubeServiceHub jkubeServiceHub; @@ -371,6 +374,7 @@ public final void execute() throws MojoExecutionException, MojoFailureException protected void init() { log = new AnsiLogger(getLog(), useColorForLogging(), verbose, !settings.getInteractiveMode(), getLogPrefix()); authConfigFactory = new DockerAuthConfigFactory(log); + clusterConfiguration = initClusterConfiguration(); runtimeMode = getConfiguredRuntimeMode(); } @@ -428,7 +432,7 @@ protected JKubeConfiguration initJKubeConfiguration() throws DependencyResolutio .buildArgs(buildArgs) .pullRegistryConfig(getRegistryConfig(pullRegistry)) .pushRegistryConfig(getRegistryConfig(pushRegistry)) - .clusterConfiguration(initClusterConfiguration()) + .clusterConfiguration(clusterConfiguration) .build(); } diff --git a/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractJKubeMojo.java b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractJKubeMojo.java index 1392c2673f..afd154865f 100644 --- a/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractJKubeMojo.java +++ b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractJKubeMojo.java @@ -144,6 +144,8 @@ public abstract class AbstractJKubeMojo extends AbstractMojo implements KitLogge protected KitLogger log; + protected ClusterConfiguration clusterConfiguration; + // The JKube service hub protected JKubeServiceHub jkubeServiceHub; @@ -161,7 +163,7 @@ public void execute() throws MojoExecutionException, MojoFailureException { protected void init() throws MojoFailureException { log = createLogger(null); - access = initClusterConfiguration(); + clusterConfiguration = initClusterConfiguration(); try { javaProject = MavenUtil.convertMavenProjectToJKubeProject(project, session); } catch (DependencyResolutionRequiredException e) { @@ -240,7 +242,7 @@ protected JKubeServiceHub.JKubeServiceHubBuilder initJKubeServiceHubBuilder(Java .settings(MavenUtil.getRegistryServerFromMavenSettings(settings)) .passwordDecryptionMethod(this::decrypt) .build()) - .clusterConfiguration(access) + .clusterConfiguration(clusterConfiguration) .build()) .offline(offline) .platformMode(getRuntimeMode()) diff --git a/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/ApplyMojo.java b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/ApplyMojo.java index 0856b7933b..e6d220650f 100644 --- a/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/ApplyMojo.java +++ b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/ApplyMojo.java @@ -18,7 +18,6 @@ import java.util.Collection; import java.util.List; -import io.fabric8.kubernetes.client.KubernetesClientBuilder; import org.eclipse.jkube.kit.common.util.KubernetesHelper; import org.eclipse.jkube.kit.common.util.MavenUtil; import org.eclipse.jkube.kit.common.util.OpenshiftHelper; @@ -227,7 +226,7 @@ private void configureApplyService(KubernetesClient kubernetes) { applyService.setRollingUpgradePreserveScale(isRollingUpgradePreserveScale()); applyService.setRecreateMode(recreate); applyService.setNamespace(namespace); - applyService.setFallbackNamespace(resolveFallbackNamespace(resources, jkubeServiceHub.getClient())); + applyService.setFallbackNamespace(resolveFallbackNamespace(resources, clusterConfiguration)); boolean openShift = OpenshiftHelper.isOpenShift(kubernetes); if (openShift) { diff --git a/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/ResourceMojo.java b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/ResourceMojo.java index fb4de251b3..5fab601cdf 100644 --- a/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/ResourceMojo.java +++ b/kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/ResourceMojo.java @@ -20,7 +20,6 @@ import javax.validation.ConstraintViolationException; -import io.fabric8.kubernetes.client.KubernetesClientBuilder; import org.apache.commons.lang3.StringUtils; import org.eclipse.jkube.generator.api.GeneratorContext; @@ -233,7 +232,7 @@ private List getResolvedImages(List imag .useProjectClasspath(useProjectClasspath) .strategy(JKubeBuildStrategy.docker) .prePackagePhase(true) - .openshiftNamespace(StringUtils.isNotBlank(this.namespace) ? this.namespace: new KubernetesClientBuilder().withConfig(access.getConfig()).build().getNamespace()) + .openshiftNamespace(StringUtils.isNotBlank(this.namespace) ? this.namespace: clusterConfiguration.getNamespace()) .buildTimestamp(getBuildTimestamp(getPluginContext(), CONTEXT_KEY_BUILD_TIMESTAMP, project.getBuild().getDirectory(), DOCKER_BUILD_TIMESTAMP)) .build()); return generatorManager.generateAndMerge(images); 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 fa7cbadcfa..107d8f7c19 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 @@ -95,7 +95,7 @@ public void executeInternal() throws MojoExecutionException { WatcherContext context = getWatcherContext(); WatcherManager.watch(getResolvedImages(), - applicableNamespace(null, namespace, resources, kubernetesClient), + applicableNamespace(null, namespace, resources, clusterConfiguration), appliedK8sResources, context); diff --git a/kubernetes-maven-plugin/plugin/src/test/java/org/eclipse/jkube/maven/plugin/mojo/build/ApplyMojoTest.java b/kubernetes-maven-plugin/plugin/src/test/java/org/eclipse/jkube/maven/plugin/mojo/build/ApplyMojoTest.java index 021a9a3d98..0c154b5394 100644 --- a/kubernetes-maven-plugin/plugin/src/test/java/org/eclipse/jkube/maven/plugin/mojo/build/ApplyMojoTest.java +++ b/kubernetes-maven-plugin/plugin/src/test/java/org/eclipse/jkube/maven/plugin/mojo/build/ApplyMojoTest.java @@ -19,6 +19,7 @@ import java.nio.file.Path; import java.util.Properties; +import io.fabric8.kubernetes.client.ConfigBuilder; import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; import org.eclipse.jkube.kit.common.access.ClusterConfiguration; @@ -53,7 +54,11 @@ void setUp(@TempDir Path temporaryFolder) throws IOException { when(mavenProject.getProperties()).thenReturn(new Properties()); // @formatter:off applyMojo = new ApplyMojo() {{ - access = ClusterConfiguration.from(defaultKubernetesClient.getConfiguration()).build(); + access = ClusterConfiguration.from( + new ConfigBuilder(defaultKubernetesClient.getConfiguration()) + .withNamespace("kubernetes-client-config-namespace") + .build() + ).build(); project = mavenProject; settings = mock(Settings.class); kubernetesManifest = kubernetesManifestFile; @@ -122,7 +127,7 @@ void resolveEffectiveNamespace_whenNoNamespaceConfigured() throws MojoExecutionE // Then assertThat(applyMojo.applyService) .hasFieldOrPropertyWithValue("namespace", null) - .hasFieldOrPropertyWithValue("fallbackNamespace", defaultKubernetesClient.getNamespace()); + .hasFieldOrPropertyWithValue("fallbackNamespace", "kubernetes-client-config-namespace"); } } diff --git a/kubernetes-maven-plugin/plugin/src/test/java/org/eclipse/jkube/maven/plugin/mojo/develop/WatchMojoTest.java b/kubernetes-maven-plugin/plugin/src/test/java/org/eclipse/jkube/maven/plugin/mojo/develop/WatchMojoTest.java index 8511bbb84c..2d5ad62f29 100644 --- a/kubernetes-maven-plugin/plugin/src/test/java/org/eclipse/jkube/maven/plugin/mojo/develop/WatchMojoTest.java +++ b/kubernetes-maven-plugin/plugin/src/test/java/org/eclipse/jkube/maven/plugin/mojo/develop/WatchMojoTest.java @@ -17,16 +17,14 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.Collections; -import java.util.HashMap; import java.util.Properties; +import io.fabric8.kubernetes.client.ConfigBuilder; import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient; -import org.eclipse.jkube.kit.common.JKubeConfiguration; -import org.eclipse.jkube.kit.common.JavaProject; +import org.eclipse.jkube.kit.common.access.ClusterConfiguration; import org.eclipse.jkube.kit.config.image.build.JKubeBuildStrategy; import org.eclipse.jkube.kit.config.resource.ResourceConfig; -import org.eclipse.jkube.kit.config.service.JKubeServiceHub; import org.eclipse.jkube.watcher.api.WatcherManager; import org.apache.maven.project.MavenProject; @@ -35,43 +33,29 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; -import org.mockito.MockedConstruction; import org.mockito.MockedStatic; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.mockConstruction; import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.times; import static org.mockito.Mockito.when; -import static org.mockito.Mockito.withSettings; @EnableKubernetesMockClient(crud = true) class WatchMojoTest { - private File kubernetesManifestFile; + private KubernetesClient kubernetesClient; + private File kubernetesManifestFile; private MavenProject mavenProject; - private Settings mavenSettings; private MockedStatic watcherManagerMockedStatic; - private MockedConstruction jKubeServiceHubMockedConstruction; private TestWatchMojo watchMojo; - private KubernetesClient kubernetesClient; @BeforeEach void setUp(@TempDir Path temporaryFolder) throws Exception { final Path srcDir = Files.createDirectory(temporaryFolder.resolve("src")); final File targetDir = Files.createDirectory(temporaryFolder.resolve("target")).toFile(); - jKubeServiceHubMockedConstruction = mockConstruction(JKubeServiceHub.class, - withSettings().defaultAnswer(RETURNS_DEEP_STUBS), (mock, context) -> { - when(mock.getClient()).thenReturn(kubernetesClient); - when(mock.getConfiguration()).thenReturn(JKubeConfiguration.builder() - .project(JavaProject.builder() - .baseDirectory(temporaryFolder.toFile()) - .build()) - .build()); - }); kubernetesManifestFile = Files.createTempFile(srcDir, "kubernetes", ".yml").toFile(); mavenProject = mock(MavenProject.class, RETURNS_DEEP_STUBS); when(mavenProject.getProperties()).thenReturn(new Properties()); @@ -81,16 +65,19 @@ void setUp(@TempDir Path temporaryFolder) throws Exception { when(mavenProject.getBasedir()).thenReturn(temporaryFolder.toFile()); when(mavenProject.getBuild().getDirectory()).thenReturn(targetDir.getAbsolutePath()); when(mavenProject.getBuild().getOutputDirectory()).thenReturn(targetDir.getAbsolutePath()); - mavenSettings = mock(Settings.class); watcherManagerMockedStatic = mockStatic(WatcherManager.class); // @formatter:off watchMojo = new TestWatchMojo() {{ project = mavenProject; - settings = mavenSettings; + settings = mock(Settings.class); kubernetesManifest = kubernetesManifestFile; resourceDir = temporaryFolder.resolve("src").resolve("main").resolve("jkube").toFile().getAbsoluteFile(); buildStrategy = JKubeBuildStrategy.jib; - setPluginContext(new HashMap<>()); + access = ClusterConfiguration.from( + new ConfigBuilder(kubernetesClient.getConfiguration()) + .withNamespace("kubernetes-client-config-namespace") + .build() + ).build(); }}; // @formatter:on } @@ -98,15 +85,14 @@ void setUp(@TempDir Path temporaryFolder) throws Exception { @AfterEach void tearDown() { watcherManagerMockedStatic.close(); - jKubeServiceHubMockedConstruction.close(); } @Test - void executeInternal_whenNoNamespaceConfigured_shouldDelegateToWatcherManagerWithKubernetesClientNamespace() throws Exception { + void executeInternal_whenNoNamespaceConfigured_shouldDelegateToWatcherManagerWithClusterConfigurationNamespace() throws Exception { // When watchMojo.execute(); // Then - watcherManagerMockedStatic.verify(() -> WatcherManager.watch(any(), eq(kubernetesClient.getNamespace()), any(), any()), times(1)); + watcherManagerMockedStatic.verify(() -> WatcherManager.watch(any(), eq("kubernetes-client-config-namespace"), any(), any()), times(1)); } @Test