Skip to content

Commit

Permalink
Merge pull request #345 from ibuildthecloud/clientconfig
Browse files Browse the repository at this point in the history
Refactor k8s startup to return ClientConfig
  • Loading branch information
ibuildthecloud authored Feb 12, 2020
2 parents 309abf1 + 8d0eea5 commit 041e160
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 34 deletions.
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

0 comments on commit 041e160

Please sign in to comment.