diff --git a/.gitignore b/.gitignore index 01102e688..380bc03d0 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ bin/ vendor/ hack/code-gen +hack/manifest-gen hack/controller-gen test/.git-credentials reports/ diff --git a/Makefile b/Makefile index 72c39eb75..896feef23 100644 --- a/Makefile +++ b/Makefile @@ -112,7 +112,7 @@ endif .PHONY: generate-clean generate-clean: - rm -rf hack/code-gen + rm -rf ./hack/code-gen # Build CLI but don't lint or run code generation first. cli-fast: @@ -166,3 +166,26 @@ todo: --text \ --color \ -nRo -E " *[^\.]TODO.*|SkipNow" . + +# requires manifests. generate-manifests should be run first +# updating webhook requires ngrok to be running and updates the wh-config with the latest +# ngrok configuration. ngrok config changes for each restart which is why this is a separate target +.PHONY: update-webhook-config +update-webhook-config: + ./hack/update-webhook-config.sh + +# generates manifests from the kudo cli (kudo init) and captures the manifests needed for +# local development. These are cached under the /hack/manifest-gen folder and are used to quickly +# start running and debugging kudo controller and webhook locally. +.PHONY: generate-manifests +generate-manifests: + ./hack/update-manifests.sh + +# requires manifests. generate-manifests should be run first +# quickly sets a local dev env with the minimum necessary configurations to run +# the kudo manager locally. after running dev-ready, it is possible to 'make run' or run from an editor for debugging. +# it currently does require ngrok. +.PHONY: dev-ready +dev-ready: + ./hack/deploy-dev-prereqs.sh + ./hack/update-webhook-config.sh diff --git a/hack/deploy-dev-prereqs.sh b/hack/deploy-dev-prereqs.sh new file mode 100755 index 000000000..d298a99e8 --- /dev/null +++ b/hack/deploy-dev-prereqs.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +if ! command -v kubectl &> /dev/null +then + echo "required kubectl command NOT found" + exit 1 +fi + +# cache folder +MANCACHE=hack/manifest-gen + +if [ ! -d "$MANCACHE" ] +then + echo "manifests do not exist." + echo "you must run 'make generate-manifests' or './hack/update-manifests.sh'" + exit 1 +fi + + +kubectl apply -f "$MANCACHE"/ +kubectl apply -f config/crds + +echo "Finished" \ No newline at end of file diff --git a/hack/update-manifests.sh b/hack/update-manifests.sh new file mode 100755 index 000000000..98bd74218 --- /dev/null +++ b/hack/update-manifests.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env bash + +if ! command -v yq &> /dev/null +then + echo "required yq command NOT found" + exit 1 +fi + +if ! command -v awk &> /dev/null +then + echo "required awk command NOT found" + exit 1 +fi + +# cache folder +MANCACHE=hack/manifest-gen +TEMP="$MANCACHE/temp" + +# hack/manifest-gen is the build folder +mkdir -p "$MANCACHE" +mkdir -p "$TEMP" + +# gen manifests to hack/manifest-gen/manifests.yaml +go run cmd/kubectl-kudo/main.go init --dry-run --version dev --unsafe-self-signed-webhook-ca -o yaml > "$TEMP/manifests.yaml" + +cd "$TEMP" || exit + +# separate the manifests +awk 'BEGIN{file = 0; filename = "output_" file ".txt"} + /---$/ {getline; file ++; filename = "output_" file ".txt"} + {print $0 > filename}' manifests.yaml +cd - || exit + +# loop through all the files +for f in "$TEMP"/*.txt; +do + KIND=$(yq r "$f" kind) + +case "$KIND" in + "CustomResourceDefinition") + NAME=$(yq r "$f" spec.names.kind) + echo "skip '$NAME' crd" + continue;; + + "StatefulSet") + echo "skipping statefulset" + continue;; + "Service") + echo "skipping service" + continue;; + "Secret") + echo "skipping secret" + continue;; + "Namespace") + KIND=ns;; + "ServiceAccount") + KIND=sa;; + *) + KIND="";; +esac + +NAME=$(yq r "$f" metadata.name) + +if [ -n "$KIND" ] +then + NAME="$NAME-$KIND" +fi +NAME="$NAME.yaml" + +echo "Working with $NAME" + +cp "$f" "$MANCACHE/$NAME" +done + +# update webhook (add config.url and remove config.caBundle and config.service) +yq w -i "$MANCACHE/kudo-manager-instance-admission-webhook-config.yaml" webhooks[0].clientConfig.url https://replace-url.com +yq d -i "$MANCACHE/kudo-manager-instance-admission-webhook-config.yaml" webhooks[0].clientConfig.caBundle +yq d -i "$MANCACHE/kudo-manager-instance-admission-webhook-config.yaml" webhooks[0].clientConfig.service + +rm -rf "$TEMP" +echo "Finished" \ No newline at end of file diff --git a/hack/update-webhook-config.sh b/hack/update-webhook-config.sh new file mode 100755 index 000000000..e889281af --- /dev/null +++ b/hack/update-webhook-config.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash + +if ! command -v curl &> /dev/null +then + echo "curl command NOT found" + exit 1 +fi + + +CODE=$(curl -s -o /dev/null -w "%{http_code}" localhost:4040) + + +case "$CODE" in + "000") + echo "ngrok server is not up." + exit 1;; + + "302") echo "ngrok up and running" ;; +esac + +if ! command -v yq &> /dev/null +then + echo "yq command NOT found" + exit 1 +fi + +# need to update the webhook url to the current tunnel. ngrok array order changes requiring a tunnel select for https and pull the public_url of that tunnel +# template webfile located at: config/admit-wh.yaml relative to the project root +# this script requires "update-manifests.sh" to run first +yq w hack/manifest-gen/kudo-manager-instance-admission-webhook-config.yaml webhooks[0].clientConfig.url "$(curl -s localhost:4040/api/tunnels | jq '.tunnels[] | select(.proto == "https") | .public_url' -r)/admit-kudo-dev-v1beta1-instance" | kubectl apply -f - + +# debug notes: This kubectl apply will fail if a kudo init creates a webhook with a clientConfig.service. By adding a clientConfig.url it creates a service and url which is not valid +RESULT=$? +if [ "$RESULT" -ne 0 ]; then + echo "updating the webhook config FAILED. It is likely because the deploy configuration includes a service which is incompatible with a config url." + echo "you may need to 'kubectl delete MutatingWebhookConfiguration kudo-manager-instance-admission-webhook-config'" +fi \ No newline at end of file