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/bake): Add additional input fields where we can fill in details of the APIs versions (backport #1020) #1028

Merged
merged 2 commits into from
Oct 18, 2023
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 @@ -3,12 +3,16 @@
import com.netflix.spinnaker.kork.artifacts.model.Artifact;
import com.netflix.spinnaker.rosco.manifests.BakeManifestRequest;
import java.util.List;
import javax.annotation.Nullable;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper = true)
public class HelmBakeManifestRequest extends BakeManifestRequest {
@Nullable String apiVersions;
@Nullable String kubeVersion;

String namespace;

/**
Expand All @@ -19,6 +23,13 @@ public class HelmBakeManifestRequest extends BakeManifestRequest {

boolean rawOverrides;

/**
* Helm v3 adds a new flag to include custom resource definition manifests in the templated
* output. In the previous versions crds were usually included as part of templates, so the `helm
* template` command always included them in the rendered output.
*/
boolean includeCRDs;

/**
* When the helm chart is (in) a git/repo artifact, the path to the chart.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
@Slf4j
Expand Down Expand Up @@ -108,6 +109,23 @@ public BakeRecipe buildBakeRecipe(BakeManifestEnvironment env, HelmBakeManifestR
command.add(namespace);
}

if (request.isIncludeCRDs()
&& request.getTemplateRenderer() == BakeManifestRequest.TemplateRenderer.HELM3) {
command.add("--include-crds");
}

String apiVersions = request.getApiVersions();
if (StringUtils.hasText(apiVersions)) {
command.add("--api-versions");
command.add(apiVersions);
}

String kubeVersion = request.getKubeVersion();
if (StringUtils.hasText(kubeVersion)) {
command.add("--kube-version");
command.add(kubeVersion);
}

Map<String, Object> overrides = request.getOverrides();
if (!overrides.isEmpty()) {
List<String> overrideList = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doThrow;
Expand Down Expand Up @@ -342,6 +343,111 @@ public void buildBakeRecipeWithGitRepoArtifactUsingHelmChartFilePath(@TempDir Pa
}
}

@Test
public void buildBakeRecipeIncludingHelmVersionsOptionsWithHelm3() throws IOException {
ArtifactDownloader artifactDownloader = mock(ArtifactDownloader.class);
RoscoHelmConfigurationProperties helmConfigurationProperties =
new RoscoHelmConfigurationProperties();
HelmTemplateUtils helmTemplateUtils =
new HelmTemplateUtils(artifactDownloader, helmConfigurationProperties);

HelmBakeManifestRequest request = new HelmBakeManifestRequest();
Artifact artifact = Artifact.builder().build();
request.setTemplateRenderer(BakeManifestRequest.TemplateRenderer.HELM3);
request.setApiVersions("customApiVersion");
request.setKubeVersion("customKubernetesVersion");
request.setInputArtifacts(Collections.singletonList(artifact));
request.setOverrides(Collections.emptyMap());

try (BakeManifestEnvironment env = BakeManifestEnvironment.create()) {
BakeRecipe bakeRecipe = helmTemplateUtils.buildBakeRecipe(env, request);
assertTrue(bakeRecipe.getCommand().contains("--api-versions"));
assertTrue(bakeRecipe.getCommand().indexOf("--api-versions") > 3);
assertTrue(bakeRecipe.getCommand().contains("--kube-version"));
assertTrue(bakeRecipe.getCommand().indexOf("--kube-version") > 5);
}
}

@Test
public void buildBakeRecipeIncludingHelmVersionsOptionsWithHelm2() throws IOException {
ArtifactDownloader artifactDownloader = mock(ArtifactDownloader.class);
RoscoHelmConfigurationProperties helmConfigurationProperties =
new RoscoHelmConfigurationProperties();
HelmTemplateUtils helmTemplateUtils =
new HelmTemplateUtils(artifactDownloader, helmConfigurationProperties);

HelmBakeManifestRequest request = new HelmBakeManifestRequest();
Artifact artifact = Artifact.builder().build();
request.setTemplateRenderer(BakeManifestRequest.TemplateRenderer.HELM2);
request.setApiVersions("customApiVersion");
request.setKubeVersion("customKubernetesVersion");
request.setInputArtifacts(Collections.singletonList(artifact));
request.setOverrides(Collections.emptyMap());

try (BakeManifestEnvironment env = BakeManifestEnvironment.create()) {
BakeRecipe bakeRecipe = helmTemplateUtils.buildBakeRecipe(env, request);
assertTrue(bakeRecipe.getCommand().contains("--api-versions"));
assertTrue(bakeRecipe.getCommand().indexOf("--api-versions") > 3);
assertTrue(bakeRecipe.getCommand().contains("--kube-version"));
assertTrue(bakeRecipe.getCommand().indexOf("--kube-version") > 5);
}
}

@Test
public void buildBakeRecipeIncludingCRDsWithHelm3() throws IOException {
ArtifactDownloader artifactDownloader = mock(ArtifactDownloader.class);
RoscoHelmConfigurationProperties helmConfigurationProperties =
new RoscoHelmConfigurationProperties();
HelmTemplateUtils helmTemplateUtils =
new HelmTemplateUtils(artifactDownloader, helmConfigurationProperties);

HelmBakeManifestRequest request = new HelmBakeManifestRequest();
Artifact artifact = Artifact.builder().build();
request.setTemplateRenderer(BakeManifestRequest.TemplateRenderer.HELM3);
request.setIncludeCRDs(true);
request.setInputArtifacts(Collections.singletonList(artifact));
request.setOverrides(Collections.emptyMap());

try (BakeManifestEnvironment env = BakeManifestEnvironment.create()) {
BakeRecipe recipe = helmTemplateUtils.buildBakeRecipe(env, request);
assertTrue(recipe.getCommand().contains("--include-crds"));
// Assert that the flag position goes after 'helm template' subcommand
assertTrue(recipe.getCommand().indexOf("--include-crds") > 1);
}
}

@ParameterizedTest
@MethodSource("helmRendererArgsCRDs")
public void buildBakeRecipeNotIncludingCRDs(
boolean includeCRDs, BakeManifestRequest.TemplateRenderer templateRenderer)
throws IOException {
ArtifactDownloader artifactDownloader = mock(ArtifactDownloader.class);
RoscoHelmConfigurationProperties helmConfigurationProperties =
new RoscoHelmConfigurationProperties();
HelmTemplateUtils helmTemplateUtils =
new HelmTemplateUtils(artifactDownloader, helmConfigurationProperties);

HelmBakeManifestRequest request = new HelmBakeManifestRequest();
Artifact artifact = Artifact.builder().build();
request.setInputArtifacts(Collections.singletonList(artifact));
request.setOverrides(Collections.emptyMap());

request.setTemplateRenderer(templateRenderer);
request.setIncludeCRDs(includeCRDs);

try (BakeManifestEnvironment env = BakeManifestEnvironment.create()) {
BakeRecipe recipe = helmTemplateUtils.buildBakeRecipe(env, request);
assertFalse(recipe.getCommand().contains("--include-crds"));
}
}

private static Stream<Arguments> helmRendererArgsCRDs() {
return Stream.of(
Arguments.of(true, BakeManifestRequest.TemplateRenderer.HELM2),
Arguments.of(false, BakeManifestRequest.TemplateRenderer.HELM2),
Arguments.of(false, BakeManifestRequest.TemplateRenderer.HELM3));
}

@Test
public void httpExceptionDownloading() throws IOException {
// When artifactDownloader throws a SpinnakerHttpException, make sure we
Expand Down
Loading