Skip to content
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

feat: generate kube manifests #560

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
5 changes: 5 additions & 0 deletions templates/Makefile.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ _ := $(shell ./scripts/devbase.sh)

include .bootstrap/root/Makefile

# creates kubernetes manifests from jsonnet files
.PHONY: pre-gogenerate
pre-gogenerate::
bash ./deployments/generate.sh

{{- range (stencil.GetModuleHook "Makefile.commands") }}
{{ . }}
{{- end }}
Expand Down
69 changes: 69 additions & 0 deletions templates/deployments/generate.sh.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env bash
# This script generates kubernetes config files for all jsonnet files in the deployments folder.
# This way we can easily check what changes are being made to which environment.

ROOT=./deployments/{{ .Config.Name }}
OUTPUT=$ROOT/kubecfg
DOWNLOAD_LIBS=1

if [ $# -eq 1 ] && [ "$1" = "--quick" ]; then
DOWNLOAD_LIBS=0
fi

# Exit when any command fails
set -e

# Prepare output folder
rm -rf $OUTPUT
mkdir -p $OUTPUT

generate() {
line=$1

channel=$(echo "$line" | jq -r '.channel')
cluster=$(echo "$line" | jq -r '.cluster')
environment=$(echo "$line" | jq -r '.environment')
name=$(echo "$line" | jq -r '.name')
region=$(echo "$line" | jq -r '.region')
dev_email=$(echo "$line" | jq -r '.dev_email')

echo "Generating kubernetes config for '$name'"
kubecfg -J ./deployments/libs show \
-V "appImageRegistry=gcr.io/outreach-docker" -V "bento=$name" -V "channel=$channel" -V "cluster=$cluster" \
-V "environment=$environment" -V "namespace={{ .Config.Name }}--$name" -V "region=$region" -V "ts=1656492859" -V "version=v0.0.1" \
-V "dev_email=$dev_email" \
$ROOT/{{ .Config.Name }}.jsonnet -oyaml >"$OUTPUT/$name.yaml"
}

# Download libsonnet files
if [ $DOWNLOAD_LIBS -eq 1 ]; then
rm -rf ./deployments/libs
mkdir -p ./deployments/libs/kubernetes

outreach_libs=(app.libsonnet outreach.libsonnet kube.libsonnet cluster.libsonnet database.libsonnet)
for lib in "${outreach_libs[@]}"; do
curl -s -o "./deployments/libs/kubernetes/$lib" https://raw.githubusercontent.com/getoutreach/jsonnet-libs/master/kubernetes/"$lib"
done
curl -s -o "./deployments/libs/kubernetes/clusters.yaml" http://k8s-clusters.outreach.cloud/
curl -s -o "./deployments/libs/kubecfg.libsonnet" https://raw.githubusercontent.com/kubecfg/kubecfg/master/lib/kubecfg.libsonnet

find ./deployments/libs -name '*.libsonnet' -exec jsonnetfmt -i {} +
fi

generate '{"name":"bento1a", "cluster":"none", "region":"none", "channel":"white", "environment":"development", "dev_email":"stub@email"}'
generate '{"name":"staging1a", "cluster":"staging.us-east-2", "region":"us-east-2", "channel":"white", "environment":"staging"}'

generate '{"name":"app1a", "cluster":"production.us-west-2", "region":"us-west-2", "channel":"green", "environment":"production"}'
generate '{"name":"app1b", "cluster":"production.us-west-2", "region":"us-west-2", "channel":"yellow", "environment":"production"}'
generate '{"name":"app1c", "cluster":"production.us-west-2", "region":"us-west-2", "channel":"green", "environment":"production"}'
generate '{"name":"app1d", "cluster":"production.us-west-2", "region":"us-west-2", "channel":"orange", "environment":"production"}'
generate '{"name":"app1e", "cluster":"production.us-west-2", "region":"us-west-2", "channel":"green", "environment":"production"}'
generate '{"name":"app1f", "cluster":"production.us-west-2", "region":"us-west-2", "channel":"green", "environment":"production"}'
generate '{"name":"app2a", "cluster":"production.us-east-1", "region":"us-east-1", "channel":"green", "environment":"production"}'
generate '{"name":"app2b", "cluster":"production.us-east-1", "region":"us-east-1", "channel":"green", "environment":"production"}'
generate '{"name":"app2c", "cluster":"production.us-east-1", "region":"us-east-1", "channel":"green", "environment":"production"}'
generate '{"name":"app4a", "cluster":"production.eu-west-1", "region":"eu-west-1", "channel":"green", "environment":"production"}'

## <<Stencil::Block(extras)>>
{{ file.Block "extras" }}
## <</Stencil::Block>>