From 263c664acdfade650128e0bf220485fa6ee2a048 Mon Sep 17 00:00:00 2001 From: Michael Bridgen Date: Tue, 26 Jan 2021 17:05:55 +0000 Subject: [PATCH] Factor out more common secrets command code Making the secret without data is always the same, so factor that out. Signed-off-by: Michael Bridgen --- cmd/flux/create_secret.go | 15 +++++++++++++++ cmd/flux/create_secret_git.go | 19 ++++--------------- cmd/flux/create_secret_helm.go | 14 +------------- cmd/flux/create_secret_tls.go | 12 +----------- 4 files changed, 21 insertions(+), 39 deletions(-) diff --git a/cmd/flux/create_secret.go b/cmd/flux/create_secret.go index e091652e..6ca3b438 100644 --- a/cmd/flux/create_secret.go +++ b/cmd/flux/create_secret.go @@ -39,6 +39,21 @@ func init() { createCmd.AddCommand(createSecretCmd) } +func makeSecret(name string) (corev1.Secret, error) { + secretLabels, err := parseLabels() + if err != nil { + return corev1.Secret{}, err + } + + return corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, + Namespace: rootArgs.namespace, + Labels: secretLabels, + }, + }, nil +} + func upsertSecret(ctx context.Context, kubeClient client.Client, secret corev1.Secret) error { namespacedName := types.NamespacedName{ Namespace: secret.GetNamespace(), diff --git a/cmd/flux/create_secret_git.go b/cmd/flux/create_secret_git.go index 0178fa3e..eab2cb85 100644 --- a/cmd/flux/create_secret_git.go +++ b/cmd/flux/create_secret_git.go @@ -24,8 +24,6 @@ import ( "time" "github.com/spf13/cobra" - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/fluxcd/flux2/internal/flags" "github.com/fluxcd/flux2/internal/utils" @@ -106,6 +104,10 @@ func createSecretGitCmdRun(cmd *cobra.Command, args []string) error { return fmt.Errorf("secret name is required") } name := args[0] + secret, err := makeSecret(name) + if err != nil { + return err + } if secretGitArgs.url == "" { return fmt.Errorf("url is required") @@ -116,22 +118,9 @@ func createSecretGitCmdRun(cmd *cobra.Command, args []string) error { return fmt.Errorf("git URL parse failed: %w", err) } - secretLabels, err := parseLabels() - if err != nil { - return err - } - ctx, cancel := context.WithTimeout(context.Background(), rootArgs.timeout) defer cancel() - secret := corev1.Secret{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: rootArgs.namespace, - Labels: secretLabels, - }, - } - switch u.Scheme { case "ssh": pair, err := generateKeyPair(ctx, secretGitArgs.keyAlgorithm, secretGitArgs.rsaBits, secretGitArgs.ecdsaCurve) diff --git a/cmd/flux/create_secret_helm.go b/cmd/flux/create_secret_helm.go index 355afd7a..7ad282f9 100644 --- a/cmd/flux/create_secret_helm.go +++ b/cmd/flux/create_secret_helm.go @@ -21,8 +21,6 @@ import ( "fmt" "github.com/spf13/cobra" - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/fluxcd/flux2/internal/utils" ) @@ -74,21 +72,11 @@ func createSecretHelmCmdRun(cmd *cobra.Command, args []string) error { return fmt.Errorf("secret name is required") } name := args[0] - - secretLabels, err := parseLabels() + secret, err := makeSecret(name) if err != nil { return err } - secret := corev1.Secret{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: rootArgs.namespace, - Labels: secretLabels, - }, - StringData: map[string]string{}, - } - if secretHelmArgs.username != "" && secretHelmArgs.password != "" { secret.StringData["username"] = secretHelmArgs.username secret.StringData["password"] = secretHelmArgs.password diff --git a/cmd/flux/create_secret_tls.go b/cmd/flux/create_secret_tls.go index 70627963..0a7800ec 100644 --- a/cmd/flux/create_secret_tls.go +++ b/cmd/flux/create_secret_tls.go @@ -24,7 +24,6 @@ import ( "github.com/spf13/cobra" "github.com/spf13/pflag" corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "github.com/fluxcd/flux2/internal/utils" ) @@ -99,20 +98,11 @@ func createSecretTLSCmdRun(cmd *cobra.Command, args []string) error { return fmt.Errorf("secret name is required") } name := args[0] - - secretLabels, err := parseLabels() + secret, err := makeSecret(name) if err != nil { return err } - secret := corev1.Secret{ - ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: rootArgs.namespace, - Labels: secretLabels, - }, - StringData: map[string]string{}, - } if err = populateSecretTLS(&secret, secretTLSArgs); err != nil { return err }