Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat : helm dependency update support added to HelmService #3098

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public class HelmConfig {
private String security;
private boolean lintStrict;
private boolean lintQuiet;
private boolean debug;
private boolean dependencyVerify;
private boolean dependencySkipRefresh;


@JsonProperty("dependencies")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand All @@ -32,6 +33,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.marcnuri.helm.DependencyCommand;
import com.marcnuri.helm.Helm;
import com.marcnuri.helm.LintCommand;
import com.marcnuri.helm.LintResult;
Expand Down Expand Up @@ -167,6 +169,26 @@ public void uploadHelmChart(HelmConfig helm) throws BadUploadException, IOExcept
}
}

public void dependencyUpdate(HelmConfig helmConfig) {
for (HelmConfig.HelmType helmType : helmConfig.getTypes()) {
logger.info("Running Helm Dependency Upgrade %s %s", helmConfig.getChart(), helmConfig.getVersion());
DependencyCommand.DependencySubcommand<String> dependencyUpdateCommand = new Helm(Paths.get(helmConfig.getOutputDir(), helmType.getOutputDir()))
.dependency().update();
if (helmConfig.isDebug()) {
dependencyUpdateCommand.debug();
}
if (helmConfig.isDependencyVerify()) {
dependencyUpdateCommand.verify();
}
if (helmConfig.isDependencySkipRefresh()) {
dependencyUpdateCommand.skipRefresh();
}
Arrays.stream(dependencyUpdateCommand.call()
.split(System.lineSeparator()))
.forEach(l -> logger.info("[[W]]%s", l));
}
}

