-
Notifications
You must be signed in to change notification settings - Fork 330
/
Copy pathkubeapi.go
72 lines (63 loc) · 1.4 KB
/
kubeapi.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
package kubeapi
import (
"bytes"
"fmt"
"os"
"path/filepath"
"time"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
// KubeAPI is the main data structure containing
// the various settings required to use the package.
// All methods use this structure as the receiver.
type KubeAPI struct {
Client *kubernetes.Clientset
Config *rest.Config
Timeout time.Duration
InCluster bool
}
// New returns a new instance of KubeAPI.
func New(t time.Duration, inCluster bool) (*KubeAPI, error) {
var api KubeAPI
api.Timeout = t
api.InCluster = inCluster
var err error
if api.InCluster {
api.Config, err = rest.InClusterConfig()
if err != nil {
return &api, err
}
} else {
kubeconfig := filepath.Join(homeDir(), ".kube", "config")
api.Config, err = clientcmd.BuildConfigFromFlags("", kubeconfig)
}
if err != nil {
return &api, err
}
api.Client, err = kubernetes.NewForConfig(api.Config)
if err != nil {
return &api, err
}
return &api, nil
}
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}
func createLabel(m map[string]string) string {
b := new(bytes.Buffer)
count := 0
for key, value := range m {
fmt.Fprintf(b, "%s=%s", key, value)
if count < (len(m) - 1) {
fmt.Fprintf(b, ",")
}
count++
}
return b.String()
}