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+
1746set -o nounset
1847set -o pipefail
1948set -o errexit
2049
2150cd " $( 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) "
53+
54+ if [ ! -x " ${PROMETHEUS} " ]; then
55+ echo " prometheus binary not found at ${PROMETHEUS} " >&2
56+ exit 1
57+ fi
2458
2559touch .prometheus-config.yaml
2660" $PROMETHEUS " \
@@ -36,6 +70,43 @@ while ! curl -s http://localhost:9090/-/ready; do
3670done
3771echo ' Prometheus is ready!'
3872
73+ # Dev-only: when running a local `kcp start` (not in Prow CI), watch for kcp's
74+ # cert+kubeconfig to appear and auto-populate a scrape config. In Prow, e2e tests
75+ # populate .prometheus-config.yaml via the Go testing framework
76+ # (staging/.../testing/server/metrics.go) — this watcher would race and clobber
77+ # those per-server entries, so it stays off. Gating on PROW_JOB_ID rather than
78+ # ARTIFACT_DIR because users set ARTIFACT_DIR locally to collect tsdb archives.
79+ WATCHER_PID=" "
80+ if [ -z " ${PROW_JOB_ID:- } " ]; then
81+ (
82+ for _ in $( seq 1 120) ; do
83+ if [ -f .kcp/apiserver.crt ] && [ -f .kcp/admin.kubeconfig ]; then
84+ # Use the shard-admin token (system:master) — kcp-admin is workspace-scoped
85+ # and gets 401 on the raw /metrics endpoint.
86+ TOKEN=$( awk ' /name: shard-admin/{found=1} found && /token:/{print $2; exit}' .kcp/admin.kubeconfig)
87+ if [ -n " ${TOKEN} " ]; then
88+ cat > .prometheus-config.yaml << EOF
89+ scrape_configs:
90+ - job_name: kcp
91+ scrape_interval: 5s
92+ scheme: https
93+ bearer_token: ${TOKEN}
94+ tls_config:
95+ ca_file: $( pwd) /.kcp/apiserver.crt
96+ static_configs:
97+ - targets: ["localhost:6443"]
98+ EOF
99+ curl -sX POST http://localhost:9090/-/reload > /dev/null
100+ echo ' Wrote kcp scrape config and reloaded Prometheus.'
101+ exit 0
102+ fi
103+ fi
104+ sleep 1
105+ done
106+ ) &
107+ WATCHER_PID=$!
108+ fi
109+
39110set -o xtrace
40111set +o errexit
41112" ${@ } "
@@ -44,6 +115,7 @@ set +o xtrace
44115set -o errexit
45116echo " Command terminated with ${EXIT_CODE} "
46117
118+ [ -n " ${WATCHER_PID} " ] && kill -TERM ${WATCHER_PID} 2> /dev/null || true
47119kill -TERM ${PROM_PID}
48120
49121if [ -n " ${ARTIFACT_DIR:= } " ]; then
0 commit comments