|
7 | 7 | "context" |
8 | 8 | "encoding/json" |
9 | 9 | "fmt" |
| 10 | + "io" |
10 | 11 | "net/url" |
11 | 12 | "os" |
12 | 13 | pathpkg "path" |
@@ -79,6 +80,7 @@ type HelmReleaseModel struct { |
79 | 80 | DisableOpenapiValidation types.Bool `tfsdk:"disable_openapi_validation"` |
80 | 81 | DisableWebhooks types.Bool `tfsdk:"disable_webhooks"` |
81 | 82 | ForceUpdate types.Bool `tfsdk:"force_update"` |
| 83 | + HookOutputLogPolicy types.String `tfsdk:"hook_output_log_policy"` |
82 | 84 | ID types.String `tfsdk:"id"` |
83 | 85 | Keyring types.String `tfsdk:"keyring"` |
84 | 86 | Lint types.Bool `tfsdk:"lint"` |
@@ -339,6 +341,15 @@ func (r *HelmRelease) Schema(ctx context.Context, req resource.SchemaRequest, re |
339 | 341 | Default: booldefault.StaticBool(defaultAttributes["force_update"].(bool)), |
340 | 342 | Description: "Force resource update through delete/recreate if needed.", |
341 | 343 | }, |
| 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 | + }, |
342 | 353 | "id": schema.StringAttribute{ |
343 | 354 | Computed: true, |
344 | 355 | }, |
@@ -757,6 +768,27 @@ func getInstalledReleaseVersion(ctx context.Context, m *Meta, cfg *action.Config |
757 | 768 | return installedVersion, nil |
758 | 769 | } |
759 | 770 |
|
| 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 | + |
760 | 792 | func (r *HelmRelease) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
761 | 793 | var state HelmReleaseModel |
762 | 794 | diags := req.Plan.Get(ctx, &state) |
@@ -792,6 +824,8 @@ func (r *HelmRelease) Create(ctx context.Context, req resource.CreateRequest, re |
792 | 824 | resp.Diagnostics.AddError("Error getting helm configuration", fmt.Sprintf("Unable to get Helm configuration for namespace %s: %s", namespace, err)) |
793 | 825 | return |
794 | 826 | } |
| 827 | + configureHookOutputLogPolicy(actionConfig, state.HookOutputLogPolicy.ValueString()) |
| 828 | + |
795 | 829 | ociDiags := OCIRegistryLogin(ctx, meta, actionConfig, meta.RegistryClient, state.Repository.ValueString(), state.Chart.ValueString(), state.RepositoryUsername.ValueString(), state.RepositoryPassword.ValueString()) |
796 | 830 | resp.Diagnostics.Append(ociDiags...) |
797 | 831 | if resp.Diagnostics.HasError() { |
@@ -1106,6 +1140,8 @@ func (r *HelmRelease) Update(ctx context.Context, req resource.UpdateRequest, re |
1106 | 1140 | resp.Diagnostics.AddError("Error getting helm configuration", fmt.Sprintf("Unable to get Helm configuration for namespace %s: %s", namespace, err)) |
1107 | 1141 | return |
1108 | 1142 | } |
| 1143 | + configureHookOutputLogPolicy(actionConfig, plan.HookOutputLogPolicy.ValueString()) |
| 1144 | + |
1109 | 1145 | ociDiags := OCIRegistryLogin(ctx, meta, actionConfig, meta.RegistryClient, state.Repository.ValueString(), state.Chart.ValueString(), state.RepositoryUsername.ValueString(), state.RepositoryPassword.ValueString()) |
1110 | 1146 | resp.Diagnostics.Append(ociDiags...) |
1111 | 1147 | if resp.Diagnostics.HasError() { |
|
0 commit comments