From 732b9611fa24a1544fdfe110c69a6ee40ef097d8 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Wed, 12 Feb 2020 14:04:31 -0700 Subject: [PATCH 1/2] Refactor k8s startup to return ClientConfig --- pkg/kwrapper/k8s/config.go | 46 +++++++++++++++-------------------- pkg/kwrapper/k8s/k3s_linux.go | 13 +++++----- 2 files changed, 25 insertions(+), 34 deletions(-) diff --git a/pkg/kwrapper/k8s/config.go b/pkg/kwrapper/k8s/config.go index 8b10a0dd6..21b1e5191 100644 --- a/pkg/kwrapper/k8s/config.go +++ b/pkg/kwrapper/k8s/config.go @@ -3,20 +3,15 @@ package k8s import ( "context" "fmt" - "io/ioutil" - "os" + "github.com/rancher/wrangler/pkg/kubeconfig" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" ) -func Auto(ctx context.Context) (bool, context.Context, *rest.Config, error) { - return GetConfig(ctx, "auto", os.Getenv("KUBECONFIG")) -} - -func GetConfig(ctx context.Context, k8sMode string, kubeConfig string) (bool, context.Context, *rest.Config, error) { +func GetConfig(ctx context.Context, k8sMode string, kubeConfig string) (bool, clientcmd.ClientConfig, error) { var ( - cfg *rest.Config + cfg clientcmd.ClientConfig err error ) @@ -26,33 +21,30 @@ func GetConfig(ctx context.Context, k8sMode string, kubeConfig string) (bool, co case "embedded": return getEmbedded(ctx) case "external": - cfg, err = getExternal(kubeConfig) + cfg = getExternal(kubeConfig) default: - return false, nil, nil, fmt.Errorf("invalid k8s-mode %s", k8sMode) + return false, nil, fmt.Errorf("invalid k8s-mode %s", k8sMode) } - return false, ctx, cfg, err + return false, cfg, err } -func getAuto(ctx context.Context, kubeConfig string) (bool, context.Context, *rest.Config, error) { - if kubeConfig != "" { - cfg, err := getExternal(kubeConfig) - return false, ctx, cfg, err - } - - if config, err := rest.InClusterConfig(); err == nil { - if config.BearerToken == "" { - tokenBytes, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/token") - if err == nil { - config.BearerToken = string(tokenBytes) - } - } - return false, ctx, config, nil +func getAuto(ctx context.Context, kubeConfig string) (bool, clientcmd.ClientConfig, error) { + if isManual(kubeConfig) { + return false, kubeconfig.GetNonInteractiveClientConfig(kubeConfig), nil } return getEmbedded(ctx) } -func getExternal(kubeConfig string) (*rest.Config, error) { - return clientcmd.BuildConfigFromFlags("", kubeConfig) +func isManual(kubeConfig string) bool { + if kubeConfig != "" { + return true + } + _, inClusterErr := rest.InClusterConfig() + return inClusterErr == nil +} + +func getExternal(kubeConfig string) clientcmd.ClientConfig { + return kubeconfig.GetNonInteractiveClientConfig(kubeConfig) } diff --git a/pkg/kwrapper/k8s/k3s_linux.go b/pkg/kwrapper/k8s/k3s_linux.go index cf30c9c90..6f52be1eb 100644 --- a/pkg/kwrapper/k8s/k3s_linux.go +++ b/pkg/kwrapper/k8s/k3s_linux.go @@ -12,7 +12,6 @@ import ( "github.com/rancher/norman/pkg/kwrapper/etcd" "github.com/sirupsen/logrus" - "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" ) @@ -20,26 +19,26 @@ var ( kubeConfig = ".kube/k3s.yaml" ) -func getEmbedded(ctx context.Context) (bool, context.Context, *rest.Config, error) { +func getEmbedded(ctx context.Context) (bool, clientcmd.ClientConfig, error) { var ( err error ) etcdEndpoints, err := etcd.RunETCD(ctx, "./management-state") if err != nil { - return false, ctx, nil, err + return false, nil, err } kubeConfig, err := k3sServer(ctx, etcdEndpoints) if err != nil { - return false, ctx, nil, err + return false, nil, err } os.Setenv("KUBECONFIG", kubeConfig) - restConfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( - &clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeConfig}, &clientcmd.ConfigOverrides{}).ClientConfig() + clientConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig( + &clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeConfig}, &clientcmd.ConfigOverrides{}) - return true, ctx, restConfig, err + return true, clientConfig, nil } func k3sServer(ctx context.Context, endpoints []string) (string, error) { From 8d0eea5619af9d951b6342cf7758439dbc0f6a7d Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Wed, 12 Feb 2020 14:19:36 -0700 Subject: [PATCH 2/2] Update vendor --- .../rancher/wrangler/pkg/kubeconfig/loader.go | 52 +++++++++++++++++++ vendor/modules.txt | 1 + 2 files changed, 53 insertions(+) create mode 100644 vendor/github.com/rancher/wrangler/pkg/kubeconfig/loader.go diff --git a/vendor/github.com/rancher/wrangler/pkg/kubeconfig/loader.go b/vendor/github.com/rancher/wrangler/pkg/kubeconfig/loader.go new file mode 100644 index 000000000..24ac4375a --- /dev/null +++ b/vendor/github.com/rancher/wrangler/pkg/kubeconfig/loader.go @@ -0,0 +1,52 @@ +package kubeconfig + +import ( + "io" + "io/ioutil" + "os" + "path/filepath" + + "k8s.io/client-go/tools/clientcmd" +) + +func GetNonInteractiveClientConfig(kubeConfig string) clientcmd.ClientConfig { + return GetClientConfig(kubeConfig, nil) +} + +func GetInteractiveClientConfig(kubeConfig string) clientcmd.ClientConfig { + return GetClientConfig(kubeConfig, os.Stdin) +} + +func GetClientConfig(kubeConfig string, reader io.Reader) clientcmd.ClientConfig { + loadingRules := GetLoadingRules(kubeConfig) + overrides := &clientcmd.ConfigOverrides{ClusterDefaults: clientcmd.ClusterDefaults} + return clientcmd.NewInteractiveDeferredLoadingClientConfig(loadingRules, overrides, reader) +} + +func GetLoadingRules(kubeConfig string) *clientcmd.ClientConfigLoadingRules { + loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() + loadingRules.DefaultClientConfig = &clientcmd.DefaultClientConfig + if kubeConfig != "" { + loadingRules.ExplicitPath = kubeConfig + } + + var otherFiles []string + homeDir, err := os.UserHomeDir() + if err == nil { + otherFiles = append(otherFiles, filepath.Join(homeDir, ".kube", "k3s.yaml")) + } + otherFiles = append(otherFiles, "/etc/rancher/k3s/k3s.yaml") + loadingRules.Precedence = append(loadingRules.Precedence, canRead(otherFiles)...) + + return loadingRules +} + +func canRead(files []string) (result []string) { + for _, f := range files { + _, err := ioutil.ReadFile(f) + if err == nil { + result = append(result, f) + } + } + return +} diff --git a/vendor/modules.txt b/vendor/modules.txt index 8a277490f..8a96e7fc8 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -66,6 +66,7 @@ github.com/prometheus/procfs github.com/prometheus/procfs/internal/fs github.com/prometheus/procfs/internal/util # github.com/rancher/wrangler v0.4.1 +github.com/rancher/wrangler/pkg/kubeconfig github.com/rancher/wrangler/pkg/name github.com/rancher/wrangler/pkg/ratelimit # github.com/sirupsen/logrus v1.4.2