From 74655b76b0a280a669b6649bc9b3bd9316c002e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arthur=20Woimb=C3=A9e?= Date: Thu, 24 Oct 2024 09:16:08 +0200 Subject: [PATCH] helm.v4.Chart: option to deploy helm hooks --- .../pulumi-resource-kubernetes/schema.json | 4 ++++ provider/pkg/gen/examples/overlays/chartV4.md | 4 +++- provider/pkg/gen/overlays.go | 6 +++++ provider/pkg/provider/helm/v4/chart.go | 23 +++++++++++-------- provider/pkg/provider/helm/v4/chart_test.go | 18 +++++++++++++++ .../templates/hook_poddisruptionbudget.yaml | 12 ++++++++++ 6 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 provider/pkg/provider/helm/v4/testdata/reference/templates/hook_poddisruptionbudget.yaml diff --git a/provider/cmd/pulumi-resource-kubernetes/schema.json b/provider/cmd/pulumi-resource-kubernetes/schema.json index fba434d885..33bd601d1c 100644 --- a/provider/cmd/pulumi-resource-kubernetes/schema.json +++ b/provider/cmd/pulumi-resource-kubernetes/schema.json @@ -91043,6 +91043,10 @@ "type": "boolean", "description": "If set, no CRDs will be installed. By default, CRDs are installed if not already present." }, + "deployHookedResources": { + "type": "boolean", + "description": "If set, deploy all resources that would be managed by Helm hooks as regular resources, ignoring their hook annotations. When disabled (default), these resources are ignored." + }, "valueYamlFiles": { "type": "array", "items": { diff --git a/provider/pkg/gen/examples/overlays/chartV4.md b/provider/pkg/gen/examples/overlays/chartV4.md index 7574d11f00..4faa49a828 100644 --- a/provider/pkg/gen/examples/overlays/chartV4.md +++ b/provider/pkg/gen/examples/overlays/chartV4.md @@ -56,7 +56,9 @@ Use the `dependencyUpdate` input to have Pulumi update the dependencies (see: [H The `Chart` resource renders the templates from your chart and then manages the resources directly with the Pulumi Kubernetes provider. A default namespace is applied based on the `namespace` input, the provider's configured namespace, and the active Kubernetes context. Use the `skipCrds` option to skip installing the -Custom Resource Definition (CRD) objects located in the chart's `crds/` special directory. +Custom Resource Definition (CRD) objects located in the chart's `crds/` special directory. By default, +resources managed by helm hooks are ignored, use `deployHookedResources` to deploy them as regular resources, +ignoring their `helm.sh/hook*` annotations. Use the `postRenderer` input to pipe the rendered manifest through a [post-rendering command](https://helm.sh/docs/topics/advanced/#post-rendering). diff --git a/provider/pkg/gen/overlays.go b/provider/pkg/gen/overlays.go index b3c24a9113..0cfa5e835e 100644 --- a/provider/pkg/gen/overlays.go +++ b/provider/pkg/gen/overlays.go @@ -231,6 +231,12 @@ var helmV4ChartResource = pschema.ResourceSpec{ }, Description: "If set, no CRDs will be installed. By default, CRDs are installed if not already present.", }, + "deployHookedResourcess": { + TypeSpec: pschema.TypeSpec{ + Type: "boolean", + }, + Description: "If set, deploy all resources that would be managed by Helm hooks as regular resources, ignoring their hook annotations. When disabled (default), these resources are ignored.", + }, "postRenderer": { TypeSpec: pschema.TypeSpec{ Ref: "#/types/kubernetes:helm.sh/v4:PostRenderer", diff --git a/provider/pkg/provider/helm/v4/chart.go b/provider/pkg/provider/helm/v4/chart.go index 9ce9990f6a..2347a8af24 100644 --- a/provider/pkg/provider/helm/v4/chart.go +++ b/provider/pkg/provider/helm/v4/chart.go @@ -52,10 +52,11 @@ type ChartArgs struct { Verify pulumi.BoolInput `pulumi:"verify,optional"` Keyring pulumi.AssetInput `pulumi:"keyring,optional"` - Values pulumi.MapInput `pulumi:"values,optional"` - ValuesFiles pulumi.AssetArrayInput `pulumi:"valueYamlFiles,optional"` - SkipCrds pulumi.BoolInput `pulumi:"skipCrds,optional"` - PostRenderer helmv4.PostRendererInput `pulumi:"postRenderer,optional"` + Values pulumi.MapInput `pulumi:"values,optional"` + ValuesFiles pulumi.AssetArrayInput `pulumi:"valueYamlFiles,optional"` + SkipCrds pulumi.BoolInput `pulumi:"skipCrds,optional"` + DeployHookedResources pulumi.BoolInput `pulumi:"DeployHookedResources,optional"` + PostRenderer helmv4.PostRendererInput `pulumi:"postRenderer,optional"` ResourcePrefix pulumi.StringInput `pulumi:"resourcePrefix,optional"` SkipAwait pulumi.BoolInput `pulumi:"skipAwait,optional"` @@ -72,10 +73,11 @@ type chartArgs struct { Verify bool Keyring pulumi.Asset - Values map[string]any - ValuesFiles []pulumi.Asset - SkipCrds bool - PostRenderer *helmv4.PostRenderer + Values map[string]any + ValuesFiles []pulumi.Asset + SkipCrds bool + DeployHookedResources bool + PostRenderer *helmv4.PostRenderer ResourcePrefix *string SkipAwait bool @@ -85,7 +87,7 @@ func unwrapChartArgs(ctx context.Context, args *ChartArgs) (*chartArgs, internal result, err := internals.UnsafeAwaitOutput(ctx, pulumi.All( args.Name, args.Namespace, args.Chart, args.Version, args.Devel, args.RepositoryOpts, args.DependencyUpdate, args.Verify, args.Keyring, - args.Values, args.ValuesFiles, args.SkipCrds, args.PostRenderer, + args.Values, args.ValuesFiles, args.SkipCrds, args.DeployHookedResources, args.PostRenderer, args.ResourcePrefix, args.SkipAwait)) if err != nil || !result.Known { return nil, result, err @@ -110,6 +112,7 @@ func unwrapChartArgs(ctx context.Context, args *ChartArgs) (*chartArgs, internal r.Values, _ = pop().(map[string]any) r.ValuesFiles, _ = pop().([]pulumi.Asset) r.SkipCrds, _ = pop().(bool) + r.DeployHookedResources, _ = pop().(bool) if v, ok := pop().(helmv4.PostRenderer); ok { r.PostRenderer = &v } @@ -212,7 +215,7 @@ func (r *ChartProvider) Construct(ctx *pulumi.Context, typ, name string, inputs cmd.Values.Values = chartArgs.Values cmd.Values.ValuesFiles = chartArgs.ValuesFiles cmd.IncludeCRDs = !chartArgs.SkipCrds - cmd.DisableHooks = true + cmd.DisableHooks = !chartArgs.DeployHookedResources cmd.ReleaseName = chartArgs.Name cmd.Namespace = chartArgs.Namespace diff --git a/provider/pkg/provider/helm/v4/chart_test.go b/provider/pkg/provider/helm/v4/chart_test.go index 67892b756c..4c74f15f6d 100644 --- a/provider/pkg/provider/helm/v4/chart_test.go +++ b/provider/pkg/provider/helm/v4/chart_test.go @@ -357,6 +357,24 @@ var _ = Describe("Construct", func() { }) }) + Describe("DeployHookedResources", func() { + Context("given deployHookedResources", func() { + BeforeEach(func() { + inputs["deployHookedResources"] = resource.NewBoolProperty(true) + }) + It("should deploy hooked resources", func(ctx context.Context) { + resp, err := pulumiprovider.Construct(ctx, req, tc.EngineConn(), k.Construct) + Expect(err).ShouldNot(HaveOccurred()) + outputs := unmarshalProperties(GinkgoTB(), resp.State) + Expect(outputs).To(MatchProps(IgnoreExtras, Props{ + "resources": MatchArrayValue(ContainElements( + MatchResourceReferenceValue("urn:pulumi:stack::project::kubernetes:helm/v4:Chart$kubernetes:policy/v1:PodDisruptionBudget::test:default/test-reference", "test:default/test-reference"), + )), + })) + }) + }) + }) + Describe("Post Renderer", func() { Context("given a postRenderer", func() { var tempdir string diff --git a/provider/pkg/provider/helm/v4/testdata/reference/templates/hook_poddisruptionbudget.yaml b/provider/pkg/provider/helm/v4/testdata/reference/templates/hook_poddisruptionbudget.yaml new file mode 100644 index 0000000000..8214ba19b1 --- /dev/null +++ b/provider/pkg/provider/helm/v4/testdata/reference/templates/hook_poddisruptionbudget.yaml @@ -0,0 +1,12 @@ +apiVersion: policy/v1 +kind: PodDisruptionBudget +metadata: + name: {{ include "reference.fullname" . }} + annotations: + "helm.sh/hook": pre-install,pre-upgrade + "helm.sh/hook-weight": "-5" + "helm.sh/hook-delete-policy": before-hook-creation +spec: + minAvailable: 0 + selector: + matchLabels: ~