-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
122 lines (101 loc) · 2.82 KB
/
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package k8s
import (
"context"
"fmt"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"os"
"strconv"
)
type IPResourceInfo struct {
Name string
Namespace string
}
var _, clientset = configClusterClient()
var disabledK8sResource, _ = strconv.ParseBool(os.Getenv("K8S_PACKET_K8S_RESOURCES_DISABLED"))
func FetchK8SInfo() map[string]IPResourceInfo {
if disabledK8sResource {
fmt.Println("Getting k8s resources is disabled")
return map[string]IPResourceInfo{}
}
fmt.Println("Getting k8s resources")
m := make(map[string]IPResourceInfo)
pods, err := clientset.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{})
if err != nil {
fmt.Println(err.Error())
panic(err.Error())
}
fmt.Printf("Found %d pods\n", len(pods.Items))
for i := range pods.Items {
ipResourceInfo := new(IPResourceInfo)
pod := pods.Items[i]
ipResourceInfo.Name = "pod." + pod.Name
ipResourceInfo.Namespace = pod.Namespace
m[pod.Status.PodIP] = *ipResourceInfo
}
services, err := clientset.CoreV1().Services("").List(context.TODO(), metav1.ListOptions{})
if err != nil {
fmt.Println(err.Error())
panic(err.Error())
}
fmt.Printf("Found %d services\n", len(services.Items))
for i := range services.Items {
ipResourceInfo := new(IPResourceInfo)
service := services.Items[i]
ipResourceInfo.Name = "svc." + service.Name
ipResourceInfo.Namespace = service.Namespace
m[service.Spec.ClusterIP] = *ipResourceInfo
}
nodes, err := clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
fmt.Println(err.Error())
panic(err.Error())
}
fmt.Printf("Found %d nodes\n", len(nodes.Items))
for i := range nodes.Items {
ipResourceInfo := new(IPResourceInfo)
node := nodes.Items[i]
ipResourceInfo.Name = "node." + node.Name
ipResourceInfo.Namespace = "N/A"
for _, address := range node.Status.Addresses {
if address.Type == v1.NodeInternalIP {
m[address.Address] = *ipResourceInfo
break
}
}
}
return m
}
func GetPodIPsBySelectors(fieldSelector string, labelSelector string) []string {
if disabledK8sResource {
return []string{}
}
list := make([]string, 0)
pods, err := clientset.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{FieldSelector: fieldSelector, LabelSelector: labelSelector})
if err != nil {
fmt.Println(err.Error())
panic(err.Error())
}
for _, pod := range pods.Items {
list = append(list, pod.Status.PodIP)
}
return list
}
func configClusterClient() (error, *kubernetes.Clientset) {
if disabledK8sResource {
return nil, nil
}
config, err := rest.InClusterConfig()
if err != nil {
fmt.Println(err.Error())
panic(err.Error())
}
cs, err := kubernetes.NewForConfig(config)
if err != nil {
fmt.Println(err.Error())
panic(err.Error())
}
return err, cs
}