github.com/SupersunnySea/draft@v0.16.0/pkg/kube/kube.go (about)

     1  package kube
     2  
     3  import (
     4  	"os"
     5  
     6  	"k8s.io/client-go/kubernetes"
     7  	"k8s.io/client-go/rest"
     8  	"k8s.io/client-go/tools/clientcmd"
     9  )
    10  
    11  // GetInClusterClient returns an in cluster Kubernetes client
    12  func GetInClusterClient() (kubernetes.Interface, error) {
    13  	cfg, err := rest.InClusterConfig()
    14  	if err != nil {
    15  		return nil, err
    16  	}
    17  	k8s, err := kubernetes.NewForConfig(cfg)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  	return k8s, nil
    22  }
    23  
    24  // GetOutOfClusterClient returns a client side Kubernetes client
    25  func GetOutOfClusterClient() (kubernetes.Interface, error) {
    26  	k8scfg := os.Getenv("KUBECONFIG")
    27  	if k8scfg == "" {
    28  		k8scfg = os.Getenv("HOME") + "/.kube/config"
    29  	}
    30  	cfg, err := clientcmd.BuildConfigFromFlags("", k8scfg)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	k8s, err := kubernetes.NewForConfig(cfg)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	return k8s, nil
    39  }