Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
support multi-entry kubeconfig (#82)
Browse files Browse the repository at this point in the history
Fixes #81

Signed-off-by: Jacob Blain Christen <jacob@rancher.com>
  • Loading branch information
dweomer authored Oct 17, 2021
1 parent 8733bde commit dfee5bb
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package client
import (
"context"
"net"
"os"
"path/filepath"
"strconv"

"github.com/pkg/errors"
Expand All @@ -13,10 +15,10 @@ import (
corectlv1 "github.com/rancher/wrangler/pkg/generated/controllers/core/v1"
rbacctl "github.com/rancher/wrangler/pkg/generated/controllers/rbac"
rbacctlv1 "github.com/rancher/wrangler/pkg/generated/controllers/rbac/v1"
"github.com/rancher/wrangler/pkg/kubeconfig"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/kubernetes/pkg/credentialprovider"
"k8s.io/kubernetes/pkg/credentialprovider/secrets"
)
Expand Down Expand Up @@ -51,7 +53,20 @@ type Interface struct {
}

func NewInterface(kubecfg, kubectx, kubens string) (*Interface, error) {
cc := kubeconfig.GetNonInteractiveClientConfigWithContext(kubecfg, kubectx)
if err := os.Setenv(clientcmd.RecommendedConfigPathEnvVar, kubecfg); err != nil {
logrus.Warn(err)
}
lr := clientcmd.NewDefaultClientConfigLoadingRules()
lr.DefaultClientConfig = &clientcmd.DefaultClientConfig
if home, err := os.UserHomeDir(); err == nil {
lr.Precedence = append(lr.Precedence, filepath.Join(home, ".kube", "k3s.yaml"))
}
lr.Precedence = append(lr.Precedence, "/etc/rancher/k3s/k3s.yaml")
cc := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(lr, &clientcmd.ConfigOverrides{
ClusterDefaults: clientcmd.ClusterDefaults,
CurrentContext: kubectx,
})

ns, _, err := cc.Namespace()
if err != nil {
return nil, err
Expand Down

0 comments on commit dfee5bb

Please sign in to comment.