-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkube_client.go
95 lines (80 loc) · 2.45 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
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
//go:generate protoc -I . --gogo_out=plugins=grpc:. urkel.proto
package urkel
import (
crand "crypto/rand"
"encoding/binary"
"math/rand"
"os"
"path/filepath"
"sync"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
// PartitionMode to use with `iptables` command; REJECT or DROP.
type PartitionMode string
var (
Drop PartitionMode = "DROP"
Reject PartitionMode = "REJECT"
)
// FetchPods from Kubernetes using the given namespace and ListOptions.
// Pods are returned in randomly shuffled order.
func FetchPods(t require.TestingT, namespace, selector string) []v1.Pod {
var pods, err = kubeClient(t).CoreV1().Pods(namespace).List(metav1.ListOptions{LabelSelector: selector})
require.NoError(t, err, "listing pods")
require.NotEmpty(t, pods.Items)
return shuffled(pods.Items)
}
// kubeConfig builds and returns a K8s cluster config.
func kubeConfig(t require.TestingT) *restclient.Config {
kubeConfigOnce.Do(func() {
var cfg = os.Getenv("KUBECONFIG")
if cfg == "" {
if cfg = os.Getenv("HOME"); cfg == "" {
cfg = os.Getenv("USERPROFILE")
}
cfg = filepath.Join(cfg, ".kube", "config")
if _, err := os.Stat(cfg); err != nil {
cfg = "" // Fall back to in-cluster config mode.
}
}
var err error
kubeConfigInstance, err = clientcmd.BuildConfigFromFlags("", cfg)
require.NoError(t, err, "building kube config")
})
return kubeConfigInstance
}
// kubeClient builds and returns a K8s cluster client.
func kubeClient(t require.TestingT) *kubernetes.Clientset {
kubeClientOnce.Do(func() {
var err error
kubeClientInstance, err = kubernetes.NewForConfig(kubeConfig(t))
require.NoError(t, err, "building kube client")
})
return kubeClientInstance
}
// shuffled shuffles |pods| order, and returns it.
func shuffled(pods []v1.Pod) []v1.Pod {
rand.Shuffle(len(pods), func(i, j int) { pods[i], pods[j] = pods[j], pods[i] })
return pods
}
func init() {
// Seed prng with a cryptographic random number.
var b [8]byte
if _, err := crand.Reader.Read(b[:]); err != nil {
panic(err)
}
rand.Seed(int64(binary.LittleEndian.Uint64(b[:])))
}
var (
faultConns = make(map[string]*grpc.ClientConn)
faultConnMu sync.Mutex
kubeConfigOnce sync.Once
kubeConfigInstance *restclient.Config
kubeClientInstance *kubernetes.Clientset
kubeClientOnce sync.Once
)