-
Notifications
You must be signed in to change notification settings - Fork 3
/
pods.go
179 lines (159 loc) · 5.11 KB
/
pods.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
package main
import (
"context"
"math"
"time"
corev1 "k8s.io/api/core/v1"
_ "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
_ "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/tools/remotecommand"
metrics "k8s.io/metrics/pkg/client/clientset/versioned"
//
// Uncomment to load all auth plugins
// _ "k8s.io/client-go/plugin/pkg/client/auth"
//
// Or uncomment to load specific auth plugins
// _ "k8s.io/client-go/plugin/pkg/client/auth/azure"
// _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
// _ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
// _ "k8s.io/client-go/plugin/pkg/client/auth/openstack"
)
type K8sContainer struct {
Ready bool `json:"ready"`
RestartCount int32 `json:"restartCount"`
CpuUsage int64 `json:"cpuUsage"` // 1 Core = 1000 milli
CpuLimit int64 `json:"cpuLimit"` // 1 Core = 1000 milli
RamUsage int64 `json:"ramUsage"` // KB
RamLimit int64 `json:"ramLimit"` // KB
}
type K8sPod struct {
Name string `json:"name"`
Status corev1.PodPhase `json:"status"`
Age int64 `json:"age"`
Ready int64 `json:"ready"`
HostIp string `json:"hostIp"`
PodIp string `json:"podIp"`
CpuUsage int64 `json:"cpuUsage"` // 1 Core = 1000 milli
CpuLimit int64 `json:"cpuLimit"` // 1 Core = 1000 milli
CpuPercentage float32 `json:"cpuPercentage"`
RamUsage int64 `json:"ramUsage"` // KB
RamLimit int64 `json:"ramLimit"` // KB
RamPercentage float32 `json:"ramPercentage"`
Containers map[string]K8sContainer `json:"containers"`
}
func GetPods(namespace string, token string) ([]K8sPod, error) {
podMap := make(map[string]*K8sPod)
config, err := loadConfig(token)
if err != nil {
return nil, err
}
// create clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
// get pod list
pl, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, err
}
for _, item := range pl.Items {
metadata := item.GetObjectMeta()
if metadata != nil {
pod := K8sPod{
Name: metadata.GetName(),
Status: item.Status.Phase,
HostIp: item.Status.HostIP,
PodIp: item.Status.PodIP,
Containers: make(map[string]K8sContainer),
Ready: 0,
CpuUsage: -1,
RamUsage: -1,
}
if item.Status.StartTime != nil {
pod.Age = int64(time.Now().UTC().Sub(item.Status.StartTime.UTC()).Seconds())
}
for _, cs := range item.Status.ContainerStatuses {
pod.Containers[cs.Name] = K8sContainer{
Ready: cs.Ready,
RestartCount: cs.RestartCount,
CpuUsage: -1,
RamUsage: -1,
}
if cs.Ready {
pod.Ready++
}
}
for _, c := range item.Spec.Containers {
if container, ok := pod.Containers[c.Name]; ok {
cpuLimit := c.Resources.Limits.Cpu()
if cpuLimit != nil {
container.CpuLimit = cpuLimit.ScaledValue(resource.Milli)
pod.CpuLimit += container.CpuLimit
}
ramLimit := c.Resources.Limits.Memory()
if ramLimit != nil {
container.RamLimit = ramLimit.ScaledValue(resource.Kilo)
pod.RamLimit += container.RamLimit
}
pod.Containers[c.Name] = container
}
}
podMap[metadata.GetName()] = &pod
}
}
// metrics clientset
mc, err := metrics.NewForConfig(config)
if err != nil {
return nil, err
}
// get metrics
pml, err := mc.MetricsV1beta1().PodMetricses(namespace).List(context.TODO(), metav1.ListOptions{})
if err == nil {
// merge into result
for _, item := range pml.Items {
metadata := item.GetObjectMeta()
if metadata != nil {
if pod, ok := podMap[metadata.GetName()]; ok {
for _, c := range item.Containers {
if container, ok := pod.Containers[c.Name]; ok {
cpuUsage := c.Usage.Cpu()
if cpuUsage != nil {
container.CpuUsage = cpuUsage.ScaledValue(resource.Milli)
if pod.CpuUsage < 0 {
pod.CpuUsage = 0
}
pod.CpuUsage += container.CpuUsage
if pod.CpuLimit > 0 {
percentage := float64(pod.CpuUsage) / (float64(pod.CpuLimit) * float64(1.0))
pod.CpuPercentage = float32(math.Round(percentage*1000)) / float32(1000.0)
}
}
ramUsage := c.Usage.Memory()
if ramUsage != nil {
container.RamUsage = ramUsage.ScaledValue(resource.Kilo)
if pod.RamUsage < 0 {
pod.RamUsage = 0
}
pod.RamUsage += container.RamUsage
if pod.RamLimit > 0 {
percentage := float64(pod.RamUsage) / (float64(pod.RamLimit) * float64(1.0))
pod.RamPercentage = float32(math.Round(percentage*1000)) / float32(1000.0)
}
}
pod.Containers[c.Name] = container
}
}
}
}
}
} // if err == nil
pods := make([]K8sPod, 0, len(podMap))
for _, pod := range podMap {
pods = append(pods, *pod)
}
return pods, nil
}