From 648b3a687fcd80f265301f9ae389d03029d602a5 Mon Sep 17 00:00:00 2001 From: Olivier Levitt Date: Thu, 12 Dec 2024 16:46:49 +0100 Subject: [PATCH 1/3] Get error from helm wrapper --- .../service/HelmInstallService.java | 34 +++++---- .../helmwrapper/service/HelmRepoService.java | 6 +- .../service/HelmVersionService.java | 4 +- .../inseefrlab/helmwrapper/utils/Command.java | 69 ++++++++++++++----- 4 files changed, 80 insertions(+), 33 deletions(-) diff --git a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmInstallService.java b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmInstallService.java index 2aeb2bc8..e30101a7 100644 --- a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmInstallService.java +++ b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmInstallService.java @@ -1,7 +1,5 @@ package io.github.inseefrlab.helmwrapper.service; -import static io.github.inseefrlab.helmwrapper.utils.Command.safeConcat; - import com.fasterxml.jackson.databind.ObjectMapper; import io.github.inseefrlab.helmwrapper.configuration.HelmConfiguration; import io.github.inseefrlab.helmwrapper.model.HelmInstaller; @@ -9,6 +7,11 @@ import io.github.inseefrlab.helmwrapper.model.HelmReleaseInfo; import io.github.inseefrlab.helmwrapper.utils.Command; import io.github.inseefrlab.helmwrapper.utils.HelmReleaseInfoParser; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.zeroturnaround.exec.InvalidExitValueException; + import java.io.File; import java.io.IOException; import java.util.List; @@ -17,10 +20,8 @@ import java.util.concurrent.TimeoutException; import java.util.regex.Pattern; import java.util.stream.Collectors; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.zeroturnaround.exec.InvalidExitValueException; + +import static io.github.inseefrlab.helmwrapper.utils.Command.safeConcat; /** HelmInstall */ public class HelmInstallService { @@ -188,11 +189,12 @@ public HelmInstaller installChart( if (reuseValues) { command.append(" --reuse-values"); } - String res = - Command.executeAndGetResponseAsJson(configuration, command.toString()) - .getOutput() - .getString(); - return new ObjectMapper().readValue(res, HelmInstaller.class); + Command.ProcessResultWithError result = + Command.executeAndGetResponseAsJson(configuration, command.toString()); + if (result.getProcessResult().getExitValue() != 0) { + throw new RuntimeException(result.getError()); + } + return new ObjectMapper().readValue(result.getProcessResult().getOutput().getString(), HelmInstaller.class); } public int uninstaller(HelmConfiguration configuration, String name, String namespace) @@ -201,7 +203,7 @@ public int uninstaller(HelmConfiguration configuration, String name, String name safeConcat(command, name); command.append(" -n "); safeConcat(command, namespace); - return Command.execute(configuration, command.toString()).getExitValue(); + return Command.execute(configuration, command.toString()).getProcessResult().getExitValue(); } public HelmLs[] listChartInstall(HelmConfiguration configuration, String namespace) @@ -214,6 +216,7 @@ public HelmLs[] listChartInstall(HelmConfiguration configuration, String namespa return new ObjectMapper() .readValue( Command.executeAndGetResponseAsJson(configuration, command.toString()) + .getProcessResult() .getOutput() .getString(), HelmLs[].class); @@ -238,7 +241,7 @@ public HelmReleaseInfo getAll(HelmConfiguration configuration, String id, String safeConcat(command, namespace); try { String unparsedReleaseInfo = - Command.execute(configuration, command.toString()).getOutput().getString(); + Command.execute(configuration, command.toString()).getProcessResult().getOutput().getString(); return helmReleaseInfoParser.parseReleaseInfo(unparsedReleaseInfo); } catch (IOException | InterruptedException | TimeoutException e) { LOGGER.warn("Exception occurred", e); @@ -259,14 +262,16 @@ private String getReleaseInfo( safeConcat(command, namespace); if (infoType.equals(NOTES_INFO_TYPE)) { return Command.executeAndGetResponseAsRaw(configuration, command.toString()) + .getProcessResult() .getOutput() .getString(); } else if (infoType.equals(VALUES_INFO_TYPE)) { return Command.executeAndGetResponseAsJson(configuration, command.toString()) + .getProcessResult() .getOutput() .getString(); } else { - return Command.execute(configuration, command.toString()).getOutput().getString(); + return Command.execute(configuration, command.toString()).getProcessResult().getOutput().getString(); } } catch (IOException | InterruptedException | TimeoutException e) { LOGGER.warn("Exception occurred", e); @@ -302,6 +307,7 @@ public HelmLs getAppById(HelmConfiguration configuration, String appId, String n .readValue( Command.executeAndGetResponseAsJson( configuration, command.toString()) + .getProcessResult() .getOutput() .getString(), HelmLs[].class); diff --git a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmRepoService.java b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmRepoService.java index 5204149d..673287a5 100644 --- a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmRepoService.java +++ b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmRepoService.java @@ -3,10 +3,11 @@ import com.fasterxml.jackson.databind.ObjectMapper; import io.github.inseefrlab.helmwrapper.model.HelmRepo; import io.github.inseefrlab.helmwrapper.utils.Command; +import org.zeroturnaround.exec.InvalidExitValueException; + import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.concurrent.TimeoutException; -import org.zeroturnaround.exec.InvalidExitValueException; /** HelmExecuter */ public class HelmRepoService { @@ -18,6 +19,7 @@ public HelmRepo[] getHelmRepo() new ObjectMapper() .readValue( Command.executeAndGetResponseAsJson("helm search repo") + .getProcessResult() .getOutput() .getString(StandardCharsets.UTF_8.name()), HelmRepo[].class); @@ -39,7 +41,7 @@ public String addHelmRepo( "--ca-file " + System.getenv("CACERTS_DIR") + "/" + caFile + " "); } command = command.concat(nomRepo + " " + url); - return Command.execute(command).getOutput().getString(); + return Command.execute(command).getProcessResult().getOutput().getString(); } public void repoUpdate() throws InterruptedException, TimeoutException, IOException { diff --git a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmVersionService.java b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmVersionService.java index 3dccd7d2..5effebbe 100644 --- a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmVersionService.java +++ b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmVersionService.java @@ -1,16 +1,18 @@ package io.github.inseefrlab.helmwrapper.service; import io.github.inseefrlab.helmwrapper.utils.Command; +import org.zeroturnaround.exec.InvalidExitValueException; + import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.concurrent.TimeoutException; -import org.zeroturnaround.exec.InvalidExitValueException; public class HelmVersionService { public String getVersion() throws InvalidExitValueException, IOException, InterruptedException, TimeoutException { return Command.executeAndGetResponseAsRaw("helm version --template={{.Version}}") + .getProcessResult() .getOutput() .getString(StandardCharsets.UTF_8.name()); } diff --git a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/utils/Command.java b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/utils/Command.java index d0519caa..53ae6813 100644 --- a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/utils/Command.java +++ b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/utils/Command.java @@ -1,11 +1,6 @@ package io.github.inseefrlab.helmwrapper.utils; import io.github.inseefrlab.helmwrapper.configuration.HelmConfiguration; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.TimeoutException; -import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -14,6 +9,15 @@ import org.zeroturnaround.exec.ProcessResult; import org.zeroturnaround.exec.listener.ProcessListener; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.nio.charset.Charset; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeoutException; +import java.util.regex.Pattern; + /** Executeur */ public class Command { @@ -22,9 +26,11 @@ public class Command { "^[ ]*[a-z0-9]([-a-z0-9 ]*[a-z0-9 ])?(\\.[a-z0-9 ]([-a-z0-9 ]*[a-z0-9 ])?)*[ ]*$"); private static final Logger LOGGER = LoggerFactory.getLogger(Command.class); - private static ProcessExecutor getProcessExecutor() { + private static ProcessExecutor getProcessExecutor(OutputStream errorOutputStream) { ProcessExecutor processExecutor = new ProcessExecutor(); - processExecutor.redirectError(System.err); + if (errorOutputStream != null) { + processExecutor.redirectError(errorOutputStream); + } processExecutor.readOutput(true); processExecutor.addListener( new ProcessListener() { @@ -37,47 +43,78 @@ public void afterStart(Process process, ProcessExecutor executor) { return processExecutor; } - public static ProcessResult executeAndGetResponseAsJson( + public static ProcessResultWithError executeAndGetResponseAsJson( HelmConfiguration helmConfiguration, String command) throws InvalidExitValueException, IOException, InterruptedException, TimeoutException { - return getProcessExecutor() + ByteArrayOutputStream errorStream = new ByteArrayOutputStream(); + ProcessResult processResult = getProcessExecutor(errorStream) .environment(getEnv(helmConfiguration)) .commandSplit(addConfigToCommand(command, helmConfiguration) + " --output json") .execute(); + return new ProcessResultWithError(processResult, errorStream); } - public static ProcessResult executeAndGetResponseAsJson(String command) + public static ProcessResultWithError executeAndGetResponseAsJson(String command) throws InvalidExitValueException, IOException, InterruptedException, TimeoutException { return executeAndGetResponseAsJson(null, command); } - public static ProcessResult executeAndGetResponseAsRaw( + public static ProcessResultWithError executeAndGetResponseAsRaw( HelmConfiguration helmConfiguration, String command) throws InvalidExitValueException, IOException, InterruptedException, TimeoutException { - return getProcessExecutor() + ByteArrayOutputStream errorStream = new ByteArrayOutputStream(); + ProcessResult processResult = getProcessExecutor(errorStream) .environment(getEnv(helmConfiguration)) .commandSplit(addConfigToCommand(command, helmConfiguration)) .execute(); + return new ProcessResultWithError(processResult, errorStream); } - public static ProcessResult executeAndGetResponseAsRaw(String command) + public static ProcessResultWithError executeAndGetResponseAsRaw(String command) throws InvalidExitValueException, IOException, InterruptedException, TimeoutException { return executeAndGetResponseAsRaw(null, command); } - public static ProcessResult execute(HelmConfiguration helmConfiguration, String command) + public static ProcessResultWithError execute(HelmConfiguration helmConfiguration, String command) throws InvalidExitValueException, IOException, InterruptedException, TimeoutException { - return getProcessExecutor() + ByteArrayOutputStream errorStream = new ByteArrayOutputStream(); + ProcessResult processResult = getProcessExecutor(errorStream) .environment(getEnv(helmConfiguration)) .commandSplit(addConfigToCommand(command, helmConfiguration)) .execute(); + return new ProcessResultWithError(processResult, errorStream); } - public static ProcessResult execute(String command) + public static ProcessResultWithError execute(String command) throws InvalidExitValueException, IOException, InterruptedException, TimeoutException { return execute(null, command); } + public static class ProcessResultWithError { + + private ProcessResult processResult; + private String error = null; + + public ProcessResultWithError() { + + } + + public ProcessResultWithError(ProcessResult processResult, ByteArrayOutputStream boas) { + this.processResult = processResult; + if (boas != null) { + error = boas.toString(Charset.defaultCharset()); + } + } + + public ProcessResult getProcessResult() { + return processResult; + } + + public String getError() { + return error; + } + } + private static Map getEnv(HelmConfiguration helmConfiguration) { Map env = new HashMap<>(); if (System.getProperty("http.proxyHost") != null) { From 5d479c972dfc17c0b3aed0a4945a16388c59db53 Mon Sep 17 00:00:00 2001 From: Olivier Levitt Date: Thu, 12 Dec 2024 16:48:31 +0100 Subject: [PATCH 2/3] Spotless: fix --- .../service/HelmInstallService.java | 26 ++++++---- .../helmwrapper/service/HelmRepoService.java | 3 +- .../service/HelmVersionService.java | 3 +- .../inseefrlab/helmwrapper/utils/Command.java | 50 ++++++++++--------- 4 files changed, 44 insertions(+), 38 deletions(-) diff --git a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmInstallService.java b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmInstallService.java index e30101a7..38927054 100644 --- a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmInstallService.java +++ b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmInstallService.java @@ -1,5 +1,7 @@ package io.github.inseefrlab.helmwrapper.service; +import static io.github.inseefrlab.helmwrapper.utils.Command.safeConcat; + import com.fasterxml.jackson.databind.ObjectMapper; import io.github.inseefrlab.helmwrapper.configuration.HelmConfiguration; import io.github.inseefrlab.helmwrapper.model.HelmInstaller; @@ -7,11 +9,6 @@ import io.github.inseefrlab.helmwrapper.model.HelmReleaseInfo; import io.github.inseefrlab.helmwrapper.utils.Command; import io.github.inseefrlab.helmwrapper.utils.HelmReleaseInfoParser; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.zeroturnaround.exec.InvalidExitValueException; - import java.io.File; import java.io.IOException; import java.util.List; @@ -20,8 +17,10 @@ import java.util.concurrent.TimeoutException; import java.util.regex.Pattern; import java.util.stream.Collectors; - -import static io.github.inseefrlab.helmwrapper.utils.Command.safeConcat; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.zeroturnaround.exec.InvalidExitValueException; /** HelmInstall */ public class HelmInstallService { @@ -194,7 +193,8 @@ public HelmInstaller installChart( if (result.getProcessResult().getExitValue() != 0) { throw new RuntimeException(result.getError()); } - return new ObjectMapper().readValue(result.getProcessResult().getOutput().getString(), HelmInstaller.class); + return new ObjectMapper() + .readValue(result.getProcessResult().getOutput().getString(), HelmInstaller.class); } public int uninstaller(HelmConfiguration configuration, String name, String namespace) @@ -241,7 +241,10 @@ public HelmReleaseInfo getAll(HelmConfiguration configuration, String id, String safeConcat(command, namespace); try { String unparsedReleaseInfo = - Command.execute(configuration, command.toString()).getProcessResult().getOutput().getString(); + Command.execute(configuration, command.toString()) + .getProcessResult() + .getOutput() + .getString(); return helmReleaseInfoParser.parseReleaseInfo(unparsedReleaseInfo); } catch (IOException | InterruptedException | TimeoutException e) { LOGGER.warn("Exception occurred", e); @@ -271,7 +274,10 @@ private String getReleaseInfo( .getOutput() .getString(); } else { - return Command.execute(configuration, command.toString()).getProcessResult().getOutput().getString(); + return Command.execute(configuration, command.toString()) + .getProcessResult() + .getOutput() + .getString(); } } catch (IOException | InterruptedException | TimeoutException e) { LOGGER.warn("Exception occurred", e); diff --git a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmRepoService.java b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmRepoService.java index 673287a5..f3c64116 100644 --- a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmRepoService.java +++ b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmRepoService.java @@ -3,11 +3,10 @@ import com.fasterxml.jackson.databind.ObjectMapper; import io.github.inseefrlab.helmwrapper.model.HelmRepo; import io.github.inseefrlab.helmwrapper.utils.Command; -import org.zeroturnaround.exec.InvalidExitValueException; - import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.concurrent.TimeoutException; +import org.zeroturnaround.exec.InvalidExitValueException; /** HelmExecuter */ public class HelmRepoService { diff --git a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmVersionService.java b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmVersionService.java index 5effebbe..1843e1bd 100644 --- a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmVersionService.java +++ b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmVersionService.java @@ -1,11 +1,10 @@ package io.github.inseefrlab.helmwrapper.service; import io.github.inseefrlab.helmwrapper.utils.Command; -import org.zeroturnaround.exec.InvalidExitValueException; - import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.concurrent.TimeoutException; +import org.zeroturnaround.exec.InvalidExitValueException; public class HelmVersionService { diff --git a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/utils/Command.java b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/utils/Command.java index 53ae6813..0dc26100 100644 --- a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/utils/Command.java +++ b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/utils/Command.java @@ -1,14 +1,6 @@ package io.github.inseefrlab.helmwrapper.utils; import io.github.inseefrlab.helmwrapper.configuration.HelmConfiguration; -import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.zeroturnaround.exec.InvalidExitValueException; -import org.zeroturnaround.exec.ProcessExecutor; -import org.zeroturnaround.exec.ProcessResult; -import org.zeroturnaround.exec.listener.ProcessListener; - import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; @@ -17,6 +9,13 @@ import java.util.Map; import java.util.concurrent.TimeoutException; import java.util.regex.Pattern; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.zeroturnaround.exec.InvalidExitValueException; +import org.zeroturnaround.exec.ProcessExecutor; +import org.zeroturnaround.exec.ProcessResult; +import org.zeroturnaround.exec.listener.ProcessListener; /** Executeur */ public class Command { @@ -47,10 +46,12 @@ public static ProcessResultWithError executeAndGetResponseAsJson( HelmConfiguration helmConfiguration, String command) throws InvalidExitValueException, IOException, InterruptedException, TimeoutException { ByteArrayOutputStream errorStream = new ByteArrayOutputStream(); - ProcessResult processResult = getProcessExecutor(errorStream) - .environment(getEnv(helmConfiguration)) - .commandSplit(addConfigToCommand(command, helmConfiguration) + " --output json") - .execute(); + ProcessResult processResult = + getProcessExecutor(errorStream) + .environment(getEnv(helmConfiguration)) + .commandSplit( + addConfigToCommand(command, helmConfiguration) + " --output json") + .execute(); return new ProcessResultWithError(processResult, errorStream); } @@ -63,10 +64,11 @@ public static ProcessResultWithError executeAndGetResponseAsRaw( HelmConfiguration helmConfiguration, String command) throws InvalidExitValueException, IOException, InterruptedException, TimeoutException { ByteArrayOutputStream errorStream = new ByteArrayOutputStream(); - ProcessResult processResult = getProcessExecutor(errorStream) - .environment(getEnv(helmConfiguration)) - .commandSplit(addConfigToCommand(command, helmConfiguration)) - .execute(); + ProcessResult processResult = + getProcessExecutor(errorStream) + .environment(getEnv(helmConfiguration)) + .commandSplit(addConfigToCommand(command, helmConfiguration)) + .execute(); return new ProcessResultWithError(processResult, errorStream); } @@ -75,13 +77,15 @@ public static ProcessResultWithError executeAndGetResponseAsRaw(String command) return executeAndGetResponseAsRaw(null, command); } - public static ProcessResultWithError execute(HelmConfiguration helmConfiguration, String command) + public static ProcessResultWithError execute( + HelmConfiguration helmConfiguration, String command) throws InvalidExitValueException, IOException, InterruptedException, TimeoutException { ByteArrayOutputStream errorStream = new ByteArrayOutputStream(); - ProcessResult processResult = getProcessExecutor(errorStream) - .environment(getEnv(helmConfiguration)) - .commandSplit(addConfigToCommand(command, helmConfiguration)) - .execute(); + ProcessResult processResult = + getProcessExecutor(errorStream) + .environment(getEnv(helmConfiguration)) + .commandSplit(addConfigToCommand(command, helmConfiguration)) + .execute(); return new ProcessResultWithError(processResult, errorStream); } @@ -95,9 +99,7 @@ public static class ProcessResultWithError { private ProcessResult processResult; private String error = null; - public ProcessResultWithError() { - - } + public ProcessResultWithError() {} public ProcessResultWithError(ProcessResult processResult, ByteArrayOutputStream boas) { this.processResult = processResult; From 97068bc594f63df9ad6612ecdce26cb4e327a622 Mon Sep 17 00:00:00 2001 From: Olivier Levitt Date: Thu, 12 Dec 2024 16:54:16 +0100 Subject: [PATCH 3/3] Minor removal --- .../github/inseefrlab/helmwrapper/service/HelmRepoService.java | 1 - 1 file changed, 1 deletion(-) diff --git a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmRepoService.java b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmRepoService.java index f3c64116..8d7428b9 100644 --- a/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmRepoService.java +++ b/helm-wrapper/src/main/java/io/github/inseefrlab/helmwrapper/service/HelmRepoService.java @@ -13,7 +13,6 @@ public class HelmRepoService { public HelmRepo[] getHelmRepo() throws InvalidExitValueException, IOException, InterruptedException, TimeoutException { - // System.out.println(new ProcessExecutor().getDirectory().getAbsolutePath()); HelmRepo[] repo = new ObjectMapper() .readValue(