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