Repository navigation
reloader: default to non-root and lock provisioned datasource #1244
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,48 @@ replace_template() { | |
| sed -i "s|$1|$replacement|g" -- "$3" | ||
| } | ||
|
|
||
| # Run after copying/restoring rules on every Pod initialization. Do not chown /data. | ||
| prepare_reloader_rules() { | ||
| reloader_uid=${RELOADER_UID:-1000} | ||
| reloader_gid=${RELOADER_GID:-2000} | ||
| for reloader_id in "$reloader_uid" "$reloader_gid"; do | ||
| case "$reloader_id" in | ||
| ''|*[!0-9]*) echo "Reloader UID/GID must be numeric" >&2; return 1;; | ||
| esac | ||
| done | ||
| reloader_version=$1 | ||
| reloader_store= | ||
| set -- "$PROM_CONFIG_PATH/rules" | ||
| if [ -n "$PROM_PERSISTENT_DIR" ]; then | ||
| case "$reloader_version" in | ||
| ''|.|..) echo "Invalid persistent rules version" >&2; return 1;; | ||
| esac | ||
| reloader_store="$PROM_PERSISTENT_DIR/latest-rules/$reloader_version" | ||
| set -- "$@" "$PROM_PERSISTENT_DIR/latest-rules" "$reloader_store" | ||
| fi | ||
| for reloader_dir do | ||
| if [ -L "$reloader_dir" ] || { [ -e "$reloader_dir" ] && [ ! -d "$reloader_dir" ]; }; then | ||
| echo "Rules path must be a directory, not a symlink: $reloader_dir" >&2 | ||
| return 1 | ||
| fi | ||
| mkdir -p -- "$reloader_dir" && | ||
| chown "$reloader_uid:$reloader_gid" "$reloader_dir" && | ||
| chmod u+rwx,go-w "$reloader_dir" || return 1 | ||
| done | ||
| for reloader_dir in "$PROM_CONFIG_PATH/rules" "$reloader_store"; do | ||
| [ -n "$reloader_dir" ] || continue | ||
| for reloader_file in "$reloader_dir"/*.yml; do | ||
| [ -e "$reloader_file" ] || [ -L "$reloader_file" ] || continue | ||
| if [ -L "$reloader_file" ] || [ ! -f "$reloader_file" ]; then | ||
| echo "Rule must be a regular file: $reloader_file" >&2 | ||
| return 1 | ||
| fi | ||
| chown "$reloader_uid:$reloader_gid" "$reloader_file" && | ||
| chmod u+rw,go-w,a-x "$reloader_file" || return 1 | ||
|
Comment on lines
+41
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| done | ||
| done | ||
| } | ||
|
|
||
| # Multiline replacements would become new sed commands or malformed configuration. | ||
| for value in "$DM_CLUSTER_NAME" "$TIDB_CLUSTER_NAME" "$TIDB_CLUSTER_NAMESPACE" \ | ||
| "$GF_DM_PROMETHEUS_URL" "$GF_K8S_PROMETHEUS_URL" "$GF_TIDB_PROMETHEUS_URL"; do | ||
|
|
@@ -61,6 +103,7 @@ if [ -n "$DM_CLUSTER_NAME" ]; then | |
| fi | ||
| cp -f -- /tmp/dm-cluster-datasource.yaml "$GF_DATASOURCE_PATH/" | ||
| fi | ||
| prepare_reloader_rules "${DM_VERSION##*/}" || exit 1 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| echo "running for DM done" | ||
| exit | ||
| fi | ||
|
|
@@ -176,6 +219,8 @@ if [ -n "$PROM_PERSISTENT_DIR" ]; then | |
| fi | ||
|
|
||
| # Datasources | ||
| prepare_reloader_rules "${TIDB_VERSION##*/}" || exit 1 | ||
|
|
||
| if [ -n "$GF_DATASOURCE_PATH" ]; then | ||
| if [ -n "$GF_K8S_PROMETHEUS_URL" ]; then | ||
| replace_template 'http://prometheus-k8s.monitoring.svc:9090' "${GF_K8S_PROMETHEUS_URL}" /tmp/k8s-datasource.yaml | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Build from the exact deployed initializer digest, preserving its asset lineage. | ||
| ARG INITIALIZER_IMAGE | ||
| FROM ${INITIALIZER_IMAGE} | ||
|
|
||
| USER 0:0 | ||
| ARG PINGCAP_UID=1000 | ||
| ARG PINGCAP_GID=2000 | ||
| ENV RELOADER_UID=${PINGCAP_UID} RELOADER_GID=${PINGCAP_GID} | ||
|
|
||
| # Keep the original initializer logic paired with its original rule/dashboard assets. | ||
| RUN cp /usr/bin/init.sh /usr/bin/init-before-nonroot.sh | ||
| COPY platform-monitoring/operator/init.sh /tmp/nonroot-init.sh | ||
| RUN grep -q '^# Multiline replacements' /tmp/nonroot-init.sh && \ | ||
| awk '/^# Multiline replacements/ {exit} {print}' /tmp/nonroot-init.sh > /usr/bin/init.sh && \ | ||
| printf '%s\n' \ | ||
| '/usr/bin/init-before-nonroot.sh "$@" || exit $?' \ | ||
| 'if [ -n "${DM_CLUSTER_NAME:-}" ]; then' \ | ||
| ' prepare_reloader_rules "${DM_VERSION##*/}"' \ | ||
| 'else' \ | ||
| ' prepare_reloader_rules "${TIDB_VERSION##*/}"' \ | ||
| 'fi' >> /usr/bin/init.sh && \ | ||
| chmod 0755 /usr/bin/init.sh && \ | ||
| rm /tmp/nonroot-init.sh | ||
|
|
||
| # Apply the datasource change without replacing version-specific rules/dashboards. | ||
| RUN if [ -f /tmp/k8s-datasource.yaml ]; then \ | ||
| sed -i 's/^\([[:space:]]*editable:[[:space:]]*\)true[[:space:]]*$/\1false/' /tmp/k8s-datasource.yaml; \ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| fi | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: pingcap/monitoring
Length of output: 5853
Script executed:
Repository: pingcap/monitoring
Length of output: 2854
Make the new test set the fixture mode explicitly.
os.WriteFile(..., 0444)applies the process umask. Under umask 077, the fixture becomes 0400. The helper'schmod u+rw,go-w,a-xthen leaves it at 0600, so the test's0644check fails. Add anos.Chmodcall after each fixture write so the input mode does not depend on the umask.Suggested fix
if err := os.WriteFile(filepath.Join(path, "test.yml"), []byte("groups: []"), 0444); err != nil { t.Fatal(err) } + if err := os.Chmod(filepath.Join(path, "test.yml"), 0444); err != nil { + t.Fatal(err) + }