Skip to content

Commit

Permalink
✨ (kubernetes): Add support for colon-separated paths in KUBECONFIG
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Feb 28, 2023
1 parent 6167220 commit a1d91d3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
7 changes: 6 additions & 1 deletion internal/config/flags/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/clientcmd"
"os"
"path/filepath"
)
Expand Down Expand Up @@ -33,7 +34,11 @@ func Context(cmd *cobra.Command) {
if err != nil {
panic(err)
}
conf, err := kubernetes.RawConfig(kubeconfig)
configLoader := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{Precedence: filepath.SplitList(kubeconfig)},
nil,
)
conf, err := configLoader.RawConfig()
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
Expand Down
17 changes: 5 additions & 12 deletions internal/kubernetes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
"path/filepath"
)

type KubeClient struct {
Expand All @@ -27,20 +27,20 @@ func (client KubeClient) Secrets() v1.SecretInterface {
return client.ClientSet.CoreV1().Secrets(client.Namespace)
}

func NewConfigLoader(kubeconfigPath, context string) clientcmd.ClientConfig {
func NewConfigLoader(kubeconfig, context string) clientcmd.ClientConfig {
var overrides *clientcmd.ConfigOverrides
if context != "" {
overrides = &clientcmd.ConfigOverrides{CurrentContext: context}
}

return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfigPath},
&clientcmd.ClientConfigLoadingRules{Precedence: filepath.SplitList(kubeconfig)},
overrides,
)
}

func NewClient(kubeconfigPath, context, namespace string) (config KubeClient, err error) {
configLoader := NewConfigLoader(kubeconfigPath, context)
func NewClient(kubeconfig, context, namespace string) (config KubeClient, err error) {
configLoader := NewConfigLoader(kubeconfig, context)

config.ClientConfig, err = configLoader.ClientConfig()
if err != nil {
Expand Down Expand Up @@ -82,10 +82,3 @@ func NewClientFromCmd(cmd *cobra.Command) (KubeClient, error) {

return NewClient(kubeconfig, context, namespace)
}

func RawConfig(kubeconfigPath string) (api.Config, error) {
configLoader := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfigPath}, nil,
)
return configLoader.RawConfig()
}

0 comments on commit a1d91d3

Please sign in to comment.