|
| 1 | +# Validate YAML blocks |
| 2 | + |
| 3 | +How to dry-run validate extracted YAML blocks efficiently. |
| 4 | + |
| 5 | +## Important: use a script file, not inline bash |
| 6 | + |
| 7 | +Long-running `for` loops with nested `if/elif/else` and subshells do NOT work reliably as inline Bash tool calls - they get backgrounded or time out. Always write the validation loop to a temporary `.sh` file and execute it with `bash`. |
| 8 | + |
| 9 | +## Step 1: Write the validation script |
| 10 | + |
| 11 | +Write the following to a temp file (e.g., `$YAML_DIR/../validate.sh`). Replace `<yaml-dir>` with the actual YAML directory path. |
| 12 | + |
| 13 | +```bash |
| 14 | +#!/bin/bash |
| 15 | +set -euo pipefail |
| 16 | + |
| 17 | +YAML_DIR="<yaml-dir>" |
| 18 | +RESULTS="$YAML_DIR/../results.csv" |
| 19 | +> "$RESULTS" |
| 20 | + |
| 21 | +for f in "$YAML_DIR"/*.yaml; do |
| 22 | + bn=$(basename "$f") |
| 23 | + doc=$(echo "$bn" | sed 's/_[0-9]*\.yaml$//') |
| 24 | + |
| 25 | + # Skip incomplete resources (snippets without kind/apiVersion/name) |
| 26 | + if ! grep -q 'kind:' "$f" || ! grep -q 'apiVersion:' "$f" || ! grep -q 'name:' "$f"; then |
| 27 | + echo "$doc,$bn,skip,incomplete fragment" >> "$RESULTS" |
| 28 | + continue |
| 29 | + fi |
| 30 | + |
| 31 | + OUT=$(kubectl apply --dry-run=server -f "$f" 2>&1) || true |
| 32 | + |
| 33 | + if echo "$OUT" | grep -q 'created (server dry run)'; then |
| 34 | + echo "$doc,$bn,pass," >> "$RESULTS" |
| 35 | + elif echo "$OUT" | grep -q 'namespaces.*not found'; then |
| 36 | + echo "$doc,$bn,pass,namespace missing but schema valid" >> "$RESULTS" |
| 37 | + elif echo "$OUT" | grep -qE '<SERVER_NAME>|<NAMESPACE>|<SERVER_URL>'; then |
| 38 | + echo "$doc,$bn,expected,placeholder name" >> "$RESULTS" |
| 39 | + elif echo "$OUT" | grep -qE 'incomingAuth: Required|compositeTools.*steps: Required'; then |
| 40 | + echo "$doc,$bn,expected,partial snippet" >> "$RESULTS" |
| 41 | + else |
| 42 | + ERR=$(echo "$OUT" | tr '\n' ' ' | head -c 200) |
| 43 | + echo "$doc,$bn,fail,$ERR" >> "$RESULTS" |
| 44 | + fi |
| 45 | +done |
| 46 | + |
| 47 | +echo "Done: $(wc -l < "$RESULTS") lines" |
| 48 | +``` |
| 49 | + |
| 50 | +## Step 2: Run the script |
| 51 | + |
| 52 | +Use the Write tool to create the script file, then execute it: |
| 53 | + |
| 54 | +```bash |
| 55 | +bash $YAML_DIR/../validate.sh |
| 56 | +``` |
| 57 | + |
| 58 | +This runs in a single process and completes reliably regardless of how many YAML blocks are validated. |
| 59 | + |
| 60 | +## Result classification |
| 61 | + |
| 62 | +| Result | Meaning | Action needed? | |
| 63 | +| ---------- | ---------------------------------------- | ----------------- | |
| 64 | +| `pass` | Schema valid (or only namespace missing) | No | |
| 65 | +| `expected` | Placeholder name or partial snippet | No | |
| 66 | +| `fail` | Real schema error | Yes - fix the doc | |
| 67 | +| `skip` | Incomplete fragment (no kind/name) | No | |
| 68 | + |
| 69 | +## Important notes |
| 70 | + |
| 71 | +- ALWAYS write to a script file first, then execute with `bash` |
| 72 | +- Use `|| true` after `kubectl apply` to prevent `set -e` from exiting on validation failures |
| 73 | +- Check for `created (server dry run)` in output rather than exit code, because some versions of kubectl return non-zero even on dry-run success with warnings |
| 74 | +- Use a CSV file for results, not bash variables or associative arrays |
| 75 | +- Write one line per YAML block with: doc name, filename, result, detail |
| 76 | +- The CSV is consumed by the report procedure to build the table |
0 commit comments