From 1e2845ed953e778d96f7582fc6bc6d509a9373b2 Mon Sep 17 00:00:00 2001 From: Raffael Sahli Date: Mon, 6 Mar 2023 13:50:54 +0000 Subject: [PATCH 1/3] 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 1a513369b4..bd219d5aac 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 +} From 0e6f03d014ae4607e9d81c8897f1705ec30cc78f Mon Sep 17 00:00:00 2001 From: Raffael Sahli Date: Mon, 6 Mar 2023 17:00:31 +0000 Subject: [PATCH 2/3] test: update e2e testdata Signed-off-by: Raffael Sahli --- cmd/flux/main_e2e_test.go | 4 ++-- .../helmrelease/reconcile_helmrelease_from_git.golden | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/cmd/flux/main_e2e_test.go b/cmd/flux/main_e2e_test.go index 22e27fdf60..4a802f1577 100644 --- a/cmd/flux/main_e2e_test.go +++ b/cmd/flux/main_e2e_test.go @@ -41,7 +41,7 @@ func TestMain(m *testing.M) { // Install Flux. output, err := executeCommand("install --components-extra=image-reflector-controller,image-automation-controller") if err != nil { - panic(fmt.Errorf("install falied: %s error:'%w'", output, err)) + panic(fmt.Errorf("install failed: %s error:'%w'", output, err)) } // Run tests @@ -50,7 +50,7 @@ func TestMain(m *testing.M) { // Uninstall Flux output, err = executeCommand("uninstall -s --keep-namespace") if err != nil { - panic(fmt.Errorf("uninstall falied: %s error:'%w'", output, err)) + panic(fmt.Errorf("uninstall failed: %s error:'%w'", output, err)) } // Delete namespace and wait for finalisation diff --git a/cmd/flux/testdata/helmrelease/reconcile_helmrelease_from_git.golden b/cmd/flux/testdata/helmrelease/reconcile_helmrelease_from_git.golden index 968c220b2b..a7591dfa7b 100644 --- a/cmd/flux/testdata/helmrelease/reconcile_helmrelease_from_git.golden +++ b/cmd/flux/testdata/helmrelease/reconcile_helmrelease_from_git.golden @@ -6,3 +6,7 @@ ✔ HelmRelease annotated ◎ waiting for HelmRelease reconciliation ✔ applied revision 6.0.0 +► annotating HelmChart {{ .ns }}-thrfg in {{ .ns }} namespace +✔ HelmChart annotated +◎ waiting for HelmChart reconciliation +✔ fetched revision 6.0.0 \ No newline at end of file From eba1536cfe7ea5bd66cf0862b45322f84a96d379 Mon Sep 17 00:00:00 2001 From: Raffael Sahli Date: Tue, 7 Mar 2023 07:29:50 +0000 Subject: [PATCH 3/3] test: add missing new line Signed-off-by: Raffael Sahli --- .../testdata/helmrelease/reconcile_helmrelease_from_git.golden | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/flux/testdata/helmrelease/reconcile_helmrelease_from_git.golden b/cmd/flux/testdata/helmrelease/reconcile_helmrelease_from_git.golden index a7591dfa7b..a3892eccc0 100644 --- a/cmd/flux/testdata/helmrelease/reconcile_helmrelease_from_git.golden +++ b/cmd/flux/testdata/helmrelease/reconcile_helmrelease_from_git.golden @@ -9,4 +9,4 @@ ► annotating HelmChart {{ .ns }}-thrfg in {{ .ns }} namespace ✔ HelmChart annotated ◎ waiting for HelmChart reconciliation -✔ fetched revision 6.0.0 \ No newline at end of file +✔ fetched revision 6.0.0