Skip to content

Commit f330f68

Browse files
committed
Modifying endpoints and html ouput
1 parent 47b6315 commit f330f68

File tree

5 files changed

+39
-30
lines changed

5 files changed

+39
-30
lines changed

k8s/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ RUN apk --no-cache add ca-certificates
88
WORKDIR /root/
99
COPY --from=builder /go/src/k8s-workshop/k8s .
1010

11-
EXPOSE 9090
11+
EXPOSE 8080
1212
ENTRYPOINT ./k8s

k8s/k8s.go

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
)
88

99
var (
10-
version = "1.3-liveness"
1110
bannedIp = "0.0.0.0"
1211
)
1312

@@ -20,42 +19,52 @@ func getServerIP() string {
2019
return conn.LocalAddr().(*net.UDPAddr).IP.String()
2120
}
2221

22+
func getHTML(image string) string {
23+
message := "Customer Support, Mr. Cat speaking, how can I help you?"
24+
color := "orange"
25+
if image == "1.3.1" {
26+
message = "High five budy, I'm healthy as ever!"
27+
color = "green"
28+
}
29+
if image == "1.3.2" {
30+
message = "I don't want to die Mr. Stark ( x_x ) ..."
31+
color = "red"
32+
}
33+
return `<!DOCTYPE html><html><body><center>
34+
<img src="https://raw.githubusercontent.com/twogg-git/k8s-workshop/master/src` + image + `.png">
35+
<h1 style="color:` + color + `">` + message + `</h3>
36+
<h2 style="color:green">Playing with Kubernetes</h1>
37+
<h2 style="color:blue">Server IP ` + getServerIP() + `</h2>
38+
<h3 style="color:blue">Version twogghub/k8s-workshop:1.3-liveness</h3>
39+
</center></body></html>`
40+
}
41+
2342
func playHome(w http.ResponseWriter, r *http.Request) {
24-
status := "Customer Support, Mr. Cat speaking, how can I help you?"
25-
img := "1.3.0"
26-
if bannedIp == getServerIP() {
27-
status = "I don't want to die Mr. Stark ( x_x ) ..."
28-
img = "1.3.1"
43+
if getServerIP() == bannedIp {
44+
fmt.Fprintf(w, getHTML("1.3.2"))
45+
} else {
46+
fmt.Fprintf(w, getHTML("1.3.0"))
2947
}
30-
html := `<!DOCTYPE html><html><body><center>
31-
<img src="https://raw.githubusercontent.com/twogg-git/k8s-workshop/master/src/` + img + `.png">
32-
<h1 style="color:green">Playing with Kubernetes</h1>
33-
<h2 style="color:blue">Server IP ` + getServerIP() + `</h2>
34-
<h3 style="color:blue">Version twogghub/k8s-workshop:` + version + `</h3>
35-
<h3 style="color:red">` + status + `</h3>
36-
</center></body></html>`
37-
fmt.Fprintf(w, html)
3848
}
3949

4050
func playHealth(w http.ResponseWriter, r *http.Request) {
41-
ip := getServerIP()
42-
if ip == bannedIp {
51+
if getServerIP() == bannedIp {
4352
w.WriteHeader(http.StatusServiceUnavailable)
4453
} else {
45-
fmt.Fprintf(w, "I'm alive!.. but also dead for IP: "+bannedIp)
54+
fmt.Fprintf(w, getHTML("1.3.1"))
4655
}
4756
}
4857

49-
func playDead(w http.ResponseWriter, r *http.Request) {
50-
bannedIp = r.URL.Query().Get("ip")
51-
fmt.Fprintf(w, "Now playing dead for IP: "+bannedIp)
58+
func playKillMe(w http.ResponseWriter, r *http.Request) {
59+
bannedIp = getServerIP()
60+
fmt.Fprintf(w, getHTML("1.3.2"))
5261
}
5362

5463
func main() {
5564
http.HandleFunc("/", playHome)
5665
http.HandleFunc("/health", playHealth)
57-
http.HandleFunc("/kill", playDead)
58-
if err := http.ListenAndServe(":9090", nil); err != nil {
66+
http.HandleFunc("/killme", playKillMe)
67+
if err := http.ListenAndServe(":8080", nil); err != nil {
5968
panic(err)
6069
}
6170
}

src/1.3.1.png

14.1 KB
Loading

src/1.3.2.png

158 KB
Loading
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apiVersion: extensions/v1beta1
22
kind: Deployment
33
metadata:
4-
name: k8sliveness
4+
name: k8sdp
55
spec:
66
replicas: 3
77
strategy:
@@ -11,23 +11,23 @@ spec:
1111
maxUnavailable: 1
1212
selector:
1313
matchLabels:
14-
env: prod
14+
env: k8sdp
1515
template:
1616
metadata:
1717
labels:
18-
env: prod
18+
env: k8sdp
1919
spec:
2020
containers:
2121
- image: twogghub/k8s-workshop:1.3-liveness
22-
name: k8sliveness
22+
name: k8sdp
2323
livenessProbe:
2424
httpGet:
25-
path: /healthy
26-
port: 9090
25+
path: /health
26+
port: 8080
2727
initialDelaySeconds: 5
2828
timeoutSeconds: 1
2929
periodSeconds: 5
3030
failureThreshold: 1
3131
ports:
3232
- name: http
33-
containerPort: 9090
33+
containerPort: 8080

0 commit comments

Comments
 (0)