-
Notifications
You must be signed in to change notification settings - Fork 0
/
pod.go
129 lines (107 loc) · 2.71 KB
/
pod.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
package watcher
import (
"sync"
"time"
"github.com/jawahars16/kubex/infra"
"github.com/jawahars16/kubex/kube"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func addQuantity() {
}
func mapPod(pod *v1.Pod, action string) Payload {
meta := Meta{
Name: pod.Name,
ID: string(pod.UID),
Namespace: pod.Namespace,
Labels: pod.ObjectMeta.Labels,
Created: pod.CreationTimestamp,
}
var cpuRequest float64
var cpuLimit float64
var memoryRequest float64
var memoryLimit float64
for _, container := range pod.Spec.Containers {
_cpuRequest := float64(container.Resources.Requests.Cpu().MilliValue())
_cpuLimit := float64(container.Resources.Limits.Cpu().MilliValue())
_memoryRequest := float64(container.Resources.Requests.Memory().MilliValue())
_memoryLimit := float64(container.Resources.Limits.Memory().MilliValue())
cpuRequest += _cpuRequest
cpuLimit += _cpuLimit
memoryRequest += _memoryRequest
memoryLimit += _memoryLimit
}
resource := Pod{
Meta: meta,
Status: pod.Status,
Request: Resources{
CPU: cpuRequest,
Memory: memoryRequest,
},
Limit: Resources{
CPU: cpuLimit,
Memory: memoryLimit,
},
Node: pod.Spec.NodeName,
}
return Payload{
Resource: resource,
Action: action,
}
}
// WatchPods ...
func WatchPods(socket infra.Socket, mutex *sync.Mutex) {
channel := kube.GetPodChannel(metav1.ListOptions{}, "") // TODO: Get namespace from user
for event := range channel {
if pod, ok := event.Object.(*v1.Pod); ok {
podDetail := mapPod(pod, "POD_"+string(event.Type))
mutex.Lock()
socket.Write(podDetail)
mutex.Unlock()
}
}
}
// WatchPodMetrics ...
func WatchPodMetrics(socket infra.Socket, mutex *sync.Mutex) {
ticker := time.NewTicker(time.Millisecond * 1000 * 3) // TODO: Get poll interval from user
writePodMetrics(socket, mutex)
for range ticker.C {
writePodMetrics(socket, mutex)
}
}
func writePodMetrics(socket infra.Socket, mutex *sync.Mutex) {
list, err := kube.GetPodMetrics()
if err != nil {
payload := Payload{
Action: "METRICS_ERROR",
Resource: PodMetrics{
Error: err.Error(),
},
}
mutex.Lock()
socket.Write(payload)
mutex.Unlock()
}
for _, pod := range list {
cpuUsage := 0.0
memoryUsage := 0.0
podContainers := pod.Containers
for _, container := range podContainers {
cpuQuantity := float64(container.Usage.Cpu().MilliValue())
memQuantity := float64(container.Usage.Memory().MilliValue())
cpuUsage += cpuQuantity
memoryUsage += memQuantity
}
payload := Payload{
Action: "METRICS",
Resource: PodMetrics{
Pod: pod.Name,
CPU: cpuUsage,
Memory: memoryUsage,
},
}
mutex.Lock()
socket.Write(payload)
mutex.Unlock()
}
}