2
0
Эх сурвалжийг харах

Add options to load kubeconfig

Daniel Ramich 4 жил өмнө
parent
commit
b0cb0030c3

+ 2 - 14
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
 	}

+ 3 - 14
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

+ 29 - 0
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)
+}