diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..94f1a54 Binary files /dev/null and b/.DS_Store differ diff --git a/Dockerfile b/Dockerfile index 550f25f..9258657 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,6 +2,6 @@ FROM alpine:latest MAINTAINER The Dockbit Team "team@dockbit.com" ARG version=1.0 -COPY source/$version/app /app +COPY source/1.0/app /app EXPOSE 8080 ENTRYPOINT ["/app"] diff --git a/Dockerfile2 b/Dockerfile2 new file mode 100644 index 0000000..e606efb --- /dev/null +++ b/Dockerfile2 @@ -0,0 +1,7 @@ +FROM alpine:latest +MAINTAINER The Dockbit Team "team@dockbit.com" + +ARG version=2.0 +COPY source/2.0/app /app +EXPOSE 8080 +ENTRYPOINT ["/app"] diff --git a/cmd b/cmd new file mode 100644 index 0000000..27f09a1 --- /dev/null +++ b/cmd @@ -0,0 +1,4 @@ +vi source/1.0/app.go +GOOS=linux GOARCH=amd64 go build -tags netgo -o app +export app_version=1.0 +docker build --build-arg version=$app_version -t harbor.jpe1-apv1-prod.r-local.net/travel-poc/canary:1.1 . diff --git a/k8s/ingress/app-canary.yml b/k8s/ingress/app-canary.yml index 19fcd1b..a173947 100644 --- a/k8s/ingress/app-canary.yml +++ b/k8s/ingress/app-canary.yml @@ -13,7 +13,7 @@ spec: spec: containers: - name: kubeapp - image: gcr.io/PROJECT_ID/app:2.0 + image: harbor.jpe1-apv1-prod.r-local.net/travel-poc/canary:2.0 imagePullPolicy: Always readinessProbe: httpGet: @@ -38,4 +38,4 @@ spec: targetPort: 8080 selector: app: kubeapp - env: canary \ No newline at end of file + env: canary diff --git a/k8s/ingress/app-production.yml b/k8s/ingress/app-production.yml index 855701f..9dd5081 100644 --- a/k8s/ingress/app-production.yml +++ b/k8s/ingress/app-production.yml @@ -1,35 +1,35 @@ kind: Deployment apiVersion: extensions/v1beta1 metadata: - name: kubeapp-production + name: kubeapp-production1 spec: replicas: 3 template: metadata: - name: kubeapp + name: kubeapp1 labels: - app: kubeapp + app: kubeapp1 env: production spec: containers: - - name: kubeapp - image: gcr.io/PROJECT_ID/app:1.0 - imagePullPolicy: Always + - name: kubeapp1 + image: app2:1 + imagePullPolicy: Never readinessProbe: httpGet: path: /health port: 8080 command: ["/app"] ports: - - name: kubeapp + - name: kubeapp1 containerPort: 8080 --- kind: Service apiVersion: v1 metadata: - name: kubeapp-production-service + name: kubeapp-production-service1 labels: - app: kubeapp + app: kubeapp1 env: production spec: type: NodePort @@ -37,5 +37,5 @@ spec: - port: 80 targetPort: 8080 selector: - app: kubeapp - env: production \ No newline at end of file + app: kubeapp1 + env: production diff --git a/k8s/lb/app-production.yml b/k8s/lb/app-production.yml index 285bd9e..490de22 100644 --- a/k8s/lb/app-production.yml +++ b/k8s/lb/app-production.yml @@ -13,8 +13,8 @@ spec: spec: containers: - name: kubeapp - image: gcr.io/PROJECT_ID/app:1.0 - imagePullPolicy: Always + image: app1:1 + imagePullPolicy: Never readinessProbe: httpGet: path: /health diff --git a/source/.DS_Store b/source/.DS_Store new file mode 100644 index 0000000..49c447a Binary files /dev/null and b/source/.DS_Store differ diff --git a/source/1.0/app b/source/1.0/app index a2bb4e7..92b0a4f 100755 Binary files a/source/1.0/app and b/source/1.0/app differ diff --git a/source/1.0/app.go b/source/1.0/app.go index 5933fb6..c5bb7ec 100644 --- a/source/1.0/app.go +++ b/source/1.0/app.go @@ -7,6 +7,59 @@ import ( const version string = "1.0" +func reverse(s string) string { + runes := []rune(s) + for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 { + runes[i], runes[j] = runes[j], runes[i] + } + return string(runes) +} + +func dumpJsonRequestHandlerFunc(w http.ResponseWriter, req *http.Request){ + //Validate request + if req.Method != "POST" { + w.WriteHeader(http.StatusBadRequest) + return + } + + if req.Header.Get("Content-Type") != "application/json" { + w.WriteHeader(http.StatusBadRequest) + return + } + + //To allocate slice for request body + length, err := strconv.Atoi(req.Header.Get("Content-Length")) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + //Read body data to parse json + body := make([]byte, length) + length, err = req.Body.Read(body) + if err != nil && err != io.EOF { + w.WriteHeader(http.StatusInternalServerError) + return + } + + //parse json + var jsonBody map[string]interface{} + err = json.Unmarshal(body[:length], &jsonBody) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + // myString := jsonBody["message"].(string) + jsonBody["message"] = reverse(jsonBody["message"].(string)) + + jsonString, err := json.Marshal(jsonBody) + fmt.Println(err) + fmt.Fprintf(w, "%s\n", jsonString) + + w.WriteHeader(http.StatusOK) +} + func getFrontpage(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Congratulations! Version %s of your application is running on Kubernetes.", version) } @@ -23,5 +76,6 @@ func main() { http.HandleFunc("/", getFrontpage) http.HandleFunc("/health", health) http.HandleFunc("/version", getVersion) + http.HandleFunc("/reverse", dumpJsonRequestHandlerFunc) http.ListenAndServe(":8080", nil) } diff --git a/source/2.0/app b/source/2.0/app index 028afec..71c9488 100755 Binary files a/source/2.0/app and b/source/2.0/app differ diff --git a/source/2.0/app.go b/source/2.0/app.go index 81af120..6a18588 100644 --- a/source/2.0/app.go +++ b/source/2.0/app.go @@ -1,14 +1,86 @@ package main import ( - "fmt" - "net/http" + "io/ioutil" + "log" + "net/http" + "io" + "encoding/json" + "fmt" + "strconv" + "math/rand" ) const version string = "2.0" +func reverse(s string) string { + runes := []rune(s) + for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 { + runes[i], runes[j] = runes[j], runes[i] + } + return string(runes) +} + +func dumpJsonRequestHandlerFunc(w http.ResponseWriter, req *http.Request){ + //Validate request + if req.Method != "POST" { + w.WriteHeader(http.StatusBadRequest) + return + } + + if req.Header.Get("Content-Type") != "application/json" { + w.WriteHeader(http.StatusBadRequest) + return + } + + //To allocate slice for request body + length, err := strconv.Atoi(req.Header.Get("Content-Length")) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + //Read body data to parse json + body := make([]byte, length) + length, err = req.Body.Read(body) + if err != nil && err != io.EOF { + w.WriteHeader(http.StatusInternalServerError) + return + } + + //parse json + var jsonBody map[string]interface{} + err = json.Unmarshal(body[:length], &jsonBody) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + // myString := jsonBody["message"].(string) + jsonBody["message"] = reverse(jsonBody["message"].(string)) + jsonBody["rand"] = rand.Float64() + + jsonString, err := json.Marshal(jsonBody) + fmt.Println(err) + fmt.Fprintf(w, "%s\n", jsonString) + + w.WriteHeader(http.StatusOK) +} + func getFrontpage(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "Congratulations! Version %s of your application is running on Kubernetes.", version) + resp, err := http.Get("http://kubeapp-production-service/") + if err != nil { + // handle error + log.Fatalln(err) + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + // handle error + log.Fatalln(err) + } + + fmt.Fprintf(w, "Congratulations! Version %s of your application is running on Kubernetes. \n from another:\n %s", version, string(body)) } func health(w http.ResponseWriter, r *http.Request) { @@ -23,5 +95,6 @@ func main() { http.HandleFunc("/", getFrontpage) http.HandleFunc("/health", health) http.HandleFunc("/version", getVersion) + http.HandleFunc("/api", dumpJsonRequestHandlerFunc) http.ListenAndServe(":8080", nil) }