跳到正文
DEV Community· pavel·· 2 小時前AI 評分58

ConfigMap 內的 Shell 腳本在每個 Pod 均執行失敗,卻沒有觸發警報

A shell script in a ConfigMap failed in every pod, and nothing alerted

AI 導讀

一個透過 ConfigMap 部署的 Shell 輔助腳本因 Windows CRLF 換行,在每個 Pod 內執行失敗,但應用程式仍運作,健康檢查亦正常,沒有觸發警報。

正文

pavel

pavel

Posted on Nov 30 AI-assisted

Disclosure: I'm the maintainer of k8shealth, the open-source tool mentioned at the end. The problem and the fix below apply to any setup that ships shell scripts through ConfigMaps.

What we saw

On a demo cluster, one pod showed "Configs (0)" and an HTTP 403 in our UI. Every other signal looked normal: pods running, health checks green, no alerts.

The cause was not in the app. A helper script that runs inside the application container (started from postStart) was failing on its first lines, in every pod. Nothing was red, because the thing that failed was the thing that would have reported the failure.

The cause

The script lives in a Git repo and is shipped to pods as a ConfigMap, mounted with subPath. It had been saved with Windows (CRLF) line endings. The shell read set -eu as set -eu followed by a carriage return, and stopped with:

set: illegal option -

That is the whole bug. A \r at the end of a line is invisible in most editors and diffs.

Two details made it hard to notice:

  1. The app container kept running. Only the helper's job was lost, so no pod went unhealthy.
  2. subPath mounts do not update live. If you fix the ConfigMap, running pods keep the old file until they restart, so the fix looked like it "did nothing".

The fix

Fix the cause, not the symptom: make a CRLF script impossible to commit. One line in .gitattributes:

*.sh text eol=lf

Git then checks shell scripts out with LF on every platform, including Windows developers' machines. After that, update the ConfigMap and restart the pods that mount it.

To check a ConfigMap you already have:

kubectl get configmap my-script -o jsonpath='{.data.script\.sh}' | od -c | grep '\\r'

If that prints anything, the script has carriage returns.

What I took from it

  • Test the scripts you inject, not only the app. A script in a ConfigMap has no compiler and no unit tests. A syntax check (sh -n) does not catch this: on dash it passes a CRLF script, and the error only appears when the script runs. Add a CI step that fails the build when a script contains a carriage return: ! grep -q "$(printf '\r')" script.sh.
  • A tool that reports problems needs its own check. The agent was healthy; the helper it relied on was not. Missing data should look different from "all green".
  • Pin line endings in the repo, not in each developer's editor settings.

About the tool

k8shealth runs health checks (HTTP, MySQL, files) from a sidecar next to each pod. The code is public at github.com/k8shealth/k8shealth, the chart installs with Helm, and the site is k8shealth.io.

來源:DEV Community · dev.to