diff options
Diffstat (limited to 'mod2/helm-generator/helmchartgenerator-core/src/main/java/org/onap/dcaegen2/platform/helmchartgenerator/chartbuilder')
4 files changed, 99 insertions, 9 deletions
diff --git a/mod2/helm-generator/helmchartgenerator-core/src/main/java/org/onap/dcaegen2/platform/helmchartgenerator/chartbuilder/AddOnsManager.java b/mod2/helm-generator/helmchartgenerator-core/src/main/java/org/onap/dcaegen2/platform/helmchartgenerator/chartbuilder/AddOnsManager.java new file mode 100644 index 0000000..3aff2f0 --- /dev/null +++ b/mod2/helm-generator/helmchartgenerator-core/src/main/java/org/onap/dcaegen2/platform/helmchartgenerator/chartbuilder/AddOnsManager.java @@ -0,0 +1,81 @@ +/* + * # ============LICENSE_START======================================================= + * # Copyright (c) 2021 AT&T Intellectual Property. All rights reserved. + * # ================================================================================ + * # Licensed under the Apache License, Version 2.0 (the "License"); + * # you may not use this file except in compliance with the License. + * # You may obtain a copy of the License at + * # + * # http://www.apache.org/licenses/LICENSE-2.0 + * # + * # Unless required by applicable law or agreed to in writing, software + * # distributed under the License is distributed on an "AS IS" BASIS, + * # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * # See the License for the specific language governing permissions and + * # limitations under the License. + * # ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.helmchartgenerator.chartbuilder; + +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.FileUtils; +import org.onap.dcaegen2.platform.helmchartgenerator.Utils; +import org.onap.dcaegen2.platform.helmchartgenerator.models.componentspec.base.ComponentSpec; +import org.onap.dcaegen2.platform.helmchartgenerator.models.componentspec.common.TlsInfo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +/** + * manages addOn template files + */ +@Slf4j +@Component +public class AddOnsManager { + + public AddOnsManager(Utils utils) { + this.utils = utils; + } + + @Autowired + private Utils utils; + + /** + * include addons template files based on parameters in componentSpec file + * @param specFileLocation spec file location + * @param chart chart directory + * @param chartTemplateLocation chart template location + */ + public void includeAddons(String specFileLocation, File chart, String chartTemplateLocation) { + if(externalTlsExists(specFileLocation)){ + includeCertificateYamlAddOn(chart, chartTemplateLocation); + } + } + + private void includeCertificateYamlAddOn(File chart, String chartTemplateLocation) { + Path certificateYaml = Paths.get(chartTemplateLocation, "addons/templates/certificates.yaml"); + if(!Files.exists(certificateYaml)) { + throw new RuntimeException("certificates.yaml not found under templates directory in addons"); + } + try { + File templates = new File(chart, "templates"); + FileUtils.copyFileToDirectory(certificateYaml.toFile(), templates); + } catch (IOException e) { + log.error(e.getMessage(), e); + throw new RuntimeException("could not add certificates.yaml to templates directory"); + } + } + + private boolean externalTlsExists(String specFileLocation) { + ComponentSpec cs = utils.deserializeJsonFileToModel(specFileLocation, ComponentSpec.class); + TlsInfo tlsInfo = cs.getAuxilary().getTlsInfo(); + return tlsInfo != null && tlsInfo.getUseExternalTls() != null && tlsInfo.getUseExternalTls(); + } +} + 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 21f5dae..677466b 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 @@ -69,6 +69,6 @@ public class ChartBuilder { public File build(String specFileLocation, String chartTemplateLocation, String outputLocation, String specSchemaLocation ) throws Exception { validator.validateChartTemplateStructure(chartTemplateLocation); ChartInfo chartInfo = specParser.extractChartInfo(specFileLocation, chartTemplateLocation, specSchemaLocation); - return chartGenerator.generate(chartTemplateLocation, chartInfo, outputLocation); + return chartGenerator.generate(chartTemplateLocation, chartInfo, outputLocation, specFileLocation); } } 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 be02d68..c884e47 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 @@ -43,16 +43,21 @@ public class ChartGenerator { @Autowired private Utils utils; + @Autowired + private AddOnsManager addOnsManager; + /** * Constructor for ChartGenerator * @param helmClient HelmClient implementation * @param merger KeyValueMerger implementation * @param utils + * @param addOnsManager */ - public ChartGenerator(HelmClient helmClient, KeyValueMerger merger, Utils utils) { + public ChartGenerator(HelmClient helmClient, KeyValueMerger merger, Utils utils, AddOnsManager addOnsManager) { this.helmClient = helmClient; this.merger = merger; this.utils = utils; + this.addOnsManager = addOnsManager; } /** @@ -60,10 +65,12 @@ public class ChartGenerator { * @param chartBlueprintLocation location of the base helm chart template * @param chartInfo chartInfo object with key-values parsed from the specfile. * @param outputLocation location to store the helm chart + * @param specFileLocation * @return generated helm chart tgz file */ - public File generate(String chartBlueprintLocation, ChartInfo chartInfo, String outputLocation) { + public File generate(String chartBlueprintLocation, ChartInfo chartInfo, String outputLocation, String specFileLocation) { File newChartDir = utils.cloneFileToTempLocation(chartBlueprintLocation + "/base"); + addOnsManager.includeAddons(specFileLocation, newChartDir, chartBlueprintLocation); merger.mergeValuesToChart(chartInfo, newChartDir); helmClient.lint(newChartDir); final File chartLocation = helmClient.packageChart(newChartDir, outputLocation); 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 9a9f8f1..82d70c8 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 @@ -243,7 +243,8 @@ public class ComponentSpecParser { Map<String, Object> keystore = new LinkedHashMap<>(); Map<String, Object> passwordsSecretRef = new LinkedHashMap<>(); TlsInfo tlsInfo = cs.getAuxilary().getTlsInfo(); - String componentName = getComponentNameWithOmitFirstWord(cs); + String componentName = getComponentNameWithOmitFirstWordAndTrimHyphens(cs); + outerValues.put("useCmpv2Certificates", false); if(externalTlsExists(tlsInfo)) { String mountPath = tlsInfo.getCertDirectory(); if(tlsInfo.getUseExternalTls() != null && tlsInfo.getUseExternalTls()) { @@ -260,15 +261,16 @@ public class ComponentSpecParser { utils.putIfNotNull(certificate,"dnsNames", List.of(cs.getSelf().getName())); certificate.put("keystore", keystore); outerValues.put("certificates", List.of(certificate)); + outerValues.put("useCmpv2Certificates", true); } } - private String getComponentNameWithOmitFirstWord(ComponentSpec cs) { - return cs.getSelf().getName().substring(cs.getSelf().getName().indexOf("-") + 1); + private String getComponentNameWithOmitFirstWordAndTrimHyphens(ComponentSpec cs) { + return cs.getSelf().getName().substring(cs.getSelf().getName().indexOf("-") + 1).replaceAll("-",""); } private boolean externalTlsExists(TlsInfo tlsInfo) { - return tlsInfo != null && tlsInfo.getUseExternalTls() != null && tlsInfo.getUseExternalTls().equals(true); + return tlsInfo != null && tlsInfo.getUseExternalTls() != null && tlsInfo.getUseExternalTls(); } private void checkCertificateYamlExists(String chartTemplateLocation) { @@ -301,7 +303,7 @@ public class ComponentSpecParser { private void populatePostgresSection(Map<String, Object> outerValues, ComponentSpec cs) { if(cs.getAuxilary().getDatabases() != null) { String componentFullName = cs.getSelf().getName(); - String component = getComponentNameWithOmitFirstWord(cs); + String component = getComponentNameWithOmitFirstWordAndTrimHyphens(cs); Map<String, Object> postgres = new LinkedHashMap<>(); Map<String, Object> service = new LinkedHashMap<>(); Map<String, Object> container = new LinkedHashMap<>(); @@ -332,7 +334,7 @@ public class ComponentSpecParser { private void populateSecretsSection(Map<String, Object> outerValues, ComponentSpec cs) { if(cs.getAuxilary().getDatabases() != null) { - String component = getComponentNameWithOmitFirstWord(cs); + String component = getComponentNameWithOmitFirstWordAndTrimHyphens(cs); List<Object> secrets = new ArrayList<>(); Map<String, Object> secret = new LinkedHashMap<>(); secret.put("uid", "pg-user-creds"); |