From 69feb7214a89ab37810e757ce3b3822d36c7237b Mon Sep 17 00:00:00 2001 From: Matheus Pimenta Date: Mon, 17 Nov 2025 13:28:22 +0000 Subject: [PATCH] Fix panic on reconcile with source of ExternalArtifact kind Signed-off-by: Matheus Pimenta --- cmd/flux/reconcile_helmrelease.go | 33 +++++++++++++++++------------ cmd/flux/reconcile_kustomization.go | 12 +++++------ cmd/flux/reconcile_source_chart.go | 12 +++++------ cmd/flux/reconcile_with_source.go | 21 ++++++++++++------ 4 files changed, 46 insertions(+), 32 deletions(-) diff --git a/cmd/flux/reconcile_helmrelease.go b/cmd/flux/reconcile_helmrelease.go index fdcefdcb..b00c5409 100644 --- a/cmd/flux/reconcile_helmrelease.go +++ b/cmd/flux/reconcile_helmrelease.go @@ -20,7 +20,6 @@ import ( "fmt" "github.com/spf13/cobra" - "k8s.io/apimachinery/pkg/types" helmv2 "github.com/fluxcd/helm-controller/api/v2" sourcev1 "github.com/fluxcd/source-controller/api/v1" @@ -67,7 +66,7 @@ func (obj helmReleaseAdapter) reconcileSource() bool { return rhrArgs.syncHrWithSource } -func (obj helmReleaseAdapter) getSource() (reconcileSource, types.NamespacedName) { +func (obj helmReleaseAdapter) getSource() (reconcileSource, sourceReference) { var ( name string ns string @@ -78,21 +77,26 @@ func (obj helmReleaseAdapter) getSource() (reconcileSource, types.NamespacedName if ns == "" { ns = obj.Namespace } - namespacedName := types.NamespacedName{ - Name: name, - Namespace: ns, + srcRef := sourceReference{ + kind: obj.Spec.ChartRef.Kind, + name: name, + namespace: ns, } - if obj.Spec.ChartRef.Kind == sourcev1.HelmChartKind { + switch obj.Spec.ChartRef.Kind { + case sourcev1.HelmChartKind: return reconcileWithSourceCommand{ apiType: helmChartType, object: helmChartAdapter{&sourcev1.HelmChart{}}, force: true, - }, namespacedName + }, srcRef + case sourcev1.OCIRepositoryKind: + return reconcileCommand{ + apiType: ociRepositoryType, + object: ociRepositoryAdapter{&sourcev1.OCIRepository{}}, + }, srcRef + default: + return nil, srcRef } - return reconcileCommand{ - apiType: ociRepositoryType, - object: ociRepositoryAdapter{&sourcev1.OCIRepository{}}, - }, namespacedName default: // default case assumes the HelmRelease is using a HelmChartTemplate ns = obj.Spec.Chart.Spec.SourceRef.Namespace @@ -104,9 +108,10 @@ func (obj helmReleaseAdapter) getSource() (reconcileSource, types.NamespacedName apiType: helmChartType, object: helmChartAdapter{&sourcev1.HelmChart{}}, force: true, - }, types.NamespacedName{ - Name: name, - Namespace: ns, + }, sourceReference{ + kind: sourcev1.HelmChartKind, + name: name, + namespace: ns, } } } diff --git a/cmd/flux/reconcile_kustomization.go b/cmd/flux/reconcile_kustomization.go index 2eaee5a8..696f0e1f 100644 --- a/cmd/flux/reconcile_kustomization.go +++ b/cmd/flux/reconcile_kustomization.go @@ -18,7 +18,6 @@ package main import ( "github.com/spf13/cobra" - "k8s.io/apimachinery/pkg/types" kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1" sourcev1 "github.com/fluxcd/source-controller/api/v1" @@ -62,8 +61,8 @@ func (obj kustomizationAdapter) reconcileSource() bool { return rksArgs.syncKsWithSource } -func (obj kustomizationAdapter) getSource() (reconcileSource, types.NamespacedName) { - var cmd reconcileCommand +func (obj kustomizationAdapter) getSource() (reconcileSource, sourceReference) { + var cmd reconcileSource switch obj.Spec.SourceRef.Kind { case sourcev1.OCIRepositoryKind: cmd = reconcileCommand{ @@ -82,9 +81,10 @@ func (obj kustomizationAdapter) getSource() (reconcileSource, types.NamespacedNa } } - return cmd, types.NamespacedName{ - Name: obj.Spec.SourceRef.Name, - Namespace: obj.Spec.SourceRef.Namespace, + return cmd, sourceReference{ + kind: obj.Spec.SourceRef.Kind, + name: obj.Spec.SourceRef.Name, + namespace: obj.Spec.SourceRef.Namespace, } } diff --git a/cmd/flux/reconcile_source_chart.go b/cmd/flux/reconcile_source_chart.go index bd00f2bd..c31482d0 100644 --- a/cmd/flux/reconcile_source_chart.go +++ b/cmd/flux/reconcile_source_chart.go @@ -18,7 +18,6 @@ package main import ( "github.com/spf13/cobra" - "k8s.io/apimachinery/pkg/types" sourcev1 "github.com/fluxcd/source-controller/api/v1" ) @@ -58,8 +57,8 @@ func (obj helmChartAdapter) reconcileSource() bool { return rhcArgs.syncHrWithSource } -func (obj helmChartAdapter) getSource() (reconcileSource, types.NamespacedName) { - var cmd reconcileCommand +func (obj helmChartAdapter) getSource() (reconcileSource, sourceReference) { + var cmd reconcileSource switch obj.Spec.SourceRef.Kind { case sourcev1.HelmRepositoryKind: cmd = reconcileCommand{ @@ -78,9 +77,10 @@ func (obj helmChartAdapter) getSource() (reconcileSource, types.NamespacedName) } } - return cmd, types.NamespacedName{ - Name: obj.Spec.SourceRef.Name, - Namespace: obj.Namespace, + return cmd, sourceReference{ + kind: obj.Spec.SourceRef.Kind, + name: obj.Spec.SourceRef.Name, + namespace: obj.Namespace, } } diff --git a/cmd/flux/reconcile_with_source.go b/cmd/flux/reconcile_with_source.go index 4e31af83..95220207 100644 --- a/cmd/flux/reconcile_with_source.go +++ b/cmd/flux/reconcile_with_source.go @@ -15,11 +15,17 @@ import ( "github.com/fluxcd/flux2/v2/internal/utils" ) +type sourceReference struct { + kind string + name string + namespace string +} + type reconcileWithSource interface { adapter reconcilable reconcileSource() bool - getSource() (reconcileSource, types.NamespacedName) + getSource() (reconcileSource, sourceReference) } type reconcileSource interface { @@ -61,14 +67,17 @@ func (reconcile reconcileWithSourceCommand) run(cmd *cobra.Command, args []strin } if reconcile.object.reconcileSource() || reconcile.force { - reconcileCmd, nsName := reconcile.object.getSource() + reconcileCmd, srcRef := reconcile.object.getSource() + if reconcileCmd == nil { + return fmt.Errorf("cannot reconcile source of kind %s", srcRef.kind) + } + nsCopy := *kubeconfigArgs.Namespace - if nsName.Namespace != "" { - *kubeconfigArgs.Namespace = nsName.Namespace + if srcRef.namespace != "" { + *kubeconfigArgs.Namespace = srcRef.namespace } - err := reconcileCmd.run(nil, []string{nsName.Name}) - if err != nil { + if err := reconcileCmd.run(nil, []string{srcRef.name}); err != nil { return err } *kubeconfigArgs.Namespace = nsCopy