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 <rohaan@redhat.com>
  • Loading branch information
rohanKanojia committed Nov 2, 2022
1 parent 0692e2f commit eeb263f
Show file tree
Hide file tree
Showing 143 changed files with 1,353 additions and 669 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 All @@ -49,6 +48,7 @@
import static org.eclipse.jkube.kit.build.service.docker.helper.ConfigHelper.initImageConfiguration;
import static org.eclipse.jkube.kit.common.util.BuildReferenceDateUtil.getBuildTimestamp;
import static org.eclipse.jkube.kit.config.service.kubernetes.KubernetesClientUtil.updateResourceConfigNamespace;
import static org.eclipse.jkube.kit.config.service.kubernetes.SummaryServiceUtil.printSummaryIfLastExecuting;

public abstract class AbstractJKubeTask extends DefaultTask implements KubernetesJKubeTask {

Expand All @@ -67,16 +67,15 @@ 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();
String lastExecutingTask = GradleUtil.getLastExecutingTask(getProject(), getTaskPrioritiesMap());
if (lastExecutingTask != null && lastExecutingTask.equals(getName())) {
SummaryUtil.printSummary(kubernetesExtension.javaProject.getBaseDirectory(), kubernetesExtension.getSummaryEnabledOrDefault());
SummaryUtil.clear();
}
run();
} finally {
printSummaryIfLastExecuting(jKubeServiceHub, getName(), GradleUtil.getLastExecutingTask(getProject(), getTaskPrioritiesMap()));
}
}

Expand All @@ -89,8 +88,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 +101,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 +122,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 +152,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 All @@ -33,6 +32,8 @@
import java.util.List;

import static org.eclipse.jkube.kit.config.service.kubernetes.KubernetesClientUtil.resolveFallbackNamespace;
import static org.eclipse.jkube.kit.config.service.kubernetes.SummaryServiceUtil.handleExceptionAndSummary;
import static org.eclipse.jkube.kit.config.service.kubernetes.SummaryServiceUtil.printSummary;

@SuppressWarnings("CdiInjectionPointsInspection")
public class KubernetesApplyTask extends AbstractJKubeTask {
Expand Down Expand Up @@ -63,15 +64,13 @@ 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(jKubeServiceHub);
throw illegalStateException;
} catch (IOException ioException) {
kitLogger.error("Error in loading Kubernetes Manifests ", ioException);
SummaryUtil.setFailureIfSummaryEnabledOrThrow(kubernetesExtension.getSummaryEnabledOrDefault(), ioException.getMessage(), () -> new IllegalStateException(ioException));
} catch (InterruptedException interruptedException) {
Thread.currentThread().interrupt();
SummaryUtil.setFailureIfSummaryEnabledOrThrow(kubernetesExtension.getSummaryEnabledOrDefault(),
interruptedException.getMessage(),
() -> new IllegalStateException(interruptedException.getMessage(), interruptedException));
handleExceptionAndSummary(jKubeServiceHub, ioException);
throw new IllegalStateException(ioException);
}
}

Expand All @@ -80,7 +79,7 @@ protected boolean shouldSkip() {
return super.shouldSkip() || kubernetesExtension.getSkipApplyOrDefault();
}

private void applyEntities(String fileName, final Collection<HasMetadata> entities) throws InterruptedException {
private void applyEntities(String fileName, final Collection<HasMetadata> entities) {
KitLogger serviceLogger = createLogger("[[G]][SVC][[G]] [[s]]");
applyService.applyEntities(fileName, entities, serviceLogger, kubernetesExtension.getServiceUrlWaitTimeSecondsOrDefault());
}
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 All @@ -30,6 +29,7 @@
import static org.eclipse.jkube.kit.common.util.BuildReferenceDateUtil.getBuildTimestamp;
import static org.eclipse.jkube.kit.common.util.BuildReferenceDateUtil.getBuildTimestampFile;
import static org.eclipse.jkube.kit.common.util.EnvUtil.storeTimestamp;
import static org.eclipse.jkube.kit.config.service.kubernetes.SummaryServiceUtil.handleExceptionAndSummary;

@SuppressWarnings("CdiInjectionPointsInspection")
public class KubernetesBuildTask extends AbstractJKubeTask {
Expand Down Expand Up @@ -66,8 +66,8 @@ 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));
handleExceptionAndSummary(jKubeServiceHub, e);
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,7 @@ 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));
throw new GradleException("Failure in debug task", ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
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;

import static org.eclipse.jkube.kit.config.service.kubernetes.SummaryServiceUtil.handleExceptionAndSummary;
import static org.eclipse.jkube.kit.resource.helm.HelmServiceUtil.initHelmConfig;
import static org.eclipse.jkube.kit.resource.helm.HelmServiceUtil.initHelmPushConfig;

Expand All @@ -42,9 +42,8 @@ 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));
handleExceptionAndSummary(jKubeServiceHub, exp);
throw new IllegalStateException(exp.getMessage(), exp);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
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;
import java.io.File;
import java.io.IOException;

