From 5fd28439dc35fc75aac0c11d2bd21b189ccf5585 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CAnton?= Date: Mon, 19 Oct 2020 12:46:10 +0300 Subject: [PATCH] Add kustomization source and decryption provider flags --- cmd/gotk/create_kustomization.go | 35 +++---------- cmd/gotk/main.go | 6 +-- internal/flags/decryption_provider.go | 50 ++++++++++++++++++ internal/flags/kustomization_source.go | 72 ++++++++++++++++++++++++++ internal/flags/log_level.go | 2 +- 5 files changed, 133 insertions(+), 32 deletions(-) create mode 100644 internal/flags/decryption_provider.go create mode 100644 internal/flags/kustomization_source.go diff --git a/cmd/gotk/create_kustomization.go b/cmd/gotk/create_kustomization.go index 26f6315d..e19d9731 100644 --- a/cmd/gotk/create_kustomization.go +++ b/cmd/gotk/create_kustomization.go @@ -33,7 +33,7 @@ import ( helmv2 "github.com/fluxcd/helm-controller/api/v2beta1" kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1beta1" "github.com/fluxcd/pkg/apis/meta" - sourcev1 "github.com/fluxcd/source-controller/api/v1beta1" + "github.com/fluxcd/toolkit/internal/flags" "github.com/fluxcd/toolkit/internal/utils" ) @@ -72,7 +72,7 @@ var createKsCmd = &cobra.Command{ } var ( - ksSource string + ksSource flags.KustomizationSource ksPath string ksPrune bool ksDependsOn []string @@ -81,13 +81,12 @@ var ( ksHealthTimeout time.Duration ksSAName string ksSANamespace string - ksDecryptionProvider string + ksDecryptionProvider flags.DecryptionProvider ksDecryptionSecret string ) func init() { - createKsCmd.Flags().StringVar(&ksSource, "source", "", - "source that contains the Kubernetes manifests in the format '[/]', where kind can be GitRepository or Bucket, if kind is not specified it defaults to GitRepository") + createKsCmd.Flags().Var(&ksSource, "source", ksSource.Description()) 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 '/.'") @@ -96,7 +95,7 @@ func init() { createKsCmd.Flags().StringArrayVar(&ksDependsOn, "depends-on", nil, "Kustomization that must be ready before this Kustomization can be applied, supported formats '' and '/'") createKsCmd.Flags().StringVar(&ksSAName, "sa-name", "", "service account name") createKsCmd.Flags().StringVar(&ksSANamespace, "sa-namespace", "", "service account namespace") - createKsCmd.Flags().StringVar(&ksDecryptionProvider, "decryption-provider", "", "enables secrets decryption, provider can be 'sops'") + createKsCmd.Flags().Var(&ksDecryptionProvider, "decryption-provider", ksDecryptionProvider.Description()) createKsCmd.Flags().StringVar(&ksDecryptionSecret, "decryption-secret", "", "set the Kubernetes secret name that contains the OpenPGP private keys used for sops decryption") createCmd.AddCommand(createKsCmd) } @@ -107,19 +106,6 @@ func createKsCmdRun(cmd *cobra.Command, args []string) error { } name := args[0] - if ksSource == "" { - return fmt.Errorf("source is required") - } - - 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 == "" { return fmt.Errorf("path is required") } @@ -150,8 +136,8 @@ func createKsCmdRun(cmd *cobra.Command, args []string) error { Path: ksPath, Prune: ksPrune, SourceRef: kustomizev1.CrossNamespaceSourceReference{ - Kind: sourceKind, - Name: sourceName, + Kind: ksSource.Kind, + Name: ksSource.Name, }, Suspend: false, Validation: ksValidation, @@ -207,13 +193,8 @@ func createKsCmdRun(cmd *cobra.Command, args []string) error { } if ksDecryptionProvider != "" { - if !utils.ContainsItemString(supportedDecryptionProviders, ksDecryptionProvider) { - return fmt.Errorf("decryption provider %s is not supported, can be %v", - ksDecryptionProvider, supportedDecryptionProviders) - } - kustomization.Spec.Decryption = &kustomizev1.Decryption{ - Provider: ksDecryptionProvider, + Provider: ksDecryptionProvider.String(), } if ksDecryptionSecret != "" { diff --git a/cmd/gotk/main.go b/cmd/gotk/main.go index 0b305fc9..58192e21 100644 --- a/cmd/gotk/main.go +++ b/cmd/gotk/main.go @@ -110,10 +110,8 @@ var ( defaultNamespace = "gotk-system" defaultNotification = "notification-controller" - supportedDecryptionProviders = []string{"sops"} - supportedKustomizationSourceKinds = []string{sourcev1.GitRepositoryKind, sourcev1.BucketKind} - supportedHelmChartSourceKinds = []string{sourcev1.HelmRepositoryKind, sourcev1.GitRepositoryKind, sourcev1.BucketKind} - supportedSourceBucketProviders = []string{sourcev1.GenericBucketProvider, sourcev1.AmazonBucketProvider} + supportedHelmChartSourceKinds = []string{sourcev1.HelmRepositoryKind, sourcev1.GitRepositoryKind, sourcev1.BucketKind} + supportedSourceBucketProviders = []string{sourcev1.GenericBucketProvider, sourcev1.AmazonBucketProvider} ) func init() { diff --git a/internal/flags/decryption_provider.go b/internal/flags/decryption_provider.go new file mode 100644 index 00000000..99593ebb --- /dev/null +++ b/internal/flags/decryption_provider.go @@ -0,0 +1,50 @@ +/* +Copyright 2020 The Flux CD contributors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package flags + +import ( + "fmt" + "strings" + + "github.com/fluxcd/toolkit/internal/utils" +) + +var supportedDecryptionProviders = []string{"sops"} + +type DecryptionProvider string + +func (d *DecryptionProvider) String() string { + return string(*d) +} + +func (d *DecryptionProvider) Set(str string) error { + if !utils.ContainsItemString(supportedDecryptionProviders, str) { + return fmt.Errorf("unsupported decryption provider '%s', must be one of: %s", + str, strings.Join(supportedDecryptionProviders, ", ")) + + } + *d = DecryptionProvider(str) + return nil +} + +func (d *DecryptionProvider) Type() string { + return "decryptionProvider" +} + +func (d *DecryptionProvider) Description() string { + return fmt.Sprintf("decryption provider, available options are: (%s)", strings.Join(supportedDecryptionProviders, ", ")) +} diff --git a/internal/flags/kustomization_source.go b/internal/flags/kustomization_source.go new file mode 100644 index 00000000..f7b637f9 --- /dev/null +++ b/internal/flags/kustomization_source.go @@ -0,0 +1,72 @@ +/* +Copyright 2020 The Flux CD contributors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package flags + +import ( + "fmt" + "strings" + + sourcev1 "github.com/fluxcd/source-controller/api/v1beta1" + "github.com/fluxcd/toolkit/internal/utils" +) + +var supportedKustomizationSourceKinds = []string{sourcev1.GitRepositoryKind, sourcev1.BucketKind} + +type KustomizationSource struct { + Kind string + Name string +} + +func (k *KustomizationSource) String() string { + if k.Name == "" { + return "" + } + return fmt.Sprintf("%s/%s", k.Kind, k.Name) +} + +func (k *KustomizationSource) Set(str string) error { + if strings.TrimSpace(str) == "" { + return fmt.Errorf("no kustomization source given, please specify %s", + k.Description()) + } + + sourceKind, sourceName := utils.ParseObjectKindName(str) + if sourceKind == "" { + sourceKind = sourcev1.GitRepositoryKind + } + if !utils.ContainsItemString(supportedKustomizationSourceKinds, sourceKind) { + return fmt.Errorf("source kind '%s' is not supported, can be one of: %v", + sourceKind, strings.Join(supportedKustomizationSourceKinds, ", ")) + } + + k.Name = sourceName + k.Kind = sourceKind + + return nil +} + +func (k *KustomizationSource) Type() string { + return "kustomizationSource" +} + +func (k *KustomizationSource) Description() string { + return fmt.Sprintf( + "source that contains the Kubernetes manifests in the format '[/]',"+ + "where kind can be one of: %s, if kind is not specified it defaults to GitRepository", + strings.Join(supportedKustomizationSourceKinds, ", "), + ) +} diff --git a/internal/flags/log_level.go b/internal/flags/log_level.go index 37d2a6eb..f2014a59 100644 --- a/internal/flags/log_level.go +++ b/internal/flags/log_level.go @@ -50,5 +50,5 @@ func (l *LogLevel) Type() string { } func (l *LogLevel) Description() string { - return fmt.Sprintf("log level, available options are: (%s)", strings.Join(supportedArchs, ", ")) + return fmt.Sprintf("log level, available options are: (%s)", strings.Join(supportedLogLevels, ", ")) }