Skip to content

Commit

Permalink
Add support for creating HR with .spec.ChartRef
Browse files Browse the repository at this point in the history
Signed-off-by: Soule BA <bah.soule@gmail.com>
  • Loading branch information
souleb committed May 10, 2024
1 parent ea30859 commit f2e77db
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 32 deletions.
63 changes: 42 additions & 21 deletions cmd/flux/create_helmrelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,17 @@ var createHelmReleaseCmd = &cobra.Command{
--source=HelmRepository/podinfo \
--chart=podinfo \
--values=./values.yaml \
--export > podinfo-release.yaml`,
--export > podinfo-release.yaml
# Create a HelmRelease using a chart from a HelmChart resource
flux create hr podinfo \
--namespace=default \
--chart-ref=HelmChart/podinfo.flux-system \
# Create a HelmRelease using a chart from an OCIRepository resource
flux create hr podinfo \
--namespace=default \
--chart-ref=OCIRepository/podinfo.flux-system`,
RunE: createHelmReleaseCmdRun,
}

Expand All @@ -114,6 +124,7 @@ type helmReleaseFlags struct {
dependsOn []string
chart string
chartVersion string
chartRef string
targetNamespace string
createNamespace bool
valuesFiles []string
Expand Down Expand Up @@ -144,14 +155,15 @@ func init() {
createHelmReleaseCmd.Flags().StringSliceVar(&helmReleaseArgs.valuesFrom, "values-from", nil, "a Kubernetes object reference that contains the values.yaml data key in the format '<kind>/<name>', where kind must be one of: (Secret,ConfigMap)")
createHelmReleaseCmd.Flags().Var(&helmReleaseArgs.crds, "crds", helmReleaseArgs.crds.Description())
createHelmReleaseCmd.Flags().StringVar(&helmReleaseArgs.kubeConfigSecretRef, "kubeconfig-secret-ref", "", "the name of the Kubernetes Secret that contains a key with the kubeconfig file for connecting to a remote cluster")
createHelmReleaseCmd.Flags().StringVar(&helmReleaseArgs.chartRef, "chart-ref", "", "the name of the HelmChart resource to use as source for the HelmRelease")
createCmd.AddCommand(createHelmReleaseCmd)
}

func createHelmReleaseCmdRun(cmd *cobra.Command, args []string) error {
name := args[0]

if helmReleaseArgs.chart == "" {
return fmt.Errorf("chart name or path is required")
if helmReleaseArgs.chart == "" && helmReleaseArgs.chartRef == "" {
return fmt.Errorf("chart or chart-ref is required")
}

sourceLabels, err := parseLabels()
Expand Down Expand Up @@ -181,21 +193,36 @@ func createHelmReleaseCmdRun(cmd *cobra.Command, args []string) error {
Duration: createArgs.interval,
},
TargetNamespace: helmReleaseArgs.targetNamespace,
Suspend: false,
},
}

Chart: helmv2.HelmChartTemplate{
Spec: helmv2.HelmChartTemplateSpec{
Chart: helmReleaseArgs.chart,
Version: helmReleaseArgs.chartVersion,
SourceRef: helmv2.CrossNamespaceObjectReference{
Kind: helmReleaseArgs.source.Kind,
Name: helmReleaseArgs.source.Name,
Namespace: helmReleaseArgs.source.Namespace,
},
ReconcileStrategy: helmReleaseArgs.reconcileStrategy,
switch {
case helmReleaseArgs.chart != "":
helmRelease.Spec.Chart = helmv2.HelmChartTemplate{
Spec: helmv2.HelmChartTemplateSpec{
Chart: helmReleaseArgs.chart,
Version: helmReleaseArgs.chartVersion,
SourceRef: helmv2.CrossNamespaceObjectReference{
Kind: helmReleaseArgs.source.Kind,
Name: helmReleaseArgs.source.Name,
Namespace: helmReleaseArgs.source.Namespace,
},
ReconcileStrategy: helmReleaseArgs.reconcileStrategy,
},
Suspend: false,
},
}
if helmReleaseArgs.chartInterval != 0 {
helmRelease.Spec.Chart.Spec.Interval = &metav1.Duration{
Duration: helmReleaseArgs.chartInterval,
}
}
case helmReleaseArgs.chartRef != "":
kind, name, ns := utils.ParseObjectKindNameNamespace(helmReleaseArgs.chartRef)
helmRelease.Spec.ChartRef = &helmv2.CrossNamespaceSourceReference{
Kind: kind,
Name: name,
Namespace: ns,
}
}

if helmReleaseArgs.kubeConfigSecretRef != "" {
Expand All @@ -206,12 +233,6 @@ func createHelmReleaseCmdRun(cmd *cobra.Command, args []string) error {
}
}

if helmReleaseArgs.chartInterval != 0 {
helmRelease.Spec.Chart.Spec.Interval = &metav1.Duration{
Duration: helmReleaseArgs.chartInterval,
}
}

if helmReleaseArgs.createNamespace {
if helmRelease.Spec.Install == nil {
helmRelease.Spec.Install = &helmv2.Install{}
Expand Down
30 changes: 19 additions & 11 deletions cmd/flux/reconcile_helmrelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

helmv2 "github.com/fluxcd/helm-controller/api/v2"
sourcev1 "github.com/fluxcd/source-controller/api/v1"
sourcev1b2 "github.com/fluxcd/source-controller/api/v1beta2"
)

var reconcileHrCmd = &cobra.Command{
Expand Down Expand Up @@ -68,23 +69,30 @@ func (obj helmReleaseAdapter) reconcileSource() bool {
}

func (obj helmReleaseAdapter) getSource() (reconcileSource, types.NamespacedName) {
cmd := reconcileWithSourceCommand{
apiType: helmChartType,
object: helmChartAdapter{&sourcev1.HelmChart{}},
force: true,
}

var (
name string
ns string
ns string
cmd reconcileCommand
)
switch {
case obj.Spec.Chart != &helmv2.HelmChart{}:
case obj.Spec.ChartRef != nil:
name, ns = obj.Spec.ChartRef.Name, obj.Spec.ChartRef.Namespace
cmd = reconcileCommand{
apiType: ociRepositoryType,
object: ociRepositoryAdapter{&sourcev1b2.OCIRepository{}},
}
if obj.Spec.ChartRef.Kind == sourcev1.HelmChartKind {
cmd.apiType = helmChartType
cmd.object = helmChartAdapter{&sourcev1.HelmChart{}}
}
default:
// default case assumes the HelmRelease is using a HelmChartTemplate
ns = obj.Spec.Chart.Spec.SourceRef.Namespace
name = fmt.Sprintf("%s-%s", obj.Namespace, obj.Name)
case obj.Spec.ChartRef != nil:
ns = obj.Spec.ChartRef.Namespace
name = obj.Spec.ChartRef.Name
cmd = reconcileCommand{
apiType: helmChartType,
object: helmChartAdapter{&sourcev1.HelmChart{}},
}
}

if ns == "" {
Expand Down

0 comments on commit f2e77db

Please sign in to comment.