Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add keb helm chart tests #226

Merged
merged 14 commits into from
Nov 29, 2023
98 changes: 98 additions & 0 deletions .github/workflows/run-keb-chart-tests-reusable.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: Validate and deploy keb helm chart (reusable)

on:
workflow_call:
inputs:
last-k3s-versions:
description: number of the most recent K3s versions to be used
required: false
default: 1
type: string

jobs:
validate-keb-helm-chart:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Validate keb helm chart
run: |
cd resources/keb
helm template .

prepare-tests:
runs-on: ubuntu-latest
needs: validate-keb-helm-chart
outputs:
versions: ${{ steps.get-versions.outputs.k3s_versions }}
steps:
- name: Checkout code
uses: actions/checkout@v4

- id: get-versions
name: Get K3s versions
# prepare json representing GitHub matrix:
# {"include": [
# {"version":"v1.26.10+k3s1"},
# ...
# {"version":"v1.28.3+k3s1"}
# ]
# }
run: |
VERSIONS=($(./scripts/testing/get-latest-k3s-releases.sh ${{ inputs.last-k3s-versions }}))
MATRIX_AS_JSON=$(echo ${VERSIONS[*]} | awk 'END {printf "{\"include\":[";for (i = 1; i < NF; i++) printf "{\"version\":%s},",$i;printf "{\"version\":%s}]}",$i }'|jq -c)
echo "k3s_versions=${MATRIX_AS_JSON}" >> "${GITHUB_OUTPUT}"

run-keb-chart-matrix:
runs-on: ubuntu-latest
needs: prepare-tests
timeout-minutes: 5
strategy:
matrix: ${{ fromJSON(needs.prepare-tests.outputs.versions) }}
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Prepare K3s cluster and docker registry
run: "./scripts/testing/k3s-setup.sh ${{ matrix.version }} --wait"

- name: Create namespaces
run: |
kubectl create namespace kcp-system
kubectl create namespace kyma-system
kubectl create namespace istio-system

- name: Install istio
run: |
helm repo add istio https://istio-release.storage.googleapis.com/charts
helm install istio-base istio/base -n istio-system --set defaultRevision=default

- name: Deploy keb helm chart
run: |
cd resources/keb
helm template . | kubectl apply -f -

confirm-keb-chart-tests-result:
needs: run-keb-chart-matrix
runs-on: ubuntu-latest
if: success()
outputs:
success: ${{ steps.set-output.outputs.success }}
steps:
- name: Confirm keb chart tests result
id: set-output
run: echo "success=true" >> "${GITHUB_OUTPUT}"

finish-keb-chart-tests:
runs-on: ubuntu-latest
if: always()
needs: confirm-keb-chart-tests-result
steps:
- name: Check keb chart tests result
run: |
if [ "${{ needs.confirm-keb-chart-tests-result.outputs.success }}" != "true" ]; then
echo "keb chart tests failed"
exit 1
fi
echo "keb chart tests passed"
18 changes: 18 additions & 0 deletions .github/workflows/run-keb-chart-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Validate and deploy keb helm chart

on:
pull_request:
branches: [ main ]
types:
- opened
- reopened
- synchronize
- ready_for_review

jobs:
run-keb-chart-tests:
uses: "./.github/workflows/run-keb-chart-tests-reusable.yaml"
if: ${{ !github.event.pull_request.draft }}
with:
last-k3s-versions: 3

35 changes: 35 additions & 0 deletions scripts/testing/get-latest-k3s-releases.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash

LIST_LEN=${1:-5}

# standard bash error handling
set -o nounset # treat unset variables as an error and exit immediately.
set -o errexit # exit immediately when a command fails.
set -E # needs to be set if we want the ERR trap
set -o pipefail # prevents errors in a pipeline from being masked


REPOSITORY=k3s-io/k3s
GITHUB_URL=https://api.github.com/repos/${REPOSITORY}

# selecting at most ${LIST_LEN} recent minor version releases with maximal patch number for the given minor
# Example
# given:
# "v1.24.17+k3s1"
# "v1.25.13+k3s1"
# "v1.25.14+k3s1"
# "v1.26.8+k3s1"
# "v1.26.9+k3s1"
# "v1.27.5+k3s1"
# "v1.27.6+k3s1"
# "v1.28.1+k3s1"
# "v1.28.2+k3s1"
# outputs:
# "v1.25.14+k3s1" "v1.26.9+k3s1" "v1.27.6+k3s1" "v1.28.1+k3s1"
LATEST_RELEASES=($(curl -sS "${GITHUB_URL}/releases" \
| jq '.[] | select(.name|test("v[0-9]{1,2}.[0-9]{1,3}.[0-9]{1,3}\\+")) | .name' \
| sort -rV\
| awk -F. '/k3s1/ {if ($2 != p) {print $0; p=$2}}' \
| head -n ${LIST_LEN}))

echo ${LATEST_RELEASES[*]}
40 changes: 40 additions & 0 deletions scripts/testing/k3s-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env bash

DEFAULT_K3S_VERSION="v1.26.6+k3s1"

if [ "${1}" == "--wait" ]
then
WAIT_OPT=$1
K3S_VERSION=${2:-${DEFAULT_K3S_VERSION}}
else
WAIT_OPT=$2
K3S_VERSION=${1:-${DEFAULT_K3S_VERSION}}
fi

# standard bash error handling
set -o nounset # treat unset variables as an error and exit immediately.
set -o errexit # exit immediately when a command fails.
set -E # needs to be set if we want the ERR trap
set -o pipefail # prevents errors in a pipeline from being masked

echo "Starting docker registry"
sudo mkdir -p /etc/rancher/k3s
sudo cp scripts/testing/yaml/registries.yaml /etc/rancher/k3s
docker run -d \
-p 5000:5000 \
--restart=always \
--name registry.localhost \
-v "$PWD/registry:/var/lib/registry" \
registry:2

echo "Starting K3s cluster (K3s version: ${K3S_VERSION})"
curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION=${K3S_VERSION} K3S_KUBECONFIG_MODE=777 INSTALL_K3S_EXEC="server --disable traefik" sh -
mkdir -p ~/.kube
cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
chmod 600 ~/.kube/config

if [ "${WAIT_OPT}" == "--wait" ]
then
while [[ $(kubectl get nodes -o 'jsonpath={..status.conditions[?(@.type=="Ready")].status}') != "True" ]];
do echo "Waiting for cluster nodes to be ready"; sleep 1; done
fi
6 changes: 6 additions & 0 deletions scripts/testing/yaml/registries.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mirrors:
registry.localhost:5000:
endpoint:
- http://registry.localhost:5000
configs: {}
auths: {}
Loading