Skip to content

Commit

Permalink
feat(github): add kubeconform workflow (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinohmann authored Dec 14, 2023
1 parent 12f9073 commit a383fe4
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
56 changes: 56 additions & 0 deletions .github/scripts/kubeconform.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env bash
set -o errexit

KUBERNETES_DIR=$1
SCHEMA_DIR=$2
KUBE_VERSION="${3:-1.28.0}"

[[ -z "${KUBERNETES_DIR}" ]] && echo "Kubernetes location not specified" && exit 1
[[ -z "${SCHEMA_DIR}" ]] && echo "Schema location not specified" && exit 1

kustomize_args=("--load-restrictor=LoadRestrictionsNone")
kustomize_config="kustomization.yaml"
kubeconform_args=(
"-strict"
"-ignore-missing-schemas"
"-kubernetes-version"
"${KUBE_VERSION}"
"-skip"
"ReplicationSource,ReplicationDestination,Secret"
"-schema-location"
"default"
"-schema-location"
"${SCHEMA_DIR}/{{.Group}}/{{.ResourceKind}}_{{.ResourceAPIVersion}}.json"
"-verbose"
)

echo "=== Validating standalone manifests in ${KUBERNETES_DIR}/flux ==="
find "${KUBERNETES_DIR}/flux" -maxdepth 1 -type f -name '*.yaml' -print0 | while IFS= read -r -d $'\0' file;
do
kubeconform "${kubeconform_args[@]}" "${file}"
if [[ ${PIPESTATUS[0]} != 0 ]]; then
exit 1
fi
done

echo "=== Validating kustomizations in ${KUBERNETES_DIR}/flux ==="
find "${KUBERNETES_DIR}/flux" -type f -name $kustomize_config -print0 | while IFS= read -r -d $'\0' file;
do
echo "=== Validating kustomizations in ${file/%$kustomize_config} ==="
kustomize build "${file/%$kustomize_config}" "${kustomize_args[@]}" | \
kubeconform "${kubeconform_args[@]}"
if [[ ${PIPESTATUS[0]} != 0 ]]; then
exit 1
fi
done

echo "=== Validating kustomizations in ${KUBERNETES_DIR}/apps ==="
find "${KUBERNETES_DIR}/apps" -type f -name $kustomize_config -print0 | while IFS= read -r -d $'\0' file;
do
echo "=== Validating kustomizations in ${file/%$kustomize_config} ==="
kustomize build "${file/%$kustomize_config}" "${kustomize_args[@]}" | \
kubeconform "${kubeconform_args[@]}"
if [[ ${PIPESTATUS[0]} != 0 ]]; then
exit 1
fi
done
52 changes: 52 additions & 0 deletions .github/workflows/kubeconform.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
name: "Kubeconform"

on:
workflow_dispatch:
pull_request:
branches: ["main"]
paths:
- "kubernetes/**"

env:
SCHEMA_DIR: /home/runner/crds

jobs:
kubeconform:
name: Kubeconform
runs-on: ubuntu-latest
strategy:
matrix:
path: ["kubernetes"]
steps:
- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

- name: Setup Homebrew
uses: Homebrew/actions/setup-homebrew@master

- name: Setup Tools
shell: bash
run: brew install kubeconform kustomize

- name: Setup QEMU
uses: docker/setup-qemu-action@68827325e0b33c7199eb31dd4e31fbe9023e06e3 # v3.0.0

- name: Setup Docker Buildx
id: buildx
uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0

- name: Extract files from container image
shell: bash
run: |
mkdir -p ${{ env.SCHEMA_DIR }}
docker run --rm \
-v ${{ env.SCHEMA_DIR }}:/crds \
-u $(id -u) \
--entrypoint /bin/sh \
ghcr.io/martinohmann/kubernetes-schemas:latest \
-c "cp -r /usr/share/nginx/html/* /crds"
- name: Run kubeconform
shell: bash
run: bash ./.github/scripts/kubeconform.sh ${{ matrix.path }} ${{ env.SCHEMA_DIR }}

0 comments on commit a383fe4

Please sign in to comment.