-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from appuio/feat/local-env
Add tooling to spin up local environments
- Loading branch information
Showing
13 changed files
with
2,307 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Set Shell to bash, otherwise some targets fail with dash/zsh etc. | ||
SHELL := /bin/bash | ||
|
||
# Disable built-in rules | ||
MAKEFLAGS += --no-builtin-rules | ||
MAKEFLAGS += --no-builtin-variables | ||
.SUFFIXES: | ||
.SECONDARY: | ||
|
||
PROJECT_ROOT_DIR = .. | ||
include ../Makefile.vars.mk | ||
include kind.mk | ||
|
||
uname_s := $(shell uname -s) | ||
ifeq ($(uname_s),Linux) | ||
xargs := xargs --no-run-if-empty | ||
else | ||
xargs := xargs | ||
endif | ||
|
||
.DEFAULT_GOAL := help | ||
|
||
.PHONY: setup | ||
setup: export KUBECONFIG = $(KIND_KUBECONFIG) | ||
setup: $(localenv_dir_created) kind-setup ## Setup the local environment | ||
|
||
.PHONY: clean-setup | ||
clean-setup: export KUBECONFIG = $(KIND_KUBECONFIG) | ||
clean-setup: kind-clean ## Clean the local environment (e.g. to rerun the setup) | ||
rm $(localenv_dir_created) || true | ||
|
||
.PHONY: help | ||
help: ## Show this help | ||
@grep -E -h '\s##\s' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' | ||
|
||
### | ||
### Artifacts | ||
### | ||
|
||
# a marker file must be created, because the date of the | ||
# directory may update when content in it is created/updated, | ||
# which would cause a rebuild / re-initialization of dependants | ||
$(localenv_dir_created): | ||
@touch $(localenv_dir_created) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Local development environment | ||
|
||
We provide a script and some templates to setup a local test environment based on [kind](https://kind.sigs.k8s.io/). | ||
The templates can be found in directory `templates/`. | ||
|
||
## Prerequisites | ||
|
||
* `bash` | ||
* `sed` | ||
* `kind` | ||
* `kubectl` | ||
* `kubelogin` as `kubectl-oidc_login` | ||
|
||
The setup script will provide links to the install guides for `kubectl` and `kubelogin` if no appropriate command is found. | ||
|
||
## Installation | ||
|
||
The `setup-kind.sh` script will guide you through the setup. | ||
There are some steps that you have to perform manually on a Keycloak instance, which the script prompts you for. | ||
The script defaults to VSHN's APPUiO Dev Keycloak instance, but you can provide an URL pointing to a different instance during the install process. | ||
|
||
Since the setup script requires a few arguments, we provide a make target to run the script: | ||
|
||
``` | ||
make setup | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
kind_marker := $(localenv_dir)/.kind-setup_complete | ||
|
||
curl_args ?= --location --fail --silent --show-error | ||
|
||
.DEFAULT_TARGET: kind-setup | ||
|
||
.PHONY: kind-setup | ||
kind-setup: export KUBECONFIG = $(KIND_KUBECONFIG) | ||
kind-setup: $(kind_marker) $(localenv_dir_created) ## Creates the kind cluster | ||
|
||
.PHONY: kind-clean | ||
kind-clean: export KUBECONFIG = $(KIND_KUBECONFIG) | ||
kind-clean: ## Remove the kind Cluster | ||
@$(KIND) delete cluster --name $(KIND_CLUSTER) || true | ||
@rm $(kind_marker) $(KIND_KUBECONFIG) || true | ||
|
||
### | ||
### Artifacts | ||
### | ||
|
||
$(KIND_KUBECONFIG): export KUBECONFIG = $(KIND_KUBECONFIG) | ||
$(KIND_KUBECONFIG): | ||
$(localenv_dir)/setup-kind.sh "$(KIND)" "$(KIND_CLUSTER)" "$(KIND_NODE_VERSION)" "$(KIND_KUBECONFIG)" | ||
@kubectl version | ||
@kubectl cluster-info | ||
|
||
$(kind_marker): export KUBECONFIG = $(KIND_KUBECONFIG) | ||
$(kind_marker): $(KIND_KUBECONFIG) | ||
@kubectl config use-context kind-$(KIND_CLUSTER) | ||
@touch $(kind_marker) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#!/bin/bash | ||
# vim:sts=2:ts=2:et:sw=2:tw=0 | ||
|
||
set -euo pipefail | ||
|
||
readonly script_dir=$(dirname "$0") | ||
readonly kind_cmd="${1:-kind}" | ||
readonly kind_cluster="${2:-control-api-localenv}" | ||
readonly kind_node_version="${3:-v1.22.1}" | ||
readonly kind_kubeconfig="${4:-"${script_dir}/control-api.kubeconfig"}" | ||
|
||
export KUBECONFIG="${kind_kubeconfig}" | ||
|
||
keycloak_url=https://id.dev.appuio.cloud | ||
|
||
step() { | ||
echo | ||
echo -e "$1" | ||
read -n 1 -s -r -p "Press any key to continue" | ||
echo | ||
} | ||
|
||
check_command() { | ||
if ! command -v "${1}" >/dev/null 2>&1; then | ||
step "Install ${2}. Follow the instructions at ${3}" | ||
fi | ||
} | ||
|
||
check_command "kubectl" "kubectl" "https://kubernetes.io/docs/tasks/tools/#kubectl" | ||
check_command "kubectl-oidc_login" "kubectl oidc-login plugin" "https://github.com/int128/kubelogin#setup" | ||
|
||
echo | ||
read -r -p "Provide the URL of the Keycloak to connect the local environment to (default=${keycloak_url}): " user_url | ||
if [ x"${user_url}" != x"" ]; then | ||
keycloak_url="${user_url}" | ||
fi | ||
|
||
echo | ||
identifier= | ||
while [ x"$identifier" == x"" ]; do | ||
read -r -p "Provide an identifier for your local-dev Keycloak realm: " identifier | ||
done | ||
|
||
realm_name="local-dev-${identifier}" | ||
sed -e "s/REPLACEME/${realm_name}/g" "${script_dir}/templates/realm.json.tpl" > "${script_dir}/realm.json" | ||
|
||
step "Navigate to ${keycloak_url} and create a new realm by importing the '$(realpath "${script_dir}/realm.json")' file." | ||
|
||
step "Create a user in the new realm, grant it realm role 'admin'.\nMake sure the user has an email configured and 'Email Verified' is set to 'On'." | ||
|
||
echo "" | ||
echo -e "\033[1m================================================================================" | ||
echo "Note: After the cluster is created, a browser window will open where you have to sign in to Keycloak with the user you've created in the previous step." | ||
echo -e "================================================================================\033[0m" | ||
echo "" | ||
|
||
sed -e "s#ISSUER_KEYCLOAK#${keycloak_url}#; s/REALM/${realm_name}/g" "${script_dir}/templates/kind-oidc.yaml.tpl" > "${script_dir}/.kind-oidc.yaml" | ||
${kind_cmd} create cluster \ | ||
--name "${kind_cluster}" \ | ||
--image "kindest/node:${kind_node_version}" \ | ||
--config="${script_dir}/.kind-oidc.yaml" | ||
rm "${script_dir}/.kind-oidc.yaml" | ||
kubectl apply -f - <<EOF | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRoleBinding | ||
metadata: | ||
name: oidc-cluster-admin | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: cluster-admin | ||
subjects: | ||
- kind: Group | ||
name: admin | ||
EOF | ||
kubectl oidc-login setup \ | ||
--oidc-issuer-url="${keycloak_url}/auth/realms/${realm_name}" \ | ||
--oidc-client-id=local-dev >/dev/null 2>&1 | ||
kubectl config set-credentials oidc-user \ | ||
--exec-api-version=client.authentication.k8s.io/v1beta1 \ | ||
--exec-command=kubectl \ | ||
--exec-arg=oidc-login \ | ||
--exec-arg=get-token \ | ||
--exec-arg=--oidc-issuer-url="${keycloak_url}/auth/realms/${realm_name}" \ | ||
--exec-arg=--oidc-client-id=local-dev \ | ||
--exec-arg=--oidc-extra-scope="email offline_access profile openid" | ||
kubectl config set-context --current --user=oidc-user | ||
kubectl apply -k "${script_dir}/../config/crd/apiextensions.k8s.io/v1" | ||
|
||
echo ======= | ||
echo "Setup finished. To interact with the local dev cluster, set the KUBECONFIG environment variable as follows:" | ||
echo "\"export \$KUBECONFIG=$(realpath "${kind_kubeconfig}")\"" | ||
echo ======= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Templates for setting up local environment | ||
|
||
This directory contains templates that are used by the `setup-kind.sh` script. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
kind: Cluster | ||
apiVersion: kind.x-k8s.io/v1alpha4 | ||
nodes: | ||
- role: control-plane | ||
kubeadmConfigPatches: | ||
- | | ||
kind: ClusterConfiguration | ||
apiServer: | ||
extraArgs: | ||
oidc-issuer-url: ISSUER_KEYCLOAK/auth/realms/REALM | ||
oidc-client-id: local-dev | ||
oidc-username-claim: email | ||
oidc-groups-claim: groups |
Oops, something went wrong.