-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkube_conf.go
38 lines (33 loc) · 1.19 KB
/
kube_conf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package config
import (
"go/build"
"log"
"os"
"path/filepath"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"sigs.k8s.io/controller-runtime/pkg/envtest"
)
// KubeConfig returns REST config based on the environment
func KubeConfig() (*rest.Config, error) {
// if we're in k8s cluster, create in cluster config using service account
// otherwise, create out of cluster config using kubeconfig at $HOME/.kube/config
if os.Getenv("MOCK") == "true" {
log.Println("creating k8s client using test environment ...")
testEnv := envtest.Environment{
CRDDirectoryPaths: []string{
filepath.Join(build.Default.GOPATH, "pkg", "mod", "github.com", "kotalco", "kotal@v0.1.1-0.20230514162448-fbcd8ae2ec31", "config", "crd", "bases"),
},
ErrorIfCRDPathMissing: true,
}
return testEnv.Start()
} else if os.Getenv("KUBERNETES_SERVICE_HOST") != "" {
log.Println("creating k8s client using in-cluster config ...")
return rest.InClusterConfig()
} else {
log.Println("creating k8s client using out-of-cluster config ...")
kubeconfig := filepath.Join(homedir.HomeDir(), ".kube", "config")
return clientcmd.BuildConfigFromFlags("", kubeconfig)
}
}