From b6b865b56e7d321010c289d31c4733522b6659a7 Mon Sep 17 00:00:00 2001 From: Herve Nicol <12008875+hervenicol@users.noreply.github.com> Date: Mon, 16 Dec 2024 14:32:17 +0100 Subject: [PATCH 1/4] add a local run tool for dev purposes --- Makefile.custom.mk | 3 ++ README.md | 2 ++ hack/bin/run-local.sh | 84 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100755 hack/bin/run-local.sh diff --git a/Makefile.custom.mk b/Makefile.custom.mk index e8b77cb8..99f8b3d7 100644 --- a/Makefile.custom.mk +++ b/Makefile.custom.mk @@ -34,6 +34,9 @@ docker-compose: ## Download docker-compose locally if necessary. curl -sL "https://github.com/docker/compose/releases/download/$(LATEST_RELEASE)/docker-compose-linux-x86_64" -o $(DOCKER_COMPOSE) chmod +x $(DOCKER_COMPOSE) +local: + ./hack/bin/run-local.sh + # go-get-tool will 'go get' any package $2 and install it to $1. PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST)))) define go-get-tool diff --git a/README.md b/README.md index b702bf9c..9ddf1460 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,8 @@ make See `make help` for help. +If you want to run the operator locally against an existing cluster, you can use `make local` which will use `hack/bin/run-local.sh` to setup a local instance for the operator. + ## Architecture TODO(atlas): Fill this out diff --git a/hack/bin/run-local.sh b/hack/bin/run-local.sh new file mode 100755 index 00000000..dedcc2bf --- /dev/null +++ b/hack/bin/run-local.sh @@ -0,0 +1,84 @@ +#!/bin/bash + +# When developing the observability-operator, it is useful to run it locally against a real cluster. +# This script sets up the environment and runs the operator locally. + +NAMESPACE="monitoring" +declare -a OLLYOPARGS +declare PORTFORWARDPID + + +# Define the arguments for the observability-operator +function defineOLLYOPARGS { + # We take all args from the deployment, except the leader-elect one which we don't want for local development + mapfile -t OLLYOPARGS < <(kubectl -n "$NAMESPACE" get deployment observability-operator -ojson | jq -Mr '.spec.template.spec.containers[0].args[]' | grep -v "leader-elect") +} + +# Populate environment variables +function setEnvFromSecrets { + local specenv envname secretname secretkey envvalue + + for specenv in $(kubectl get deployment -n "$NAMESPACE" observability-operator -ojson | jq -c -M '.spec.template.spec.containers[0].env[]') ; do + envname=$(echo "$specenv" | jq -r '.name') + secretname=$(echo "$specenv" | jq -r '.valueFrom.secretKeyRef.name') + secretkey=$(echo "$specenv" | jq -r '.valueFrom.secretKeyRef.key') + envvalue=$(kubectl get secret -n "$NAMESPACE" "$secretname" -ojson | jq -c -M -r '.data["'"$secretkey"'"]' | base64 -d) + + echo "### setting $envname" + export "$envname"="$envvalue" + + done +} + +# Port-forward the Grafana service +function portForwardGrafana { + kubectl port-forward -n "$NAMESPACE" svc/grafana 3000:80 &>/dev/null & + PORTFORWARDPID="$!" +} + +# Stop the Grafana service port-forward +function stopPortForward { + kill "$PORTFORWARDPID" +} + +# Pause the in-cluster operator +function pauseInClusterOperator { + kubectl -n monitoring scale deployment observability-operator --replicas 0 +} + +# Resume the in-cluster operator +function resumeInClusterOperator { + kubectl -n monitoring scale deployment observability-operator --replicas 1 +} + +# Cleanup function +function cleanupAtExit { + stopPortForward + resumeInClusterOperator +} + + +function main { + + # make sure the script restores cluster at exit + trap cleanupAtExit SIGINT SIGQUIT SIGABRT SIGTERM + + echo "### set env vars set" + setEnvFromSecrets + echo "### define ollyop args" + defineOLLYOPARGS + echo "### ollyorg args set" + + echo "### starting port-forward" + portForwardGrafana + + echo "### Pausing in-cluster operator" + pauseInClusterOperator + + echo "### Running operator" + go run . "${OLLYOPARGS[@]}" -kubeconfig ~/.kube/config + + echo "### Cleanup" +} + +main "$@" From 3c1947305294be446b4bf9d2d0ebca66f59f8306 Mon Sep 17 00:00:00 2001 From: Herve Nicol <12008875+hervenicol@users.noreply.github.com> Date: Tue, 17 Dec 2024 09:35:19 +0100 Subject: [PATCH 2/4] improve error and exit handling --- hack/bin/run-local.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hack/bin/run-local.sh b/hack/bin/run-local.sh index dedcc2bf..970a1322 100755 --- a/hack/bin/run-local.sh +++ b/hack/bin/run-local.sh @@ -1,4 +1,5 @@ #!/bin/bash +set -euo pipefail # When developing the observability-operator, it is useful to run it locally against a real cluster. # This script sets up the environment and runs the operator locally. @@ -61,7 +62,7 @@ function cleanupAtExit { function main { # make sure the script restores cluster at exit - trap cleanupAtExit SIGINT SIGQUIT SIGABRT SIGTERM + trap cleanupAtExit SIGINT SIGQUIT SIGABRT SIGTERM EXIT echo "### set env vars set" setEnvFromSecrets From b8a053773597ca0f58cad7487efd4c85559f675d Mon Sep 17 00:00:00 2001 From: Herve Nicol <12008875+hervenicol@users.noreply.github.com> Date: Tue, 17 Dec 2024 15:57:40 +0100 Subject: [PATCH 3/4] add mimir portforward --- hack/bin/run-local.sh | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/hack/bin/run-local.sh b/hack/bin/run-local.sh index 970a1322..163fd979 100755 --- a/hack/bin/run-local.sh +++ b/hack/bin/run-local.sh @@ -6,7 +6,7 @@ set -euo pipefail NAMESPACE="monitoring" declare -a OLLYOPARGS -declare PORTFORWARDPID +declare GRAFANAPORTFORWARDPID MIMIRPORTFORWARDPID # Define the arguments for the observability-operator @@ -32,14 +32,25 @@ function setEnvFromSecrets { } # Port-forward the Grafana service -function portForwardGrafana { +function grafanaPortForward { kubectl port-forward -n "$NAMESPACE" svc/grafana 3000:80 &>/dev/null & - PORTFORWARDPID="$!" + GRAFANAPORTFORWARDPID="$!" } # Stop the Grafana service port-forward -function stopPortForward { - kill "$PORTFORWARDPID" +function stopGrafanaPortForward { + kill "$GRAFANAPORTFORWARDPID" +} + +# Port-forward the mimir service +function mimirPortForward { + kubectl port-forward -n mimir svc/mimir-gateway 8180:80 &>/dev/null & + MIMIRPORTFORWARDPID="$!" +} + +# Stop the Grafana service port-forward +function stopMimirPortForward { + kill "$MIMIRPORTFORWARDPID" } # Pause the in-cluster operator @@ -54,7 +65,8 @@ function resumeInClusterOperator { # Cleanup function function cleanupAtExit { - stopPortForward + stopGrafanaPortForward + stopMimirPortForward resumeInClusterOperator } @@ -71,13 +83,14 @@ function main { echo "### ollyorg args set" echo "### starting port-forward" - portForwardGrafana + grafanaPortForward + mimirPortForward echo "### Pausing in-cluster operator" pauseInClusterOperator echo "### Running operator" - go run . "${OLLYOPARGS[@]}" -kubeconfig ~/.kube/config + go run . "${OLLYOPARGS[@]}" echo "### Cleanup" } From 206a6703cccbf1cbdecf255da8aa64debbeaa8e3 Mon Sep 17 00:00:00 2001 From: Herve Nicol <12008875+hervenicol@users.noreply.github.com> Date: Wed, 18 Dec 2024 15:03:12 +0100 Subject: [PATCH 4/4] run-local: set local addresses --- hack/bin/run-local.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hack/bin/run-local.sh b/hack/bin/run-local.sh index 163fd979..445e518e 100755 --- a/hack/bin/run-local.sh +++ b/hack/bin/run-local.sh @@ -39,7 +39,7 @@ function grafanaPortForward { # Stop the Grafana service port-forward function stopGrafanaPortForward { - kill "$GRAFANAPORTFORWARDPID" + kill "$GRAFANAPORTFORWARDPID" || true } # Port-forward the mimir service @@ -50,7 +50,7 @@ function mimirPortForward { # Stop the Grafana service port-forward function stopMimirPortForward { - kill "$MIMIRPORTFORWARDPID" + kill "$MIMIRPORTFORWARDPID" || true } # Pause the in-cluster operator @@ -90,7 +90,7 @@ function main { pauseInClusterOperator echo "### Running operator" - go run . "${OLLYOPARGS[@]}" + go run . "${OLLYOPARGS[@]}" -grafana-url http://localhost:3000 -monitoring-metrics-query-url http://localhost:8180 echo "### Cleanup" }