This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 328
/
api.go
103 lines (86 loc) · 2.79 KB
/
api.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package k8s
import (
"io/ioutil"
"strings"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
// clientsetInfo is used by some functions to collect the results returned by
// the clientset functions.
type clientsetInfo struct {
Clientset *kubernetes.Clientset
Namespace string
Config *rest.Config
}
// Clientset returns a K8S clientset and configured namespace. This will
// attempt to use in-cluster auth if available if kubeconfig is not explicitly
// specified. Otherwise, this will fall back to out of cluster auth.
func Clientset(kubeconfig, context string) (*kubernetes.Clientset, string, *rest.Config, error) {
if kubeconfig == "" {
cs, ns, c, err := ClientsetInCluster()
if err == nil {
return cs, ns, c, nil
}
// If we got an error about not being in the cluster, that's okay
// and fall back to out of cluster auth. If we got any other error
// though then report an error.
if err != rest.ErrNotInCluster {
return nil, "", nil, err
}
}
return ClientsetOutOfCluster(kubeconfig, context)
}
// ClientsetOutOfCluster loads a Kubernetes clientset using only a kubeconfig.
func ClientsetOutOfCluster(kubeconfig, context string) (*kubernetes.Clientset, string, *rest.Config, error) {
loader := clientcmd.NewDefaultClientConfigLoadingRules()
// Path to the kube config file
if kubeconfig != "" {
loader.ExplicitPath = kubeconfig
}
// Build our config and client
config := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
loader,
&clientcmd.ConfigOverrides{
CurrentContext: context,
},
)
// Get our configured namespace
ns, _, err := config.Namespace()
if err != nil {
return nil, "", nil, status.Errorf(codes.Aborted,
"failed to initialize K8S client configuration: %s", err)
}
clientconfig, err := config.ClientConfig()
if err != nil {
return nil, "", nil, status.Errorf(codes.Aborted,
"failed to initialize K8S client configuration: %s", err)
}
clientset, err := kubernetes.NewForConfig(clientconfig)
if err != nil {
return nil, "", nil, status.Errorf(codes.Aborted,
"failed to initialize K8S client: %s", err)
}
return clientset, ns, clientconfig, nil
}
// ClientsetInCluster returns a K8S clientset and configured namespace for
// in-cluster usage.
func ClientsetInCluster() (*kubernetes.Clientset, string, *rest.Config, error) {
config, err := rest.InClusterConfig()
if err != nil {
return nil, "", nil, err
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, "", nil, err
}
ns := "default"
if data, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"); err == nil {
if v := strings.TrimSpace(string(data)); len(v) > 0 {
ns = v
}
}
return clientset, ns, config, nil
}