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

add a local run tool for dev purposes #202

Merged
merged 4 commits into from
Dec 18, 2024
Merged
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
3 changes: 3 additions & 0 deletions Makefile.custom.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
98 changes: 98 additions & 0 deletions hack/bin/run-local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/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.

NAMESPACE="monitoring"
declare -a OLLYOPARGS
declare GRAFANAPORTFORWARDPID MIMIRPORTFORWARDPID


# 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 grafanaPortForward {
kubectl port-forward -n "$NAMESPACE" svc/grafana 3000:80 &>/dev/null &
GRAFANAPORTFORWARDPID="$!"
}

# Stop the Grafana service port-forward
function stopGrafanaPortForward {
kill "$GRAFANAPORTFORWARDPID" || true
}

# 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" || true
}

# 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 {
stopGrafanaPortForward
stopMimirPortForward
resumeInClusterOperator
}


function main {

# make sure the script restores cluster at exit
trap cleanupAtExit SIGINT SIGQUIT SIGABRT SIGTERM EXIT

echo "### set env vars set"
setEnvFromSecrets
echo "### define ollyop args"
defineOLLYOPARGS
echo "### ollyorg args set"

echo "### starting port-forward"
grafanaPortForward
mimirPortForward

echo "### Pausing in-cluster operator"
pauseInClusterOperator

echo "### Running operator"
go run . "${OLLYOPARGS[@]}" -grafana-url http://localhost:3000 -monitoring-metrics-query-url http://localhost:8180

echo "### Cleanup"
}

main "$@"
Loading