Skip to content

Commit

Permalink
refactor (jkube-kit/common) : Add launderThrowable method to JKubeExc…
Browse files Browse the repository at this point in the history
…eption

+ Port KubernetesClientException.launderThrowable to JKubeException so
  that we can wrap checked exceptions inside it.
+ Use abovementioned method in ProfileUtil.blendProfileWithConfiguration

Signed-off-by: Rohan Kumar <rohaan@redhat.com>
  • Loading branch information
rohanKanojia committed Mar 5, 2024
1 parent 6818c5b commit 5865c27
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.eclipse.jkube.kit.config.access.ClusterConfiguration;
import org.eclipse.jkube.kit.config.image.GeneratorManager;
import org.eclipse.jkube.kit.config.image.ImageConfiguration;
import org.eclipse.jkube.kit.config.resource.ProcessorConfig;
import org.eclipse.jkube.kit.config.resource.ResourceConfig;
import org.eclipse.jkube.kit.config.resource.ResourceServiceConfig;
import org.eclipse.jkube.kit.config.service.JKubeServiceHub;
Expand Down Expand Up @@ -173,7 +172,7 @@ private ResourceServiceConfig initResourceServiceConfig() {

protected GeneratorContext.GeneratorContextBuilder initGeneratorContextBuilder() {
return GeneratorContext.builder()
.config(extractGeneratorConfig())
.config(ProfileUtil.blendProfileWithConfiguration(ProfileUtil.GENERATOR_CONFIG, kubernetesExtension.getProfileOrNull(), ResourceUtil.getFinalResourceDirs(kubernetesExtension.getResourceSourceDirectoryOrDefault(), kubernetesExtension.getResourceEnvironmentOrNull()), kubernetesExtension.generator))
.project(kubernetesExtension.javaProject)
.logger(kitLogger)
.runtimeMode(kubernetesExtension.getRuntimeMode())
Expand All @@ -192,14 +191,6 @@ protected final List<File> resolveResourceSourceDirectory() {
}


protected ProcessorConfig extractGeneratorConfig() {
try {
return ProfileUtil.blendProfileWithConfiguration(ProfileUtil.GENERATOR_CONFIG, kubernetesExtension.getProfileOrNull(), ResourceUtil.getFinalResourceDirs(kubernetesExtension.getResourceSourceDirectoryOrDefault(), kubernetesExtension.getResourceEnvironmentOrNull()), kubernetesExtension.generator);
} catch (IOException e) {
throw new IllegalArgumentException("Cannot extract generator config: " + e, e);
}
}

protected List<ImageConfiguration> resolveImages(ImageConfigResolver imageConfigResolver) throws IOException {
return initImageConfiguration(
getBuildTimestamp(null, null, kubernetesExtension.javaProject.getBuildDirectory().getAbsolutePath(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.eclipse.jkube.kit.build.service.docker.watch.WatchContext;
import org.eclipse.jkube.kit.common.util.KubernetesHelper;
import org.eclipse.jkube.kit.common.util.ResourceUtil;
import org.eclipse.jkube.kit.config.resource.ProcessorConfig;
import org.eclipse.jkube.kit.config.service.JKubeServiceHub;
import org.eclipse.jkube.kit.enricher.api.util.KubernetesResourceUtil;
import org.eclipse.jkube.kit.profile.ProfileUtil;
Expand Down Expand Up @@ -78,7 +77,7 @@ private WatcherContext createWatcherContext() throws IOException {
return WatcherContext.builder()
.buildContext(jKubeServiceHub.getConfiguration())
.watchContext(watchContext)
.config(extractWatcherConfig())
.config(ProfileUtil.blendProfileWithConfiguration(ProfileUtil.WATCHER_CONFIG, kubernetesExtension.getProfileOrNull(), ResourceUtil.getFinalResourceDirs(kubernetesExtension.getResourceSourceDirectoryOrDefault(), kubernetesExtension.getResourceEnvironmentOrNull()), kubernetesExtension.watcher))
.logger(kitLogger)
.newPodLogger(createLogger("[[C]][NEW][[C]] "))
.oldPodLogger(createLogger("[[R]][OLD][[R]] "))
Expand All @@ -87,14 +86,6 @@ private WatcherContext createWatcherContext() throws IOException {
.build();
}

private ProcessorConfig extractWatcherConfig() {
try {
return ProfileUtil.blendProfileWithConfiguration(ProfileUtil.WATCHER_CONFIG, kubernetesExtension.getProfileOrNull(), ResourceUtil.getFinalResourceDirs(kubernetesExtension.getResourceSourceDirectoryOrDefault(), kubernetesExtension.getResourceEnvironmentOrNull()), kubernetesExtension.watcher);
} catch (IOException e) {
throw new IllegalArgumentException("Cannot extract watcher config: " + e, e);
}
}

private WatchContext getWatchContext() throws IOException {
final DockerServiceHub hub = jKubeServiceHub.getDockerServiceHub();
return WatchContext.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,25 @@ public JKubeException(String message) {
super(message);
}

public JKubeException(String message, Throwable cause) {
super(message, cause);
}

/**
* Convert exception into an instance of JKubeException
*
* @param message message of exception
* @param cause actual cause of exception
* @return {@link RuntimeException} wrapping actual cause
*/
public static RuntimeException launderThrowable(String message, Throwable cause) {
if (cause instanceof RuntimeException) {
return ((RuntimeException) cause);

Check warning on line 35 in jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/JKubeException.java

View check run for this annotation

Codecov / codecov/patch

jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/JKubeException.java#L35

Added line #L35 was not covered by tests
} else if (cause instanceof Error) {
throw ((Error) cause);

Check warning on line 37 in jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/JKubeException.java

View check run for this annotation

Codecov / codecov/patch

jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/JKubeException.java#L37

Added line #L37 was not covered by tests
} else if (cause instanceof InterruptedException) {
Thread.currentThread().interrupt();

Check warning on line 39 in jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/JKubeException.java

View check run for this annotation

Codecov / codecov/patch

jkube-kit/common/src/main/java/org/eclipse/jkube/kit/common/JKubeException.java#L39

Added line #L39 was not covered by tests
}
throw new JKubeException(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.common;

import org.junit.jupiter.api.Test;

import java.io.IOException;

import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

class JKubeExceptionTest {
@Test
void launderThrowable_whenInvoked_shouldWrapExceptionAsJKubeException() {
// Given
IOException actualException = new IOException("I/O Error");

// When
assertThatExceptionOfType(JKubeException.class)
.isThrownBy(() -> {
throw JKubeException.launderThrowable("custom message", actualException);
})
.withMessage("custom message")
.withCause(actualException);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package org.eclipse.jkube.kit.profile;

import com.fasterxml.jackson.core.type.TypeReference;
import org.eclipse.jkube.kit.common.JKubeException;
import org.eclipse.jkube.kit.common.util.ClassUtil;
import org.eclipse.jkube.kit.common.util.Serialization;
import org.eclipse.jkube.kit.config.resource.ProcessorConfig;
Expand Down Expand Up @@ -46,7 +47,7 @@ private ProfileUtil() {}
// Default profile which will be always there
public static final String DEFAULT_PROFILE = "default";

public static Profile findProfile(String profileArg, List<File> resourceDirs) throws IOException {
public static Profile findProfile(String profileArg, List<File> resourceDirs) {
return findProfile(profileArg, resourceDirs == null ? new File[0] : resourceDirs.toArray(new File[0]));
}

Expand All @@ -59,12 +60,11 @@ public static Profile findProfile(String profileArg, List<File> resourceDirs) th
* @param profileArg the profile's name
* @param resourceDirs directories to check for profiles.
* @return the profile found or the default profile if none of this name is given
* @throws IOException if there's a problem while performing IO operations.
*/
public static Profile findProfile(String profileArg, File... resourceDirs) throws IOException {
try {
final String profile = profileArg == null ? DEFAULT_PROFILE : profileArg;
for (File resourceDir : resourceDirs) {
public static Profile findProfile(String profileArg, File... resourceDirs) {
final String profile = profileArg == null ? DEFAULT_PROFILE : profileArg;
for (File resourceDir : resourceDirs) {
try {
Profile profileFound = lookup(profile, resourceDir);
if (profileFound != null) {
if (profileFound.getParentProfile() != null) {
Expand All @@ -73,11 +73,11 @@ public static Profile findProfile(String profileArg, File... resourceDirs) throw
}
return profileFound;
}
} catch (IOException ioException) {
throw JKubeException.launderThrowable("Error in looking up profile in " + resourceDir.getAbsolutePath(), ioException);

Check warning on line 77 in jkube-kit/profile/src/main/java/org/eclipse/jkube/kit/profile/ProfileUtil.java

View check run for this annotation

Codecov / codecov/patch

jkube-kit/profile/src/main/java/org/eclipse/jkube/kit/profile/ProfileUtil.java#L76-L77

Added lines #L76 - L77 were not covered by tests
}
throw new IllegalArgumentException("No profile '" + profile + "' defined");
} catch (IOException e) {
throw new IOException("Error while looking up profile " + profileArg + ": " + e.getMessage(),e);
}
throw new IllegalArgumentException("No profile '" + profile + "' defined");
}

private static Profile inheritFromParentProfile(Profile aProfile, File resourceDir) throws IOException {
Expand All @@ -100,14 +100,13 @@ private static Profile inheritFromParentProfile(Profile aProfile, File resourceD
* @param resourceDirs resource directory where to lookup the profile (in addition to a classpath lookup)
* @return the merged configuration which can be empty if no profile is given
* @param config the provided configuration
* @throws IOException
*/
public static ProcessorConfig blendProfileWithConfiguration(ProcessorConfigurationExtractor configExtractor,
String profile,
List<File> resourceDirs,
ProcessorConfig config) throws IOException {
ProcessorConfig config) {
// Get specified profile or the default profile
ProcessorConfig profileConfig = extractProcesssorConfiguration(configExtractor, profile, resourceDirs);
ProcessorConfig profileConfig = extractProcessorConfiguration(configExtractor, profile, resourceDirs);

return ProcessorConfig.mergeProcessorConfigs(config, profileConfig);
}
Expand Down Expand Up @@ -141,9 +140,8 @@ public static Profile lookup(String name, File directory) throws IOException {
return mergeProfiles(profiles);
}

private static ProcessorConfig extractProcesssorConfiguration(ProcessorConfigurationExtractor extractor,
String profile,
List<File> resourceDirs) throws IOException {
private static ProcessorConfig extractProcessorConfiguration(ProcessorConfigurationExtractor extractor,
String profile, List<File> resourceDirs) {
Profile profileFound = findProfile(profile, resourceDirs);
return extractor.extract(profileFound);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ protected JKubeBuildStrategy getJKubeBuildStrategy() {
public EnricherContext getEnricherContext() throws DependencyResolutionRequiredException {
return JKubeEnricherContext.builder()
.project(MavenUtil.convertMavenProjectToJKubeProject(project, session))
.processorConfig(extractEnricherConfig())
.processorConfig(ProfileUtil.blendProfileWithConfiguration(ProfileUtil.ENRICHER_CONFIG, profile, ResourceUtil.getFinalResourceDirs(resourceDir, environment), enricher))

Check warning on line 601 in kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractDockerMojo.java

View check run for this annotation

Codecov / codecov/patch

kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractDockerMojo.java#L601

Added line #L601 was not covered by tests
.images(getResolvedImages())
.resources(resources)
.log(log)
Expand All @@ -608,31 +608,13 @@ public EnricherContext getEnricherContext() throws DependencyResolutionRequiredE
// Get generator context
protected GeneratorContext.GeneratorContextBuilder generatorContextBuilder() throws DependencyResolutionRequiredException {
return GeneratorContext.builder()
.config(extractGeneratorConfig())
.config(ProfileUtil.blendProfileWithConfiguration(ProfileUtil.GENERATOR_CONFIG, profile, ResourceUtil.getFinalResourceDirs(resourceDir, environment), generator))

Check warning on line 611 in kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractDockerMojo.java

View check run for this annotation

Codecov / codecov/patch

kubernetes-maven-plugin/plugin/src/main/java/org/eclipse/jkube/maven/plugin/mojo/build/AbstractDockerMojo.java#L611

Added line #L611 was not covered by tests
.project(MavenUtil.convertMavenProjectToJKubeProject(project, session))
.logger(log)
.runtimeMode(runtimeMode)
.useProjectClasspath(useProjectClasspath);
}

// Get generator config
protected ProcessorConfig extractGeneratorConfig() {
try {
return ProfileUtil.blendProfileWithConfiguration(ProfileUtil.GENERATOR_CONFIG, profile, ResourceUtil.getFinalResourceDirs(resourceDir, environment), generator);
} catch (IOException e) {
throw new IllegalArgumentException("Cannot extract generator config: " + e, e);
}
}

// Get enricher config
protected ProcessorConfig extractEnricherConfig() {
try {
return ProfileUtil.blendProfileWithConfiguration(ProfileUtil.ENRICHER_CONFIG, profile, ResourceUtil.getFinalResourceDirs(resourceDir, environment), enricher);
} catch (IOException e) {
throw new IllegalArgumentException("Cannot extract enricher config: " + e, e);
}
}

/**
* Determine whether to enable colorized log messages
* @return true if log statements should be colorized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.eclipse.jkube.kit.common.util.ResourceUtil;
import org.eclipse.jkube.kit.common.JKubeConfiguration;
import org.eclipse.jkube.kit.config.resource.ProcessorConfig;
import org.eclipse.jkube.kit.config.resource.ResourceConfig;
import org.eclipse.jkube.kit.enricher.api.util.KubernetesResourceUtil;
import org.eclipse.jkube.kit.profile.ProfileUtil;
import org.eclipse.jkube.maven.plugin.mojo.ManifestProvider;
Expand Down Expand Up @@ -116,7 +115,7 @@ private WatcherContext getWatcherContext() throws MojoExecutionException {
return WatcherContext.builder()
.buildContext(buildContext)
.watchContext(watchContext)
.config(extractWatcherConfig())
.config(ProfileUtil.blendProfileWithConfiguration(ProfileUtil.WATCHER_CONFIG, profile, ResourceUtil.getFinalResourceDirs(resourceDir, environment), watcher))
.logger(log)
.newPodLogger(createLogger("[[C]][NEW][[C]] "))
.oldPodLogger(createLogger("[[R]][OLD][[R]] "))
Expand All @@ -133,23 +132,14 @@ private WatcherContext getWatcherContext() throws MojoExecutionException {
@Override
protected GeneratorContext.GeneratorContextBuilder generatorContextBuilder() throws DependencyResolutionRequiredException {
return GeneratorContext.builder()
.config(extractGeneratorConfig())
.config(ProfileUtil.blendProfileWithConfiguration(ProfileUtil.GENERATOR_CONFIG, profile, ResourceUtil.getFinalResourceDirs(resourceDir, environment), generator))
.project(MavenUtil.convertMavenProjectToJKubeProject(project, session))
.logger(log)
.runtimeMode(getConfiguredRuntimeMode())
.useProjectClasspath(useProjectClasspath)
.generatorMode(GeneratorMode.WATCH);
}

// Get watcher config
private ProcessorConfig extractWatcherConfig() {
try {
return ProfileUtil.blendProfileWithConfiguration(ProfileUtil.WATCHER_CONFIG, profile, ResourceUtil.getFinalResourceDirs(resourceDir, environment), watcher);
} catch (IOException e) {
throw new IllegalArgumentException("Cannot extract watcher config: " + e, e);
}
}

protected KitLogger createLogger(String prefix) {
return new AnsiLogger(getLog(), useColor, verbose, !settings.getInteractiveMode(), getLogPrefix() + prefix);
}
Expand Down

0 comments on commit 5865c27

Please sign in to comment.