-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgather.go
258 lines (218 loc) · 5.98 KB
/
gather.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package main
import (
"fmt"
"io/ioutil"
"strings"
"github.com/mojo-zd/go-library/traverse"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/pkg/api"
"k8s.io/client-go/pkg/api/v1"
beta "k8s.io/client-go/pkg/apis/apps/v1beta1"
"k8s.io/client-go/pkg/apis/extensions/v1beta1"
"k8s.io/client-go/rest"
)
// Data kubernate rest data
type Data struct {
}
// Discovery kubernate discovery client
type Discovery struct {
client kubernetes.Interface
role string
}
// GathData kubernates data
type GathData struct {
pods *v1.PodList
nodes *v1.NodeList
services *v1.ServiceList
endpoints *v1.EndpointsList
deployments *v1beta1.DeploymentList
daemonsets *v1beta1.DaemonSetList
statefulsets *beta.StatefulSetList
stacks map[Stack]*[]v1beta1.Deployment
components map[KubeComponent]*[]v1.Pod
}
type ResourceStatus struct {
resources map[string]bool
status chan string
}
// Run fetch kubernates data
func (d *Discovery) Run() (gathData *GathData) {
over := false
resourceStatus := ResourceStatus{
resources: map[string]bool{"daemonSets": true, "deployments": true, "statefuls": true, "pods": true, "nodes": true, "services": true, "endpoints": true},
status: make(chan string),
}
gathData = &GathData{}
go func() {
daemonSets, err := d.client.ExtensionsV1beta1().DaemonSets(api.NamespaceAll).List(v1.ListOptions{})
if err != nil {
fmt.Printf("daemonSets error info is %s\n", err.Error())
}
gathData.daemonsets = daemonSets
resourceStatus.status <- "daemonSets"
}()
go func() {
deployments, err := d.client.ExtensionsV1beta1().Deployments(api.NamespaceAll).List(v1.ListOptions{})
if err != nil {
fmt.Printf("deployments error info is %s\n", err.Error())
}
gathData.deployments = deployments
gathData.stacks = assembleStack(deployments)
resourceStatus.status <- "deployments"
}()
go func() {
statefuls, err := d.client.AppsV1beta1().StatefulSets(api.NamespaceAll).List(v1.ListOptions{})
if err != nil {
fmt.Printf("statefuls error info is %s\n", err.Error())
}
gathData.statefulsets = statefuls
resourceStatus.status <- "statefuls"
}()
go func() {
pods, err := d.client.CoreV1().Pods(api.NamespaceAll).List(v1.ListOptions{})
if err != nil {
fmt.Printf("pods error info is %s\n", err.Error())
}
gathData.pods = pods
gathData.components = assembleComponent(pods)
resourceStatus.status <- "pods"
}()
go func() {
nodes, err := d.client.CoreV1().Nodes().List(v1.ListOptions{})
if err != nil {
fmt.Printf("nodes error info is %s\n", err.Error())
}
gathData.nodes = nodes
resourceStatus.status <- "nodes"
}()
go func() {
services, err := d.client.CoreV1().Services(api.NamespaceAll).List(v1.ListOptions{})
if err != nil {
fmt.Printf("services error info is %s\n", err.Error())
}
gathData.services = services
resourceStatus.status <- "services"
}()
go func() {
endpoints, err := d.client.CoreV1().Endpoints(api.NamespaceAll).List(v1.ListOptions{})
if err != nil {
fmt.Printf("endpoints error info is %s\n", err.Error())
}
gathData.endpoints = endpoints
resourceStatus.status <- "endpoints"
}()
for {
select {
case state := <-resourceStatus.status:
delete(resourceStatus.resources, state)
default:
if len(resourceStatus.resources) == 0 {
over = true
}
}
if over {
break
}
}
return
}
func assembleStack(deployments *v1beta1.DeploymentList) (stacks map[Stack]*[]v1beta1.Deployment) {
stacks = map[Stack]*[]v1beta1.Deployment{}
traverse.Iterator(deployments.Items, func(index int, value interface{}) (flag traverse.CYCLE_FLAG) {
item := value.(v1beta1.Deployment)
stack := Stack{
Name: stackName(item),
Namespace: item.Namespace,
}
if deployment, ok := stacks[stack]; ok {
*deployment = append(*deployment, item)
stacks[stack] = deployment
} else {
stacks[stack] = &[]v1beta1.Deployment{item}
}
return
})
return
}
func assembleComponent(podList *v1.PodList) (components map[KubeComponent]*[]v1.Pod) {
components = map[KubeComponent]*[]v1.Pod{}
traverse.Iterator(podList.Items, func(index int, value interface{}) (flag traverse.CYCLE_FLAG) {
pod := value.(v1.Pod)
if pod.Namespace != "kube-system" {
flag = traverse.CONTINUE_FLAT
return
}
for _, component := range kubeComponents {
if strings.Contains(pod.Name, component) {
cmp := KubeComponent{
Name: component,
Namespace: pod.Namespace,
}
if pods, ok := components[cmp]; ok {
*pods = append(*pods, pod)
components[cmp] = pods
} else {
components[cmp] = &[]v1.Pod{pod}
}
}
}
return
})
return
}
// New new discovery instance
func (e *Exporter) New() (*Discovery, error) {
var (
kcfg *rest.Config
)
kcfg = &rest.Config{
Host: e.APIServer.String(),
TLSClientConfig: rest.TLSClientConfig{
CAFile: e.TLSConfig.CAFile,
CertFile: e.TLSConfig.CertFile,
KeyFile: e.TLSConfig.KeyFile,
},
Insecure: e.TLSConfig.InsecureSkipVerify,
}
token := e.BearerToken
if e.BearerTokenFile != "" {
bf, err1 := ioutil.ReadFile(e.BearerTokenFile)
if err1 != nil {
return nil, err1
}
token = string(bf)
}
kcfg.BearerToken = token
kcfg.UserAgent = "prometheus/kubernates-exporter"
c, err2 := kubernetes.NewForConfig(kcfg)
if err2 != nil {
return nil, err2
}
return &Discovery{
client: c,
}, nil
}
func (e *Exporter) gatherData(ch chan<- prometheus.Metric) (*GathData, error) {
discovery, err := e.New()
if err != nil {
fmt.Println(err)
return nil, err
}
data := discovery.Run()
return data, nil
}
// Stack group of deployment
type Stack struct {
Name string
Namespace string
}
// KubeComponent group of pod
type KubeComponent struct {
Name string
Namespace string
}
func stackName(deployment v1beta1.Deployment) string {
return strings.Split(deployment.Name, "-")[0]
}
var kubeComponents = []string{"etcd", "kube-apiserver", "kube-controller-manager", "kube-dns", "kube-flannel", "kube-proxy", "kube-scheduler", "kubernetes-dashboard"}