mirror of
				https://github.com/fluxcd/flux2.git
				synced 2025-11-01 01:25:53 +08:00 
			
		
		
		
	Support GitRepository source in HelmRelease cmds
This commit is contained in:
		
							
								
								
									
										27
									
								
								.github/workflows/e2e.yaml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										27
									
								
								.github/workflows/e2e.yaml
									
									
									
									
										vendored
									
									
								
							| @ -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 --silent | ||||
|           ./bin/gotk delete hr podinfo-helm --silent | ||||
|       - name: gotk delete helmrelease podinfo-git | ||||
|         run: | | ||||
|           ./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 | ||||
|  | ||||
| @ -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 '<target-namespace>-<hr-name>'") | ||||
| 	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 (<kind>/<name>)") | ||||
| 	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 <kind>/<name>") | ||||
| 	} | ||||
| 	if hrChartVersion == "" { | ||||
| 		return fmt.Errorf("chart version is required") | ||||
| 	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 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, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
|  | ||||
| @ -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 | ||||
| 		} | ||||
|  | ||||
| @ -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 '<target-namespace>-<hr-name>' | ||||
|       --source string             HelmRepository name | ||||
|       --source string             source that contains the chart (<kind>/<name>) | ||||
|       --target-namespace string   namespace to install this release, defaults to the HelmRelease namespace | ||||
|       --values string             local path to the values.yaml file | ||||
| ``` | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Hidde Beydals
					Hidde Beydals