Skip to content

Commit bf742d4

Browse files
committed
update run-with-prmetheus to support local dev
1 parent 3843bcf commit bf742d4

1 file changed

Lines changed: 68 additions & 1 deletion

File tree

hack/run-with-prometheus.sh

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,42 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
# Usage: ./hack/run-with-prometheus.sh <command> [args...]
18+
#
19+
# Starts a local Prometheus (http://localhost:9090) using .prometheus-config.yaml,
20+
# runs the given command, then tears Prometheus down and exits with the command's
21+
# exit code. Without arguments the script runs nothing, exits 0, and immediately
22+
# kills Prometheus — you must pass a command.
23+
#
24+
# Examples:
25+
# # Single-node kcp (production binary). The dev watcher below auto-populates
26+
# # .prometheus-config.yaml with a scrape config pointing at localhost:6443.
27+
# ./hack/run-with-prometheus.sh go run ./cmd/kcp start
28+
# ./hack/run-with-prometheus.sh ./bin/kcp start
29+
#
30+
# # Sharded setup via the dev test-server — the server itself registers scrape
31+
# # configs (one job per shard plus kcp-front-proxy) through the Go testing
32+
# # framework, so nothing extra is needed here. Build once with `make build-e2e`.
33+
# ./hack/run-with-prometheus.sh ./bin/sharded-test-server --work-dir-path=.
34+
# ./hack/run-with-prometheus.sh ./bin/sharded-test-server --work-dir-path=. --number-of-shards=3
35+
#
36+
# When a local cmd/kcp dev server is detected (.kcp/apiserver.crt and
37+
# .kcp/admin.kubeconfig appear in the repo root), the script auto-writes a scrape
38+
# config for it and reloads Prometheus. sharded-test-server writes .kcp/serving-ca.crt
39+
# (no apiserver.crt) so the watcher stays silent and lets the server's own
40+
# ScrapeMetrics calls populate .prometheus-config.yaml. E2e runs use tempdirs and
41+
# follow the same pattern — watcher idle, Go framework owns the config.
42+
#
43+
# If ARTIFACT_DIR is set, the tsdb is archived to
44+
# ${ARTIFACT_DIR}/metrics/prometheus.tar after shutdown.
45+
1746
set -o nounset
1847
set -o pipefail
1948
set -o errexit
2049

2150
cd "$(dirname $0)/.."
2251

23-
PROMETHEUS="$(UGET_PRINT_PATH=relative make --no-print-directory prometheus)"
52+
PROMETHEUS="$(UGET_PRINT_PATH=absolute make --no-print-directory prometheus)"
2453

2554
touch .prometheus-config.yaml
2655
"$PROMETHEUS" \
@@ -36,6 +65,43 @@ while ! curl -s http://localhost:9090/-/ready; do
3665
done
3766
echo 'Prometheus is ready!'
3867

68+
# Dev-only: when running a local `kcp start` (not in Prow CI), watch for kcp's
69+
# cert+kubeconfig to appear and auto-populate a scrape config. In Prow, e2e tests
70+
# populate .prometheus-config.yaml via the Go testing framework
71+
# (staging/.../testing/server/metrics.go) — this watcher would race and clobber
72+
# those per-server entries, so it stays off. Gating on PROW_JOB_ID rather than
73+
# ARTIFACT_DIR because users set ARTIFACT_DIR locally to collect tsdb archives.
74+
WATCHER_PID=""
75+
if [ -z "${PROW_JOB_ID:-}" ]; then
76+
(
77+
for _ in $(seq 1 120); do
78+
if [ -f .kcp/apiserver.crt ] && [ -f .kcp/admin.kubeconfig ]; then
79+
# Use the shard-admin token (system:master) — kcp-admin is workspace-scoped
80+
# and gets 401 on the raw /metrics endpoint.
81+
TOKEN=$(awk '/name: shard-admin/{found=1} found && /token:/{print $2; exit}' .kcp/admin.kubeconfig)
82+
if [ -n "${TOKEN}" ]; then
83+
cat > .prometheus-config.yaml <<EOF
84+
scrape_configs:
85+
- job_name: kcp
86+
scrape_interval: 5s
87+
scheme: https
88+
bearer_token: ${TOKEN}
89+
tls_config:
90+
ca_file: $(pwd)/.kcp/apiserver.crt
91+
static_configs:
92+
- targets: ["localhost:6443"]
93+
EOF
94+
curl -sX POST http://localhost:9090/-/reload >/dev/null
95+
echo 'Wrote kcp scrape config and reloaded Prometheus.'
96+
exit 0
97+
fi
98+
fi
99+
sleep 1
100+
done
101+
) &
102+
WATCHER_PID=$!
103+
fi
104+
39105
set -o xtrace
40106
set +o errexit
41107
"${@}"
@@ -44,6 +110,7 @@ set +o xtrace
44110
set -o errexit
45111
echo "Command terminated with ${EXIT_CODE}"
46112

113+
[ -n "${WATCHER_PID}" ] && kill -TERM ${WATCHER_PID} 2>/dev/null || true
47114
kill -TERM ${PROM_PID}
48115

49116
if [ -n "${ARTIFACT_DIR:=}" ]; then

0 commit comments

Comments
 (0)