public void lint(HelmConfig helmConfig) {
for (HelmConfig.HelmType helmType : helmConfig.getTypes()) {
final Path helmPackage = resolveTarballFile(helmConfig, helmType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public class HelmServiceUtil {

protected static final String PROPERTY_HELM_LINT_STRICT = "jkube.helm.lint.strict";
protected static final String PROPERTY_HELM_LINT_QUIET = "jkube.helm.lint.quiet";
protected static final String PROPERTY_HELM_DEBUG = "jkube.helm.debug";
protected static final String PROPERTY_HELM_DEPENDENCY_VERIFY = "jkube.helm.dependencyVerify";
protected static final String PROPERTY_HELM_DEPENDENCY_SKIP_REFRESH = "jkube.helm.dependencySkipRefresh";

private HelmServiceUtil() { }

Expand Down Expand Up @@ -117,6 +120,9 @@ public static HelmConfig.HelmConfigBuilder initHelmConfig(
helmConfig::getOutputDir));
helmConfig.setLintStrict(resolveBooleanFromPropertyOrDefault(PROPERTY_HELM_LINT_STRICT, project, helmConfig::isLintStrict));
helmConfig.setLintQuiet(resolveBooleanFromPropertyOrDefault(PROPERTY_HELM_LINT_QUIET, project, helmConfig::isLintQuiet));
helmConfig.setDebug(resolveBooleanFromPropertyOrDefault(PROPERTY_HELM_DEBUG, project, helmConfig::isDebug));
helmConfig.setDependencyVerify(resolveBooleanFromPropertyOrDefault(PROPERTY_HELM_DEPENDENCY_VERIFY, project, helmConfig::isDependencyVerify));
helmConfig.setDependencySkipRefresh(resolveBooleanFromPropertyOrDefault(PROPERTY_HELM_DEPENDENCY_SKIP_REFRESH, project, helmConfig::isDependencySkipRefresh));
return helmConfig.toBuilder();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.kit.resource.helm;

import com.marcnuri.helm.Helm;
import org.eclipse.jkube.kit.common.JKubeConfiguration;
import org.eclipse.jkube.kit.common.KitLogger;
import org.eclipse.jkube.kit.config.resource.ResourceServiceConfig;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.Collections;

import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@DisplayName("HelmService.dependencyUpdate")
class HelmServiceDependencyUpdateIT {
@TempDir
private Path tempDir;
private HelmConfig helmConfig;
private HelmService helmService;
private KitLogger kitLogger;

@BeforeEach
void setUp() throws URISyntaxException {
kitLogger = spy(new KitLogger.SilentLogger());
Path outputDir = tempDir.resolve("output");
helmConfig = HelmConfig.builder()
.chart("helm-test")
.version("0.1.0")
.chartExtension("tgz")
.types(Collections.singletonList(HelmConfig.HelmType.KUBERNETES))
.tarballOutputDir(outputDir.toFile().getAbsolutePath())
.dependencies(Collections.singletonList(HelmDependency.builder()
.name("the-dependency")
.version("0.1.0")
.repository("file://../../the-dependency")
.build()))
.outputDir(outputDir.toString())
.sourceDir(new File(HelmServiceDependencyUpdateIT.class.getResource("/it/sources").toURI()).getAbsolutePath())
.build();
helmService = new HelmService(JKubeConfiguration.builder().build(), new ResourceServiceConfig(), kitLogger);
}

@Nested
@DisplayName("valid helm chart provided")
class ValidChart {
@BeforeEach
void validChartPackage() throws IOException {
helmService.generateHelmCharts(helmConfig);
}

@Test
@DisplayName("valid dependency provided, then dependency pulled")
void whenNoExplicitDependencyConfigProvided_thenDependencyDownloaded() {
// Given
Helm.create().withName("the-dependency").withDir(tempDir).call();
// When
helmService.dependencyUpdate(helmConfig);
// Then
verifyHelmDependencyDownloaded();
}

@Test
void whenConfigurationOptionsProvided_thenDependencyDownloaded() {
// Given
helmConfig = helmConfig.toBuilder()
.debug(true)
.dependencySkipRefresh(true)
.dependencyVerify(true)
.build();
Helm.create().withName("the-dependency").withDir(tempDir).call();
// When
helmService.dependencyUpdate(helmConfig);
// Then
verifyHelmDependencyDownloaded();
}

@Test
@DisplayName("non existing dependency provided, then throw exception")
void whenInvalidDependencyProvided_thenThrowException() {
// When + Then
assertThatIllegalStateException()
.isThrownBy(() -> helmService.dependencyUpdate(helmConfig))
.withMessageContaining("not found");
}

private void verifyHelmDependencyDownloaded() {
verify(kitLogger, times(1))
.info("Running Helm Dependency Upgrade %s %s", "helm-test", "0.1.0");
verify(kitLogger, times(1))
.info("[[W]]%s", "Saving 1 charts");
verify(kitLogger, times(1))
.info("[[W]]%s", "Deleting outdated charts");
}
}

@Test
@DisplayName("invalid chart provided, then throw exception")
void whenInvalidChartDirProvided_thenThrowException() {
// When + Then
assertThatIllegalStateException()
.isThrownBy(() -> helmService.dependencyUpdate(helmConfig))
.withMessageContaining("no such file or directory");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ void initHelmConfig_withOriginalConfig_shouldInitConfigWithoutOverriding() throw
.outputDir("output")
.lintStrict(true)
.lintQuiet(true)
.debug(true)
.dependencySkipRefresh(true)
.dependencyVerify(true)
.build();
// When
final HelmConfig result = HelmServiceUtil
Expand All @@ -127,7 +130,10 @@ void initHelmConfig_withOriginalConfig_shouldInitConfigWithoutOverriding() throw
.hasFieldOrPropertyWithValue("sourceDir", "sources")
.hasFieldOrPropertyWithValue("outputDir", "output")
.hasFieldOrPropertyWithValue("lintStrict", true)
.hasFieldOrPropertyWithValue("lintQuiet", true);
.hasFieldOrPropertyWithValue("lintQuiet", true)
.hasFieldOrPropertyWithValue("debug", true)
.hasFieldOrPropertyWithValue("dependencySkipRefresh", true)
.hasFieldOrPropertyWithValue("dependencyVerify", true);
}

@Test
Expand Down Expand Up @@ -164,6 +170,21 @@ void initHelmConfig_withLintProperties_shouldInitConfigWithLintSettings() throws
.hasFieldOrPropertyWithValue("lintQuiet", true);
}

@Test
void initHelmConfig_withHelmDependencyProperties_shouldInitConfigWithHelmDependencySettings() throws IOException {
// Given
javaProject.getProperties().put("jkube.helm.dependencyVerify", "True");
javaProject.getProperties().put("jkube.helm.dependencySkipRefresh", "trUe");
// When
final HelmConfig result = HelmServiceUtil
.initHelmConfig(HelmConfig.HelmType.KUBERNETES, javaProject, templateDir, null)
.build();
// Then
assertThat(result)
.hasFieldOrPropertyWithValue("dependencyVerify", true)
.hasFieldOrPropertyWithValue("dependencySkipRefresh", true);
}

@Test
void initHelmConfig_whenValuesSchemaJsonPresentInProjectBaseDir_thenAddToHelmConfig() throws IOException {
// Given
Expand Down