import static org.eclipse.jkube.kit.config.service.kubernetes.SummaryServiceUtil.handleExceptionAndSummary;
import static org.eclipse.jkube.kit.resource.helm.HelmServiceUtil.initHelmConfig;

public class KubernetesHelmTask extends AbstractJKubeTask {
Expand All @@ -42,9 +42,8 @@ public void run() {
kubernetesExtension.helm).build();
jKubeServiceHub.getHelmService().generateHelmCharts(helm);
} catch (IOException exception) {
SummaryUtil.setFailureIfSummaryEnabledOrThrow(kubernetesExtension.getSummaryEnabledOrDefault(),
exception.getMessage(),
() -> new IllegalStateException(exception.getMessage(), exception));
handleExceptionAndSummary(jKubeServiceHub, exception);
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,7 @@ public void run() {
null,
true);
} catch (IOException exception) {
SummaryUtil.setFailureIfSummaryEnabledOrThrow(kubernetesExtension.getSummaryEnabledOrDefault(),
exception.getMessage(),
() -> new GradleException("Failure in getting logs", exception));
throw new GradleException("Failure in getting logs", exception);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
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;

import javax.inject.Inject;
import java.util.Collections;

import static org.eclipse.jkube.kit.config.service.kubernetes.SummaryServiceUtil.handleExceptionAndSummary;

public class KubernetesPushTask extends AbstractJKubeTask {
@Inject
public KubernetesPushTask(Class<? extends KubernetesExtension> extensionClass) {
Expand All @@ -45,9 +46,8 @@ 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));
handleExceptionAndSummary(jKubeServiceHub, e);
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 All @@ -35,6 +34,7 @@

import static org.eclipse.jkube.kit.common.util.DekorateUtil.DEFAULT_RESOURCE_LOCATION;
import static org.eclipse.jkube.kit.common.util.DekorateUtil.useDekorate;
import static org.eclipse.jkube.kit.config.service.kubernetes.SummaryServiceUtil.handleExceptionAndSummary;
import static org.eclipse.jkube.kit.enricher.api.util.KubernetesResourceUtil.updateKindFilenameMappings;
import static org.eclipse.jkube.kit.common.JKubeFileInterpolator.interpolate;

Expand Down Expand Up @@ -89,13 +89,12 @@ 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));
handleExceptionAndSummary(jKubeServiceHub, e);
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 All @@ -24,6 +23,8 @@
import java.util.List;
import java.util.Optional;

import static org.eclipse.jkube.kit.config.service.kubernetes.SummaryServiceUtil.handleExceptionAndSummary;

public class KubernetesUndeployTask extends AbstractJKubeTask {

@Inject
Expand All @@ -45,9 +46,8 @@ 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));
handleExceptionAndSummary(jKubeServiceHub, e);
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 All @@ -38,6 +37,8 @@

import static org.eclipse.jkube.kit.common.util.BuildReferenceDateUtil.getBuildTimestamp;
import static org.eclipse.jkube.kit.config.service.kubernetes.KubernetesClientUtil.applicableNamespace;
import static org.eclipse.jkube.kit.config.service.kubernetes.SummaryServiceUtil.handleExceptionAndSummary;
import static org.eclipse.jkube.kit.config.service.kubernetes.SummaryServiceUtil.printSummary;

public class KubernetesWatchTask extends AbstractJKubeTask {
@Inject
Expand Down Expand Up @@ -69,11 +70,12 @@ public void run() {
resources,
context);
} catch (KubernetesClientException kubernetesClientException) {
KubernetesResourceUtil.handleKubernetesClientException(kubernetesClientException, kitLogger, kubernetesExtension.getSummaryEnabledOrDefault());
IllegalStateException illegalStateException = KubernetesResourceUtil.handleKubernetesClientException(kubernetesClientException, kitLogger, jKubeServiceHub.getSummaryService());
printSummary(jKubeServiceHub);
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));
handleExceptionAndSummary(jKubeServiceHub, ioException);
throw new IllegalStateException("An error has occurred while while trying to watch the resources", ioException);
}
}
}
Expand Down
Loading

0 comments on commit eeb263f

Please sign in to comment.