-
Notifications
You must be signed in to change notification settings - Fork 1
/
kube_client.go
66 lines (55 loc) · 1.57 KB
/
kube_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
package main
import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
// KubeClient abstracts interaction with k8s.
type KubeClient struct {
clientset *kubernetes.Clientset
}
// NewKubeClient is a constructor for the new kube client.
// configPath should be an absolute path.
func NewKubeClient(inCluster bool, configPath string) (*KubeClient, error) {
clientset, err := newClientset(inCluster, configPath)
if err != nil {
return nil, err
}
return &KubeClient{
clientset: clientset,
}, nil
}
// GetDisks retrives all the disk names used by different persistent volumes.
func (k *KubeClient) GetDisks() ([]string, error) {
pvs, err := k.clientset.CoreV1().PersistentVolumes().List(metav1.ListOptions{})
if err != nil {
return nil, err
}
var disks []string
for _, pv := range pvs.Items {
disks = append(disks, pv.Spec.GCEPersistentDisk.PDName)
}
return disks, nil
}
func newClientset(inCluster bool, configPath string) (*kubernetes.Clientset, error) {
var config *rest.Config
var err error
if inCluster {
config, err = rest.InClusterConfig()
if err != nil {
return nil, fmt.Errorf("can't build the *in cluster* config: %s", err)
}
} else {
config, err = clientcmd.BuildConfigFromFlags("", configPath)
if err != nil {
return nil, fmt.Errorf("can't build the *out of cluster* config: %s", err)
}
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, fmt.Errorf("can't build kubernetes client: %s", err)
}
return clientset, nil
}