Skip to content

Commit a2c013c

Browse files
authored
Merge pull request #11 from wnqueiroz/feature-kyverno
feat(kyverno): validate XQueueClaim fields via policy and enforce on PR with GitHub Action
2 parents 21ceaae + 656e9d1 commit a2c013c

File tree

4 files changed

+178
-0
lines changed

4 files changed

+178
-0
lines changed

.bootstrap/kyverno/up.sh

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# Change to platform cluster
6+
if [[ "$(kubectl config current-context)" != "kind-platform" ]]; then
7+
kubectl config use-context kind-platform || {
8+
echo "Failed to switch context to kind-platform"
9+
exit 1
10+
}
11+
fi
12+
13+
NS=kyverno-system
14+
KYVERNO_CHART=kyverno/kyverno
15+
POLICIES_DIR=./kyverno
16+
TIMEOUT=120
17+
INTERVAL=5
18+
ELAPSED=0
19+
20+
echo "Adding Kyverno Helm repository..."
21+
helm repo add kyverno https://kyverno.github.io/kyverno/ 2>/dev/null || true
22+
helm repo update
23+
24+
echo "Installing or upgrading Kyverno via Helm..."
25+
helm upgrade --install kyverno "$KYVERNO_CHART" \
26+
--namespace "$NS" \
27+
--create-namespace
28+
29+
echo "Waiting for Kyverno webhook to be ready..."
30+
kubectl wait deployment/kyverno-admission-controller \
31+
-n "$NS" \
32+
--for=condition=Available=True \
33+
--timeout=${TIMEOUT}s
34+
35+
echo "Ensuring all Kyverno pods are ready..."
36+
while true; do
37+
READY=$(kubectl get pods -n "$NS" -o jsonpath='{.items[*].status.containerStatuses[*].ready}' | tr " " "\n" | grep -c false || true)
38+
if [[ "$READY" -eq 0 ]]; then
39+
break
40+
fi
41+
if [[ "$ELAPSED" -ge "$TIMEOUT" ]]; then
42+
echo "Timeout reached. Kyverno pods are not ready."
43+
exit 1
44+
fi
45+
echo "Waiting for Kyverno pods to become ready..."
46+
sleep $INTERVAL
47+
ELAPSED=$((ELAPSED + INTERVAL))
48+
done
49+
50+
echo "All Kyverno pods are ready!"
51+
52+
apply_kyverno_policies() {
53+
local retries=20
54+
local delay=3
55+
56+
for i in $(seq 1 $retries); do
57+
if kubectl apply -f "$POLICIES_DIR" --recursive; then
58+
echo "✅ Kyverno policies applied successfully!"
59+
return 0
60+
else
61+
echo "⚠️ Failed to apply policies, retrying in ${delay}s... (${i}/${retries})"
62+
sleep $delay
63+
fi
64+
done
65+
66+
echo "❌ Failed to apply Kyverno policies after ${retries} attempts."
67+
exit 1
68+
}
69+
70+
echo "Applying Kyverno policies from $POLICIES_DIR..."
71+
apply_kyverno_policies
72+
73+
echo "✅ Kyverno setup and policy application completed successfully!"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
name: Validate Crossplane Claims
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- "crossplane/claims/**/*.yaml"
7+
- "crossplane/claims/**/*.yml"
8+
- "kyverno/**/*.yaml"
9+
- "kyverno/**/*.yml"
10+
11+
jobs:
12+
validate-claims:
13+
name: Validate Claims YAML with Kyverno
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
20+
- name: Install Kyverno CLI
21+
uses: kyverno/action-install-cli@v0.2.0
22+
with:
23+
release: "v1.13.4"
24+
25+
- name: Check install
26+
run: kyverno version
27+
28+
- name: Run Kyverno policy checks on claims
29+
run: |
30+
echo "## 🛡️ Kyverno Policy Validation Results" >> $GITHUB_STEP_SUMMARY
31+
echo "" >> $GITHUB_STEP_SUMMARY
32+
echo '```' >> $GITHUB_STEP_SUMMARY
33+
34+
set +e
35+
kyverno apply ./kyverno --resource ./crossplane/claims 2>&1 | tee result.txt
36+
KYVERNO_EXIT_CODE=${PIPESTATUS[0]}
37+
set -e
38+
39+
cat result.txt >> $GITHUB_STEP_SUMMARY
40+
echo '```' >> $GITHUB_STEP_SUMMARY
41+
42+
if [[ $KYVERNO_EXIT_CODE -ne 0 ]]; then
43+
echo "" >> $GITHUB_STEP_SUMMARY
44+
echo "❌ One or more Kyverno policies failed. Please fix the issues above." >> $GITHUB_STEP_SUMMARY
45+
exit 1
46+
else
47+
echo "✅ All policies passed." >> $GITHUB_STEP_SUMMARY
48+
fi
File renamed without changes.

kyverno/validate-xqueue-fields.yaml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
apiVersion: kyverno.io/v1
2+
kind: ClusterPolicy
3+
metadata:
4+
name: validate-xqueue-fields
5+
spec:
6+
validationFailureAction: Enforce
7+
background: true
8+
rules:
9+
- name: deny-invalid-location
10+
match:
11+
resources:
12+
kinds:
13+
- platform.hooli.tech/v1alpha1/XQueueClaim
14+
validate:
15+
message: "Invalid location: only 'EU' or 'US' are allowed in spec.location"
16+
deny:
17+
conditions:
18+
all:
19+
- key: "{{ request.object.spec.location }}"
20+
operator: AllNotIn
21+
value:
22+
- "EU"
23+
- "US"
24+
25+
- name: deny-invalid-max-message-size
26+
match:
27+
resources:
28+
kinds:
29+
- platform.hooli.tech/v1alpha1/XQueueClaim
30+
validate:
31+
message: "Invalid maxMessageSize: must be between 1024 and 262144 (bytes)"
32+
deny:
33+
conditions:
34+
any:
35+
- key: "{{ request.object.spec.maxMessageSize }}"
36+
operator: GreaterThan
37+
value: 262144
38+
- key: "{{ request.object.spec.maxMessageSize }}"
39+
operator: LessThan
40+
value: 1024
41+
42+
- name: deny-invalid-visibility-timeout
43+
match:
44+
resources:
45+
kinds:
46+
- platform.hooli.tech/v1alpha1/XQueueClaim
47+
validate:
48+
message: "Invalid visibilityTimeoutSeconds: must be between 0 and 43200 (seconds)"
49+
deny:
50+
conditions:
51+
any:
52+
- key: "{{ request.object.spec.visibilityTimeoutSeconds }}"
53+
operator: GreaterThan
54+
value: 43200
55+
- key: "{{ request.object.spec.visibilityTimeoutSeconds }}"
56+
operator: LessThan
57+
value: 0

0 commit comments

Comments
 (0)