From e5aebf3bb2d6ab1d085d91b36d858a8d9052ef59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Luk=C5=A1a?= Date: Wed, 7 Jun 2023 13:25:54 +0200 Subject: [PATCH] Allow configuring custom ResourceVisitor in Install and Upgrade MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marko Lukša --- pkg/action/install.go | 13 +++++++++++++ pkg/action/upgrade.go | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/pkg/action/install.go b/pkg/action/install.go index d5c34cef7..ce8f057e4 100644 --- a/pkg/action/install.go +++ b/pkg/action/install.go @@ -104,6 +104,12 @@ type Install struct { // OutputDir/ UseReleaseName bool PostRenderer postrender.PostRenderer + // ResourceVisitor is an optional VisitorFunc applied to each rendered resource + // + // This is an alternative to PostRenderer. Unike PostRenderer, it allows each + // individual resource to be modified without having to parse and re-serialize the + // manifests. + ResourceVisitor resource.VisitorFunc // Lock to control raceconditions when the process receives a SIGTERM Lock sync.Mutex } @@ -302,6 +308,13 @@ func (i *Install) RunWithContext(ctx context.Context, chrt *chart.Chart, vals ma return nil, errors.Wrap(err, "unable to build kubernetes objects from release manifest") } + if i.ResourceVisitor != nil { + err = resources.Visit(i.ResourceVisitor) + if err != nil { + return nil, err + } + } + // It is safe to use "force" here because these are resources currently rendered by the chart. err = resources.Visit(setMetadataVisitor(rel.Name, rel.Namespace, true)) if err != nil { diff --git a/pkg/action/upgrade.go b/pkg/action/upgrade.go index 829be51df..3a28b6696 100644 --- a/pkg/action/upgrade.go +++ b/pkg/action/upgrade.go @@ -98,6 +98,12 @@ type Upgrade struct { // If this is non-nil, then after templates are rendered, they will be sent to the // post renderer before sending to the Kubernetes API server. PostRenderer postrender.PostRenderer + // ResourceVisitor is an optional VisitorFunc applied to each rendered resource + // + // This is an alternative to PostRenderer. Unike PostRenderer, it allows each + // individual resource to be modified without having to parse and re-serialize the + // manifests. + ResourceVisitor resource.VisitorFunc // DisableOpenAPIValidation controls whether OpenAPI validation is enforced. DisableOpenAPIValidation bool // Get missing dependencies @@ -285,6 +291,13 @@ func (u *Upgrade) performUpgrade(ctx context.Context, originalRelease, upgradedR return upgradedRelease, errors.Wrap(err, "unable to build kubernetes objects from new release manifest") } + if u.ResourceVisitor != nil { + err = target.Visit(u.ResourceVisitor) + if err != nil { + return upgradedRelease, err + } + } + // It is safe to use force only on target because these are resources currently rendered by the chart. err = target.Visit(setMetadataVisitor(upgradedRelease.Name, upgradedRelease.Namespace, true)) if err != nil {