From f91f69b01064ba12938515d34b0c061e44c51c3a Mon Sep 17 00:00:00 2001 From: ItalyPaleAle <43508+ItalyPaleAle@users.noreply.github.com> Date: Sun, 6 Aug 2023 15:09:27 -0700 Subject: [PATCH] Fixed parsing kubeconfig CLI flag Make CLI flag deprecated Add support for env var Signed-off-by: ItalyPaleAle <43508+ItalyPaleAle@users.noreply.github.com> --- bindings/kubernetes/kubernetes.go | 2 +- crypto/kubernetes/secrets/component.go | 9 ++-- internal/authentication/kubernetes/client.go | 48 +++++++++++++------- secretstores/kubernetes/kubernetes.go | 2 +- 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/bindings/kubernetes/kubernetes.go b/bindings/kubernetes/kubernetes.go index 0312888db3..55d94d4a52 100644 --- a/bindings/kubernetes/kubernetes.go +++ b/bindings/kubernetes/kubernetes.go @@ -65,7 +65,7 @@ func NewKubernetes(logger logger.Logger) bindings.InputBinding { } func (k *kubernetesInput) Init(ctx context.Context, metadata bindings.Metadata) error { - client, err := kubeclient.GetKubeClient() + client, err := kubeclient.GetKubeClient(k.logger) if err != nil { return err } diff --git a/crypto/kubernetes/secrets/component.go b/crypto/kubernetes/secrets/component.go index 7acd88238d..3f4222aad0 100644 --- a/crypto/kubernetes/secrets/component.go +++ b/crypto/kubernetes/secrets/component.go @@ -41,14 +41,17 @@ const ( type kubeSecretsCrypto struct { contribCrypto.LocalCryptoBaseComponent + logger logger.Logger md secretsMetadata kubeClient kubernetes.Interface } // NewKubeSecretsCrypto returns a new Kubernetes secrets crypto provider. // The key arguments in methods can be in the format "namespace/secretName/key" or "secretName/key" if using the default namespace passed as component metadata. -func NewKubeSecretsCrypto(_ logger.Logger) contribCrypto.SubtleCrypto { - k := &kubeSecretsCrypto{} +func NewKubeSecretsCrypto(log logger.Logger) contribCrypto.SubtleCrypto { + k := &kubeSecretsCrypto{ + logger: log, + } k.RetrieveKeyFn = k.retrieveKeyFromSecret return k } @@ -62,7 +65,7 @@ func (k *kubeSecretsCrypto) Init(_ context.Context, metadata contribCrypto.Metad } // Init Kubernetes client - k.kubeClient, err = kubeclient.GetKubeClient() + k.kubeClient, err = kubeclient.GetKubeClient(k.logger) if err != nil { return fmt.Errorf("failed to init Kubernetes client: %w", err) } diff --git a/internal/authentication/kubernetes/client.go b/internal/authentication/kubernetes/client.go index f4d3d3af6e..d5b732487c 100644 --- a/internal/authentication/kubernetes/client.go +++ b/internal/authentication/kubernetes/client.go @@ -15,38 +15,54 @@ package kubernetes import ( "flag" + "os" "path/filepath" "k8s.io/client-go/kubernetes" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/util/homedir" + + "github.com/dapr/kit/logger" +) + +const ( + cliFlagName = "kubeconfig" + envVarName = "KUBECONFIG" ) -//nolint:gochecknoglobals -var kubeconfig *string +func getKubeconfigPath(log logger.Logger) string { + // Check if the path is set via the CLI flag `-kubeconfig` + // This is deprecated + var cliVal string + fs := flag.NewFlagSet(cliFlagName, flag.ContinueOnError) + fs.StringVar(&cliVal, cliFlagName, "", "absolute path to the kubeconfig file") + _ = fs.Parse(os.Args) + cliFlag := fs.Lookup(cliFlagName) + if cliFlag != nil && cliFlag.Value.String() != "" { + log.Warn("Setting kubeconfig using the CLI flag --kubeconfig is deprecated and will be removed in a future version") + return cliFlag.Value.String() + } -//nolint:gochecknoinits -func init() { - if f := flag.CommandLine.Lookup("kubeconfig"); f != nil { - kubeconfigStr := f.Value.String() - kubeconfig = &kubeconfigStr - } else { - if home := homedir.HomeDir(); home != "" { - kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file") - } else { - kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file") + // Check if we have the KUBECONFIG env var + envVal := os.Getenv(envVarName) + if envVal != "" { + return envVal + } - } + // Return the default value + home := homedir.HomeDir() + if home != "" { + return filepath.Join(home, ".kube", "config") } + return "" } // GetKubeClient returns a kubernetes client. -func GetKubeClient() (*kubernetes.Clientset, error) { - flag.Parse() +func GetKubeClient(log logger.Logger) (*kubernetes.Clientset, error) { conf, err := rest.InClusterConfig() if err != nil { - conf, err = clientcmd.BuildConfigFromFlags("", *kubeconfig) + conf, err = clientcmd.BuildConfigFromFlags("", getKubeconfigPath(log)) if err != nil { return nil, err } diff --git a/secretstores/kubernetes/kubernetes.go b/secretstores/kubernetes/kubernetes.go index 3147c1ba37..9ae6878b42 100644 --- a/secretstores/kubernetes/kubernetes.go +++ b/secretstores/kubernetes/kubernetes.go @@ -42,7 +42,7 @@ func NewKubernetesSecretStore(logger logger.Logger) secretstores.SecretStore { // Init creates a Kubernetes client. func (k *kubernetesSecretStore) Init(_ context.Context, metadata secretstores.Metadata) error { - client, err := kubeclient.GetKubeClient() + client, err := kubeclient.GetKubeClient(k.logger) if err != nil { return err }