Skip to content

Commit

Permalink
Fixed parsing kubeconfig CLI flag
Browse files Browse the repository at this point in the history
Make CLI flag deprecated
Add support for env var

Signed-off-by: ItalyPaleAle <[email protected]>
  • Loading branch information
ItalyPaleAle committed Aug 6, 2023
1 parent 717bf5d commit f91f69b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
2 changes: 1 addition & 1 deletion bindings/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
9 changes: 6 additions & 3 deletions crypto/kubernetes/secrets/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}
Expand Down
48 changes: 32 additions & 16 deletions internal/authentication/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion secretstores/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit f91f69b

Please sign in to comment.