diff --git a/cautils/k8sinterface/k8sconfig.go b/cautils/k8sinterface/k8sconfig.go index 15007bdb..84c5ab6b 100644 --- a/cautils/k8sinterface/k8sconfig.go +++ b/cautils/k8sinterface/k8sconfig.go @@ -4,15 +4,14 @@ import ( "context" "fmt" "os" - "path/filepath" "k8s.io/client-go/dynamic" "k8s.io/client-go/kubernetes" restclient "k8s.io/client-go/rest" - "k8s.io/client-go/tools/clientcmd" // DO NOT REMOVE - load cloud providers auth _ "k8s.io/client-go/plugin/pkg/client/auth" + "sigs.k8s.io/controller-runtime/pkg/client/config" ) // K8SConfig pointer to k8s config @@ -46,20 +45,17 @@ func NewKubernetesApi() *KubernetesApi { } } -var ConfigPath = filepath.Join(os.Getenv("HOME"), ".kube", "config") +// RunningIncluster whether running in cluster var RunningIncluster bool // LoadK8sConfig load config from local file or from cluster func LoadK8sConfig() error { - kubeconfig, err := clientcmd.BuildConfigFromFlags("", ConfigPath) + kubeconfig, err := config.GetConfig() if err != nil { - // kubeconfig, err = restclient.InClusterConfig() - // if err != nil { - return fmt.Errorf("Failed to load kubernetes config from file: '%s'.\n", ConfigPath) - // } - // RunningIncluster = true - } else { - RunningIncluster = false + return fmt.Errorf("Failed to load kubernetes config: %s\n", err) + } + if _, err := restclient.InClusterConfig(); err == nil { + RunningIncluster = true } K8SConfig = kubeconfig return nil diff --git a/go.mod b/go.mod index eaacf8ca..ce3983ca 100644 --- a/go.mod +++ b/go.mod @@ -23,8 +23,8 @@ require ( github.com/satori/go.uuid v1.2.0 golang.org/x/oauth2 v0.0.0-20210810183815-faf39c7919d5 gopkg.in/square/go-jose.v2 v2.6.0 // indirect - gotest.tools/v3 v3.0.3 // indirect k8s.io/api v0.22.0 k8s.io/apimachinery v0.22.0 k8s.io/client-go v0.22.0 + sigs.k8s.io/controller-runtime v0.9.6 )