Skip to content

Commit 15a25ce

Browse files
committed
Feat #1707: Add hook_output_log_policy attribute for HIP-0019 support
1 parent a4f1c1d commit 15a25ce

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

helm/resource_helm_release.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"encoding/json"
99
"fmt"
10+
"io"
1011
"net/url"
1112
"os"
1213
pathpkg "path"
@@ -79,6 +80,7 @@ type HelmReleaseModel struct {
7980
DisableOpenapiValidation types.Bool `tfsdk:"disable_openapi_validation"`
8081
DisableWebhooks types.Bool `tfsdk:"disable_webhooks"`
8182
ForceUpdate types.Bool `tfsdk:"force_update"`
83+
HookOutputLogPolicy types.String `tfsdk:"hook_output_log_policy"`
8284
ID types.String `tfsdk:"id"`
8385
Keyring types.String `tfsdk:"keyring"`
8486
Lint types.Bool `tfsdk:"lint"`
@@ -339,6 +341,15 @@ func (r *HelmRelease) Schema(ctx context.Context, req resource.SchemaRequest, re
339341
Default: booldefault.StaticBool(defaultAttributes["force_update"].(bool)),
340342
Description: "Force resource update through delete/recreate if needed.",
341343
},
344+
"hook_output_log_policy": schema.StringAttribute{
345+
Optional: true,
346+
Computed: true,
347+
Description: "Copy hook output logs to the provider logs. Valid values are 'hook-succeeded', 'hook-failed', 'always', 'never'. Defaults to 'never'.",
348+
Validators: []validator.String{
349+
stringvalidator.OneOf("hook-succeeded", "hook-failed", "always", "never"),
350+
},
351+
Default: stringdefault.StaticString("never"),
352+
},
342353
"id": schema.StringAttribute{
343354
Computed: true,
344355
},
@@ -757,6 +768,27 @@ func getInstalledReleaseVersion(ctx context.Context, m *Meta, cfg *action.Config
757768
return installedVersion, nil
758769
}
759770

771+
func configureHookOutputLogPolicy(actionConfig *action.Configuration, policy string) {
772+
if policy == "never" || policy == "" {
773+
return
774+
}
775+
776+
actionConfig.SetHookOutputFunc(func(namespace, pod, container string) io.Writer {
777+
return &hookLogWriter{namespace: namespace, pod: pod, container: container}
778+
})
779+
}
780+
781+
type hookLogWriter struct {
782+
namespace string
783+
pod string
784+
container string
785+
}
786+
787+
func (w *hookLogWriter) Write(p []byte) (int, error) {
788+
tflog.Info(context.Background(), fmt.Sprintf("[hook-log] namespace=%s pod=%s container=%s\n%s", w.namespace, w.pod, w.container, string(p)))
789+
return len(p), nil
790+
}
791+
760792
func (r *HelmRelease) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
761793
var state HelmReleaseModel
762794
diags := req.Plan.Get(ctx, &state)
@@ -792,6 +824,8 @@ func (r *HelmRelease) Create(ctx context.Context, req resource.CreateRequest, re
792824
resp.Diagnostics.AddError("Error getting helm configuration", fmt.Sprintf("Unable to get Helm configuration for namespace %s: %s", namespace, err))
793825
return
794826
}
827+
configureHookOutputLogPolicy(actionConfig, state.HookOutputLogPolicy.ValueString())
828+
795829
ociDiags := OCIRegistryLogin(ctx, meta, actionConfig, meta.RegistryClient, state.Repository.ValueString(), state.Chart.ValueString(), state.RepositoryUsername.ValueString(), state.RepositoryPassword.ValueString())
796830
resp.Diagnostics.Append(ociDiags...)
797831
if resp.Diagnostics.HasError() {
@@ -1106,6 +1140,8 @@ func (r *HelmRelease) Update(ctx context.Context, req resource.UpdateRequest, re
11061140
resp.Diagnostics.AddError("Error getting helm configuration", fmt.Sprintf("Unable to get Helm configuration for namespace %s: %s", namespace, err))
11071141
return
11081142
}
1143+
configureHookOutputLogPolicy(actionConfig, plan.HookOutputLogPolicy.ValueString())
1144+
11091145
ociDiags := OCIRegistryLogin(ctx, meta, actionConfig, meta.RegistryClient, state.Repository.ValueString(), state.Chart.ValueString(), state.RepositoryUsername.ValueString(), state.RepositoryPassword.ValueString())
11101146
resp.Diagnostics.Append(ociDiags...)
11111147
if resp.Diagnostics.HasError() {

0 commit comments

Comments
 (0)