Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor k8s startup to return ClientConfig #345

Merged
merged 2 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 19 additions & 27 deletions pkg/kwrapper/k8s/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand All @@ -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)
}
13 changes: 6 additions & 7 deletions pkg/kwrapper/k8s/k3s_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,33 @@ import (

"github.com/rancher/norman/pkg/kwrapper/etcd"
"github.com/sirupsen/logrus"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)

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) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down