Created
December 30, 2021 11:15
-
-
Save jmunozal/dabd0eeb8e5fa7a3858d49f451a0f538 to your computer and use it in GitHub Desktop.
get Kubernetes client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ( | |
"context" | |
"encoding/json" | |
mux "github.com/gorilla/mux" | |
"github.com/spotahome/kooper/v2/log" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
"k8s.io/client-go/kubernetes" | |
"k8s.io/client-go/rest" | |
"k8s.io/client-go/tools/clientcmd" | |
"k8s.io/client-go/util/homedir" | |
"net/http" | |
"os" | |
"path/filepath" | |
) | |
func getK8sClient() *kubernetes.Clientset { | |
// Get k8s client. | |
k8scfg, err := rest.InClusterConfig() | |
if err != nil { | |
// Find | |
logger.Infof("Search K8s config locally") | |
// https://github.com/kubernetes/client-go/blob/master/examples/out-of-cluster-client-configuration/main.go | |
kubehome := filepath.Join(homedir.HomeDir(), ".kube", "config") | |
k8scfg, err = clientcmd.BuildConfigFromFlags("", kubehome) | |
if err != nil { | |
logger.Errorf("Error loading kubernetes configuration: %s", err) | |
os.Exit(1) | |
} | |
} | |
k8scli, err := kubernetes.NewForConfig(k8scfg) | |
if err != nil { | |
logger.Errorf("Error creating kubernetes client: %s\n", err) | |
os.Exit(1) | |
} | |
return k8scli | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment