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

Skip Helm test hook resources by default #1467

Merged
merged 6 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## HEAD (Unreleased)

- Skip Helm test hook resources by default (https://github.com/pulumi/pulumi-kubernetes/pull/1467)

## 2.8.0 (February 3, 2021)

Note: This release fixes a bug with the Helm v3 SDK that omitted any chart resources that included a hook annotation.
Expand Down
6 changes: 6 additions & 0 deletions provider/pkg/gen/dotnet-templates/helm/ChartBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,12 @@ public InputList<string> ApiVersions
set => _apiVersions = value;
}

/// <summary>
/// By default, Helm resources with the 'test', 'test-success', and 'test-failure' hooks are not installed. Set
/// this flag to true to include these resources.
/// </summary>
public Input<bool>? IncludeTestHookResources { get; set; }

/// <summary>
/// The optional namespace to install chart resources into.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions provider/pkg/gen/dotnet-templates/helm/v3/Chart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ internal class JsonOpts
{
[JsonPropertyName("api_versions")]
public ImmutableArray<string> ApiVersions { get; set; }
[JsonPropertyName("include_test_hook_resources")]
public bool? IncludeTestHookResources { get; set; }
[JsonPropertyName("namespace")]
public string? Namespace { get; set; }
[JsonPropertyName("values")]
Expand Down
24 changes: 14 additions & 10 deletions provider/pkg/gen/go-templates/helm/v3/pulumiTypes.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ func (o FetchArgsOutput) ToFetchArgsOutputWithContext(ctx context.Context) Fetch
type ChartArgs struct {
// The optional Kubernetes API versions used for Capabilities.APIVersions.
APIVersions pulumi.StringArrayInput
// By default, Helm resources with the `test`, `test-success`, and `test-failure` hooks are not installed. Set
// this flag to true to include these resources.
IncludeTestHookResources pulumi.BoolInput
// The optional namespace to install chart resources into.
Namespace pulumi.StringInput
// Overrides for chart values.
Expand Down Expand Up @@ -148,16 +151,17 @@ type ChartArgs struct {
// chartArgs is a copy of ChartArgs but without using TInput in types.
// Note that Transformations are omitted in JSON marshaling because functions are not serializable.
type chartArgs struct {
APIVersions []string `json:"api_versions,omitempty" pulumi:"apiVersions"`
Namespace string `json:"namespace,omitempty" pulumi:"namespace"`
Values map[string]interface{} `json:"values,omitempty" pulumi:"values"`
Transformations []yaml.Transformation `json:"-" pulumi:"transformations"`
ResourcePrefix string `json:"resource_prefix,omitempty" pulumi:"resourcePrefix"`
Repo string `json:"repo,omitempty" pulumi:"repo"`
Chart string `json:"chart,omitempty" pulumi:"chart"`
Version string `json:"version,omitempty" pulumi:"version"`
FetchArgs fetchArgs `json:"fetch_opts,omitempty" pulumi:"fetchArgs"`
Path string `json:"path,omitempty" pulumi:"path"`
APIVersions []string `json:"api_versions,omitempty" pulumi:"apiVersions"`
IncludeTestHookResources bool `json:"include_test_hook_resources,omitempty" pulumi:"includeTestHookResources"`
Namespace string `json:"namespace,omitempty" pulumi:"namespace"`
Values map[string]interface{} `json:"values,omitempty" pulumi:"values"`
Transformations []yaml.Transformation `json:"-" pulumi:"transformations"`
ResourcePrefix string `json:"resource_prefix,omitempty" pulumi:"resourcePrefix"`
Repo string `json:"repo,omitempty" pulumi:"repo"`
Chart string `json:"chart,omitempty" pulumi:"chart"`
Version string `json:"version,omitempty" pulumi:"version"`
FetchArgs fetchArgs `json:"fetch_opts,omitempty" pulumi:"fetchArgs"`
Path string `json:"path,omitempty" pulumi:"path"`
}

type ChartArgsInput interface {
Expand Down
9 changes: 9 additions & 0 deletions provider/pkg/gen/nodejs-templates/helm/v3/helm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ export class Chart extends yaml.CollectionComponentResource {
obj["fetch_opts"] = value;
break;
}
case "includeTestHookResources": {
obj["include_test_hook_resources"] = value;
break;
}
case "releaseName": {
obj["release_name"] = value;
break;
Expand Down Expand Up @@ -232,6 +236,11 @@ interface BaseChartOpts {
* The optional kubernetes api versions used for Capabilities.APIVersions.
*/
apiVersions?: pulumi.Input<pulumi.Input<string>[]>;
/**
* By default, Helm resources with the `test`, `test-success`, and `test-failure` hooks are not installed. Set
* this flag to true to include these resources.
*/
includeTestHookResources?: boolean;
/**
* The optional namespace to install chart resources into.
*/
Expand Down
31 changes: 26 additions & 5 deletions provider/pkg/gen/python-templates/helm/v3/helm.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,12 +383,19 @@ class BaseChartOpts:
Optional kubernetes api versions used for Capabilities.APIVersions.
"""

include_test_hook_resources: Optional[pulumi.Input[bool]]
"""
By default, Helm resources with the 'test', 'test-success', and 'test-failure' hooks are not installed. Set
this flag to true to include these resources.
"""

def __init__(self,
namespace: Optional[pulumi.Input[str]] = None,
values: Optional[pulumi.Inputs] = None,
transformations: Optional[Sequence[Callable[[Any, pulumi.ResourceOptions], None]]] = None,
resource_prefix: Optional[str] = None,
api_versions: Optional[Sequence[pulumi.Input[str]]] = None):
api_versions: Optional[Sequence[pulumi.Input[str]]] = None,
include_test_hook_resources: Optional[pulumi.Input[bool]] = None):
"""
:param Optional[pulumi.Input[str]] namespace: Optional namespace to install chart resources into.
:param Optional[pulumi.Inputs] values: Optional overrides for chart values.
Expand All @@ -399,8 +406,12 @@ def __init__(self,
Example: A resource created with resource_prefix="foo" would produce a resource named "foo-resourceName".
:param Optional[Sequence[pulumi.Input[str]]] api_versions: Optional kubernetes api versions used for
Capabilities.APIVersions.
:param Optional[pulumi.Input[bool]] include_test_hook_resources: By default, Helm resources with the 'test',
'test-success', and 'test-failure' hooks are not installed. Set this flag to true to include these
resources.
"""
self.namespace = namespace
self.include_test_hook_resources = include_test_hook_resources
self.values = values
self.transformations = transformations
self.resource_prefix = resource_prefix
Expand Down Expand Up @@ -448,7 +459,8 @@ def __init__(self,
repo: Optional[pulumi.Input[str]] = None,
version: Optional[pulumi.Input[str]] = None,
fetch_opts: Optional[pulumi.Input[FetchOpts]] = None,
api_versions: Optional[Sequence[pulumi.Input[str]]] = None):
api_versions: Optional[Sequence[pulumi.Input[str]]] = None,
include_test_hook_resources: Optional[pulumi.Input[bool]] = None):
"""
:param pulumi.Input[str] chart: The name of the chart to deploy. If `repo` is provided, this chart name
will be prefixed by the repo name.
Expand All @@ -469,8 +481,12 @@ def __init__(self,
fetching of the Helm chart.
:param Optional[Sequence[pulumi.Input[str]]] api_versions: Optional kubernetes api versions used for
Capabilities.APIVersions.
:param Optional[pulumi.Input[bool]] include_test_hook_resources: By default, Helm resources with the 'test',
'test-success', and 'test-failure' hooks are not installed. Set this flag to true to include these
resources.
"""
super(ChartOpts, self).__init__(namespace, values, transformations, resource_prefix, api_versions)
super(ChartOpts, self).__init__(namespace, values, transformations, resource_prefix, api_versions,
include_test_hook_resources)
self.chart = chart
self.repo = repo
self.version = version
Expand All @@ -493,7 +509,8 @@ def __init__(self,
values: Optional[pulumi.Inputs] = None,
transformations: Optional[Sequence[Callable[[Any, pulumi.ResourceOptions], None]]] = None,
resource_prefix: Optional[str] = None,
api_versions: Optional[Sequence[pulumi.Input[str]]] = None):
api_versions: Optional[Sequence[pulumi.Input[str]]] = None,
include_test_hook_resources: Optional[pulumi.Input[bool]] = None):
"""
:param pulumi.Input[str] path: The path to the chart directory which contains the
`Chart.yaml` file.
Expand All @@ -506,9 +523,13 @@ def __init__(self,
Example: A resource created with resource_prefix="foo" would produce a resource named "foo-resourceName".
:param Optional[Sequence[pulumi.Input[str]]] api_versions: Optional kubernetes api versions used for
Capabilities.APIVersions.
:param Optional[pulumi.Input[bool]] include_test_hook_resources: By default, Helm resources with the 'test',
'test-success', and 'test-failure' hooks are not installed. Set this flag to true to include these
resources.
"""

super(LocalChartOpts, self).__init__(namespace, values, transformations, resource_prefix, api_versions)
super(LocalChartOpts, self).__init__(namespace, values, transformations, resource_prefix, api_versions,
include_test_hook_resources)
self.path = path


Expand Down
31 changes: 21 additions & 10 deletions provider/pkg/provider/invoke_helm_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strings"

pkgerrors "github.com/pkg/errors"
logger "github.com/pulumi/pulumi/sdk/v2/go/common/util/logging"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/chartutil"
Expand All @@ -32,6 +33,9 @@ import (
"helm.sh/helm/v3/pkg/storage/driver"
)

// testHookAnnotation matches test-related Helm hook annotations (test, test-success, test-failure)
var testHookAnnotation = regexp.MustCompile(`"?helm.sh\/hook"?:.*test`)

type HelmFetchOpts struct {
CAFile string `json:"ca_file,omitempty"`
CertFile string `json:"cert_file,omitempty"`
Expand All @@ -52,14 +56,15 @@ type HelmFetchOpts struct {
type HelmChartOpts struct {
HelmFetchOpts `json:"fetch_opts,omitempty"`

APIVersions []string `json:"api_versions,omitempty"`
Chart string `json:"chart,omitempty"`
Namespace string `json:"namespace,omitempty"`
Path string `json:"path,omitempty"`
ReleaseName string `json:"release_name,omitempty"`
Repo string `json:"repo,omitempty"`
Values map[string]interface{} `json:"values,omitempty"`
Version string `json:"version,omitempty"`
APIVersions []string `json:"api_versions,omitempty"`
Chart string `json:"chart,omitempty"`
IncludeTestHookResources bool `json:"include_test_hook_resources,omitempty"`
Namespace string `json:"namespace,omitempty"`
Path string `json:"path,omitempty"`
ReleaseName string `json:"release_name,omitempty"`
Repo string `json:"repo,omitempty"`
Values map[string]interface{} `json:"values,omitempty"`
Version string `json:"version,omitempty"`
}

// helmTemplate performs Helm fetch/pull + template operations and returns the resulting YAML manifest based on the
Expand Down Expand Up @@ -225,8 +230,14 @@ func (c *chart) template() (string, error) {
manifests := strings.Builder{}
manifests.WriteString(rel.Manifest)
for _, hook := range rel.Hooks {
manifests.WriteString("\n---\n")
manifests.WriteString(hook.Manifest)
switch {
case !c.opts.IncludeTestHookResources && testHookAnnotation.MatchString(hook.Manifest):
logger.V(9).Infof("Skipping Helm resource with test hook: %s", hook.Name)
// Skip test hook.
default:
manifests.WriteString("\n---\n")
manifests.WriteString(hook.Manifest)
}
}

return manifests.String(), nil
Expand Down
10 changes: 7 additions & 3 deletions provider/pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"os/user"
"path/filepath"
"reflect"
"regexp"
"strings"
"sync"

Expand Down Expand Up @@ -1035,7 +1036,7 @@ func (k *kubeProvider) Check(ctx context.Context, req *pulumirpc.CheckRequest) (
var failures []*pulumirpc.CheckFailure

hasHelmHook := false
for key := range newInputs.GetAnnotations() {
for key, value := range newInputs.GetAnnotations() {
// If annotations with a reserved internal prefix exist, ignore them.
if metadata.IsInternalAnnotation(key) {
_ = k.host.Log(ctx, diag.Warning, urn,
Expand All @@ -1044,12 +1045,15 @@ func (k *kubeProvider) Check(ctx context.Context, req *pulumirpc.CheckRequest) (

// If the Helm hook annotation is found, set the hasHelmHook flag.
if has := metadata.IsHelmHookAnnotation(key); has {
hasHelmHook = hasHelmHook || has
// Test hooks are handled, so ignore this one.
if match, _ := regexp.MatchString(`test|test-success|test-failure`, value); !match {
hasHelmHook = hasHelmHook || has
}
}
}
if hasHelmHook {
_ = k.host.Log(ctx, diag.Warning, urn,
"This resource contains Helm hooks that are not currently supported by Pulumi. The resource will"+
"This resource contains Helm hooks that are not currently supported by Pulumi. The resource will "+
"be created, but any hooks will not be executed. Hooks support is tracked at "+
"https://github.com/pulumi/pulumi-kubernetes/issues/555")
}
Expand Down
6 changes: 6 additions & 0 deletions sdk/dotnet/Helm/ChartBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,12 @@ public InputList<string> ApiVersions
set => _apiVersions = value;
}

/// <summary>
/// By default, Helm resources with the 'test', 'test-success', and 'test-failure' hooks are not installed. Set
/// this flag to true to include these resources.
/// </summary>
public Input<bool>? IncludeTestHookResources { get; set; }

/// <summary>
/// The optional namespace to install chart resources into.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions sdk/dotnet/Helm/V3/Chart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ internal class JsonOpts
{
[JsonPropertyName("api_versions")]
public ImmutableArray<string> ApiVersions { get; set; }
[JsonPropertyName("include_test_hook_resources")]
public bool? IncludeTestHookResources { get; set; }
[JsonPropertyName("namespace")]
public string? Namespace { get; set; }
[JsonPropertyName("values")]
Expand Down
24 changes: 14 additions & 10 deletions sdk/go/kubernetes/helm/v3/pulumiTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ func (o FetchArgsOutput) ToFetchArgsOutputWithContext(ctx context.Context) Fetch
type ChartArgs struct {
// The optional Kubernetes API versions used for Capabilities.APIVersions.
APIVersions pulumi.StringArrayInput
// By default, Helm resources with the `test`, `test-success`, and `test-failure` hooks are not installed. Set
// this flag to true to include these resources.
IncludeTestHookResources pulumi.BoolInput
// The optional namespace to install chart resources into.
Namespace pulumi.StringInput
// Overrides for chart values.
Expand Down Expand Up @@ -148,16 +151,17 @@ type ChartArgs struct {
// chartArgs is a copy of ChartArgs but without using TInput in types.
// Note that Transformations are omitted in JSON marshaling because functions are not serializable.
type chartArgs struct {
APIVersions []string `json:"api_versions,omitempty" pulumi:"apiVersions"`
Namespace string `json:"namespace,omitempty" pulumi:"namespace"`
Values map[string]interface{} `json:"values,omitempty" pulumi:"values"`
Transformations []yaml.Transformation `json:"-" pulumi:"transformations"`
ResourcePrefix string `json:"resource_prefix,omitempty" pulumi:"resourcePrefix"`
Repo string `json:"repo,omitempty" pulumi:"repo"`
Chart string `json:"chart,omitempty" pulumi:"chart"`
Version string `json:"version,omitempty" pulumi:"version"`
FetchArgs fetchArgs `json:"fetch_opts,omitempty" pulumi:"fetchArgs"`
Path string `json:"path,omitempty" pulumi:"path"`
APIVersions []string `json:"api_versions,omitempty" pulumi:"apiVersions"`
IncludeTestHookResources bool `json:"include_test_hook_resources,omitempty" pulumi:"includeTestHookResources"`
Namespace string `json:"namespace,omitempty" pulumi:"namespace"`
Values map[string]interface{} `json:"values,omitempty" pulumi:"values"`
Transformations []yaml.Transformation `json:"-" pulumi:"transformations"`
ResourcePrefix string `json:"resource_prefix,omitempty" pulumi:"resourcePrefix"`
Repo string `json:"repo,omitempty" pulumi:"repo"`
Chart string `json:"chart,omitempty" pulumi:"chart"`
Version string `json:"version,omitempty" pulumi:"version"`
FetchArgs fetchArgs `json:"fetch_opts,omitempty" pulumi:"fetchArgs"`
Path string `json:"path,omitempty" pulumi:"path"`
}

type ChartArgsInput interface {
Expand Down
9 changes: 9 additions & 0 deletions sdk/nodejs/helm/v3/helm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ export class Chart extends yaml.CollectionComponentResource {
obj["fetch_opts"] = value;
break;
}
case "includeTestHookResources": {
obj["include_test_hook_resources"] = value;
break;
}
case "releaseName": {
obj["release_name"] = value;
break;
Expand Down Expand Up @@ -232,6 +236,11 @@ interface BaseChartOpts {
* The optional kubernetes api versions used for Capabilities.APIVersions.
*/
apiVersions?: pulumi.Input<pulumi.Input<string>[]>;
/**
* By default, Helm resources with the `test`, `test-success`, and `test-failure` hooks are not installed. Set
* this flag to true to include these resources.
*/
includeTestHookResources?: boolean;
/**
* The optional namespace to install chart resources into.
*/
Expand Down
Loading