-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
245 lines (208 loc) · 5.9 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
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"strings"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
var clientset *kubernetes.Clientset
func getPods() (*corev1.PodList, error) {
return clientset.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{
LabelSelector: "app=k8s-node-check",
})
}
func deletePod(pod corev1.Pod) {
clientset.CoreV1().Pods(pod.Namespace).Delete(context.TODO(), pod.Name, metav1.DeleteOptions{})
}
func deletePodForce(pod corev1.Pod) {
force := int64(0)
clientset.CoreV1().Pods(pod.Namespace).Delete(context.TODO(), pod.Name, metav1.DeleteOptions{
GracePeriodSeconds: &force,
})
}
func updateNodeStatus(node *corev1.Node) {
var nodeConditions []corev1.NodeCondition
for _, nodeCondition := range node.Status.Conditions {
if nodeCondition.Type == corev1.NodePIDPressure {
nodeCondition.LastHeartbeatTime = metav1.Now()
nodeCondition.LastTransitionTime = metav1.Now()
nodeCondition.Message = "k8s-node-check"
nodeCondition.Status = corev1.ConditionTrue
}
nodeConditions = append(nodeConditions, nodeCondition)
}
node.Status.Conditions = nodeConditions
clientset.CoreV1().Nodes().UpdateStatus(context.TODO(), node, metav1.UpdateOptions{})
}
func main() {
var settings struct {
create time.Duration
terminate time.Duration
every time.Duration
pods time.Duration
}
flag.DurationVar(&settings.create, "create", 10*time.Second, "")
flag.DurationVar(&settings.terminate, "terminate", 15*time.Second, "")
flag.DurationVar(&settings.every, "every", 5*time.Second, "")
flag.DurationVar(&settings.pods, "pods", 10*time.Minute, "")
flag.Parse()
var config *rest.Config
var err error
if path, ok := os.LookupEnv("KUBECONFIG"); !ok {
if config, err = rest.InClusterConfig(); err != nil {
panic(err)
}
} else {
if config, err = clientcmd.BuildConfigFromFlags("", path); err != nil {
panic(err)
}
}
if clientset, err = kubernetes.NewForConfig(config); err != nil {
panic(err)
}
for {
pods, err := getPods()
if err != nil {
time.Sleep(time.Second * 1)
continue
}
for _, pod := range pods.Items {
deletePod(pod)
}
break
}
podsLastCheckedAt := time.Now().Add(-settings.pods)
for {
startedAt := time.Now()
nodes, err := clientset.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Println("node list error", err)
continue
}
nodeNames := make(map[string]*corev1.Node)
for _, node := range nodes.Items {
nodeNames[node.Name] = node.DeepCopy()
}
if time.Since(podsLastCheckedAt) > settings.pods {
if pods, err := clientset.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{}); err != nil {
continue
} else {
for _, pod := range pods.Items {
if pod.ObjectMeta.DeletionTimestamp == nil {
continue
}
node := nodeNames[pod.Spec.NodeName]
if node == nil {
continue
}
nodeAge := time.Since(node.CreationTimestamp.Time)
podAge := time.Since(pod.CreationTimestamp.Time)
if podAge > nodeAge {
deletePodForce(pod)
}
}
}
podsLastCheckedAt = time.Now()
}
for _, node := range nodes.Items {
if node.Spec.Unschedulable {
//log.Println("node", node.Name, "unschedulable")
continue
}
ready := false
for _, condition := range node.Status.Conditions {
if condition.Type == "Ready" && condition.Status == "True" {
ready = true
break
}
}
if !ready {
//log.Println("node", node.Name, "not ready")
continue
}
podSpec := &corev1.Pod{
TypeMeta: metav1.TypeMeta{
Kind: "Pod",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("k8s-node-check-%s", node.ObjectMeta.GetUID()),
Labels: map[string]string{
"app": "k8s-node-check",
},
},
Spec: corev1.PodSpec{
NodeName: node.Name,
Containers: []corev1.Container{
{
Name: "bause",
Image: "ghcr.io/matti/bause:user",
},
},
},
}
if _, err := clientset.CoreV1().Pods("default").Create(context.TODO(), podSpec, metav1.CreateOptions{}); err != nil {
if !strings.Contains(err.Error(), "already exists") {
log.Println("pod create failed", err)
}
}
}
pods, err := getPods()
if err != nil {
continue
}
for _, pod := range pods.Items {
node := nodeNames[pod.Spec.NodeName]
if node == nil {
//log.Println("node", pod.Spec.NodeName, "no longer present, deleting pod", pod.Name)
deletePod(pod)
continue
}
podAge := time.Since(pod.GetCreationTimestamp().Time)
nodeAge := time.Since(node.GetObjectMeta().GetCreationTimestamp().Time)
switch pod.Status.Phase {
case "Pending":
if nodeAge < time.Minute*3 {
//log.Println("node", node.Name, nodeAge, "too young")
break
}
if podAge > settings.create {
log.Println("PROBLEM", "CREATE", pod.Spec.NodeName, podAge)
updateNodeStatus(node)
}
case "Running":
if pod.ObjectMeta.DeletionTimestamp == nil {
// Running
deletePod(pod)
} else if pod.ObjectMeta.DeletionGracePeriodSeconds != nil {
// Terminating
deletionGracePeriodSecondsDuration := time.Duration(*pod.ObjectMeta.DeletionGracePeriodSeconds) * time.Second
inTerminating := deletionGracePeriodSecondsDuration - time.Until(pod.ObjectMeta.DeletionTimestamp.Time)
if inTerminating > settings.terminate {
log.Println("PROBLEM", "TERMINATING", pod.Spec.NodeName, inTerminating)
updateNodeStatus(node)
}
}
case "Failed":
log.Println("PROBLEM", "FAILED", pod.Spec.NodeName, pod.Status.Reason)
updateNodeStatus(node)
deletePodForce(pod)
default:
log.Println("UNKNOWN", "PHASE", pod.Status.Phase)
}
}
took := time.Since(startedAt)
remaining := settings.every - took
if remaining > 0 {
time.Sleep(remaining)
}
}
}