From e53d0dadbfd20af7cfd79f1d3a9bc4422da67a21 Mon Sep 17 00:00:00 2001 From: Hidde Beydals Date: Wed, 2 Sep 2020 20:00:31 +0200 Subject: [PATCH] Support GitRepository source in HelmRelease cmds --- .github/workflows/e2e.yaml | 27 ++++++++++------ cmd/gotk/create_helmrelease.go | 50 ++++++++++++++++++----------- cmd/gotk/reconcile_helmrelease.go | 8 ++++- docs/cmd/gotk_create_helmrelease.md | 28 ++++++++++------ 4 files changed, 75 insertions(+), 38 deletions(-) diff --git a/.github/workflows/e2e.yaml b/.github/workflows/e2e.yaml index 06cbff55..cb100bd5 100644 --- a/.github/workflows/e2e.yaml +++ b/.github/workflows/e2e.yaml @@ -88,32 +88,41 @@ jobs: - name: gotk delete kustomization run: | ./bin/gotk delete kustomization podinfo --silent - - name: gotk delete source git - run: | - ./bin/gotk delete source git podinfo --silent - name: gotk create source helm run: | ./bin/gotk create source helm podinfo \ --url https://stefanprodan.github.io/podinfo - - name: gotk create helmrelease + - name: gotk create helmrelease --source=HelmRepository/podinfo run: | - ./bin/gotk create hr podinfo \ + ./bin/gotk create hr podinfo-helm \ --target-namespace=default \ - --source=podinfo \ - --chart-name=podinfo \ + --source=HelmRepository/podinfo \ + --chart=podinfo \ --chart-version=">4.0.0 <5.0.0" + - name: gotk create helmrelease --source=GitRepository/podinfo + run: | + ./bin/gotk create hr podinfo-git \ + --target-namespace=default \ + --source=GitRepository/podinfo \ + --chart=./charts/podinfo - name: gotk get helmreleases run: | ./bin/gotk get helmreleases - name: gotk export helmrelease run: | ./bin/gotk export hr --all - - name: gotk delete helmrelease + - name: gotk delete helmrelease podinfo-helm + run: | + ./bin/gotk delete hr podinfo-helm --silent + - name: gotk delete helmrelease podinfo-git run: | - ./bin/gotk delete hr podinfo --silent + ./bin/gotk delete hr podinfo-git --silent - name: gotk delete source helm run: | ./bin/gotk delete source helm podinfo --silent + - name: gotk delete source git + run: | + ./bin/gotk delete source git podinfo --silent - name: gotk check run: | ./bin/gotk check diff --git a/cmd/gotk/create_helmrelease.go b/cmd/gotk/create_helmrelease.go index cff99edd..c172079e 100644 --- a/cmd/gotk/create_helmrelease.go +++ b/cmd/gotk/create_helmrelease.go @@ -20,6 +20,7 @@ import ( "context" "fmt" "io/ioutil" + "strings" "github.com/spf13/cobra" corev1 "k8s.io/api/core/v1" @@ -41,28 +42,36 @@ var createHelmReleaseCmd = &cobra.Command{ Aliases: []string{"hr"}, Short: "Create or update a HelmRelease resource", Long: "The helmrelease create command generates a HelmRelease resource for a given HelmRepository source.", - Example: ` # Create a HelmRelease from a source + Example: ` # Create a HelmRelease from a HelmRepository source gotk create hr podinfo \ --interval=10m \ --release-name=podinfo \ --target-namespace=default \ - --source=podinfo \ - --chart-name=podinfo \ + --source=HelmRepository/podinfo \ + --chart=podinfo \ --chart-version=">4.0.0" + # Create a HelmRelease from a GitRepository source + gotk create hr podinfo \ + --interval=10m \ + --release-name=podinfo \ + --target-namespace=default \ + --source=GitRepository/podinfo \ + --chart=./charts/podinfo + # Create a HelmRelease with values for a local YAML file gotk create hr podinfo \ --target-namespace=default \ - --source=podinfo \ - --chart-name=podinfo \ + --source=HelmRepository/podinfo \ + --chart=podinfo \ --chart-version=4.0.5 \ --values=./my-values.yaml # Create a HelmRelease definition on disk without applying it on the cluster gotk create hr podinfo \ --target-namespace=default \ - --source=podinfo \ - --chart-name=podinfo \ + --source=HelmRepository/podinfo \ + --chart=podinfo \ --chart-version=4.0.5 \ --values=./values.yaml \ --export > podinfo-release.yaml @@ -74,7 +83,7 @@ var ( hrName string hrSource string hrDependsOn []string - hrChartName string + hrChart string hrChartVersion string hrTargetNamespace string hrValuesFile string @@ -82,9 +91,9 @@ var ( func init() { createHelmReleaseCmd.Flags().StringVar(&hrName, "release-name", "", "name used for the Helm release, defaults to a composition of '-'") - createHelmReleaseCmd.Flags().StringVar(&hrSource, "source", "", "HelmRepository name") - createHelmReleaseCmd.Flags().StringVar(&hrChartName, "chart-name", "", "Helm chart name") - createHelmReleaseCmd.Flags().StringVar(&hrChartVersion, "chart-version", "", "Helm chart version, accepts semver range") + createHelmReleaseCmd.Flags().StringVar(&hrSource, "source", "", "source that contains the chart (/)") + createHelmReleaseCmd.Flags().StringVar(&hrChart, "chart", "", "Helm chart name or path") + createHelmReleaseCmd.Flags().StringVar(&hrChartVersion, "chart-version", "", "Helm chart version, accepts semver range (ignored for charts from GitRepository sources)") createHelmReleaseCmd.Flags().StringArrayVar(&hrDependsOn, "depends-on", nil, "HelmReleases that must be ready before this release can be installed") createHelmReleaseCmd.Flags().StringVar(&hrTargetNamespace, "target-namespace", "", "namespace to install this release, defaults to the HelmRelease namespace") createHelmReleaseCmd.Flags().StringVar(&hrValuesFile, "values", "", "local path to the values.yaml file") @@ -100,11 +109,16 @@ func createHelmReleaseCmdRun(cmd *cobra.Command, args []string) error { if hrSource == "" { return fmt.Errorf("source is required") } - if hrChartName == "" { - return fmt.Errorf("chart name is required") + hrSourceElements := strings.Split(hrSource, "/") + if len(hrSourceElements) != 2 { + return fmt.Errorf("source must be in format /") + } + hrSourceKind, hrSourceName := hrSourceElements[0], hrSourceElements[1] + if hrSourceKind != sourcev1.HelmRepositoryKind && hrSourceKind != sourcev1.GitRepositoryKind { + return fmt.Errorf("source kind must be one of: %s", []string{sourcev1.HelmRepositoryKind, sourcev1.GitRepositoryKind}) } - if hrChartVersion == "" { - return fmt.Errorf("chart version is required") + if hrChart == "" { + return fmt.Errorf("chart name or path is required") } ctx, cancel := context.WithTimeout(context.Background(), timeout) @@ -133,11 +147,11 @@ func createHelmReleaseCmdRun(cmd *cobra.Command, args []string) error { TargetNamespace: hrTargetNamespace, Chart: helmv2.HelmChartTemplate{ Spec: helmv2.HelmChartTemplateSpec{ - Chart: hrChartName, + Chart: hrChart, Version: hrChartVersion, SourceRef: helmv2.CrossNamespaceObjectReference{ - Kind: sourcev1.HelmRepositoryKind, - Name: hrSource, + Kind: hrSourceKind, + Name: hrSourceName, }, }, }, diff --git a/cmd/gotk/reconcile_helmrelease.go b/cmd/gotk/reconcile_helmrelease.go index f45cbdb7..7d733eb0 100644 --- a/cmd/gotk/reconcile_helmrelease.go +++ b/cmd/gotk/reconcile_helmrelease.go @@ -21,6 +21,7 @@ import ( "fmt" "time" + sourcev1 "github.com/fluxcd/source-controller/api/v1alpha1" "github.com/spf13/cobra" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" @@ -81,7 +82,12 @@ func reconcileHrCmdRun(cmd *cobra.Command, args []string) error { } if syncHrWithSource { - err := syncSourceHelmCmdRun(nil, []string{helmRelease.Spec.Chart.Spec.SourceRef.Name}) + switch helmRelease.Spec.Chart.Spec.SourceRef.Kind { + case sourcev1.HelmRepositoryKind: + err = syncSourceHelmCmdRun(nil, []string{helmRelease.Spec.Chart.Spec.SourceRef.Name}) + case sourcev1.GitRepositoryKind: + err = syncSourceGitCmdRun(nil, []string{helmRelease.Spec.Chart.Spec.SourceRef.Name}) + } if err != nil { return err } diff --git a/docs/cmd/gotk_create_helmrelease.md b/docs/cmd/gotk_create_helmrelease.md index a01e4bbb..ac8d1ebe 100644 --- a/docs/cmd/gotk_create_helmrelease.md +++ b/docs/cmd/gotk_create_helmrelease.md @@ -13,28 +13,36 @@ gotk create helmrelease [name] [flags] ### Examples ``` - # Create a HelmRelease from a source + # Create a HelmRelease from a HelmRepository source gotk create hr podinfo \ --interval=10m \ --release-name=podinfo \ --target-namespace=default \ - --source=podinfo \ - --chart-name=podinfo \ + --source=HelmRepository/podinfo \ + --chart=podinfo \ --chart-version=">4.0.0" + # Create a HelmRelease from a GitRepository source + gotk create hr podinfo \ + --interval=10m \ + --release-name=podinfo \ + --target-namespace=default \ + --source=GitRepository/podinfo \ + --chart=./charts/podinfo + # Create a HelmRelease with values for a local YAML file gotk create hr podinfo \ --target-namespace=default \ - --source=podinfo \ - --chart-name=podinfo \ + --source=HelmRepository/podinfo \ + --chart=podinfo \ --chart-version=4.0.5 \ --values=./my-values.yaml # Create a HelmRelease definition on disk without applying it on the cluster gotk create hr podinfo \ --target-namespace=default \ - --source=podinfo \ - --chart-name=podinfo \ + --source=HelmRepository/podinfo \ + --chart=podinfo \ --chart-version=4.0.5 \ --values=./values.yaml \ --export > podinfo-release.yaml @@ -44,12 +52,12 @@ gotk create helmrelease [name] [flags] ### Options ``` - --chart-name string Helm chart name - --chart-version string Helm chart version, accepts semver range + --chart string Helm chart name or path + --chart-version string Helm chart version, accepts semver range (ignored for charts from GitRepository sources) --depends-on stringArray HelmReleases that must be ready before this release can be installed -h, --help help for helmrelease --release-name string name used for the Helm release, defaults to a composition of '-' - --source string HelmRepository name + --source string source that contains the chart (/) --target-namespace string namespace to install this release, defaults to the HelmRelease namespace --values string local path to the values.yaml file ```