@@ -6,6 +6,10 @@ import (
66 "net/http"
77)
88
9+ var (
10+ bannedIp = "0.0.0.0"
11+ )
12+
913func getServerIP () string {
1014 conn , err := net .Dial ("udp" , "8.8.8.8:80" )
1115 if err != nil {
@@ -15,18 +19,54 @@ func getServerIP() string {
1519 return conn .LocalAddr ().(* net.UDPAddr ).IP .String ()
1620}
1721
22+ func getHTML (status string ) string {
23+ message := "DevOps, Mr. Cat speaking, how can I help you?"
24+ image := "1.3.0"
25+ color := "orange"
26+ if status == "alive" {
27+ message = "High five budy, I'm healthy as ever!"
28+ image = "1.3.1"
29+ color = "green"
30+ }
31+ if status == "dead" {
32+ message = "I don't want to die Mr. Stark..."
33+ image = "1.3.2"
34+ color = "red"
35+ }
36+ return `<!DOCTYPE html><html><body><center>
37+ <img src="https://raw.githubusercontent.com/twogg-git/k8s-workshop/1.3-liveness/src/` + image + `.png">
38+ <h1 style="color:` + color + `">` + message + `</h3>
39+ <h2 style="color:green">Playing with Kubernetes</h1>
40+ <h2 style="color:blue">Server IP ` + getServerIP () + `</h2>
41+ <h3 style="color:blue">Version twogghub/k8s-workshop:1.3-liveness</h3>
42+ </center></body></html>`
43+ }
44+
1845func playHome (w http.ResponseWriter , r * http.Request ) {
19- html := `<!DOCTYPE html><html><body><center>
20- <img src="https://raw.githubusercontent.com/twogg-git/k8s-workshop/master/src/1.1.2.png">
21- <h1 style="color:orange">Playing with Kubernetes</h1>
22- <h2 style="color:orange">Server IP ` + getServerIP () + `</h2>
23- <h3 style="color:orange">Version twogghub/k8s-workshop:1.1-rolling</h3>
24- </center></body></html>`
25- fmt .Fprintf (w , html )
46+ if bannedIp == getServerIP () {
47+ fmt .Fprintf (w , getHTML ("dead" ))
48+ } else {
49+ fmt .Fprintf (w , getHTML ("home" ))
50+ }
51+ }
52+
53+ func playHealth (w http.ResponseWriter , r * http.Request ) {
54+ if getServerIP () == bannedIp {
55+ w .WriteHeader (http .StatusServiceUnavailable )
56+ } else {
57+ fmt .Fprintf (w , getHTML ("alive" ))
58+ }
59+ }
60+
61+ func playKillMe (w http.ResponseWriter , r * http.Request ) {
62+ bannedIp = getServerIP ()
63+ fmt .Fprintf (w , getHTML ("dead" ))
2664}
2765
2866func main () {
2967 http .HandleFunc ("/" , playHome )
68+ http .HandleFunc ("/health" , playHealth )
69+ http .HandleFunc ("/killme" , playKillMe )
3070 if err := http .ListenAndServe (":8080" , nil ); err != nil {
3171 panic (err )
3272 }
0 commit comments