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

Use include-crds flags when using helm v3 #1102

Merged
merged 8 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from 7 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
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ install:
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b "$(go env GOPATH)/bin" "v1.24.0"

# Install Helm CLI. Do not install Tiller.
- curl -LO https://get.helm.sh/helm-v3.0.0-linux-amd64.tar.gz
- tar -xvf helm-v3.0.0-linux-amd64.tar.gz
- curl -LO https://get.helm.sh/helm-v3.2.1-linux-amd64.tar.gz
- tar -xvf helm-v3.2.1-linux-amd64.tar.gz
- sudo mv linux-amd64/helm /usr/local/bin
- helm repo add stable https://kubernetes-charts.storage.googleapis.com/
- helm repo update
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## HEAD (Unreleased)

- Support helm v3 `include-crds` argument. (https://github.com/pulumi/pulumi-kubernetes/pull/1102)

## 2.1.1 (May 8, 2020)

- Python and .NET packages failed to publish for 2.1.0, so bumping release version.
Expand Down
23 changes: 22 additions & 1 deletion provider/pkg/gen/nodejs-templates/helm/v2/helm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export class Chart extends yaml.CollectionComponentResource {
try {
let chart: string;
let defaultValues: string;
let cmd: string;
if (isChartOpts(cfg)) {
// Fetch chart.
if (cfg.repo && cfg.repo.includes("http")) {
Expand Down Expand Up @@ -203,7 +204,27 @@ export class Chart extends yaml.CollectionComponentResource {
const namespaceArg = cfg.namespace
? `--namespace ${shell.quote([cfg.namespace])}`
: "";
let cmd = `helm template ${chart} --name-template ${release} --values ${defaultValues} --values ${values} ${apiVersionsArgs} ${namespaceArg}`;

// Check the helm version - v2 or v3
let helmVerCmd = `helm version --short || true`;
const helmVer = execSync(
helmVerCmd,
{
stdio: ['pipe', 'pipe', 'ignore'], // Suppress tiller version error
},
).toString();

// Helm v2 returns version like this:
// Client: v2.16.7+g5f2584f
// Helm v3 returns a version like this:
// v3.1.2+gd878d4d
// --include-crds is available in helm v3.1+ so check for a regex matching that version
let r = RegExp('^v3\.[1-9]');
if (r.test(helmVer)) {
cmd = `helm template ${chart} --include-crds --name-template ${release} --values ${defaultValues} --values ${values} ${apiVersionsArgs} ${namespaceArg}`;
jaxxstorm marked this conversation as resolved.
Show resolved Hide resolved
} else {
cmd = `helm template ${chart} --name-template ${release} --values ${defaultValues} --values ${values} ${apiVersionsArgs} ${namespaceArg}`;
}

const yamlStream = execSync(
cmd,
Expand Down
19 changes: 19 additions & 0 deletions provider/pkg/gen/python-templates/helm/v2/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os.path
import shutil
import subprocess
import re
from tempfile import mkdtemp, mkstemp
from typing import Any, Callable, List, Optional, TextIO, Tuple, Union

Expand Down Expand Up @@ -279,6 +280,22 @@ def _run_helm_cmd(all_config: Tuple[List[Union[str, bytes]], Any]) -> str:
yaml_str: str = output.stdout
return yaml_str

def _is_helm_v3() -> bool:

cmd: List[str] = ['helm', 'version', '--short']

"""
Helm v2 returns version like this:
Client: v2.16.7+g5f2584f
Helm v3 returns a version like this:
v3.1.2+gd878d4d
--include-crds is available in helm v3.1+ so check for a regex matching that version
"""
output = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, universal_newlines=True, check=True)
version: str = output.stdout
regexp = re.compile(r'^v3.\[1-9]')
return(bool(regexp.search(version)))


def _write_override_file(all_config: Tuple[TextIO, str]) -> None:
file, data = all_config
Expand Down Expand Up @@ -337,11 +354,13 @@ def _parse_chart(all_config: Tuple[str, Union[ChartOpts, LocalChartOpts], pulumi
pulumi.Output.all(file, data).apply(_write_override_file)

namespace_arg = ['--namespace', config.namespace] if config.namespace else []
crd_arg = [ '--include-crds' ] if _is_helm_v3() else []

# Use 'helm template' to create a combined YAML manifest.
cmd = ['helm', 'template', chart, '--name-template', release_name,
'--values', default_values, '--values', overrides_filename]
cmd.extend(namespace_arg)
cmd.extend(crd_arg)

chart_resources = pulumi.Output.all(cmd, data).apply(_run_helm_cmd)

Expand Down
21 changes: 21 additions & 0 deletions sdk/dotnet/Helm/ChartBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text.Json;
using Pulumi.Kubernetes.Yaml;
using Pulumi.Utilities;
Expand Down Expand Up @@ -111,6 +112,11 @@ protected ChartBase(string releaseName, Union<ChartArgs, LocalChartArgs> args, C
flags.Add(cfgBase.Namespace);
}

if (IsHelmV3())
{
flags.Add("--include-crds");
}

var yaml = Utilities.ExecuteCommand("helm", flags.ToArray(), new Dictionary<string, string>());
return ParseTemplate(
yaml, cfgBase.Transformations, cfgBase.ResourcePrefix, dependencies, cfgBase.Namespace);
Expand All @@ -134,6 +140,21 @@ private static string GetName(Union<ChartArgs, LocalChartArgs> config, string re
return string.IsNullOrEmpty(prefix) ? releaseName : $"{prefix}-{releaseName}";
}

private static bool IsHelmV3()
{
var env = new Dictionary<string, string>();
string[] flags = {"version", "--short"};

// Helm v2 returns version like this:
// Client: v2.16.7+g5f2584f
// Helm v3 returns a version like this:
// v3.1.2+gd878d4d
// --include-crds is available in helm v3.1+ so check for a regex matching that version
var version = Utilities.ExecuteCommand("helm", flags, env);
Regex r = new Regex(@"^v3\.[1-9]");
return r.IsMatch(version);
}

private void Fetch(string chart, ChartFetchArgsUnwrap opts)
{
var flags = new List<string>(new[] { "fetch", chart });
Expand Down
41 changes: 41 additions & 0 deletions sdk/go/kubernetes/helm/v2/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strings"

Expand Down Expand Up @@ -157,6 +158,17 @@ func parseChart(ctx *pulumi.Context, name string, args chartArgs, opts ...pulumi
helmArgs = append(helmArgs, "--namespace", args.Namespace)
}

// Check for helm version
v3, err := isHelmV3()

if err != nil {
return nil, err
}

if v3 {
helmArgs = append(helmArgs, "--include-crds")
}

helmCmd := exec.Command("helm", helmArgs...)
var stderr bytes.Buffer
helmCmd.Stderr = &stderr
Expand Down Expand Up @@ -191,6 +203,35 @@ func yamlDecode(ctx *pulumi.Context, text, namespace string) ([]map[string]inter
return ret.Result, nil
}

func isHelmV3() (bool, error) {

/*
Helm v2 returns version like this:
Client: v2.16.7+g5f2584f
Helm v3 returns a version like this:
v3.1.2+gd878d4d
--include-crds is available in helm v3.1+ so check for a regex matching that version
*/
helmVerArgs := []string{"version", "--short"}
helmVerCmd := exec.Command("helm", helmVerArgs...)

var stderr bytes.Buffer
helmVerCmd.Stderr = &stderr

version, err := helmVerCmd.Output()
if err != nil {
return false, errors.Wrap(err, fmt.Sprintf("failed to check helm version: %s", stderr.String()))
}

matched, err := regexp.MatchString(`^v3\.[1-9]`, string(version))
if err != nil {
return false, errors.Wrap(err, fmt.Sprintf("failed to perform regex match: %s", stderr.String()))
}

return matched, nil

}

func fetch(name string, args fetchArgs) error {
helmArgs := []string{"fetch", name}

Expand Down
23 changes: 22 additions & 1 deletion sdk/nodejs/helm/v2/helm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export class Chart extends yaml.CollectionComponentResource {
try {
let chart: string;
let defaultValues: string;
let cmd: string;
if (isChartOpts(cfg)) {
// Fetch chart.
if (cfg.repo && cfg.repo.includes("http")) {
Expand Down Expand Up @@ -203,7 +204,27 @@ export class Chart extends yaml.CollectionComponentResource {
const namespaceArg = cfg.namespace
? `--namespace ${shell.quote([cfg.namespace])}`
: "";
let cmd = `helm template ${chart} --name-template ${release} --values ${defaultValues} --values ${values} ${apiVersionsArgs} ${namespaceArg}`;

// Check the helm version - v2 or v3
let helmVerCmd = `helm version --short || true`;
const helmVer = execSync(
helmVerCmd,
{
stdio: ['pipe', 'pipe', 'ignore'], // Suppress tiller version error
},
).toString();

// Helm v2 returns version like this:
// Client: v2.16.7+g5f2584f
// Helm v3 returns a version like this:
// v3.1.2+gd878d4d
// --include-crds is available in helm v3.1+ so check for a regex matching that version
let r = RegExp('^v3\.[1-9]');
if (r.test(helmVer)) {
cmd = `helm template ${chart} --include-crds --name-template ${release} --values ${defaultValues} --values ${values} ${apiVersionsArgs} ${namespaceArg}`;
} else {
cmd = `helm template ${chart} --name-template ${release} --values ${defaultValues} --values ${values} ${apiVersionsArgs} ${namespaceArg}`;
}

const yamlStream = execSync(
cmd,
Expand Down
19 changes: 19 additions & 0 deletions sdk/python/pulumi_kubernetes/helm/v2/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os.path
import shutil
import subprocess
import re
from tempfile import mkdtemp, mkstemp
from typing import Any, Callable, List, Optional, TextIO, Tuple, Union

Expand Down Expand Up @@ -279,6 +280,22 @@ def _run_helm_cmd(all_config: Tuple[List[Union[str, bytes]], Any]) -> str:
yaml_str: str = output.stdout
return yaml_str

def _is_helm_v3() -> bool:

cmd: List[str] = ['helm', 'version', '--short']

"""
Helm v2 returns version like this:
Client: v2.16.7+g5f2584f
Helm v3 returns a version like this:
v3.1.2+gd878d4d
--include-crds is available in helm v3.1+ so check for a regex matching that version
"""
output = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, universal_newlines=True, check=True)
version: str = output.stdout
regexp = re.compile(r'^v3.\[1-9]')
return(bool(regexp.search(version)))


def _write_override_file(all_config: Tuple[TextIO, str]) -> None:
file, data = all_config
Expand Down Expand Up @@ -337,11 +354,13 @@ def _parse_chart(all_config: Tuple[str, Union[ChartOpts, LocalChartOpts], pulumi
pulumi.Output.all(file, data).apply(_write_override_file)

namespace_arg = ['--namespace', config.namespace] if config.namespace else []
crd_arg = [ '--include-crds' ] if _is_helm_v3() else []

# Use 'helm template' to create a combined YAML manifest.
cmd = ['helm', 'template', chart, '--name-template', release_name,
'--values', default_values, '--values', overrides_filename]
cmd.extend(namespace_arg)
cmd.extend(crd_arg)

chart_resources = pulumi.Output.all(cmd, data).apply(_run_helm_cmd)

Expand Down