mirror of
https://github.com/fluxcd/flux2.git
synced 2025-11-02 10:48:03 +08:00
Refactor object kind/name parsing
This commit is contained in:
@ -20,7 +20,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
@ -119,14 +118,13 @@ func createHelmReleaseCmdRun(cmd *cobra.Command, args []string) error {
|
|||||||
if hrSource == "" {
|
if hrSource == "" {
|
||||||
return fmt.Errorf("source is required")
|
return fmt.Errorf("source is required")
|
||||||
}
|
}
|
||||||
hrSourceElements := strings.Split(hrSource, "/")
|
sourceKind, sourceName := utils.parseObjectKindName(hrSource)
|
||||||
if len(hrSourceElements) != 2 {
|
if sourceKind == "" {
|
||||||
return fmt.Errorf("invalid source '%s', must be in format <kind>/<name>", hrSource)
|
return fmt.Errorf("invalid source '%s', must be in format <kind>/<name>", hrSource)
|
||||||
}
|
}
|
||||||
hrSourceKind, hrSourceName := hrSourceElements[0], hrSourceElements[1]
|
if !utils.containsItemString(supportedHelmChartSourceKinds, sourceKind) {
|
||||||
if !utils.containsItemString(supportedHelmChartSourceKinds, hrSourceKind) {
|
|
||||||
return fmt.Errorf("source kind %s is not supported, can be %v",
|
return fmt.Errorf("source kind %s is not supported, can be %v",
|
||||||
hrSourceKind, supportedHelmChartSourceKinds)
|
sourceKind, supportedHelmChartSourceKinds)
|
||||||
}
|
}
|
||||||
if hrChart == "" {
|
if hrChart == "" {
|
||||||
return fmt.Errorf("chart name or path is required")
|
return fmt.Errorf("chart name or path is required")
|
||||||
@ -159,8 +157,8 @@ func createHelmReleaseCmdRun(cmd *cobra.Command, args []string) error {
|
|||||||
Chart: hrChart,
|
Chart: hrChart,
|
||||||
Version: hrChartVersion,
|
Version: hrChartVersion,
|
||||||
SourceRef: helmv2.CrossNamespaceObjectReference{
|
SourceRef: helmv2.CrossNamespaceObjectReference{
|
||||||
Kind: hrSourceKind,
|
Kind: sourceKind,
|
||||||
Name: hrSourceName,
|
Name: sourceName,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@ -39,7 +39,7 @@ var createKsCmd = &cobra.Command{
|
|||||||
Use: "kustomization [name]",
|
Use: "kustomization [name]",
|
||||||
Aliases: []string{"ks"},
|
Aliases: []string{"ks"},
|
||||||
Short: "Create or update a Kustomization resource",
|
Short: "Create or update a Kustomization resource",
|
||||||
Long: "The kustomization source create command generates a Kustomize resource for a given GitRepository 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
|
||||||
gotk create kustomization contour \
|
gotk create kustomization contour \
|
||||||
--source=contour \
|
--source=contour \
|
||||||
@ -85,7 +85,7 @@ var (
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
createKsCmd.Flags().StringVar(&ksSource, "source", "",
|
createKsCmd.Flags().StringVar(&ksSource, "source", "",
|
||||||
"source that contains the Kubernetes manifests, format '<kind>/<name>' where kind can be GitRepository or Bucket, if kind is not specified it defaults to GitRepository")
|
"source that contains the Kubernetes manifests in the format '[<kind>/]<name>', where kind can be GitRepository or Bucket, if kind is not specified it defaults to GitRepository")
|
||||||
createKsCmd.Flags().StringVar(&ksPath, "path", "./", "path to the directory containing the Kustomization file")
|
createKsCmd.Flags().StringVar(&ksPath, "path", "./", "path to the directory containing the Kustomization file")
|
||||||
createKsCmd.Flags().BoolVar(&ksPrune, "prune", false, "enable garbage collection")
|
createKsCmd.Flags().BoolVar(&ksPrune, "prune", false, "enable garbage collection")
|
||||||
createKsCmd.Flags().StringArrayVar(&ksHealthCheck, "health-check", nil, "workload to be included in the health assessment, in the format '<kind>/<name>.<namespace>'")
|
createKsCmd.Flags().StringArrayVar(&ksHealthCheck, "health-check", nil, "workload to be included in the health assessment, in the format '<kind>/<name>.<namespace>'")
|
||||||
@ -109,15 +109,13 @@ func createKsCmdRun(cmd *cobra.Command, args []string) error {
|
|||||||
return fmt.Errorf("source is required")
|
return fmt.Errorf("source is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
ksSourceKind := sourcev1.GitRepositoryKind
|
sourceKind, sourceName := utils.parseObjectKindName(ksSource)
|
||||||
ksSourceName := ksSource
|
if sourceKind == "" {
|
||||||
ksSourceElements := strings.Split(ksSource, "/")
|
sourceKind = sourcev1.GitRepositoryKind
|
||||||
if len(ksSourceElements) == 2 {
|
}
|
||||||
ksSourceKind, ksSourceName = ksSourceElements[0], ksSourceElements[1]
|
if !utils.containsItemString(supportedKustomizationSourceKinds, sourceKind) {
|
||||||
if !utils.containsItemString(supportedKustomizationSourceKinds, ksSourceKind) {
|
return fmt.Errorf("source kind %s is not supported, can be %v",
|
||||||
return fmt.Errorf("source kind %s is not supported, can be %v",
|
sourceKind, supportedKustomizationSourceKinds)
|
||||||
ksSourceKind, supportedKustomizationSourceKinds)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ksPath == "" {
|
if ksPath == "" {
|
||||||
@ -150,8 +148,8 @@ func createKsCmdRun(cmd *cobra.Command, args []string) error {
|
|||||||
Path: ksPath,
|
Path: ksPath,
|
||||||
Prune: ksPrune,
|
Prune: ksPrune,
|
||||||
SourceRef: kustomizev1.CrossNamespaceSourceReference{
|
SourceRef: kustomizev1.CrossNamespaceSourceReference{
|
||||||
Kind: ksSourceKind,
|
Kind: sourceKind,
|
||||||
Name: ksSourceName,
|
Name: sourceName,
|
||||||
},
|
},
|
||||||
Suspend: false,
|
Suspend: false,
|
||||||
Validation: ksValidation,
|
Validation: ksValidation,
|
||||||
|
|||||||
@ -180,6 +180,17 @@ func (*Utils) containsItemString(s []string, e string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (*Utils) parseObjectKindName(input string) (string, string) {
|
||||||
|
kind := ""
|
||||||
|
name := input
|
||||||
|
parts := strings.Split(input, "/")
|
||||||
|
if len(parts) == 2 {
|
||||||
|
kind, name = parts[0], parts[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
return kind, name
|
||||||
|
}
|
||||||
|
|
||||||
func (*Utils) makeDependsOn(deps []string) []dependency.CrossNamespaceDependencyReference {
|
func (*Utils) makeDependsOn(deps []string) []dependency.CrossNamespaceDependencyReference {
|
||||||
refs := []dependency.CrossNamespaceDependencyReference{}
|
refs := []dependency.CrossNamespaceDependencyReference{}
|
||||||
for _, dep := range deps {
|
for _, dep := range deps {
|
||||||
|
|||||||
@ -4,7 +4,7 @@ Create or update a Kustomization resource
|
|||||||
|
|
||||||
### Synopsis
|
### Synopsis
|
||||||
|
|
||||||
The kustomization source create command generates a Kustomize resource for a given GitRepository source.
|
The kustomization source create command generates a Kustomize resource for a given source.
|
||||||
|
|
||||||
```
|
```
|
||||||
gotk create kustomization [name] [flags]
|
gotk create kustomization [name] [flags]
|
||||||
@ -54,7 +54,7 @@ gotk create kustomization [name] [flags]
|
|||||||
--prune enable garbage collection
|
--prune enable garbage collection
|
||||||
--sa-name string service account name
|
--sa-name string service account name
|
||||||
--sa-namespace string service account namespace
|
--sa-namespace string service account namespace
|
||||||
--source string source that contains the Kubernetes manifests, format '<kind>/<name>' where kind can be GitRepository or Bucket, if kind is not specified it defaults to GitRepository
|
--source string source that contains the Kubernetes manifests in the format '[<kind>/]<name>', where kind can be GitRepository or Bucket, if kind is not specified it defaults to GitRepository
|
||||||
--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'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user