Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
rnzsgh committed Oct 18, 2018
0 parents commit 31e3cb8
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.swp
main
eks-workshop-sample-app-temp
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM scratch
ADD main /
EXPOSE 8080
CMD ["/main"]
35 changes: 35 additions & 0 deletions buildspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
version: 0.2
phases:
install:
commands:
- mkdir bin
- curl -sS -o bin/aws-iam-authenticator https://amazon-eks.s3-us-west-2.amazonaws.com/1.10.3/2018-07-26/bin/linux/amd64/aws-iam-authenticator
- curl -sS -o bin/kubectl https://amazon-eks.s3-us-west-2.amazonaws.com/1.10.3/2018-07-26/bin/linux/amd64/kubectl
- chmod +x ./bin/kubectl ./bin/aws-iam-authenticator
- export PATH=$PWD/bin:$PATH
- apt-get update && apt-get -y install jq golang python3-pip python3-dev && pip3 install --upgrade awscli
pre_build:
commands:
- TAG="$REPOSITORY_NAME.$REPOSITORY_BRANCH.$ENVIRONMENT_NAME.$(date +%Y-%m-%d.%H.%M.%S).$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | head -c 8)"
- sed -i 's@CONTAINER_IMAGE@'"$REPOSITORY_URI:$TAG"'@' hello-k8s.yml
- $(aws ecr get-login --no-include-email)
- export KUBECONFIG=$HOME/.kube/config
build:
commands:
- CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
- docker build --tag $REPOSITORY_URI:$TAG .

post_build:
commands:
- docker push $REPOSITORY_URI:$TAG
- CREDENTIALS=$(aws sts assume-role --role-arn $EKS_KUBECTL_ROLE_ARN --role-session-name codebuild-kubectl --duration-seconds 900)
- export AWS_ACCESS_KEY_ID="$(echo ${CREDENTIALS} | jq -r '.Credentials.AccessKeyId')"
- export AWS_SECRET_ACCESS_KEY="$(echo ${CREDENTIALS} | jq -r '.Credentials.SecretAccessKey')"
- export AWS_SESSION_TOKEN="$(echo ${CREDENTIALS} | jq -r '.Credentials.SessionToken')"
- export AWS_EXPIRATION=$(echo ${CREDENTIALS} | jq -r '.Credentials.Expiration')
- aws eks update-kubeconfig --name $EKS_CLUSTER_NAME
- kubectl apply -f hello-k8s.yml
- printf '[{"name":"hello-k8s","imageUri":"%s"}]' $REPOSITORY_URI:$TAG > build.json
artifacts:
files: build.json
32 changes: 32 additions & 0 deletions hello-k8s.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: v1
kind: Service
metadata:
name: hello-k8s
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8080
selector:
app: hello-k8s
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-k8s
spec:
replicas: 3
selector:
matchLabels:
app: hello-k8s
template:
metadata:
labels:
app: hello-k8s
spec:
containers:
- name: hello-k8s
image: CONTAINER_IMAGE
ports:
- containerPort: 8080

53 changes: 53 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
)

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {

f := fib()

res := &response{Message: "Hello World - now"}

for _, e := range os.Environ() {
pair := strings.Split(e, "=")
res.EnvVars = append(res.EnvVars, pair[0]+"="+pair[1])
}

for i := 1; i <= 90; i++ {
res.Fib = append(res.Fib, f())
}

// Beautify the JSON output
out, _ := json.MarshalIndent(res, "", " ")

// Normally this would be application/json, but we don't want to prompt downloads
w.Header().Set("Content-Type", "text/plain")

io.WriteString(w, string(out))

fmt.Println("Hello world - the log message")
})
http.ListenAndServe(":8080", nil)
}

type response struct {
Message string `json:"message"`
EnvVars []string `json:"env"`
Fib []int `json:"fib"`
}

func fib() func() int {
a, b := 0, 1
return func() int {
a, b = b, a+b
return a
}
}

0 comments on commit 31e3cb8

Please sign in to comment.