diff options
Diffstat (limited to 'mod2/helm-generator/helmchartgenerator-core/src/main/java/org/onap/dcaegen2/platform/helmchartgenerator/chartbuilder')
4 files changed, 78 insertions, 30 deletions
diff --git a/mod2/helm-generator/helmchartgenerator-core/src/main/java/org/onap/dcaegen2/platform/helmchartgenerator/chartbuilder/ChartBuilder.java b/mod2/helm-generator/helmchartgenerator-core/src/main/java/org/onap/dcaegen2/platform/helmchartgenerator/chartbuilder/ChartBuilder.java index ac73544..21f5dae 100644 --- a/mod2/helm-generator/helmchartgenerator-core/src/main/java/org/onap/dcaegen2/platform/helmchartgenerator/chartbuilder/ChartBuilder.java +++ b/mod2/helm-generator/helmchartgenerator-core/src/main/java/org/onap/dcaegen2/platform/helmchartgenerator/chartbuilder/ChartBuilder.java @@ -41,14 +41,20 @@ public class ChartBuilder { @Autowired private ChartGenerator chartGenerator; + @Autowired + private ChartTemplateStructureValidator validator; + /** * constructor of ChartBuilder * @param specParser implementation of ComponentSpecParser * @param chartGenerator implementation of ChartGenerator + * @param validator implementation of Chart Template Validator */ - public ChartBuilder(ComponentSpecParser specParser, ChartGenerator chartGenerator) { + public ChartBuilder(ComponentSpecParser specParser, ChartGenerator chartGenerator, + ChartTemplateStructureValidator validator) { this.specParser = specParser; this.chartGenerator = chartGenerator; + this.validator = validator; } /** @@ -61,7 +67,7 @@ public class ChartBuilder { * @throws Exception */ public File build(String specFileLocation, String chartTemplateLocation, String outputLocation, String specSchemaLocation ) throws Exception { - ChartTemplateStructureValidator.validateChartTemplateStructure(chartTemplateLocation); + validator.validateChartTemplateStructure(chartTemplateLocation); ChartInfo chartInfo = specParser.extractChartInfo(specFileLocation, chartTemplateLocation, specSchemaLocation); return chartGenerator.generate(chartTemplateLocation, chartInfo, outputLocation); } diff --git a/mod2/helm-generator/helmchartgenerator-core/src/main/java/org/onap/dcaegen2/platform/helmchartgenerator/chartbuilder/ChartGenerator.java b/mod2/helm-generator/helmchartgenerator-core/src/main/java/org/onap/dcaegen2/platform/helmchartgenerator/chartbuilder/ChartGenerator.java index b9980d7..be02d68 100644 --- a/mod2/helm-generator/helmchartgenerator-core/src/main/java/org/onap/dcaegen2/platform/helmchartgenerator/chartbuilder/ChartGenerator.java +++ b/mod2/helm-generator/helmchartgenerator-core/src/main/java/org/onap/dcaegen2/platform/helmchartgenerator/chartbuilder/ChartGenerator.java @@ -18,17 +18,14 @@ package org.onap.dcaegen2.platform.helmchartgenerator.chartbuilder; -import lombok.Setter; import lombok.extern.slf4j.Slf4j; +import org.onap.dcaegen2.platform.helmchartgenerator.Utils; import org.onap.dcaegen2.platform.helmchartgenerator.models.chartinfo.ChartInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.io.File; -import static org.onap.dcaegen2.platform.helmchartgenerator.Utils.cloneFileToTempLocation; -import static org.onap.dcaegen2.platform.helmchartgenerator.Utils.deleteTempFileLocation; - /** * ChartGenerator interacts with HelmClient and generates a packaged helm chart * @author Dhrumin Desai @@ -37,22 +34,25 @@ import static org.onap.dcaegen2.platform.helmchartgenerator.Utils.deleteTempFile @Slf4j public class ChartGenerator { - @Setter @Autowired private HelmClient helmClient; - @Setter @Autowired private KeyValueMerger merger; + @Autowired + private Utils utils; + /** * Constructor for ChartGenerator * @param helmClient HelmClient implementation * @param merger KeyValueMerger implementation + * @param utils */ - public ChartGenerator(HelmClient helmClient, KeyValueMerger merger) { + public ChartGenerator(HelmClient helmClient, KeyValueMerger merger, Utils utils) { this.helmClient = helmClient; this.merger = merger; + this.utils = utils; } /** @@ -63,11 +63,11 @@ public class ChartGenerator { * @return generated helm chart tgz file */ public File generate(String chartBlueprintLocation, ChartInfo chartInfo, String outputLocation) { - File newChartDir = cloneFileToTempLocation(chartBlueprintLocation + "/base"); + File newChartDir = utils.cloneFileToTempLocation(chartBlueprintLocation + "/base"); merger.mergeValuesToChart(chartInfo, newChartDir); helmClient.lint(newChartDir); final File chartLocation = helmClient.packageChart(newChartDir, outputLocation); - deleteTempFileLocation(newChartDir); + utils.deleteTempFileLocation(newChartDir); return chartLocation; } } diff --git a/mod2/helm-generator/helmchartgenerator-core/src/main/java/org/onap/dcaegen2/platform/helmchartgenerator/chartbuilder/ComponentSpecParser.java b/mod2/helm-generator/helmchartgenerator-core/src/main/java/org/onap/dcaegen2/platform/helmchartgenerator/chartbuilder/ComponentSpecParser.java index 942e5ae..b0830d1 100644 --- a/mod2/helm-generator/helmchartgenerator-core/src/main/java/org/onap/dcaegen2/platform/helmchartgenerator/chartbuilder/ComponentSpecParser.java +++ b/mod2/helm-generator/helmchartgenerator-core/src/main/java/org/onap/dcaegen2/platform/helmchartgenerator/chartbuilder/ComponentSpecParser.java @@ -18,7 +18,6 @@ package org.onap.dcaegen2.platform.helmchartgenerator.chartbuilder; -import org.jetbrains.annotations.NotNull; import org.onap.dcaegen2.platform.helmchartgenerator.Utils; import org.onap.dcaegen2.platform.helmchartgenerator.models.chartinfo.ChartInfo; import org.onap.dcaegen2.platform.helmchartgenerator.models.chartinfo.Metadata; @@ -55,12 +54,17 @@ public class ComponentSpecParser { @Autowired private ComponentSpecValidator specValidator; + @Autowired + private Utils utils; + /** * Constructor for ComponentSpecParser * @param specValidator ComponentSpecValidator implementation + * @param utils */ - public ComponentSpecParser(ComponentSpecValidator specValidator) { + public ComponentSpecParser(ComponentSpecValidator specValidator, Utils utils) { this.specValidator = specValidator; + this.utils = utils; } /** @@ -73,7 +77,7 @@ public class ComponentSpecParser { */ public ChartInfo extractChartInfo(String specFileLocation, String chartTemplateLocation, String specSchemaLocation) throws Exception { specValidator.validateSpecFile(specFileLocation, specSchemaLocation); - ComponentSpec cs = Utils.deserializeJsonFileToModel(specFileLocation, ComponentSpec.class); + ComponentSpec cs = utils.deserializeJsonFileToModel(specFileLocation, ComponentSpec.class); ChartInfo chartInfo = new ChartInfo(); chartInfo.setMetadata(extractMetadata(cs.getSelf())); chartInfo.setValues(extractValues(cs, chartTemplateLocation)); @@ -83,14 +87,14 @@ public class ComponentSpecParser { private Map<String, Object> extractValues(ComponentSpec cs, String chartTemplateLocation) { Map<String, Object> outerValues = new LinkedHashMap<>(); if(cs.getAuxilary() != null && cs.getAuxilary().getTlsInfo() != null){ - Utils.putIfNotNull(outerValues,"certDirectory", cs.getAuxilary().getTlsInfo().getCertDirectory()); - Utils.putIfNotNull(outerValues, "tlsServer", cs.getAuxilary().getTlsInfo().getUseTls()); + utils.putIfNotNull(outerValues,"certDirectory", cs.getAuxilary().getTlsInfo().getCertDirectory()); + utils.putIfNotNull(outerValues, "tlsServer", cs.getAuxilary().getTlsInfo().getUseTls()); } if(cs.getAuxilary() != null && cs.getAuxilary().getLogInfo() != null) { - Utils.putIfNotNull(outerValues,"logDirectory", cs.getAuxilary().getLogInfo().get("log_directory")); + utils.putIfNotNull(outerValues,"logDirectory", cs.getAuxilary().getLogInfo().get("log_directory")); } if(imageUriExistsForFirstArtifact(cs)){ - Utils.putIfNotNull(outerValues,"image", cs.getArtifacts()[0].getUri()); + utils.putIfNotNull(outerValues,"image", cs.getArtifacts()[0].getUri()); } populateApplicationConfigSection(outerValues, cs); populateReadinessSection(outerValues, cs); @@ -115,7 +119,7 @@ public class ComponentSpecParser { for(Parameters param : parameters){ applicationConfig.put(param.getName(), param.getValue()); } - Utils.putIfNotNull(outerValues,"applicationConfig", applicationConfig); + utils.putIfNotNull(outerValues,"applicationConfig", applicationConfig); } private void populateReadinessSection(Map<String, Object> outerValues, ComponentSpec cs) { @@ -124,7 +128,7 @@ public class ComponentSpecParser { Map<String, Object> readiness = new LinkedHashMap<>(); final HealthCheck healthcheck = cs.getAuxilary().getHealthcheck(); - Utils.putIfNotNull(readiness, "initialDelaySeconds", healthcheck.getInitialDelaySeconds()); + utils.putIfNotNull(readiness, "initialDelaySeconds", healthcheck.getInitialDelaySeconds()); if(healthcheck.getInterval() != null) { readiness.put("periodSeconds", getSeconds(healthcheck.getInterval(), "interval")); @@ -159,7 +163,7 @@ public class ComponentSpecParser { private void populateApplicationEnvSection(Map<String, Object> outerValues, ComponentSpec cs){ if(applicationEnvExists(cs)) { Object applicationEnv = cs.getAuxilary().getHelm().getApplicationEnv(); - Utils.putIfNotNull(outerValues,"applicationEnv", applicationEnv); + utils.putIfNotNull(outerValues,"applicationEnv", applicationEnv); } } @@ -178,10 +182,10 @@ public class ComponentSpecParser { if(serviceFromSpec.getPorts() != null){ List<Object> ports = mapServicePorts(serviceFromSpec.getPorts()); service.put("ports", ports); - Utils.putIfNotNull(service, "type", serviceFromSpec.getType()); + utils.putIfNotNull(service, "type", serviceFromSpec.getType()); } - Utils.putIfNotNull(service,"name", serviceFromSpec.getName()); - Utils.putIfNotNull(service,"has_internal_only_ports", serviceFromSpec.getHasInternalOnlyPorts()); + utils.putIfNotNull(service,"name", serviceFromSpec.getName()); + utils.putIfNotNull(service,"has_internal_only_ports", serviceFromSpec.getHasInternalOnlyPorts()); outerValues.put("service", service); } @@ -230,8 +234,8 @@ public class ComponentSpecParser { keystore.put("outputType", List.of("jks")); keystore.put("passwordSecretRef", passwordsSecretRef); certificate.put("mountPath", mountPath); - Utils.putIfNotNull(certificate,"commonName", cs.getSelf().getName()); - Utils.putIfNotNull(certificate,"dnsNames", List.of(cs.getSelf().getName())); + utils.putIfNotNull(certificate,"commonName", cs.getSelf().getName()); + utils.putIfNotNull(certificate,"dnsNames", List.of(cs.getSelf().getName())); certificate.put("keystore", keystore); outerValues.put("certificates", List.of(certificate)); } diff --git a/mod2/helm-generator/helmchartgenerator-core/src/main/java/org/onap/dcaegen2/platform/helmchartgenerator/chartbuilder/HelmClientImpl.java b/mod2/helm-generator/helmchartgenerator-core/src/main/java/org/onap/dcaegen2/platform/helmchartgenerator/chartbuilder/HelmClientImpl.java index 098ddda..83bc477 100644 --- a/mod2/helm-generator/helmchartgenerator-core/src/main/java/org/onap/dcaegen2/platform/helmchartgenerator/chartbuilder/HelmClientImpl.java +++ b/mod2/helm-generator/helmchartgenerator-core/src/main/java/org/onap/dcaegen2/platform/helmchartgenerator/chartbuilder/HelmClientImpl.java @@ -19,6 +19,7 @@ package org.onap.dcaegen2.platform.helmchartgenerator.chartbuilder; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.io.BufferedReader; @@ -26,6 +27,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.util.stream.Collectors; /** * HelmClient implementation which uses helm command installed in the runtime environment. @@ -35,12 +37,40 @@ import java.io.InputStreamReader; @Slf4j public class HelmClientImpl implements HelmClient { + private final String repoUrl; + + private final String username; + + private final String password; + + public HelmClientImpl(@Value("${chartmuseum.baseurl}")String repoUrl, + @Value("${chartmuseum.auth.basic.username}")String username, + @Value("${chartmuseum.auth.basic.password}")String password) { + this.repoUrl = repoUrl; + this.username = username; + this.password = password; + try{ + repoAdd(repoUrl, username,password); + }catch (Exception e){ + log.warn("Could not add helm repo."); + } + } + + private void repoAdd(String repoUrl, String username, String password) { + ProcessBuilder builder = new ProcessBuilder(); + builder.inheritIO(); + builder.command("helm", "repo", "add", "local", repoUrl, + "--username", username, "--password", password); + runProcess(builder, "repoAdd"); + } + /** * performs <code>helm lint</code> operation * @param chartLocation helm chart location */ @Override public void lint(File chartLocation) { + helmDepUp(chartLocation.getAbsolutePath()); ProcessBuilder builder = new ProcessBuilder(); builder.command("helm", "lint", chartLocation.getAbsolutePath()); runProcess(builder, "lint"); @@ -60,16 +90,24 @@ public class HelmClientImpl implements HelmClient { return runProcess(builder, "package"); } + private void helmDepUp(String chartLocation) { + ProcessBuilder builder = new ProcessBuilder(); + builder.inheritIO(); + builder.command("helm", "dep", "up",chartLocation); + runProcess(builder, "helmDepUp"); + } + private File runProcess(ProcessBuilder builder, String command) { + log.info("running: " + String.join(" ",builder.command())); Process process = null; String chartPath = ""; try { process = builder.start(); - if(command.equals("lint")) { - printLintingProcessOutput(process); + if(command.equals("package")) { + chartPath = printPackagingProcessOutput(process); } else { - chartPath = printPackagingProcessOutput(process); + printProcessOutput(process); } assertExitCode(process); } catch (IOException e) { @@ -83,7 +121,7 @@ public class HelmClientImpl implements HelmClient { return new File(chartPath); } - private void printLintingProcessOutput(Process process) throws IOException { + private void printProcessOutput(Process process) throws IOException { final InputStream inputStream = process.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); reader.lines().forEach(log::info); |