|
| 1 | +// Copyright 2018, Oracle Corporation and/or its affiliates. All rights reserved. |
| 2 | +// Licensed under the Universal Permissive License v 1.0 as shown at |
| 3 | +// http://oss.oracle.com/licenses/upl. |
| 4 | + |
| 5 | +package oracle.kubernetes.operator.helm; |
| 6 | + |
| 7 | +import java.io.BufferedReader; |
| 8 | +import java.io.File; |
| 9 | +import java.io.InputStream; |
| 10 | +import java.io.InputStreamReader; |
| 11 | +import java.util.stream.Collectors; |
| 12 | +import oracle.kubernetes.operator.utils.PathUtils; |
| 13 | + |
| 14 | +/** Gets a helm chart's default values */ |
| 15 | +@SuppressWarnings({"unchecked", "SameParameterValue"}) |
| 16 | +public class ChartDefaultValues { |
| 17 | + |
| 18 | + private String chartName; |
| 19 | + |
| 20 | + ChartDefaultValues(String chartName) { |
| 21 | + this.chartName = chartName; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Returns the default values that the chart creates. |
| 26 | + * |
| 27 | + * @return a yaml string |
| 28 | + */ |
| 29 | + String getDefaultValuesAsYaml() throws Exception { |
| 30 | + processChart(); |
| 31 | + return getDefaultValuesFromDebugProcessStdout(processChart()); |
| 32 | + } |
| 33 | + |
| 34 | + private String getDefaultValuesFromDebugProcessStdout(String stdout) throws Exception { |
| 35 | + String BEGIN_MARKER = "\nCOMPUTED VALUES:\n"; |
| 36 | + int begin = stdout.indexOf(BEGIN_MARKER); |
| 37 | + if (begin == -1) { |
| 38 | + reportProcessError("stdout does not contain " + BEGIN_MARKER + "\nstdout:\n" + stdout); |
| 39 | + } |
| 40 | + begin = begin + BEGIN_MARKER.length(); |
| 41 | + String END_MARKER = "\nHOOKS:\n"; |
| 42 | + int end = stdout.indexOf(END_MARKER, begin); |
| 43 | + if (end == -1) { |
| 44 | + reportProcessError( |
| 45 | + "stdout does not contain \"" |
| 46 | + + END_MARKER |
| 47 | + + " after " |
| 48 | + + BEGIN_MARKER |
| 49 | + + ".\nstdout:\n" |
| 50 | + + stdout); |
| 51 | + } |
| 52 | + return stdout.substring(begin, end); |
| 53 | + } |
| 54 | + |
| 55 | + private String processChart() throws Exception { |
| 56 | + ProcessBuilder pb = new ProcessBuilder(createCommandLine()); |
| 57 | + Process p = pb.start(); |
| 58 | + p.waitFor(); |
| 59 | + String stdout = read(p.getInputStream()); |
| 60 | + if (p.exitValue() != 0) { |
| 61 | + String stderr = read(p.getErrorStream()); |
| 62 | + reportProcessError( |
| 63 | + "Non-zero exit value.\nexit value: " |
| 64 | + + p.exitValue() |
| 65 | + + "\nstdout:\n" |
| 66 | + + stdout |
| 67 | + + "\nstderr:\n" |
| 68 | + + stderr); |
| 69 | + } |
| 70 | + return stdout; |
| 71 | + } |
| 72 | + |
| 73 | + private void reportProcessError(String msg) throws Exception { |
| 74 | + String cmd = String.join(" ", createCommandLine()); |
| 75 | + throw new Exception(cmd + ": " + msg); |
| 76 | + } |
| 77 | + |
| 78 | + private String read(InputStream is) throws Exception { |
| 79 | + return new BufferedReader(new InputStreamReader(is)).lines().collect(Collectors.joining("\n")); |
| 80 | + } |
| 81 | + |
| 82 | + private String[] createCommandLine() throws Exception { |
| 83 | + File chartDir = getChartDir(this.chartName); |
| 84 | + return new String[] {"helm", "template", chartDir.getAbsolutePath(), "--debug"}; |
| 85 | + } |
| 86 | + |
| 87 | + private File getChartDir(String chartName) throws Exception { |
| 88 | + return new File(getChartsParentDir(), chartName); |
| 89 | + } |
| 90 | + |
| 91 | + private File getChartsParentDir() throws Exception { |
| 92 | + return new File(PathUtils.getModuleDir(getClass()), "charts"); |
| 93 | + } |
| 94 | +} |
0 commit comments