-
Notifications
You must be signed in to change notification settings - Fork 16
/
kubernetes.go
96 lines (78 loc) · 2.33 KB
/
kubernetes.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
package main
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"github.com/ericchiang/k8s"
apiv1 "github.com/ericchiang/k8s/api/v1"
"github.com/ghodss/yaml"
)
type Kubernetes struct {
Client *k8s.Client
Context context.Context
}
type KubernetesClient interface {
GetNode(string) (*apiv1.Node, error)
GetNodeList(string) (*apiv1.NodeList, error)
}
// NewKubernetesClient return a Kubernetes client
func NewKubernetesClient(host string, port string, namespace string, kubeConfigPath string) (kubernetes KubernetesClient, err error) {
var client *k8s.Client
if len(host) > 0 && len(port) > 0 {
client, err = k8s.NewInClusterClient()
if err != nil {
err = fmt.Errorf("Error loading incluster client:\n%v", err)
return
}
} else if len(kubeConfigPath) > 0 {
client, err = loadK8sClient(kubeConfigPath)
if err != nil {
err = fmt.Errorf("Error loading client using kubeconfig:\n%v", err)
return
}
} else {
if namespace == "" {
namespace = "default"
}
client = &k8s.Client{
Endpoint: "http://127.0.0.1:8001",
Namespace: namespace,
Client: &http.Client{},
}
}
kubernetes = &Kubernetes{
Client: client,
Context: context.Background(),
}
return
}
// GetNode return the node object from given name
func (k *Kubernetes) GetNode(name string) (node *apiv1.Node, err error) {
node, err = k.Client.CoreV1().GetNode(k.Context, name)
return
}
// GetNodeList return a list of nodes from a given node pool name, if name is empty all nodes are returned
func (k *Kubernetes) GetNodeList(name string) (nodes *apiv1.NodeList, err error) {
labels := new(k8s.LabelSelector)
if name != "" {
labels.Eq("cloud.google.com/gke-nodepool", name)
}
nodes, err = k.Client.CoreV1().ListNodes(k.Context, labels.Selector())
return
}
// loadK8sClient parses a kubeconfig from a file and returns a Kubernetes
// client. It does not support extensions or client auth providers.
func loadK8sClient(kubeconfigPath string) (*k8s.Client, error) {
data, err := ioutil.ReadFile(kubeconfigPath)
if err != nil {
return nil, fmt.Errorf("Read kubeconfig error:\n%v", err)
}
// Unmarshal YAML into a Kubernetes config object.
var config k8s.Config
if err := yaml.Unmarshal(data, &config); err != nil {
return nil, fmt.Errorf("Unmarshal kubeconfig error:\n%v", err)
}
// fmt.Printf("%#v", config)
return k8s.NewClient(&config)
}