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) "
2453
2554touch .prometheus-config.yaml
2655" $PROMETHEUS " \
@@ -36,6 +65,43 @@ while ! curl -s http://localhost:9090/-/ready; do
3665done
3766echo ' 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+
39105set -o xtrace
40106set +o errexit
41107" ${@ } "
@@ -44,6 +110,7 @@ set +o xtrace
44110set -o errexit
45111echo " Command terminated with ${EXIT_CODE} "
46112
113+ [ -n " ${WATCHER_PID} " ] && kill -TERM ${WATCHER_PID} 2> /dev/null || true
47114kill -TERM ${PROM_PID}
48115
49116if [ -n " ${ARTIFACT_DIR:= } " ]; then
0 commit comments