diff options
Diffstat (limited to 'src/main/java/org/onap/sdc/helmvalidator/helm/versions')
7 files changed, 334 insertions, 0 deletions
diff --git a/src/main/java/org/onap/sdc/helmvalidator/helm/versions/ApiVersionsReader.java b/src/main/java/org/onap/sdc/helmvalidator/helm/versions/ApiVersionsReader.java new file mode 100644 index 0000000..e5c1a2e --- /dev/null +++ b/src/main/java/org/onap/sdc/helmvalidator/helm/versions/ApiVersionsReader.java @@ -0,0 +1,80 @@ +/* + * ============LICENSE_START======================================================= + * SDC-HELM-VALIDATOR + * ================================================================================ + * Copyright (C) 2021 Nokia. 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.sdc.helmvalidator.helm.versions; + +import java.io.BufferedReader; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.file.Path; +import java.util.Optional; +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; +import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; +import org.onap.sdc.helmvalidator.helm.versions.exception.ApiVersionNotFoundException; +import org.onap.sdc.helmvalidator.helm.versions.exception.ReadFileException; +import org.springframework.stereotype.Service; + +@Service +public class ApiVersionsReader { + + private static final int MAIN_CHART_DIR_DEPTH = 2; + private static final String API_VERSION_PREFIX = "apiVersion:"; + private static final Path CHART_FILE_NAME = Path.of("Chart.yaml"); + + String readVersion(String chartPath) { + return tryReadVersionFromChart(chartPath) + .orElseThrow(ApiVersionNotFoundException::new); + } + + private Optional<String> tryReadVersionFromChart(String chartPath) { + try (TarArchiveInputStream tarInput = new TarArchiveInputStream( + new GzipCompressorInputStream(new FileInputStream(chartPath)))) { + return readVersionFromChart(tarInput); + } catch (IOException e) { + throw new ReadFileException("Cannot read tar from path: " + chartPath, e); + } + } + + private Optional<String> readVersionFromChart(TarArchiveInputStream tarInput) throws IOException { + TarArchiveEntry currentEntry; + while ((currentEntry = tarInput.getNextTarEntry()) != null) { + if (isMainChartYaml(currentEntry)) { + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(tarInput)); + return bufferedReader.lines() + .filter(chartLine -> chartLine.contains(API_VERSION_PREFIX)) + .map(apiVersionLine -> apiVersionLine.replaceAll(API_VERSION_PREFIX, "")) + .map(String::trim) + .findFirst(); + } + } + return Optional.empty(); + } + + private boolean isMainChartYaml(TarArchiveEntry currentEntry) { + Path entryPath = Path.of(currentEntry.getName()); + return currentEntry.isFile() + && CHART_FILE_NAME.equals(entryPath.getFileName()) + && (entryPath.getNameCount() == MAIN_CHART_DIR_DEPTH); + } + + +} diff --git a/src/main/java/org/onap/sdc/helmvalidator/helm/versions/ChartBasedVersionProvider.java b/src/main/java/org/onap/sdc/helmvalidator/helm/versions/ChartBasedVersionProvider.java new file mode 100644 index 0000000..b892a33 --- /dev/null +++ b/src/main/java/org/onap/sdc/helmvalidator/helm/versions/ChartBasedVersionProvider.java @@ -0,0 +1,57 @@ +/* + * ============LICENSE_START======================================================= + * SDC-HELM-VALIDATOR + * ================================================================================ + * Copyright (C) 2021 Nokia. 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.sdc.helmvalidator.helm.versions; + +import org.onap.sdc.helmvalidator.helm.versions.exception.NotSupportedApiVersionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class ChartBasedVersionProvider { + + private final SupportedVersionsProvider supportedVersionsProvider; + private final ApiVersionsReader apiVersionsReader; + + @Autowired + public ChartBasedVersionProvider( + SupportedVersionsProvider supportedVersionsProvider, + ApiVersionsReader apiVersionsReader) { + this.supportedVersionsProvider = supportedVersionsProvider; + this.apiVersionsReader = apiVersionsReader; + } + + public String getVersion(String chartPath) { + String apiVersion = apiVersionsReader.readVersion(chartPath); + return mapToChartVersion(apiVersion); + } + + private String mapToChartVersion(String apiVersion) { + switch (apiVersion) { + case "v1": + return supportedVersionsProvider.getLatestVersion("2"); + case "v2": + return supportedVersionsProvider.getLatestVersion("3"); + default: + throw new NotSupportedApiVersionException("Cannot obtain Helm version from API version: " + apiVersion); + } + } + +} diff --git a/src/main/java/org/onap/sdc/helmvalidator/helm/versions/SupportedVersionsProvider.java b/src/main/java/org/onap/sdc/helmvalidator/helm/versions/SupportedVersionsProvider.java new file mode 100644 index 0000000..ff1cdde --- /dev/null +++ b/src/main/java/org/onap/sdc/helmvalidator/helm/versions/SupportedVersionsProvider.java @@ -0,0 +1,65 @@ +/* + * ============LICENSE_START======================================================= + * SDC-HELM-VALIDATOR + * ================================================================================ + * Copyright (C) 2021 Nokia. 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.sdc.helmvalidator.helm.versions; + +import java.util.Comparator; +import java.util.List; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import org.onap.sdc.helmvalidator.helm.validation.exception.NotSupportedVersionException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class SupportedVersionsProvider { + + private final SystemEnvVersionsReader versionsReader; + + @Autowired + public SupportedVersionsProvider(SystemEnvVersionsReader versionsReader) { + this.versionsReader = versionsReader; + } + + /** + * Retrieves list of available Helm client versions. + * + * @return list of available Helm client versions + */ + public List<String> getVersions() { + return versionsReader.readVersions().stream() + .filter(Predicate.not(String::isBlank)) + .sorted(Comparator.reverseOrder()) + .collect(Collectors.toList()); + } + + /** + * Retrieves latest available Helm client with given major version. + * + * @param helmMajorVersion major version of Helm client + * @return latest available Helm client with given major version + */ + public String getLatestVersion(String helmMajorVersion) { + return getVersions().stream() + .filter(supportedVersion -> supportedVersion.startsWith(helmMajorVersion + ".")) + .findFirst() + .orElseThrow(() -> new NotSupportedVersionException(helmMajorVersion)); + } +} diff --git a/src/main/java/org/onap/sdc/helmvalidator/helm/versions/SystemEnvVersionsReader.java b/src/main/java/org/onap/sdc/helmvalidator/helm/versions/SystemEnvVersionsReader.java new file mode 100644 index 0000000..8cccd2b --- /dev/null +++ b/src/main/java/org/onap/sdc/helmvalidator/helm/versions/SystemEnvVersionsReader.java @@ -0,0 +1,47 @@ +/* + * ============LICENSE_START======================================================= + * SDC-HELM-VALIDATOR + * ================================================================================ + * Copyright (C) 2021 Nokia. 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.sdc.helmvalidator.helm.versions; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import org.springframework.stereotype.Service; + +@Service +public class SystemEnvVersionsReader { + + private static final String HELM_SUPPORTED_VERSIONS = "HELM_SUPPORTED_VERSIONS"; + private static final String DELIMITER = ","; + + List<String> readVersions() { + return Arrays.stream(getSupportedVersionsFromEnv() + .split(DELIMITER)) + .filter(Predicate.not(String::isBlank)) + .collect(Collectors.toList()); + } + + String getSupportedVersionsFromEnv() { + return Optional.ofNullable(System.getenv(HELM_SUPPORTED_VERSIONS)) + .orElse(""); + } +} diff --git a/src/main/java/org/onap/sdc/helmvalidator/helm/versions/exception/ApiVersionNotFoundException.java b/src/main/java/org/onap/sdc/helmvalidator/helm/versions/exception/ApiVersionNotFoundException.java new file mode 100644 index 0000000..fadcc2a --- /dev/null +++ b/src/main/java/org/onap/sdc/helmvalidator/helm/versions/exception/ApiVersionNotFoundException.java @@ -0,0 +1,29 @@ +/* + * ============LICENSE_START======================================================= + * SDC-HELM-VALIDATOR + * ================================================================================ + * Copyright (C) 2021 Nokia. 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.sdc.helmvalidator.helm.versions.exception; + +public class ApiVersionNotFoundException extends RuntimeException { + + public ApiVersionNotFoundException() { + super("Cannot find apiVersion value in a main chart"); + } + +} diff --git a/src/main/java/org/onap/sdc/helmvalidator/helm/versions/exception/NotSupportedApiVersionException.java b/src/main/java/org/onap/sdc/helmvalidator/helm/versions/exception/NotSupportedApiVersionException.java new file mode 100644 index 0000000..368f9f1 --- /dev/null +++ b/src/main/java/org/onap/sdc/helmvalidator/helm/versions/exception/NotSupportedApiVersionException.java @@ -0,0 +1,28 @@ +/* + * ============LICENSE_START======================================================= + * SDC-HELM-VALIDATOR + * ================================================================================ + * Copyright (C) 2021 Nokia. 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.sdc.helmvalidator.helm.versions.exception; + +public class NotSupportedApiVersionException extends RuntimeException { + + public NotSupportedApiVersionException(String message) { + super(message); + } +} diff --git a/src/main/java/org/onap/sdc/helmvalidator/helm/versions/exception/ReadFileException.java b/src/main/java/org/onap/sdc/helmvalidator/helm/versions/exception/ReadFileException.java new file mode 100644 index 0000000..a6a1ec4 --- /dev/null +++ b/src/main/java/org/onap/sdc/helmvalidator/helm/versions/exception/ReadFileException.java @@ -0,0 +1,28 @@ +/* + * ============LICENSE_START======================================================= + * SDC-HELM-VALIDATOR + * ================================================================================ + * Copyright (C) 2021 Nokia. 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.sdc.helmvalidator.helm.versions.exception; + +public class ReadFileException extends RuntimeException { + + public ReadFileException(String message, Throwable cause) { + super(message, cause); + } +} |