/
k8s_client.go
67 lines (59 loc) · 2.06 KB
/
k8s_client.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package purgek8sjobs
import (
"os"
"github.com/sirupsen/logrus"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
// getK8sAPIClient authenticates to the kubernetes cluster
// and returns the authenticated client. It first tries the in-cluster
// config way of authentication and then falls back to out-of-cluster
// authentication way.
func getK8sAPIClient() *kubernetes.Clientset {
config, err := getInClusterConfig()
if err != nil {
logrus.Warnf("Failed to use in-cluster configuration, will "+
"attempt out-of-cluster authentication. Error: %v", err.Error())
config, err = getClusterConfig()
if err != nil {
logrus.Fatalf("Failed to use the out-of-cluster "+
"authentication too, error: %v", err.Error())
}
}
// Create the client object from in-cluster configuration
logrus.Debug("Creating the k8s client object")
clientset, err := kubernetes.NewForConfig(&config)
if err != nil {
logrus.Fatalf("Failed to create the K8s client "+
"authentication too, error: %v", err.Error())
}
return clientset
}
// getInClusterConfig uses the service account tokens and cert set
// in the pod volume for authentication
// Recommended for use in k8s cronjobs / jobs
func getInClusterConfig() (rest.Config, error) {
logrus.Debug("Attempting to authenticate using the In-cluster config")
config, err := rest.InClusterConfig()
if err != nil {
return rest.Config{}, err
}
return *config, nil
}
// getClusterConfig uses the KUBECONFIG set via the env var to authenticate
// Recommended for use in local testing / external scripts
func getClusterConfig() (rest.Config, error) {
kubeconfig, exists := os.LookupEnv("KUBECONFIG")
if exists != true {
logrus.Warnf("KUBECONFIG env var not set")
}
logrus.WithField("KUBECONFIG", kubeconfig).Debug("Using kubeconfig from env:")
// use the current context in kubeconfig
logrus.Debug("Using the provided KUBECONFIG for the Out-of-cluster config")
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
return rest.Config{}, err
}
return *config, nil
}