Skip to content

Commit

Permalink
refactor : Use SummaryService as injected object rather than static util
Browse files Browse the repository at this point in the history
Signed-off-by: Rohan Kumar <[email protected]>
  • Loading branch information
rohanKanojia committed Oct 18, 2022
1 parent 297d4ea commit 78c26b6
Show file tree
Hide file tree
Showing 127 changed files with 655 additions and 517 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.eclipse.jkube.kit.common.JKubeConfiguration;
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.common.RegistryConfig;
import org.eclipse.jkube.kit.common.util.SummaryUtil;
import org.eclipse.jkube.kit.common.util.ResourceUtil;
import org.eclipse.jkube.kit.config.access.ClusterAccess;
import org.eclipse.jkube.kit.config.access.ClusterConfiguration;
Expand Down Expand Up @@ -67,19 +66,30 @@ protected AbstractJKubeTask(Class<? extends KubernetesExtension> extensionClass)

@TaskAction
public final void runTask() {
init();
if (shouldSkip()) {
try {
init();
if (shouldSkip()) {
kitLogger.info("`%s` task is skipped.", this.getName());
return;
}
run();
} finally {
printSummaryIfLastExecutingTask();
}
run();
}

protected void printSummaryIfLastExecutingTask() {
String lastExecutingTask = GradleUtil.getLastExecutingTask(getProject(), getTaskPrioritiesMap());
if (lastExecutingTask != null && lastExecutingTask.equals(getName())) {
SummaryUtil.printSummary(kubernetesExtension.javaProject.getBaseDirectory(), kubernetesExtension.getSummaryEnabledOrDefault());
SummaryUtil.clear();
printSummary();
}
}

protected void printSummary() {
jKubeServiceHub.getSummaryService().printSummary(kubernetesExtension.javaProject.getBaseDirectory());
jKubeServiceHub.getSummaryService().clear();
}

private void init() {
kubernetesExtension.javaProject = GradleUtil.convertGradleProject(getProject());
kitLogger = createLogger(null);
Expand All @@ -89,8 +99,7 @@ private void init() {
jKubeServiceHub = initJKubeServiceHubBuilder().build();
kubernetesExtension.resources = updateResourceConfigNamespace(kubernetesExtension.getNamespaceOrNull(), kubernetesExtension.resources);
ImageConfigResolver imageConfigResolver = new ImageConfigResolver();
SummaryUtil.initSummary(kubernetesExtension.javaProject.getBuildDirectory(), kitLogger);
SummaryUtil.setSuccessful(true);
jKubeServiceHub.getSummaryService().setSuccessful(true);
try {
resolvedImages = resolveImages(imageConfigResolver);
final JKubeEnricherContext context = JKubeEnricherContext.builder()
Expand All @@ -103,6 +112,7 @@ private void init() {
.resources(kubernetesExtension.resources)
.log(kitLogger)
.jKubeBuildStrategy(kubernetesExtension.getBuildStrategyOrDefault())
.summaryService(jKubeServiceHub.getSummaryService())
.build();
final List<String> extraClasspathElements = kubernetesExtension.getUseProjectClassPathOrDefault() ?
kubernetesExtension.javaProject.getCompileClassPathElements() : Collections.emptyList();
Expand All @@ -123,7 +133,7 @@ public KubernetesExtension getExtension() {
}

private List<ImageConfiguration> customizeConfig(List<ImageConfiguration> configs) {
return GeneratorManager.generate(configs, initGeneratorContextBuilder().build(), false);
return GeneratorManager.generate(configs, initGeneratorContextBuilder().build(), false, jKubeServiceHub.getSummaryService());
}

private boolean isAnsiEnabled() {
Expand Down Expand Up @@ -153,6 +163,7 @@ protected JKubeServiceHub.JKubeServiceHubBuilder initJKubeServiceHubBuilder() {
.build())
.clusterAccess(clusterAccess)
.offline(kubernetesExtension.getOfflineOrDefault())
.summaryEnabled(kubernetesExtension.getSummaryEnabledOrDefault())
.platformMode(kubernetesExtension.getRuntimeMode());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.common.util.KubernetesHelper;
import org.eclipse.jkube.kit.common.util.OpenshiftHelper;
import org.eclipse.jkube.kit.common.util.SummaryUtil;
import org.eclipse.jkube.kit.config.service.ApplyService;
import org.eclipse.jkube.kit.enricher.api.util.KubernetesResourceUtil;

Expand Down Expand Up @@ -63,15 +62,19 @@ public void run() {
kitLogger.info("[[B]]HINT:[[B]] Use the command `%s get pods -w` to watch your pods start up",
clusterAccess.isOpenShift() ? "oc" : "kubectl");
} catch (KubernetesClientException e) {
KubernetesResourceUtil.handleKubernetesClientException(e, kitLogger, kubernetesExtension.getSummaryEnabledOrDefault());
IllegalStateException illegalStateException = KubernetesResourceUtil.handleKubernetesClientException(e, kitLogger, jKubeServiceHub.getSummaryService());
printSummary();
throw illegalStateException;
} catch (IOException ioException) {
kitLogger.error("Error in loading Kubernetes Manifests ", ioException);
SummaryUtil.setFailureIfSummaryEnabledOrThrow(kubernetesExtension.getSummaryEnabledOrDefault(), ioException.getMessage(), () -> new IllegalStateException(ioException));
jKubeServiceHub.getSummaryService().setFailureAndCause(ioException.getMessage());
printSummary();
throw new IllegalStateException(ioException);
} catch (InterruptedException interruptedException) {
Thread.currentThread().interrupt();
SummaryUtil.setFailureIfSummaryEnabledOrThrow(kubernetesExtension.getSummaryEnabledOrDefault(),
interruptedException.getMessage(),
() -> new IllegalStateException(interruptedException.getMessage(), interruptedException));
jKubeServiceHub.getSummaryService().setFailureAndCause(interruptedException.getMessage());
printSummary();
throw new IllegalStateException(interruptedException.getMessage(), interruptedException);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import org.eclipse.jkube.gradle.plugin.KubernetesExtension;
import org.eclipse.jkube.kit.build.service.docker.DockerServiceHub;
import org.eclipse.jkube.kit.common.util.SummaryUtil;
import org.eclipse.jkube.kit.config.image.ImageConfiguration;
import org.eclipse.jkube.kit.config.resource.RuntimeMode;
import org.eclipse.jkube.kit.config.service.BuildServiceConfig;
Expand Down Expand Up @@ -66,8 +65,9 @@ public void run() {
jKubeServiceHub.getBuildService().build(resolvedImages.toArray(new ImageConfiguration[0]));
} catch (JKubeServiceException | IOException e) {
kitLogger.error(e.getMessage());
SummaryUtil.setFailureIfSummaryEnabledOrThrow(kubernetesExtension.getSummaryEnabledOrDefault(),
e.getMessage(), () -> new GradleException(e.getMessage(), e));
jKubeServiceHub.getSummaryService().setFailureAndCause(e.getMessage());
printSummary();
throw new GradleException(e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.KubernetesClient;
import org.eclipse.jkube.kit.common.util.SummaryUtil;
import org.gradle.api.GradleException;

public class KubernetesDebugTask extends AbstractJKubeTask {
Expand All @@ -46,9 +45,9 @@ public void run() {
"" + kubernetesExtension.getLocalDebugPortOrDefault(), kubernetesExtension.getDebugSuspendOrDefault(),
createLogger("[[Y]][W][[Y]] [[s]]"));
} catch (IOException ex) {
SummaryUtil.setFailureIfSummaryEnabledOrThrow(kubernetesExtension.getSummaryEnabledOrDefault(),
ex.getMessage(),
() -> new GradleException("Failure in debug task", ex));
jKubeServiceHub.getSummaryService().setFailureAndCause(ex.getMessage());
printSummary();
throw new GradleException("Failure in debug task", ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package org.eclipse.jkube.gradle.plugin.task;

import org.eclipse.jkube.gradle.plugin.KubernetesExtension;
import org.eclipse.jkube.kit.common.util.SummaryUtil;
import org.eclipse.jkube.kit.resource.helm.HelmConfig;

import javax.inject.Inject;
Expand Down Expand Up @@ -42,9 +41,9 @@ public void run() {
jKubeServiceHub.getHelmService().uploadHelmChart(helm);
} catch (Exception exp) {
kitLogger.error("Error performing helm push", exp);
SummaryUtil.setFailureIfSummaryEnabledOrThrow(kubernetesExtension.getSummaryEnabledOrDefault(),
exp.getMessage(),
() -> new IllegalStateException(exp.getMessage(), exp));
jKubeServiceHub.getSummaryService().setFailureAndCause(exp.getMessage());
printSummary();
throw new IllegalStateException(exp.getMessage(), exp);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package org.eclipse.jkube.gradle.plugin.task;

import org.eclipse.jkube.gradle.plugin.KubernetesExtension;
import org.eclipse.jkube.kit.common.util.SummaryUtil;
import org.eclipse.jkube.kit.resource.helm.HelmConfig;

import javax.inject.Inject;
Expand Down Expand Up @@ -42,9 +41,9 @@ public void run() {
kubernetesExtension.helm).build();
jKubeServiceHub.getHelmService().generateHelmCharts(helm);
} catch (IOException exception) {
SummaryUtil.setFailureIfSummaryEnabledOrThrow(kubernetesExtension.getSummaryEnabledOrDefault(),
exception.getMessage(),
() -> new IllegalStateException(exception.getMessage(), exception));
jKubeServiceHub.getSummaryService().setFailureAndCause(exception.getMessage());
printSummary();
throw new IllegalStateException(exception.getMessage(), exception);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import org.eclipse.jkube.gradle.plugin.KubernetesExtension;
import org.eclipse.jkube.kit.common.util.KubernetesHelper;
import org.eclipse.jkube.kit.common.util.SummaryUtil;
import org.eclipse.jkube.kit.config.service.PodLogService;

import io.fabric8.kubernetes.api.model.HasMetadata;
Expand Down Expand Up @@ -51,9 +50,9 @@ public void run() {
null,
true);
} catch (IOException exception) {
SummaryUtil.setFailureIfSummaryEnabledOrThrow(kubernetesExtension.getSummaryEnabledOrDefault(),
exception.getMessage(),
() -> new GradleException("Failure in getting logs", exception));
jKubeServiceHub.getSummaryService().setFailureAndCause(exception.getMessage());
printSummary();
throw new GradleException("Failure in getting logs", exception);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.eclipse.jkube.gradle.plugin.KubernetesExtension;
import org.eclipse.jkube.kit.build.service.docker.DockerServiceHub;
import org.eclipse.jkube.kit.common.RegistryConfig;
import org.eclipse.jkube.kit.common.util.SummaryUtil;
import org.eclipse.jkube.kit.config.service.BuildServiceConfig;
import org.eclipse.jkube.kit.config.service.JKubeServiceException;
import org.eclipse.jkube.kit.config.service.JKubeServiceHub;
Expand Down Expand Up @@ -45,9 +44,9 @@ public void run() {
jKubeServiceHub.getBuildService()
.push(resolvedImages, kubernetesExtension.getPushRetriesOrDefault(), initRegistryConfig(), kubernetesExtension.getSkipTagOrDefault());
} catch (JKubeServiceException e) {
SummaryUtil.setFailureIfSummaryEnabledOrThrow(kubernetesExtension.getSummaryEnabledOrDefault(),
e.getMessage(),
() -> new IllegalStateException("Error in pushing image: " + e.getMessage(), e));
jKubeServiceHub.getSummaryService().setFailureAndCause(e.getMessage());
printSummary();
throw new IllegalStateException("Error in pushing image: " + e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import io.fabric8.kubernetes.api.model.KubernetesList;
import org.eclipse.jkube.gradle.plugin.KubernetesExtension;
import org.eclipse.jkube.kit.common.util.LazyBuilder;
import org.eclipse.jkube.kit.common.util.SummaryUtil;
import org.eclipse.jkube.kit.config.resource.ResourceConfig;
import org.eclipse.jkube.kit.config.service.JKubeServiceHub;
import org.eclipse.jkube.kit.config.resource.ResourceServiceConfig;
Expand Down Expand Up @@ -89,13 +88,13 @@ public void run() {
ResourceClassifier resourceClassifier = kubernetesExtension.getResourceClassifier();
KubernetesList resourceList = jKubeServiceHub.getResourceService().generateResources(kubernetesExtension.getPlatformMode(), enricherManager, kitLogger);
final File resourceClassifierDir = new File(kubernetesExtension.getResourceTargetDirectoryOrDefault(), resourceClassifier.getValue());
jKubeServiceHub.getResourceService().writeResources(resourceList, resourceClassifier, kitLogger);
jKubeServiceHub.getResourceService().writeResources(resourceList, resourceClassifier, kitLogger, jKubeServiceHub.getSummaryService());
validateIfRequired(resourceClassifierDir, resourceClassifier);
}
} catch (IOException e) {
SummaryUtil.setFailureIfSummaryEnabledOrThrow(kubernetesExtension.getSummaryEnabledOrDefault(),
e.getMessage(),
() -> new IllegalStateException("Failed to generate kubernetes descriptor", e));
jKubeServiceHub.getSummaryService().setFailureAndCause(e.getMessage());
printSummary();
throw new IllegalStateException("Failed to generate kubernetes descriptor", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package org.eclipse.jkube.gradle.plugin.task;

import org.eclipse.jkube.gradle.plugin.KubernetesExtension;
import org.eclipse.jkube.kit.common.util.SummaryUtil;
import org.eclipse.jkube.kit.config.resource.ResourceConfig;

import javax.inject.Inject;
Expand Down Expand Up @@ -45,9 +44,9 @@ public void run() {
jKubeServiceHub.getUndeployService()
.undeploy(environmentResourceDirs, resources, findManifestsToUndeploy().toArray(new File[0]));
} catch (IOException e) {
SummaryUtil.setFailureIfSummaryEnabledOrThrow(kubernetesExtension.getSummaryEnabledOrDefault(),
e.getMessage(),
() -> new IllegalStateException(e.getMessage(), e));
jKubeServiceHub.getSummaryService().setFailureAndCause(e.getMessage());
printSummary();
throw new IllegalStateException(e.getMessage(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,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.common.util.SummaryUtil;
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;
Expand Down Expand Up @@ -69,11 +68,13 @@ public void run() {
resources,
context);
} catch (KubernetesClientException kubernetesClientException) {
KubernetesResourceUtil.handleKubernetesClientException(kubernetesClientException, kitLogger, kubernetesExtension.getSummaryEnabledOrDefault());
IllegalStateException illegalStateException = KubernetesResourceUtil.handleKubernetesClientException(kubernetesClientException, kitLogger, jKubeServiceHub.getSummaryService());
printSummary();
throw illegalStateException;
} catch (Exception ioException) {
SummaryUtil.setFailureIfSummaryEnabledOrThrow(kubernetesExtension.getSummaryEnabledOrDefault(),
ioException.getMessage(),
() -> new IllegalStateException("An error has occurred while while trying to watch the resources", ioException));
jKubeServiceHub.getSummaryService().setFailureAndCause(ioException.getMessage());
printSummary();
throw new IllegalStateException("An error has occurred while while trying to watch the resources", ioException);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.common.archive.ArchiveCompression;
import org.eclipse.jkube.kit.common.archive.JKubeTarArchiver;
import org.eclipse.jkube.kit.common.service.SummaryService;
import org.eclipse.jkube.kit.common.util.FileUtil;
import org.eclipse.jkube.kit.common.util.JKubeProjectUtil;
import org.eclipse.jkube.kit.common.util.SummaryUtil;
import org.eclipse.jkube.kit.config.image.ImageConfiguration;
import org.eclipse.jkube.kit.config.image.build.BuildConfiguration;
import org.eclipse.jkube.kit.config.image.build.DockerFileBuilder;
Expand Down Expand Up @@ -95,12 +95,13 @@ public static AssemblyManager getInstance() {
* @param buildConfig configuration for how to build the image
* @param log KitLogger used to display warning if permissions are to be normalized
* @param finalCustomizer finalCustomizer to be applied to the tar archive
* @param summaryService {@link SummaryService}
* @return file holding the path to the created assembly tar file
* @throws IOException IO exception
*/
public File createDockerTarArchive(
String imageName, final JKubeConfiguration configuration, final BuildConfiguration buildConfig, KitLogger log,
ArchiverCustomizer finalCustomizer) throws IOException {
ArchiverCustomizer finalCustomizer, SummaryService summaryService) throws IOException {

final BuildDirs buildDirs = createBuildDirs(imageName, configuration);
final List<ArchiverCustomizer> archiveCustomizers = new ArrayList<>();
Expand All @@ -110,7 +111,7 @@ public File createDockerTarArchive(

try {
if (buildConfig.isDockerFileMode()) {
SummaryUtil.setDockerFileImageSummary(imageName, buildConfig.getDockerFileFile().getAbsolutePath());
summaryService.setDockerFileImageSummary(imageName, buildConfig.getDockerFileFile().getAbsolutePath());
createDockerTarArchiveForDockerFile(buildConfig, assemblyConfig, configuration, buildDirs, log, archiveCustomizers);
} else {
createAssemblyArchive(assemblyConfig, configuration, buildDirs, buildConfig.getCompression(), layers);
Expand Down
Loading

0 comments on commit 78c26b6

Please sign in to comment.