forked from kyma-project/kyma
/
main.go
110 lines (91 loc) · 3.54 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
package main
import (
"flag"
"fmt"
"os"
"time"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
"github.com/kyma-project/kyma/tools/watch-pods/internal/tester"
)
func main() {
kubeconfig := flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
minWaitingPeriod := flag.Duration("minWaitingPeriod", time.Minute, "Minimum waiting period")
maxWaitingPeriod := flag.Duration("maxWaitingPeriod", time.Minute*3, "Maximum waiting period")
reqStabilityPeriod := flag.Duration("reqStabilityPeriod", time.Minute, "Required stability period")
ignorePodsPattern := flag.String("ignorePodsPattern", "", "Regexp for pod name that containers will be ignored")
ignoreNsPattern := flag.String("ignoreNsPattern", "", "Regexp for namespace that containers will be ignored")
ignoreContainerPattern := flag.String("ignoreContainersPattern", "", "Regexp for namespaces that containers will be ignored")
flag.Parse()
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
// create the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
ignoreFunc, err := tester.IgnoreContainersByRegexp(*ignorePodsPattern, *ignoreNsPattern, *ignoreContainerPattern)
if err != nil {
panic(err.Error())
}
clusterState := tester.NewClusterState(ignoreFunc)
lw := cache.NewListWatchFromClient(clientset.CoreV1().RESTClient(), "pods", "", fields.Everything())
watcher := tester.NewPodWatcher(lw, createOnPodUpdateFunc(clusterState), createOnPodDeleteFunc(clusterState))
if err := watcher.StartListeningToEvents(); err != nil {
panic(err.Error())
}
deadline := time.Now().Add(*maxWaitingPeriod)
fmt.Printf("Run tester with following configuration: min waiting: %v, max waiting: %v\n", minWaitingPeriod, maxWaitingPeriod)
if *ignorePodsPattern != "" || *ignoreNsPattern != "" || *ignoreContainerPattern != "" {
fmt.Printf("Ignore configuration: pod pattern: '%s', ns pattern: '%s', container pattern: '%s'", *ignorePodsPattern, *ignoreNsPattern, *ignoreContainerPattern)
}
stable := false
var unstableContainers []tester.ContainerAndState
<-time.After(*minWaitingPeriod)
for time.Now().Before(deadline) {
unstableContainers = clusterState.GetUnstableContainers(*reqStabilityPeriod)
size := len(unstableContainers)
fmt.Printf("Got %d unstable containers: %s \n", size, shortContainersDescription(unstableContainers))
if size == 0 {
stable = true
break
}
<-time.After(time.Second * 10)
}
watcher.Stop()
if !stable {
fmt.Println("Unstable containers")
for _, c := range unstableContainers {
fmt.Printf("%+v\n", c)
}
os.Exit(1)
}
}
func createOnPodUpdateFunc(clusterState *tester.ClusterState) func(p *v1.Pod) {
return func(p *v1.Pod) {
for _, c := range p.Status.ContainerStatuses {
clusterState.UpdateState(tester.Container{Ns: p.Namespace, PodName: p.Name, ContainerName: c.Name}, tester.StateUpdate{
Ready: c.Ready,
RestartCnt: c.RestartCount,
})
}
}
}
func createOnPodDeleteFunc(clusterState *tester.ClusterState) func(ns, name string) {
return func(ns, podName string) {
clusterState.ForgetPod(ns, podName)
}
}
func shortContainersDescription(containers []tester.ContainerAndState) string {
out := ""
for _, c := range containers {
out += fmt.Sprintf("{Ns: %s, PodName: %s, Container: %s} ", c.Container.Ns, c.Container.PodName, c.Container.ContainerName)
}
return "[" + out + "]"
}