mirror of
				https://github.com/fluxcd/flux2.git
				synced 2025-10-31 16:26:36 +08:00 
			
		
		
		
	Add source namespace to create commands
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
This commit is contained in:
		
							
								
								
									
										2
									
								
								.github/workflows/e2e.yaml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/workflows/e2e.yaml
									
									
									
									
										vendored
									
									
								
							| @ -121,7 +121,7 @@ jobs: | |||||||
|         run: | |         run: | | ||||||
|           /tmp/flux create hr podinfo-helm \ |           /tmp/flux create hr podinfo-helm \ | ||||||
|             --target-namespace=default \ |             --target-namespace=default \ | ||||||
|             --source=HelmRepository/podinfo \ |             --source=HelmRepository/podinfo.flux-system \ | ||||||
|             --chart=podinfo \ |             --chart=podinfo \ | ||||||
|             --chart-version=">4.0.0 <5.0.0" |             --chart-version=">4.0.0 <5.0.0" | ||||||
|       - name: flux create helmrelease --source=GitRepository/podinfo |       - name: flux create helmrelease --source=GitRepository/podinfo | ||||||
|  | |||||||
| @ -91,6 +91,12 @@ var createHelmReleaseCmd = &cobra.Command{ | |||||||
|     --source=HelmRepository/podinfo \ |     --source=HelmRepository/podinfo \ | ||||||
|     --chart=podinfo |     --chart=podinfo | ||||||
|  |  | ||||||
|  |   # Create a HelmRelease using a source from a different namespace | ||||||
|  |   flux create hr podinfo \ | ||||||
|  |     --namespace=default \ | ||||||
|  |     --source=HelmRepository/podinfo.flux-system \ | ||||||
|  |     --chart=podinfo | ||||||
|  |  | ||||||
|   # Create a HelmRelease definition on disk without applying it on the cluster |   # Create a HelmRelease definition on disk without applying it on the cluster | ||||||
|   flux create hr podinfo \ |   flux create hr podinfo \ | ||||||
|     --source=HelmRepository/podinfo \ |     --source=HelmRepository/podinfo \ | ||||||
| @ -166,6 +172,7 @@ func createHelmReleaseCmdRun(cmd *cobra.Command, args []string) error { | |||||||
| 					SourceRef: helmv2.CrossNamespaceObjectReference{ | 					SourceRef: helmv2.CrossNamespaceObjectReference{ | ||||||
| 						Kind:      helmReleaseArgs.source.Kind, | 						Kind:      helmReleaseArgs.source.Kind, | ||||||
| 						Name:      helmReleaseArgs.source.Name, | 						Name:      helmReleaseArgs.source.Name, | ||||||
|  | 						Namespace: helmReleaseArgs.source.Namespace, | ||||||
| 					}, | 					}, | ||||||
| 				}, | 				}, | ||||||
| 			}, | 			}, | ||||||
|  | |||||||
| @ -46,7 +46,7 @@ var createKsCmd = &cobra.Command{ | |||||||
| 	Long:    "The kustomization source create command generates a Kustomize resource for a given source.", | 	Long:    "The kustomization source create command generates a Kustomize resource for a given source.", | ||||||
| 	Example: `  # Create a Kustomization resource from a source at a given path | 	Example: `  # Create a Kustomization resource from a source at a given path | ||||||
|   flux create kustomization contour \ |   flux create kustomization contour \ | ||||||
|     --source=contour \ |     --source=GitRepository/contour \ | ||||||
|     --path="./examples/contour/" \ |     --path="./examples/contour/" \ | ||||||
|     --prune=true \ |     --prune=true \ | ||||||
|     --interval=10m \ |     --interval=10m \ | ||||||
| @ -58,7 +58,16 @@ var createKsCmd = &cobra.Command{ | |||||||
|   # Create a Kustomization resource that depends on the previous one |   # Create a Kustomization resource that depends on the previous one | ||||||
|   flux create kustomization webapp \ |   flux create kustomization webapp \ | ||||||
|     --depends-on=contour \ |     --depends-on=contour \ | ||||||
|     --source=webapp \ |     --source=GitRepository/webapp \ | ||||||
|  |     --path="./deploy/overlays/dev" \ | ||||||
|  |     --prune=true \ | ||||||
|  |     --interval=5m \ | ||||||
|  |     --validation=client | ||||||
|  |  | ||||||
|  |   # Create a Kustomization using a source from a different namespace | ||||||
|  |   flux create kustomization podinfo \ | ||||||
|  |     --namespace=default \ | ||||||
|  |     --source=GitRepository/podinfo.flux-system \ | ||||||
|     --path="./deploy/overlays/dev" \ |     --path="./deploy/overlays/dev" \ | ||||||
|     --prune=true \ |     --prune=true \ | ||||||
|     --interval=5m \ |     --interval=5m \ | ||||||
| @ -147,6 +156,7 @@ func createKsCmdRun(cmd *cobra.Command, args []string) error { | |||||||
| 			SourceRef: kustomizev1.CrossNamespaceSourceReference{ | 			SourceRef: kustomizev1.CrossNamespaceSourceReference{ | ||||||
| 				Kind:      kustomizationArgs.source.Kind, | 				Kind:      kustomizationArgs.source.Kind, | ||||||
| 				Name:      kustomizationArgs.source.Name, | 				Name:      kustomizationArgs.source.Name, | ||||||
|  | 				Namespace: kustomizationArgs.source.Namespace, | ||||||
| 			}, | 			}, | ||||||
| 			Suspend:         false, | 			Suspend:         false, | ||||||
| 			Validation:      kustomizationArgs.validation, | 			Validation:      kustomizationArgs.validation, | ||||||
|  | |||||||
| @ -62,6 +62,12 @@ flux create helmrelease [name] [flags] | |||||||
|     --source=HelmRepository/podinfo \ |     --source=HelmRepository/podinfo \ | ||||||
|     --chart=podinfo |     --chart=podinfo | ||||||
|  |  | ||||||
|  |   # Create a HelmRelease using a source from a different namespace | ||||||
|  |   flux create hr podinfo \ | ||||||
|  |     --namespace=default \ | ||||||
|  |     --source=HelmRepository/podinfo.flux-system \ | ||||||
|  |     --chart=podinfo | ||||||
|  |  | ||||||
|   # Create a HelmRelease definition on disk without applying it on the cluster |   # Create a HelmRelease definition on disk without applying it on the cluster | ||||||
|   flux create hr podinfo \ |   flux create hr podinfo \ | ||||||
|     --source=HelmRepository/podinfo \ |     --source=HelmRepository/podinfo \ | ||||||
| @ -79,7 +85,7 @@ flux create helmrelease [name] [flags] | |||||||
|   -h, --help                                help for helmrelease |   -h, --help                                help for helmrelease | ||||||
|       --release-name string                 name used for the Helm release, defaults to a composition of '[<target-namespace>-]<HelmRelease-name>' |       --release-name string                 name used for the Helm release, defaults to a composition of '[<target-namespace>-]<HelmRelease-name>' | ||||||
|       --service-account string              the name of the service account to impersonate when reconciling this HelmRelease |       --service-account string              the name of the service account to impersonate when reconciling this HelmRelease | ||||||
|       --source helmChartSource              source that contains the chart in the format '<kind>/<name>', where kind must be one of: (HelmRepository, GitRepository, Bucket) |       --source helmChartSource              source that contains the chart in the format '<kind>/<name>.<namespace>', where kind must be one of: (HelmRepository, GitRepository, Bucket) | ||||||
|       --target-namespace string             namespace to install this release, defaults to the HelmRelease namespace |       --target-namespace string             namespace to install this release, defaults to the HelmRelease namespace | ||||||
|       --values stringArray                  local path to values.yaml files |       --values stringArray                  local path to values.yaml files | ||||||
|       --values-from helmReleaseValuesFrom   Kubernetes object reference that contains the values.yaml data key in the format '<kind>/<name>', where kind must be one of: (Secret, ConfigMap) |       --values-from helmReleaseValuesFrom   Kubernetes object reference that contains the values.yaml data key in the format '<kind>/<name>', where kind must be one of: (Secret, ConfigMap) | ||||||
|  | |||||||
| @ -18,7 +18,7 @@ flux create kustomization [name] [flags] | |||||||
| ``` | ``` | ||||||
|   # Create a Kustomization resource from a source at a given path |   # Create a Kustomization resource from a source at a given path | ||||||
|   flux create kustomization contour \ |   flux create kustomization contour \ | ||||||
|     --source=contour \ |     --source=GitRepository/contour \ | ||||||
|     --path="./examples/contour/" \ |     --path="./examples/contour/" \ | ||||||
|     --prune=true \ |     --prune=true \ | ||||||
|     --interval=10m \ |     --interval=10m \ | ||||||
| @ -30,7 +30,16 @@ flux create kustomization [name] [flags] | |||||||
|   # Create a Kustomization resource that depends on the previous one |   # Create a Kustomization resource that depends on the previous one | ||||||
|   flux create kustomization webapp \ |   flux create kustomization webapp \ | ||||||
|     --depends-on=contour \ |     --depends-on=contour \ | ||||||
|     --source=webapp \ |     --source=GitRepository/webapp \ | ||||||
|  |     --path="./deploy/overlays/dev" \ | ||||||
|  |     --prune=true \ | ||||||
|  |     --interval=5m \ | ||||||
|  |     --validation=client | ||||||
|  |  | ||||||
|  |   # Create a Kustomization using a source from a different namespace | ||||||
|  |   flux create kustomization podinfo \ | ||||||
|  |     --namespace=default \ | ||||||
|  |     --source=GitRepository/podinfo.flux-system \ | ||||||
|     --path="./deploy/overlays/dev" \ |     --path="./deploy/overlays/dev" \ | ||||||
|     --prune=true \ |     --prune=true \ | ||||||
|     --interval=5m \ |     --interval=5m \ | ||||||
| @ -55,7 +64,7 @@ flux create kustomization [name] [flags] | |||||||
|       --path safeRelativePath                    path to the directory containing a kustomization.yaml file (default ./) |       --path safeRelativePath                    path to the directory containing a kustomization.yaml file (default ./) | ||||||
|       --prune                                    enable garbage collection |       --prune                                    enable garbage collection | ||||||
|       --service-account string                   the name of the service account to impersonate when reconciling this Kustomization |       --service-account string                   the name of the service account to impersonate when reconciling this Kustomization | ||||||
|       --source kustomizationSource               source that contains the Kubernetes manifests in the format '[<kind>/]<name>', where kind must be one of: (GitRepository, Bucket), if kind is not specified it defaults to GitRepository |       --source kustomizationSource               source that contains the Kubernetes manifests in the format '[<kind>/]<name>.<namespace>', where kind must be one of: (GitRepository, Bucket), if kind is not specified it defaults to GitRepository | ||||||
|       --target-namespace string                  overrides the namespace of all Kustomization objects reconciled by this Kustomization |       --target-namespace string                  overrides the namespace of all Kustomization objects reconciled by this Kustomization | ||||||
|       --validation string                        validate the manifests before applying them on the cluster, can be 'client' or 'server' |       --validation string                        validate the manifests before applying them on the cluster, can be 'client' or 'server' | ||||||
| ``` | ``` | ||||||
|  | |||||||
| @ -30,6 +30,7 @@ var supportedHelmChartSourceKinds = []string{sourcev1.HelmRepositoryKind, source | |||||||
| type HelmChartSource struct { | type HelmChartSource struct { | ||||||
| 	Kind      string | 	Kind      string | ||||||
| 	Name      string | 	Name      string | ||||||
|  | 	Namespace string | ||||||
| } | } | ||||||
|  |  | ||||||
| func (s *HelmChartSource) String() string { | func (s *HelmChartSource) String() string { | ||||||
| @ -45,7 +46,7 @@ func (s *HelmChartSource) Set(str string) error { | |||||||
| 			s.Description()) | 			s.Description()) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	sourceKind, sourceName := utils.ParseObjectKindName(str) | 	sourceKind, sourceName, sourceNamespace := utils.ParseObjectKindNameNamespace(str) | ||||||
| 	if sourceKind == "" || sourceName == "" { | 	if sourceKind == "" || sourceName == "" { | ||||||
| 		return fmt.Errorf("invalid helm chart source '%s', must be in format <kind>/<name>", str) | 		return fmt.Errorf("invalid helm chart source '%s', must be in format <kind>/<name>", str) | ||||||
| 	} | 	} | ||||||
| @ -55,8 +56,9 @@ func (s *HelmChartSource) Set(str string) error { | |||||||
| 			sourceKind, strings.Join(supportedHelmChartSourceKinds, ", ")) | 			sourceKind, strings.Join(supportedHelmChartSourceKinds, ", ")) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	s.Name = sourceName |  | ||||||
| 	s.Kind = cleanSourceKind | 	s.Kind = cleanSourceKind | ||||||
|  | 	s.Name = sourceName | ||||||
|  | 	s.Namespace = sourceNamespace | ||||||
|  |  | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
| @ -67,7 +69,7 @@ func (s *HelmChartSource) Type() string { | |||||||
|  |  | ||||||
| func (s *HelmChartSource) Description() string { | func (s *HelmChartSource) Description() string { | ||||||
| 	return fmt.Sprintf( | 	return fmt.Sprintf( | ||||||
| 		"source that contains the chart in the format '<kind>/<name>', "+ | 		"source that contains the chart in the format '<kind>/<name>.<namespace>', "+ | ||||||
| 			"where kind must be one of: (%s)", | 			"where kind must be one of: (%s)", | ||||||
| 		strings.Join(supportedHelmChartSourceKinds, ", "), | 		strings.Join(supportedHelmChartSourceKinds, ", "), | ||||||
| 	) | 	) | ||||||
|  | |||||||
| @ -30,6 +30,7 @@ var supportedKustomizationSourceKinds = []string{sourcev1.GitRepositoryKind, sou | |||||||
| type KustomizationSource struct { | type KustomizationSource struct { | ||||||
| 	Kind      string | 	Kind      string | ||||||
| 	Name      string | 	Name      string | ||||||
|  | 	Namespace string | ||||||
| } | } | ||||||
|  |  | ||||||
| func (s *KustomizationSource) String() string { | func (s *KustomizationSource) String() string { | ||||||
| @ -45,7 +46,7 @@ func (s *KustomizationSource) Set(str string) error { | |||||||
| 			s.Description()) | 			s.Description()) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	sourceKind, sourceName := utils.ParseObjectKindName(str) | 	sourceKind, sourceName, sourceNamespace := utils.ParseObjectKindNameNamespace(str) | ||||||
| 	if sourceName == "" { | 	if sourceName == "" { | ||||||
| 		return fmt.Errorf("no name given for source of kind '%s'", sourceKind) | 		return fmt.Errorf("no name given for source of kind '%s'", sourceKind) | ||||||
| 	} | 	} | ||||||
| @ -61,8 +62,9 @@ func (s *KustomizationSource) Set(str string) error { | |||||||
| 			sourceKind, strings.Join(supportedKustomizationSourceKinds, ", ")) | 			sourceKind, strings.Join(supportedKustomizationSourceKinds, ", ")) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	s.Name = sourceName |  | ||||||
| 	s.Kind = cleanSourceKind | 	s.Kind = cleanSourceKind | ||||||
|  | 	s.Name = sourceName | ||||||
|  | 	s.Namespace = sourceNamespace | ||||||
|  |  | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
| @ -73,7 +75,7 @@ func (s *KustomizationSource) Type() string { | |||||||
|  |  | ||||||
| func (s *KustomizationSource) Description() string { | func (s *KustomizationSource) Description() string { | ||||||
| 	return fmt.Sprintf( | 	return fmt.Sprintf( | ||||||
| 		"source that contains the Kubernetes manifests in the format '[<kind>/]<name>', "+ | 		"source that contains the Kubernetes manifests in the format '[<kind>/]<name>.<namespace>', "+ | ||||||
| 			"where kind must be one of: (%s), if kind is not specified it defaults to GitRepository", | 			"where kind must be one of: (%s), if kind is not specified it defaults to GitRepository", | ||||||
| 		strings.Join(supportedKustomizationSourceKinds, ", "), | 		strings.Join(supportedKustomizationSourceKinds, ", "), | ||||||
| 	) | 	) | ||||||
|  | |||||||
| @ -216,9 +216,10 @@ func ContainsEqualFoldItemString(s []string, e string) (string, bool) { | |||||||
| 	return "", false | 	return "", false | ||||||
| } | } | ||||||
|  |  | ||||||
| func ParseObjectKindName(input string) (string, string) { | // ParseObjectKindName extracts the kind and name of a resource | ||||||
| 	kind := "" | // based on the '<kind>/<name>' format | ||||||
| 	name := input | func ParseObjectKindName(input string) (kind, name string) { | ||||||
|  | 	name = input | ||||||
| 	parts := strings.Split(input, "/") | 	parts := strings.Split(input, "/") | ||||||
| 	if len(parts) == 2 { | 	if len(parts) == 2 { | ||||||
| 		kind, name = parts[0], parts[1] | 		kind, name = parts[0], parts[1] | ||||||
| @ -226,6 +227,23 @@ func ParseObjectKindName(input string) (string, string) { | |||||||
| 	return kind, name | 	return kind, name | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // ParseObjectKindNameNamespace extracts the kind, name and namespace of a resource | ||||||
|  | // based on the '<kind>/<name>.<namespace>' format | ||||||
|  | func ParseObjectKindNameNamespace(input string) (kind, name, namespace string) { | ||||||
|  | 	name = input | ||||||
|  | 	parts := strings.Split(input, "/") | ||||||
|  | 	if len(parts) == 2 { | ||||||
|  | 		kind, name = parts[0], parts[1] | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	if nn := strings.Split(name, "."); len(nn) > 1 { | ||||||
|  | 		name = strings.Join(nn[:len(nn)-1], ".") | ||||||
|  | 		namespace = nn[len(nn)-1] | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return kind, name, namespace | ||||||
|  | } | ||||||
|  |  | ||||||
| func MakeDependsOn(deps []string) []dependency.CrossNamespaceDependencyReference { | func MakeDependsOn(deps []string) []dependency.CrossNamespaceDependencyReference { | ||||||
| 	refs := []dependency.CrossNamespaceDependencyReference{} | 	refs := []dependency.CrossNamespaceDependencyReference{} | ||||||
| 	for _, dep := range deps { | 	for _, dep := range deps { | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Stefan Prodan
					Stefan Prodan