Skip to content

Commit bdbb397

Browse files
committed
Create public sample blueprint
0 parents  commit bdbb397

File tree

17 files changed

+373
-0
lines changed

17 files changed

+373
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# A `rendr` sample blueprint: Go microservice
2+
3+
An blueprint example of a fully loaded, Kubernetes-ready microservice written in Go.
4+
5+
## Usage
6+
7+
1. Install [rendr](https://github.com/jamf/rendr):
8+
9+
brew install jamf/tap/rendr
10+
11+
2. Generate your project from the blueprint:
12+
13+
rendr init foo --blueprint https://github.com/jamf/rendr-sample-blueprint-go-microservice
14+
15+
3. Your newly generated project has a detailed README. Enjoy!

metadata.yaml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: go-microservice
2+
version: 1
3+
author: Brian S. <brian.stewart@jamf.com>, Tomasz K. <tomasz.kurcz@jamf.com>
4+
description: A Go microservice template
5+
values:
6+
- name: name
7+
description: The name of your microservice
8+
required: true
9+
- name: golang_version
10+
description: The version of Golang to use
11+
default: 1.14
12+
- name: port
13+
description: Specify the port the HTTP server will be exposed under
14+
required: true
15+
- name: github_create_repo
16+
description: 'Create GitHub repository (true/false)'
17+
required: true
18+
- name: github_id
19+
description: GitHub organization or username
20+
required: true
21+
- name: github_repo_visbility
22+
description: 'GitHub repo visbility (options: public, private)'
23+
default: private
24+
- name: docker_image_repo
25+
description: Docker image repository
26+
required: true

scripts/post-render.sh

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env bash
2+
3+
echo 'Creating Helm chart'
4+
5+
helm create $name
6+
rm -rf $name/templates/tests
7+
mv $name helm-chart
8+
mkdir -p helm-chart/env
9+
mv helm-values/* helm-chart/env/
10+
rm -rf helm-values
11+
sed -i '' 's/appVersion: .*/appVersion: latest/' helm-chart/Chart.yaml
12+
sed -i '' "s/containerPort: .*/containerPort: $port/" helm-chart/templates/deployment.yaml
13+
14+
echo 'Initializing Git repository'
15+
16+
git init
17+
git add .
18+
git commit -m 'Initial project generated from Go microservice blueprint'
19+
20+
if [ "$github_create_repo" != "true" ]; then
21+
echo "Skipping remote repository creation: 'github_create_repo' = '$github_create_repo'. To enable, please provide the '-v github_create_repo:true' flag when generating the project."
22+
exit 0
23+
fi
24+
25+
26+
if [ -z "$github_id" ]; then
27+
echo "Skipping remote repository creation: no GitHub ID provided. To enable, please provide the '-v github_id:<your id>' flag when generating the project."
28+
exit 1
29+
fi
30+
31+
if ! command -v hub &> /dev/null; then
32+
echo 'Skipping remote repository creation: GitHub CLI `hub` is not installed.'
33+
echo 'To automatically create a GitHub repository as part of project generation, install and configure the `hub` CLI. See hub.github.com for details.'
34+
exit 1
35+
fi
36+
37+
echo "Creating GitHub repository $github_id/$name"
38+
39+
if [ "$github_repo_visibility" == "public" ]; then
40+
flags=''
41+
else
42+
flags='-p'
43+
fi
44+
45+
hub create $flags $github_id/$name
46+
git push --set-upstream origin master
47+
48+
echo "Done: https://github.com/$github_id/$name"

template/.github/workflows/ci.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
on: [push, pull_request]
2+
name: Build and Test
3+
jobs:
4+
test:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- name: Install Go
8+
uses: actions/setup-go@v2
9+
with:
10+
go-version: 1.14.x
11+
- name: Checkout code
12+
uses: actions/checkout@v2
13+
- name: Test
14+
run: go test ./...
15+
- name: Build
16+
run: go build

template/.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Binaries for programs and plugins
2+
{{ name }}
3+
*.exe
4+
*.exe~
5+
*.dll
6+
*.so
7+
*.dylib
8+
9+
# Test binary, built with `go test -c`
10+
*.test
11+
12+
# Output of the go coverage tool, specifically when used with LiteIDE
13+
*.out
14+
15+
# Dependency directories (remove the comment below to include it)
16+
# vendor/
17+
18+
# Generated files
19+
manifest.yaml

template/Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# This is a multistage Dockerfile, including build and runtime
2+
3+
# Build container
4+
FROM golang:{{ golang_version }}-alpine AS build-env
5+
6+
RUN apk add --no-cache git
7+
ADD . /src
8+
RUN cd /src && go build -o {{ name }}
9+
10+
# Runtime container
11+
FROM alpine
12+
13+
WORKDIR /app
14+
COPY --from=build-env /src/{{ name }} /app/
15+
EXPOSE {{ port }}
16+
17+
ENTRYPOINT ./{{ name }}

template/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Jamf Software
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

template/Makefile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
ENV ?= dev
2+
NAMESPACE ?= {{ name }}-$(ENV)
3+
DOCKER_IMAGE_REPO = {{ docker_image_repo }}
4+
5+
{{ name }}:
6+
go build -o {{ name }}
7+
8+
docker-build:
9+
docker build -t $(DOCKER_IMAGE_REPO) .
10+
11+
docker-push:
12+
docker push $(DOCKER_IMAGE_REPO)
13+
14+
kube-deploy: manifest.yaml
15+
kubectl get ns $(NAMESPACE) || kubectl create ns $(NAMESPACE)
16+
kubectl apply -f manifest.yaml -n $(NAMESPACE)
17+
18+
kube-delete: manifest.yaml
19+
kubectl delete -f manifest.yaml
20+
21+
manifest.yaml:
22+
helm template helm-chart \
23+
-f helm-chart/env/values-default.yaml \
24+
-f helm-chart/env/values-$(ENV).yaml \
25+
--namespace $(NAMESPACE) \
26+
--name-template $(ENV) > manifest.yaml
27+
28+
clean:
29+
rm -f manifest.yaml

template/README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# {{ name }}
2+
3+
This is your new microservice, generated using [rendr](https://github.com/jamf/rendr)!
4+
5+
[![CI Status](https://github.com/{{ github_id }}/{{ name }}/workflows/Build%20and%20Test/badge.svg)](https://github.com/{{ github_id }}/{{ name }}/actions)
6+
7+
## Features
8+
9+
This is a small web app, written in [Go](https://golang.org). It consists of two endpoints: the root endpoint which displays a welcome message, and a health check endpoint at `/health` which returns JSON metadata about the running application.
10+
11+
In addition, there are a number of features in this project:
12+
13+
* GitHub Actions CI pipeline
14+
* `Dockerfile` to containerize the app (image size: 13.3 MB)
15+
* Helm chart to generate Kubernetes manifests
16+
* Kubernetes deployment with `make deploy`
17+
* Development, staging and production environment configuration for Kubernetes
18+
* `Makefile` to automate the Go build, Docker build, and Kubernetes deployment
19+
20+
## Development
21+
22+
You can build this project locally using `make`, which builds the Go binary:
23+
24+
```
25+
make
26+
./{{ name }}
27+
```
28+
29+
Or you can build and run the Docker container:
30+
31+
```
32+
make docker-build
33+
docker run -p {{ port }}:{{ port }} {{ docker_image_repo }}
34+
```
35+
36+
Then visit http://localhost:{{ port }}. If all goes well, there should be a "hello world" message.
37+
38+
## Health checks
39+
40+
There's a simple health check available under `/health`. To make it more meaningful, we recommend editing `health.go`. It should be fairly straightforward. The reference is [here](https://github.com/hellofresh/health-go).
41+
42+
Example:
43+
```
44+
curl -s localhost:{{ port }} | jq
45+
{
46+
"status": "OK",
47+
"timestamp": "2020-07-08T08:54:29.129544-05:00",
48+
"system": {
49+
"version": "go1.14.1",
50+
"goroutines_count": 3,
51+
"total_alloc_bytes": 315256,
52+
"heap_objects_count": 705,
53+
"alloc_bytes": 315256
54+
}
55+
}
56+
```
57+
58+
## Automation
59+
60+
### GitHub Actions pipeline
61+
62+
There is a GitHub Actions pipeline included under the `.github` directory. If you push this repository to GitHub, GitHub will automatically recognize this and start running your first CI build!
63+
64+
### Helm chart
65+
66+
Helm is a templating engine for Kubernetes manifests based on user-provided values. Custom values for each environment (`dev`/`staging`/`production`) are located in the corresponding `values-$(ENV).yaml` file under `helm-chart/env`. Shared values are located in `values-default.yaml`. The full reference for available values for the Helm chart is under `helm-chart/values.yaml`.
67+
68+
The included Helm chart was created using `helm create {{ name }}`.
69+
70+
To generate the manifest from the Helm chart, run this:
71+
72+
make manifest.yaml
73+
74+
The default environment is `dev` (as seen in the `Makefile`). Simply set the `ENV` environment variable to `staging` or `production` to deploy to those environments:
75+
76+
export ENV=staging
77+
make manifest.yaml
78+
79+
### Kubernetes deployment
80+
81+
After rendering the `manifest.yaml` from the Helm chart, deploy it to Kubernetes:
82+
83+
make kube-deploy
84+
85+
You can see your running pod within seconds:
86+
87+
kubectl get pods -n {{ name }}-dev
88+
89+
The manifest includes an ingress, so you can access your running service via HTTP:
90+
91+
curl https://{{ name }}.staging.example.com
92+
93+
There it is! Your own app running in a production-like environment within minutes!

template/app.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
)
7+
8+
func appInit() {
9+
http.HandleFunc("/", MainHandler)
10+
}
11+
12+
func MainHandler(w http.ResponseWriter, r *http.Request) {
13+
fmt.Fprintln(w, "Hello world! Welcome to {{ name }}!")
14+
}

0 commit comments

Comments
 (0)