Last active
August 16, 2018 17:00
-
-
Save DazWilkin/35dec61c2d1f9a0b4b59f1c12d38a144 to your computer and use it in GitHub Desktop.
Kubernetes Engine from sratch #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM golang:1.10-stretch as build | |
WORKDIR /go/src/app | |
COPY . . | |
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /go/bin/app/main . | |
FROM scratch | |
LABEL maintainer="Your Name <[email protected]" | |
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ | |
COPY --from=build /go/bin/app/ / | |
ARG VERSION=0 | |
ENV VERSION=${VERSION} | |
EXPOSE 8080 | |
ENTRYPOINT ["/main"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"html/template" | |
"log" | |
"net" | |
"net/http" | |
"os" | |
"strings" | |
) | |
const ( | |
html = `<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
<meta name=viewport content="width=device-width, initial-scale=1"> | |
<title>Knowing Node</title> | |
<style> | |
body { | |
font-family: Helvetica; | |
} | |
table,tr { | |
border: 2px solid #f5f5f5; | |
border-collapse: collapse; | |
} | |
tr:hover { | |
background-color: #f5f5f5; | |
} | |
</style> | |
</head> | |
<body border="1"> | |
<table> | |
{{- range . }} | |
<tr> | |
<td><a href="{{.}}">{{.}}</a></td> | |
</tr> | |
{{- end }} | |
</table> | |
</body> | |
</html>` | |
) | |
func main() { | |
http.HandleFunc("/", defaultHandler) | |
http.HandleFunc("/_ah/health", healthzHandler) | |
http.HandleFunc("/backend", backendHandler) | |
http.HandleFunc("/healthz", healthzHandler) | |
http.HandleFunc("/hostz", hostzHandler) | |
http.HandleFunc("/requestz", requestzHandler) | |
http.HandleFunc("/varz", varzHandler) | |
http.HandleFunc("/version", versionHandler) | |
http.ListenAndServe(":8080", nil) | |
} | |
func defaultHandler(w http.ResponseWriter, r *http.Request) { | |
tpl := template.Must(template.New("out").Parse(html)) | |
urls := []string{ | |
"/", | |
"/_ah/health", | |
"/backend", | |
"/healthz", | |
"/hostz", | |
"/requestz", | |
"/varz", | |
"/version"} | |
tpl.Execute(w, urls) | |
} | |
func backendHandler(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "[node] %v (%v)\n", os.Getenv("NODE_NAME"), os.Getenv("NODE_IP")) | |
fmt.Fprintf(w, "[pod] %v (%v)\n", os.Getenv("POD_NAME"), os.Getenv("POD_IP")) | |
} | |
func healthzHandler(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprint(w, "OK") | |
} | |
func hostzHandler(w http.ResponseWriter, r *http.Request) { | |
cname, addrs, err := net.LookupSRV("http", "tcp", os.Getenv("SERVICE_NAME")) | |
if err != nil { | |
http.Error(w, err.Error(), http.StatusInternalServerError) | |
} | |
log.Printf("CNAME: %v\n", cname) | |
for _, addr := range addrs { | |
url := fmt.Sprintf("http://%v:%v/backend", | |
strings.TrimSuffix(addr.Target, "."), | |
addr.Port) | |
resp, err := http.Get(url) | |
if err != nil { | |
log.Printf("%v: %v", url, err) | |
} | |
defer resp.Body.Close() | |
fmt.Fprintf(w, "%v: %v\n", url, resp.StatusCode) | |
} | |
} | |
func requestzHandler(w http.ResponseWriter, r *http.Request) { | |
urls := []string{ | |
"https://www.cnn.com", | |
"https://www.cnn.com/robots.txt", | |
"http://www.bbc.com/robots.txt"} | |
for _, url := range urls { | |
resp, err := http.Get(url) | |
if err != nil { | |
} | |
defer resp.Body.Close() | |
fmt.Fprintf(w, "%v: %v\n", url, resp.StatusCode) | |
} | |
} | |
func varzHandler(w http.ResponseWriter, r *http.Request) { | |
for _, e := range os.Environ() { | |
fmt.Fprintf(w, "%v\n", e) | |
} | |
} | |
func versionHandler(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprint(w, os.Getenv("VERSION")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment