From 1e2845ed953e778d96f7582fc6bc6d509a9373b2 Mon Sep 17 00:00:00 2001 From: Raffael Sahli Date: Mon, 6 Mar 2023 13:50:54 +0000 Subject: [PATCH] feat: reconcile referencing helmchart if helmrelease is reconciled with source Signed-off-by: Raffael Sahli --- cmd/flux/reconcile_helmrelease.go | 46 ++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/cmd/flux/reconcile_helmrelease.go b/cmd/flux/reconcile_helmrelease.go index 1a513369..bd219d5a 100644 --- a/cmd/flux/reconcile_helmrelease.go +++ b/cmd/flux/reconcile_helmrelease.go @@ -17,6 +17,8 @@ limitations under the License. package main import ( + "strings" + "github.com/spf13/cobra" "k8s.io/apimachinery/pkg/types" @@ -36,10 +38,7 @@ The reconcile kustomization command triggers a reconciliation of a HelmRelease r # Trigger a reconciliation of the HelmRelease's source and apply changes flux reconcile hr podinfo --with-source`, ValidArgsFunction: resourceNamesCompletionFunc(helmv2.GroupVersion.WithKind(helmv2.HelmReleaseKind)), - RunE: reconcileWithSourceCommand{ - apiType: helmReleaseType, - object: helmReleaseAdapter{&helmv2.HelmRelease{}}, - }.run, + RunE: reconcileSourceAndChart, } type reconcileHelmReleaseFlags struct { @@ -87,3 +86,42 @@ func (obj helmReleaseAdapter) getSource() (reconcileCommand, types.NamespacedNam Namespace: obj.Spec.Chart.Spec.SourceRef.Namespace, } } + +func (obj helmChartAdapter) lastHandledReconcileRequest() string { + return obj.Status.GetLastHandledReconcileRequest() +} + +func reconcileSourceAndChart(cmd *cobra.Command, args []string) error { + hr := helmv2.HelmRelease{} + reconcile := reconcileWithSourceCommand{ + apiType: helmReleaseType, + object: helmReleaseAdapter{&hr}, + } + + if err := reconcile.run(cmd, args); err != nil { + return err + } + + if reconcile.object.reconcileSource() { + nsName := strings.Split(hr.Status.HelmChart, "/") + if len(nsName) != 2 { + return nil + } + + nsCopy := *kubeconfigArgs.Namespace + reconcileChart := reconcileCommand{ + apiType: helmChartType, + object: helmChartAdapter{&sourcev1.HelmChart{}}, + } + + if nsName[0] != "" { + *kubeconfigArgs.Namespace = nsName[0] + } + + err := reconcileChart.run(nil, []string{nsName[1]}) + *kubeconfigArgs.Namespace = nsCopy + return err + } + + return nil +}