Selaa lähdekoodia

support running cost-model locally

expose the env var KUBECONFIG_PATH in order to run outside of a
kubernetes pod.
Brendan Almonte 5 vuotta sitten
vanhempi
sitoutus
08678177ff
3 muutettua tiedostoa jossa 24 lisäystä ja 1 poistoa
  1. 9 0
      CONTRIBUTING.md
  2. 8 1
      pkg/costmodel/router.go
  3. 7 0
      pkg/env/costmodelenv.go

+ 9 - 0
CONTRIBUTING.md

@@ -22,6 +22,15 @@ To test, build the cost-model docker container and then push it to a Kubernetes
 
 To confirm that the server is running, you can hit [http://localhost:9003/costDataModel?timeWindow=1d](http://localhost:9003/costDataModel?timeWindow=1d)
 
+## Running locally ##
+
+In order to run cost-model locally, or outside of the runtime of a Kubernetes cluster, you can set the environment variable `KUBECONFIG_PATH`.
+
+Example:
+```bash
+export KUBECONFIG_PATH=~/.kube/config
+```
+
 ## Running the integration tests ##
 To run these tests:
 * Make sure you have a kubeconfig that can point to your cluster, and have permissions to create/modify a namespace called "test"

+ 8 - 1
pkg/costmodel/router.go

@@ -36,6 +36,7 @@ import (
 
 	"k8s.io/client-go/kubernetes"
 	"k8s.io/client-go/rest"
+	"k8s.io/client-go/tools/clientcmd"
 )
 
 const (
@@ -764,7 +765,13 @@ func Initialize(additionalConfigWatchers ...ConfigWatchers) {
 	}
 
 	// Kubernetes API setup
-	kc, err := rest.InClusterConfig()
+	var kc *rest.Config
+	if kubeconfig := env.GetKubeConfigPath(); kubeconfig != "" {
+		kc, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
+	} else {
+		kc, err = rest.InClusterConfig()
+	}
+
 	if err != nil {
 		panic(err.Error())
 	}

+ 7 - 0
pkg/env/costmodelenv.go

@@ -37,6 +37,8 @@ const (
 	MultiClusterBearerToken       = "MC_BEARER_TOKEN"
 
 	InsecureSkipVerify = "INSECURE_SKIP_VERIFY"
+
+	KubeConfigPathEnvVar = "KUBECONFIG_PATH"
 )
 
 // GetAWSAccessKeyID returns the environment variable value for AWSAccessKeyIDEnvVar which represents
@@ -209,3 +211,8 @@ func GetMultiClusterBasicAuthPassword() string {
 func GetMultiClusterBearerToken() string {
 	return Get(MultiClusterBearerToken, "")
 }
+
+// GetKubeConfigPath returns the environment variable value for KubeConfigPathEnvVar
+func GetKubeConfigPath() string {
+	return Get(KubeConfigPathEnvVar, "")
+}