|
| 1 | +name: Kalco Helm Validation |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [ master ] |
| 6 | + paths: |
| 7 | + - 'chart/**' |
| 8 | + |
| 9 | +jobs: |
| 10 | + validate-drift: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + permissions: |
| 13 | + contents: read |
| 14 | + pull-requests: write |
| 15 | + steps: |
| 16 | + - name: Checkout Source Code |
| 17 | + uses: actions/checkout@v3 |
| 18 | + |
| 19 | + - name: Install Tools |
| 20 | + run: | |
| 21 | + curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash |
| 22 | + # Install Kalco |
| 23 | + curl -sL https://github.com/graz-dev/kalco/releases/download/v0.1.19/kalco_Linux_x86_64.tar.gz | tar xz |
| 24 | + sudo mv kalco /usr/local/bin/ |
| 25 | +
|
| 26 | + - name: Configure Kalco |
| 27 | + env: |
| 28 | + KUBECONFIG_DATA: ${{ secrets.KUBECONFIG }} |
| 29 | + run: | |
| 30 | + # Configure Git for Kalco's internal git operations |
| 31 | + git config --global user.email "ci-bot@kalco.dev" |
| 32 | + git config --global user.name "Kalco CI Bot" |
| 33 | +
|
| 34 | + mkdir -p ~/.kube |
| 35 | + echo "$KUBECONFIG_DATA" | base64 -d > ~/.kube/config |
| 36 | + |
| 37 | + mkdir -p tmp-snapshot |
| 38 | +
|
| 39 | + kalco context set validation-ctx \ |
| 40 | + --kubeconfig ~/.kube/config \ |
| 41 | + --output $(pwd)/tmp-snapshot |
| 42 | +
|
| 43 | + kalco context use validation-ctx |
| 44 | +
|
| 45 | + - name: Snapshot Live State |
| 46 | + run: | |
| 47 | + kalco export |
| 48 | +
|
| 49 | + - name: Render & Diff |
| 50 | + id: kalco-diff |
| 51 | + run: | |
| 52 | + # Fetch base branch for comparison |
| 53 | + git fetch origin ${{ github.base_ref }} |
| 54 | + |
| 55 | + mkdir -p generated-manifests |
| 56 | + |
| 57 | + # 1. Render PR version (Chart at current state) |
| 58 | + helm template my-release chart --namespace guestbook-helm > generated-manifests/pr-all.yaml |
| 59 | + |
| 60 | + # 2. Render Base version (Chart at base branch state) |
| 61 | + # We need to extract the chart files from the base branch to a temp dir? |
| 62 | + # Or easier: checkout base to temp dir and render there. |
| 63 | + mkdir -p base-chart-source |
| 64 | + git archive origin/${{ github.base_ref }} chart | tar -x -C base-chart-source |
| 65 | + |
| 66 | + # Check if chart existed in base |
| 67 | + IGNORE_BASE_FLAG="" |
| 68 | + if [ -d "base-chart-source/chart" ]; then |
| 69 | + helm template my-release base-chart-source/chart --namespace guestbook-helm > generated-manifests/base-all.yaml |
| 70 | + if [ -s generated-manifests/base-all.yaml ]; then |
| 71 | + IGNORE_BASE_FLAG="--ignore-base generated-manifests/base-all.yaml" |
| 72 | + fi |
| 73 | + fi |
| 74 | + |
| 75 | + TOTAL_DIFF="" |
| 76 | + HAS_DRIFT="false" |
| 77 | + |
| 78 | + echo "Comparing generated manifests against snapshot..." |
| 79 | + |
| 80 | + # Use Kalco Diff directly on the multi-doc generated file |
| 81 | + # Pass --ignore-base pointing to the base render |
| 82 | + OUTPUT=$(kalco --no-color diff --target generated-manifests/pr-all.yaml --snapshot-dir tmp-snapshot --format markdown $IGNORE_BASE_FLAG --ignore-extra-fields || true) |
| 83 | + |
| 84 | + if [ -n "$OUTPUT" ]; then |
| 85 | + echo "Diff Output for all.yaml:" |
| 86 | + echo "$OUTPUT" |
| 87 | + |
| 88 | + TOTAL_DIFF+=$'\n'"#### Helm Difference Report"$'\n'"$OUTPUT"$'\n' |
| 89 | + |
| 90 | + if echo "$OUTPUT" | grep -q "Drift Detected"; then |
| 91 | + HAS_DRIFT="true" |
| 92 | + echo "!!! CRITICAL: DRIFT DETECTED !!!" |
| 93 | + fi |
| 94 | + fi |
| 95 | + |
| 96 | + echo "has_drift=$HAS_DRIFT" >> $GITHUB_OUTPUT |
| 97 | + |
| 98 | + EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64) |
| 99 | + echo "diff_output<<$EOF" >> $GITHUB_OUTPUT |
| 100 | + echo "$TOTAL_DIFF" >> $GITHUB_OUTPUT |
| 101 | + echo "$EOF" >> $GITHUB_OUTPUT |
| 102 | +
|
| 103 | + - name: Comment PR & Label |
| 104 | + uses: actions/github-script@v6 |
| 105 | + env: |
| 106 | + DIFF_OUTPUT: ${{ steps.kalco-diff.outputs.diff_output }} |
| 107 | + HAS_DRIFT: ${{ steps.kalco-diff.outputs.has_drift }} |
| 108 | + with: |
| 109 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 110 | + script: | |
| 111 | + const output = process.env.DIFF_OUTPUT; |
| 112 | + const hasDrift = process.env.HAS_DRIFT === 'true'; |
| 113 | + |
| 114 | + if (!output || output.trim() === "") { |
| 115 | + console.log("No output to comment."); |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + const labelAdd = hasDrift ? 'kalco/deny' : 'kalco/approve'; |
| 120 | + const labelRemove = hasDrift ? 'kalco/approve' : 'kalco/deny'; |
| 121 | + |
| 122 | + await github.rest.issues.createComment({ |
| 123 | + issue_number: context.issue.number, |
| 124 | + owner: context.repo.owner, |
| 125 | + repo: context.repo.repo, |
| 126 | + body: `### 🛡️ Kalco Helm Validation Report\n\n${output}` |
| 127 | + }); |
| 128 | + |
| 129 | + try { |
| 130 | + await github.rest.issues.removeLabel({ |
| 131 | + issue_number: context.issue.number, |
| 132 | + owner: context.repo.owner, |
| 133 | + repo: context.repo.repo, |
| 134 | + name: labelRemove |
| 135 | + }); |
| 136 | + } catch (e) {} |
| 137 | + |
| 138 | + await github.rest.issues.addLabels({ |
| 139 | + issue_number: context.issue.number, |
| 140 | + owner: context.repo.owner, |
| 141 | + repo: context.repo.repo, |
| 142 | + labels: [labelAdd] |
| 143 | + }); |
0 commit comments