From 96ff1cb1f882169d0bc36bc114b275f0cc208dbc Mon Sep 17 00:00:00 2001 From: marcel-dempers Date: Thu, 12 Sep 2019 03:47:31 +0000 Subject: [PATCH] updates to add config to deployments --- docker-compose.yaml | 3 ++- golang/configs/config.json | 3 +++ golang/src/main.go | 21 +++++++++++++++++++-- kubernetes/configmaps/configmap.yaml | 10 ++++++++++ kubernetes/deployments/deployment.yaml | 9 ++++++++- 5 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 golang/configs/config.json create mode 100644 kubernetes/configmaps/configmap.yaml diff --git a/docker-compose.yaml b/docker-compose.yaml index db592bc..08e6410 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -10,13 +10,14 @@ services: - ./c#/src/:/work/ ports: - 5000:5000 - golang: #docker run -it -v ${PWD}:/go/src/work -p 5001:5000 -p 2345:2345 --security-opt "seccomp:unconfined" aimvector/golang:1.0.0 + golang: #docker run -it -v ${PWD}:/go/src/work -v ${PWD}/golang/configs/:/configs -p 5001:5000 -p 2345:2345 --security-opt "seccomp:unconfined" aimvector/golang:1.0.0 container_name: golang image: aimvector/golang:1.0.0 build: context: ./golang target: prod volumes: + - ./golang/configs:/configs/ - ./golang/src/:/go/src/work/ ports: - 5001:5000 diff --git a/golang/configs/config.json b/golang/configs/config.json new file mode 100644 index 0000000..8164832 --- /dev/null +++ b/golang/configs/config.json @@ -0,0 +1,3 @@ +{ + "environment" : "dev" +} diff --git a/golang/src/main.go b/golang/src/main.go index 376296d..e314201 100644 --- a/golang/src/main.go +++ b/golang/src/main.go @@ -6,8 +6,12 @@ import ( //our web server that will host the mock "github.com/buaazp/fasthttprouter" "github.com/valyala/fasthttp" + "os" + "io/ioutil" ) +var configuration []byte + func Response(ctx *fasthttp.RequestCtx) { fmt.Fprintf(ctx, "Hello") } @@ -15,13 +19,26 @@ func Response(ctx *fasthttp.RequestCtx) { func Status(ctx *fasthttp.RequestCtx) { fmt.Fprintf(ctx, "ok") } + +func ReadConfig(){ + fmt.Println("reading config...") + config, e := ioutil.ReadFile("/configs/config.json") + if e != nil { + fmt.Printf("Error reading config file: %v\n", e) + os.Exit(1) + } + configuration = config + fmt.Println("config loaded!") + +} + func main() { fmt.Println("starting...") - + ReadConfig() router := fasthttprouter.New() router.GET("/", Response) router.GET("/status", Status) log.Fatal(fasthttp.ListenAndServe(":5000", router.Handler)) -} \ No newline at end of file +} diff --git a/kubernetes/configmaps/configmap.yaml b/kubernetes/configmaps/configmap.yaml new file mode 100644 index 0000000..e9e3124 --- /dev/null +++ b/kubernetes/configmaps/configmap.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: example-config +data: + config.json: | + { + "environment" : "dev" + } +# kubectl create configmap example-config --from-file ./golang/configs/config.json diff --git a/kubernetes/deployments/deployment.yaml b/kubernetes/deployments/deployment.yaml index b21461c..e1aac4f 100644 --- a/kubernetes/deployments/deployment.yaml +++ b/kubernetes/deployments/deployment.yaml @@ -30,4 +30,11 @@ spec: cpu: "50m" limits: memory: "256Mi" - cpu: "500m" \ No newline at end of file + cpu: "500m" + volumeMouts: + - name: config-volume + mountPath: /configs/ + volumes: + - name: config-volume + configMap: + name: example-config #name of our configmap object