-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
284 lines (255 loc) · 7.6 KB
/
main.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"encoding/json"
"flag"
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"log"
"path/filepath"
"time"
)
var clientset *kubernetes.Clientset
// PodMetricsList : apis/metrics.k8s.io json struct
type PodMetricsList struct {
Kind string `json:"kind"`
APIVersion string `json:"apiVersion"`
Metadata struct {
SelfLink string `json:"selfLink"`
} `json:"metadata"`
Items []struct {
Metadata struct {
Name string `json:"name"`
Namespace string `json:"namespace"`
SelfLink string `json:"selfLink"`
CreationTimestamp time.Time `json:"creationTimestamp"`
} `json:"metadata"`
Timestamp time.Time `json:"timestamp"`
Window string `json:"window"`
Containers []struct {
Name string `json:"name"`
Usage struct {
CPU string `json:"cpu"`
Memory string `json:"memory"`
// add non exist GPU cnt field, fill by get pod
GPUCnt int64 `json:"gpu_cnt, omitempty"`
} `json:"usage"`
} `json:"containers"`
} `json:"items"`
}
// PodsInfo defines pod information struct
type PodsInfo struct {
Name string
UID types.UID
Status v1.PodPhase
Timestamp time.Time
}
// InactivePodsThresholdCnt defines the maximum size of InactivePods, do cleanup if exceeded
const InactivePodsThresholdCnt = 1000
// InactivePodsThresholdTime defines the maximum recent time to keep in InactivePods when cleanup
const InactivePodsThresholdTime = 24 * time.Hour
// LogInterval defines the interval of main routine
const LogInterval = 15 * time.Second
const GPUResourceKey = "nvidia.com/gpu"
// ActivePods stores active pods we are looking after
var ActivePods []PodsInfo
// InactivePods stores inactive pods which will never be looked after
var InactivePods []PodsInfo
// ActivePodsSet is the set of ActivePods.Name
var ActivePodsSet sets.String
// InactivePodsSet is the set of InactivePods.Name
var InactivePodsSet sets.String
// LastPodMetricsTime keeps the newest pod metrics by timestamp
var LastPodMetricsTime map[string]time.Time
func logPodInfo(name string) {
//podRaw, err := clientset.RESTClient().Get().Namespace("default").Resource("pod").Name(name).DoRaw()
podRaw, err := clientset.RESTClient().Get().AbsPath("api/v1/pods/" + name).DoRaw()
if err != nil {
log.Println("cannot get pod " + name + err.Error())
return
}
log.Print(string(podRaw))
}
func cleanup() {
if len(InactivePods) > InactivePodsThresholdCnt {
pos := -1
for i, m := range InactivePods {
if m.Timestamp.After(time.Now().Add(-InactivePodsThresholdTime)) {
pos = i
break
}
}
if pos == -1 {
log.Fatal("InactivePodsThresholdCnt and InactivePodsThresholdTime mismatch")
return
}
InactivePods = InactivePods[pos+1:]
InactivePodsSet = make(sets.String)
for _, m := range InactivePods {
InactivePodsSet.Insert(m.Name)
}
}
}
func worker() {
cleanup()
// clear map HasMetricsPodsSet
HasMetricsPodsSet := make(sets.String)
data, err := clientset.RESTClient().Get().AbsPath("apis/metrics.k8s.io/v1beta1/pods").DoRaw()
if err != nil {
log.Fatal(err.Error())
return
}
var pods PodMetricsList
err = json.Unmarshal(data, &pods)
for _, m := range pods.Items {
HasMetricsPodsSet.Insert(m.Metadata.Name)
if v, ok := LastPodMetricsTime[m.Metadata.Name]; !ok || m.Timestamp.After(v) {
LastPodMetricsTime[m.Metadata.Name] = m.Timestamp
// add GPU cnt field
podName, podNamespace := m.Metadata.Name, m.Metadata.Namespace
pod, err := clientset.CoreV1().Pods(podNamespace).Get(podName, metav1.GetOptions{})
if err != nil {
log.Println(err)
} else {
var i,j int
for i = range m.Containers {
find := false
for j = range pod.Spec.Containers {
if m.Containers[i].Name == pod.Spec.Containers[j].Name {
find = true
break
}
}
if find {
if quantity, exists := pod.Spec.Containers[j].Resources.Limits[GPUResourceKey]; exists {
m.Containers[i].Usage.GPUCnt = quantity.Value()
} else {
m.Containers[i].Usage.GPUCnt = 0
}
} else {
log.Println("cannot find pod GPU info: " + m.Containers[i].Name)
}
}
}
t, _ := json.Marshal(m)
log.Println(string(t))
}
}
podList, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{})
if err != nil {
log.Fatal(err.Error())
return
}
for _, pod := range podList.Items {
// ignore inactive pods
if InactivePodsSet.Has(pod.Name) {
continue
}
// first shown pods
// add to active pods set
// log pods info
if !ActivePodsSet.Has(pod.Name) {
ActivePodsSet.Insert(pod.Name)
currPodInfo := PodsInfo{
Name: pod.Name,
UID: pod.UID,
Status: pod.Status.Phase,
Timestamp: time.Now().Truncate(0),
}
ActivePods = append(ActivePods, currPodInfo)
logPodInfo(currPodInfo.Name)
} else {
// update and log when status change
// find pod in ActivePods
flag := false
for i, currPodInfo := range ActivePods {
if currPodInfo.Name == pod.Name {
flag = true
// check status change
if currPodInfo.Status != pod.Status.Phase {
// update status and timestamp
ActivePods[i].Status = pod.Status.Phase
ActivePods[i].Timestamp = time.Now().Truncate(0)
logPodInfo(currPodInfo.Name)
}
break
}
}
// should never happen
if !flag {
log.Fatal("ActivePods mismatch with ActivePodsSet")
return
}
// add pod into InactivePods if there's no metrics of it
// only consider "completed" pods
if !HasMetricsPodsSet.Has(pod.Name) {
if pod.Status.Phase == v1.PodSucceeded || pod.Status.Phase == v1.PodFailed {
ActivePodsSet.Delete(pod.Name)
InactivePodsSet.Insert(pod.Name)
currPodInfo := PodsInfo{
Name: pod.Name,
UID: pod.UID,
Status: pod.Status.Phase,
Timestamp: time.Now().Truncate(0),
}
InactivePods = append(InactivePods, currPodInfo)
pos := -1
for i, currPodInfo := range ActivePods {
if currPodInfo.Name == pod.Name {
pos = i
break
}
}
if pos != -1 {
ActivePods = append(ActivePods[:pos], ActivePods[pos+1:]...)
} else {
log.Fatal("ActivePods mismatch with ActivePodsSet when deleting")
return
}
}
}
}
}
}
func buildConfig(master, kubeconfig string) (*rest.Config, error) {
if master != "" || kubeconfig != "" {
return clientcmd.BuildConfigFromFlags(master, kubeconfig)
}
return rest.InClusterConfig()
}
func main() {
var kubeconfig *string
var logdir *string
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
logdir = flag.String("logdir", "/log", "absolute path to log dir")
flag.Parse()
log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))
logf, err := rotatelogs.New(
filepath.Join(*logdir, "PodLifecycle_log.%Y%m%d%H%M"),
rotatelogs.WithLinkName(filepath.Join(*logdir, "PodLifecycle_log")),
rotatelogs.WithRotationTime(24*time.Hour))
if err != nil {
log.Printf("failed to create rotatelogs: %s", err)
panic("can't write log to " + *logdir)
}
config, err := buildConfig("", *kubeconfig)
if err != nil {
log.Fatal(err.Error())
}
clientset, err = kubernetes.NewForConfig(config)
if err != nil {
log.Fatal(err.Error())
}
ActivePodsSet = make(sets.String)
InactivePodsSet = make(sets.String)
LastPodMetricsTime = make(map[string]time.Time)
log.Print("write log to " + *logdir)
log.SetOutput(logf)
wait.Forever(worker, LogInterval)
}