-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
127 lines (119 loc) · 3.25 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
package main
import (
"bufio"
"context"
"flag"
"fmt"
"github.com/fatih/color"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"net"
"sync"
"time"
)
var (
red = color.New(color.FgRed).SprintFunc()
wg sync.WaitGroup
)
func main() {
kubeconfig := flag.String("kubeconfig", "", "kuebconfig path")
namespace := flag.String("n", "", "namespace")
flag.Parse()
// 创建 Kubernetes 客户端配置
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(any(err))
}
// 创建 Kubernetes 客户端
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(any(err))
}
// 获取指定 namespace 中的所有 pod
//podList, err := clientset.CoreV1().Pods(*namespace).List(context.Background(), metav1.ListOptions{})
// 获取指定 namespace 中的所有 deployment
deploymentList, err := clientset.AppsV1().Deployments(*namespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
panic(any(err))
}
i := 1
for _, deployment := range deploymentList.Items {
i++
deploymentName := deployment.Name
labelSelector := metav1.LabelSelector{MatchLabels: deployment.Spec.Selector.MatchLabels}
options := metav1.ListOptions{LabelSelector: metav1.FormatLabelSelector(&labelSelector)}
podList, _ := clientset.CoreV1().Pods(*namespace).List(context.Background(), options)
// 获取 pod 的 readiness 配置信息
for _, pod := range podList.Items {
for _, container := range pod.Spec.Containers {
i++
//containerName := container.Name
readinessProbe := container.ReadinessProbe
if readinessProbe == nil {
continue
}
if readinessProbe.HTTPGet != nil {
port := readinessProbe.HTTPGet.Port.IntVal
//scheme := readinessProbe.HTTPGet.Scheme
path := readinessProbe.HTTPGet.Path
podIp := pod.Status.PodIP
address := fmt.Sprintf("%v:%v", podIp, port)
//url := fmt.Sprintf("%v://%v:%v%v", scheme, podIp, port, path)
wg.Add(i)
go request("GET", address, path, deploymentName)
}
}
// 只拿一个就行了
break
}
}
wg.Wait()
}
func request(method string, address string, path string, deploymentName string) {
//fmt.Println(deploymentName)
conn, err := net.Dial("tcp", address)
if err != nil {
fmt.Println("Error connecting:", err)
return
}
defer conn.Close()
defer wg.Done()
// 构建 HTTP GET 请求报文
req := "GET " + path + " HTTP/1.1\r\n" +
"Host: " + address + "\r\n" +
"Connection: keep-alive\r\n\r\n"
fmt.Fprintf(conn, req)
// 读取响应报文
reader := bufio.NewReader(conn)
start := time.Now()
startTimestamp := start.UnixNano() / int64(time.Millisecond) / 1000
for {
line, err := reader.ReadString('\n')
if err != nil {
fmt.Println("Error reading:", err)
return
}
//fmt.Println(line)
if line == "\r\n" {
break
}
}
timeout := 100 * time.Second
conn.SetDeadline(time.Now().Add(timeout))
for {
buf := make([]byte, 1024)
n, err := reader.Read(buf)
if err != nil {
end := time.Now()
endTimestamp := end.UnixNano() / int64(time.Millisecond) / 1000
timeout := endTimestamp - startTimestamp
fmt.Printf("%v: %v\n", deploymentName, timeout)
return
}
if n == 0 {
break
}
//fmt.Print(string(buf[:n]))
}
}