From b0cb0030c3b68f6c06cd05fd643dcdf4397b5fb0 Mon Sep 17 00:00:00 2001 From: Daniel Ramich Date: Mon, 16 May 2022 09:12:45 -0600 Subject: [PATCH] Add options to load kubeconfig --- pkg/cmd/agent/agent.go | 16 ++-------------- pkg/costmodel/router.go | 17 +++-------------- pkg/kubeconfig/loader.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 28 deletions(-) create mode 100644 pkg/kubeconfig/loader.go diff --git a/pkg/cmd/agent/agent.go b/pkg/cmd/agent/agent.go index 427f4a7d7..b86ad0e6b 100644 --- a/pkg/cmd/agent/agent.go +++ b/pkg/cmd/agent/agent.go @@ -12,6 +12,7 @@ import ( "github.com/kubecost/cost-model/pkg/costmodel" "github.com/kubecost/cost-model/pkg/costmodel/clusters" "github.com/kubecost/cost-model/pkg/env" + "github.com/kubecost/cost-model/pkg/kubeconfig" "github.com/kubecost/cost-model/pkg/log" "github.com/kubecost/cost-model/pkg/prom" "github.com/kubecost/cost-model/pkg/util/watcher" @@ -23,8 +24,6 @@ import ( "github.com/rs/cors" "k8s.io/client-go/kubernetes" - "k8s.io/client-go/rest" - "k8s.io/client-go/tools/clientcmd" ) // AgentOpts contain configuration options that can be passed to the Execute() method @@ -50,18 +49,7 @@ func newKubernetesClusterCache() (kubernetes.Interface, clustercache.ClusterCach var err error // Kubernetes API setup - var kc *rest.Config - if kubeconfig := env.GetKubeConfigPath(); kubeconfig != "" { - kc, err = clientcmd.BuildConfigFromFlags("", kubeconfig) - } else { - kc, err = rest.InClusterConfig() - } - - if err != nil { - return nil, nil, err - } - - kubeClientset, err := kubernetes.NewForConfig(kc) + kubeClientset, err := kubeconfig.LoadKubeClient("") if err != nil { return nil, nil, err } diff --git a/pkg/costmodel/router.go b/pkg/costmodel/router.go index e851f0c37..5f7d45efa 100644 --- a/pkg/costmodel/router.go +++ b/pkg/costmodel/router.go @@ -14,6 +14,7 @@ import ( "time" "github.com/kubecost/cost-model/pkg/config" + "github.com/kubecost/cost-model/pkg/kubeconfig" "github.com/kubecost/cost-model/pkg/metrics" "github.com/kubecost/cost-model/pkg/services" "github.com/kubecost/cost-model/pkg/util/httputil" @@ -46,8 +47,6 @@ import ( "github.com/patrickmn/go-cache" "k8s.io/client-go/kubernetes" - "k8s.io/client-go/rest" - "k8s.io/client-go/tools/clientcmd" ) var sanitizePolicy = bluemonday.UGCPolicy() @@ -1442,19 +1441,9 @@ func Initialize(additionalConfigWatchers ...*watcher.ConfigMapWatcher) *Accesses log.Infof("Using scrape interval of %f", scrapeInterval.Seconds()) // Kubernetes API setup - var kc *rest.Config - if kubeconfig := env.GetKubeConfigPath(); kubeconfig != "" { - kc, err = clientcmd.BuildConfigFromFlags("", kubeconfig) - } else { - kc, err = rest.InClusterConfig() - } - + kubeClientset, err := kubeconfig.LoadKubeClient("") if err != nil { - panic(err.Error()) - } - kubeClientset, err := kubernetes.NewForConfig(kc) - if err != nil { - panic(err.Error()) + log.Fatalf("Failed to build Kubernetes client: %s", err.Error()) } // Create ConfigFileManager for synchronization of shared configuration diff --git a/pkg/kubeconfig/loader.go b/pkg/kubeconfig/loader.go new file mode 100644 index 000000000..70e241912 --- /dev/null +++ b/pkg/kubeconfig/loader.go @@ -0,0 +1,29 @@ +package kubeconfig + +import ( + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd" +) + +// LoadKubeconfig attempts to load a kubeconfig based on default locations. +// If a path is passed in then only that path is checked and will error +// if not found +func LoadKubeconfig(path string) (*rest.Config, error) { + // Use the default load order: KUBECONFIG env > $HOME/.kube/config > In cluster config + loadingRules := clientcmd.NewDefaultClientConfigLoadingRules() + if path != "" { + loadingRules.ExplicitPath = path + } + loader := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{}) + return loader.ClientConfig() +} + +// LoadKubeClient accepts a path to a kubeconfig to load and returns the clientset +func LoadKubeClient(path string) (*kubernetes.Clientset, error) { + config, err := LoadKubeconfig(path) + if err != nil { + return nil, err + } + return kubernetes.NewForConfig(config) +}