בדיקות ל-values ול-chart
chart מינימלי מאפס
Section titled “chart מינימלי מאפס”שלושה קבצים מספיקים לתרגל הכול בלי להיגע ב-chart של הפרויקט.
apiVersion: v2name: miniversion: 0.1.0appVersion: "1.0.0"message: helloreplicas: 1image: repository: nginx tag: ""apiVersion: v1kind: ConfigMapmetadata: name: {{ .Release.Name }}-cfgdata: message: {{ .Values.message | quote }} image: {{ printf "%s:%s" .Values.image.repository .Values.image.tag | quote }}apiVersion: v2הוא פורמט ה-Chart.yamlשל Helm 3 (לא "גרסת Helm").version= גרסת ה-chart עצמו.appVersion= גרסת האפליקציה שהוא מתקין (מידע בלבד).Chart.yamlהוא mapping. שורה שמתחילה ב-- name:שוברת אותו:cannot load Chart.yaml.
helm lint minihelm template r mini --set-string image.tag=1.2איך מוודאים: ה-render מציג ConfigMap בשם r-cfg עם message: "hello" ו-image: "nginx:1.2".
ב-templates של Argo לא משתמשים ב-.Release.Name בשמות שהאפליקציה תלויה בהם — שם ה-release ב-Argo שונה מזה שבהרצה מקומית. כאן זה רק דוגמת תרגול.
בדיקה בסיסית: helm template | grep -q
Section titled “בדיקה בסיסית: helm template | grep -q”helm template r mini --set-string image.tag=1.2 | grep -q 'message: "hello"' && echo PASS || echo FAILhelm template r mini --set-string image.tag=1.2 | grep -q 'image: "nginx:1.2"' && echo PASS || echo FAILאיך מוודאים: PASS פעמיים. הבדיקה הזאת היא מה ש"בדיקת values" אומרת: ערך הוזרק, ולכן הוא מופיע ב-render.
negative self-test: להשחית, לראות FAIL, להחזיר
Section titled “negative self-test: להשחית, לראות FAIL, להחזיר”בדיקה שלא יכולה להיכשל גרועה מהיעדר בדיקה.
cp mini/values.yaml /tmp/values.baksed -i 's/^message: hello/message: bye/' mini/values.yamlhelm template r mini --set-string image.tag=1.2 | grep -q 'message: "hello"' && echo PASS || echo FAIL # must print FAILcp /tmp/values.bak mini/values.yamlhelm template r mini --set-string image.tag=1.2 | grep -q 'message: "hello"' && echo PASS || echo FAIL # must print PASSאיך מוודאים: FAIL אחרי ההשחתה, PASS אחרי השחזור. רק אז סומכים על הבדיקה.
לרנדר לקובץ פעם אחת
Section titled “לרנדר לקובץ פעם אחת”כש-helm template | grep -q נכשל, אי אפשר לדעת אם ה-render נכשל או שהערך חסר. לכן ב-script רינדור לקובץ עם הודעה, ואז כמה grep:
helm template r mini --set-string image.tag=1.2 > /tmp/render.yaml || { echo "render failed"; exit 1; }grep -q 'message: "hello"' /tmp/render.yaml && echo PASS || echo FAILאיך מוודאים: render שנכשל נעצר עם render failed; render תקין ממשיך לבדיקות.
grep -c ו--F: ספירות ומחרוזות מדויקות
Section titled “grep -c ו--F: ספירות ומחרוזות מדויקות”[ "$(grep -c 'kind: ConfigMap' /tmp/render.yaml)" -eq 1 ] && echo PASS || echo FAILgrep -qF 'image: "nginx:1.2"' /tmp/render.yaml && echo PASS || echo FAIL-F מתייחס למחרוזת כטקסט רגיל (נקודות לא הופכות ל-regex). צרור פונקציות לשימוש חוזר: bash/verify-script.
required: להכשיל render כשערך חסר
Section titled “required: להכשיל render כשערך חסר” image: {{ printf "%s:%s" .Values.image.repository (required "image.tag is required" .Values.image.tag) | quote }}helm template r mini ; echo "exit=$?"helm lint mini ; echo "exit=$?"הפלט שנבדק:
Error: execution error at (mini/templates/configmap.yaml:7:53): image.tag is requiredexit=1
engine.go:214: [INFO] Missing required value: image.tag is required==> Linting mini1 chart(s) linted, 0 chart(s) failedexit=0lint ≠ template
Section titled “lint ≠ template”helm lint מדווח על ערך חובה חסר רק כ-[INFO] ומסיים עם exit 0. helm template נכשל עם exit 1.
| בדיקה | required חסר | }} חסר / YAML שבור | טיפוס שגוי (replicas: "abc") |
|---|---|---|---|
helm lint | [INFO], exit 0 | נכשל | עובר |
helm template | exit 1 | נכשל | עובר |
values.schema.json | נכשל בשניהם | — | נכשל בשניהם |
values.schema.json
Section titled “values.schema.json”קובץ ב-root של ה-chart. Helm בודק אליו את ה-values (המשולבים) ב-template, lint ו-install.
{ "$schema": "https://json-schema.org/draft-07/schema#", "type": "object", "required": ["message", "image"], "properties": { "message": { "type": "string", "minLength": 1 }, "replicas": { "type": "integer", "minimum": 1 }, "image": { "type": "object", "required": ["tag"], "properties": { "repository": { "type": "string" }, "tag": { "type": "string", "minLength": 1 } } } }}helm template r mini ; echo "exit=$?"helm lint mini ; echo "exit=$?"helm template r mini --set-string image.tag=1.2 > /dev/null ; echo "exit=$?"איך מוודאים: בלי tag: values don't meet the specifications of the schema(s), at '/image/tag': minLength: got 0, want 1, exit 1 בשתי הפקודות (גם lint). עם tag: exit 0.
בדיקה מבנית: kubectl create --dry-run=client + jq
Section titled “בדיקה מבנית: kubectl create --dry-run=client + jq”אין yq ב-VM. כדי לבדוק מבנה (שדה בתוך אובייקט) ולא רק טקסט:
helm template r mini --set-string image.tag=1.2 \ | kubectl create --dry-run=client -f - -o json \ | jq -e '.data.image == "nginx:1.2"'echo "exit=$?"איך מוודאים: מדפיס true ו-exit=0; אם הערך שונה — false ו-exit=1 (-e קובע את ה-exit לפי התוצאה). ל-render עם כמה אובייקטים, kubectl מדפיס כמה אובייקטי JSON ברצף; jq -s אוסף אותם:
helm template trident <CHART_DIR> -n <NS> -f base.yaml -f <ENV>.yaml -f versions/<ENV>.yaml \ | kubectl create --dry-run=client -f - -o json \ | jq -s -r '.[] | select(.kind=="Deployment") | .metadata.name + " " + .spec.template.spec.containers[0].image'פלט: שורה לכל Deployment עם ה-image המלא, כולל ה-<CANDIDATE>. ללא גישה ל-cluster זה עובד כי --dry-run=client לא פונה לשרת.
job ב-CI: lint + template + assertions
Section titled “job ב-CI: lint + template + assertions”test-values: stage: test script: - helm lint <CHART_DIR> -f base.yaml - | for ENV in dev staging prod; do helm template trident <CHART_DIR> -n "trident-$ENV" \ -f base.yaml -f "$ENV.yaml" -f "versions/$ENV.yaml" --set-string defaultImageTag=ci > "render-$ENV.yaml" || exit 1 grep -qF "TRIDENT_ENV: \"$ENV\"" "render-$ENV.yaml" || { echo "FAIL $ENV: TRIDENT_ENV"; exit 1; } [ "$(grep -c 'image: /' "render-$ENV.yaml")" -eq 0 ] || { echo "FAIL $ENV: empty imageRepository"; exit 1; } done artifacts: paths: ["render-*.yaml"]- ב-GitLab,
- |פותח block scalar; בתוכו bash רגיל. ה-job נכשל ברגע ש-exit 1רץ. --set-string defaultImageTag=ciכי ב-versions/<ENV>.yamlהתג ריק עד שה-CI כותב אותו.- מבנה job ו-
rules: ci/patterns.
מי נכשל ראשון? תרגול של 10 דקות
Section titled “מי נכשל ראשון? תרגול של 10 דקות”- שבור
}}ב-template: מי נכשל,lintאוtemplate? - מחק את
tagמ-values עםrequiredבלבד:lintירוק,templateאדום. - הוסף
values.schema.json— עכשיו שניהם אדומים. replicas: "abc"בלי schema: שניהם ירוקים (רקkubectl --dry-runאו schema תופסים).
כל טעות כזאת מתגלה רק אם מריצים helm template — לא מסתפקים בעין.