-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
106 lines (88 loc) · 2.03 KB
/
main.go
File metadata and controls
106 lines (88 loc) · 2.03 KB
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
package main
import (
"net/http"
"os"
"text/template"
)
var (
podName string = os.Getenv("POD_NAME")
namespace string = os.Getenv("POD_NAMESPACE")
nodeName string = os.Getenv("NODE_NAME")
hostIP string = os.Getenv("HOST_IP")
podIP string = os.Getenv("POD_IP")
)
const htmlPageTemplate = `
<!DOCTYPE html>
<html>
<head>
<title>Test Kube</title>
</head>
<body>
<h1>Welcome to K8s</h1>
<p>Pod Name: {{.PodName}}</p>
<p>Namespace: {{.Namespace}}</p>
<p>Node Name: {{.NodeName}}</p>
<p>Host IP: {{.HostIP}}</p>
<p>Pod IP: {{.PodIP}}</p>
<form action="/memory-stress" method="post">
<input type="submit" value="Memory Stress">
</form>
<form action="/cpu-stress" method="post">
<input type="submit" value="CPU Stress">
</form>
<form action="/crash" method="post">
<input type="submit" value="Crash Pod">
</form>
</body>
</html>
`
func main() {
// http server start 8080
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// render html page
data := struct {
PodName string
Namespace string
NodeName string
HostIP string
PodIP string
}{
PodName: podName,
Namespace: namespace,
NodeName: nodeName,
HostIP: hostIP,
PodIP: podIP,
}
tmpl, err := template.New("webpage").Parse(htmlPageTemplate)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl.Execute(w, data)
})
http.HandleFunc("/memory-stress", func(w http.ResponseWriter, r *http.Request) {
memoryStressStart()
w.Write([]byte("Memory stress started\n"))
})
http.HandleFunc("/crash", func(w http.ResponseWriter, r *http.Request) {
os.Exit(1)
})
http.HandleFunc("/cpu-stress", func(w http.ResponseWriter, r *http.Request) {
go func() {
for {
}
}()
w.Write([]byte("CPU stress started\n"))
})
http.ListenAndServe(":8080", nil)
}
func memoryStressStart() {
// 1 GB = 1024 * 1024 * 1024 byte
const size = 1024 * 1024 * 1024
// 1 GB data
data := make([]byte, size)
// fill data
for i := 0; i < size; i++ {
data[i] = 1
}
}