Skip to content

Commit

Permalink
review:refactor: remove ClusterAccess wrapper class
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed Jul 11, 2024
1 parent 011799b commit 1868f35
Show file tree
Hide file tree
Showing 19 changed files with 54 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ImageConfiguration> resolvedImages;
Expand All @@ -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();
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public KubernetesUndeployService(JKubeServiceHub jKubeServiceHub, KitLogger logg

@Override
public void undeploy(List<File> 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<File> manifests = Stream.of(manifestFiles)
.filter(Objects::nonNull).filter(File::exists).filter(File::isFile)
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

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

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -45,16 +40,13 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@EnableKubernetesMockClient(crud = true)
class PluginServiceTest {

@TempDir
File temporaryFolder;

private KitLogger logger;
private JKubeServiceHub jKubeServiceHub;
private KubernetesMockServer mockServer;
private KubernetesClient kubernetesClient;

@BeforeEach
void setUp() {
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ public class WatcherContext {
private JKubeBuildStrategy jKubeBuildStrategy;

public String getNamespace() {
return getJKubeServiceHub().getClient().getNamespace();
return getJKubeServiceHub().getConfiguration().getClusterConfiguration().getNamespace();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -428,7 +432,7 @@ protected JKubeConfiguration initJKubeConfiguration() throws DependencyResolutio
.buildArgs(buildArgs)
.pullRegistryConfig(getRegistryConfig(pullRegistry))
.pushRegistryConfig(getRegistryConfig(pushRegistry))
.clusterConfiguration(initClusterConfiguration())
.clusterConfiguration(clusterConfiguration)
.build();
}

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

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

Expand Down Expand Up @@ -233,7 +232,7 @@ private List<ImageConfiguration> getResolvedImages(List<ImageConfiguration> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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

}
Loading

0 comments on commit 1868f35

Please sign in to comment.