Fix panic on reconcile with source of ExternalArtifact kind

Signed-off-by: Matheus Pimenta <matheuscscp@gmail.com>
pull/5630/head
Matheus Pimenta 19 hours ago
parent e95da82f5a
commit 69feb7214a
No known key found for this signature in database
GPG Key ID: 4639F038AE28FBFF

@ -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,
}
}
}

@ -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,
}
}

@ -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,
}
}

@ -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

Loading…
Cancel
Save