Skip to content

Hk #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Hk #1

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
7 changes: 7 additions & 0 deletions Dockerfile2
Original file line number Diff line number Diff line change
@@ -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"]
4 changes: 4 additions & 0 deletions cmd
Original file line number Diff line number Diff line change
@@ -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 .
4 changes: 2 additions & 2 deletions k8s/ingress/app-canary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -38,4 +38,4 @@ spec:
targetPort: 8080
selector:
app: kubeapp
env: canary
env: canary
22 changes: 11 additions & 11 deletions k8s/ingress/app-production.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
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
ports:
- port: 80
targetPort: 8080
selector:
app: kubeapp
env: production
app: kubeapp1
env: production
4 changes: 2 additions & 2 deletions k8s/lb/app-production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file added source/.DS_Store
Binary file not shown.
Binary file modified source/1.0/app
Binary file not shown.
54 changes: 54 additions & 0 deletions source/1.0/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Binary file modified source/2.0/app
Binary file not shown.
79 changes: 76 additions & 3 deletions source/2.0/app.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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)
}