Skip to content

Commit

Permalink
chore(testing): verify pepr can be deployed with zarf (#1531)
Browse files Browse the repository at this point in the history
## Description

This PR adds a test to ensure pepr can be deployed with zarf.

## Related Issue

Fixes #1516

## Type of change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Other (security config, docs update, etc)

## Checklist before merging
- [x] Unit,
[Journey](https://github.com/defenseunicorns/pepr/tree/main/journey),
[E2E Tests](https://github.com/defenseunicorns/pepr-excellent-examples),
[docs](https://github.com/defenseunicorns/pepr/tree/main/docs),
[adr](https://github.com/defenseunicorns/pepr/tree/main/adr) added or
updated as needed
- [x] [Contributor Guide
Steps](https://docs.pepr.dev/main/contribute/#submitting-a-pull-request)
followed
  • Loading branch information
samayer12 authored Dec 6, 2024
1 parent 4738896 commit 35bf589
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 1 deletion.
110 changes: 110 additions & 0 deletions .github/workflows/deploy-zarf.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
name: Deploy Test - Zarf

permissions: read-all
on:
workflow_dispatch:
push:
branches: ["main"]
pull_request:
branches: ["main"]

env:
MOD_NAME: pepr-test-zarf

jobs:
zarf:
name: deploy test
runs-on: ubuntu-latest
steps:
- name: Harden Runner
uses: step-security/harden-runner@0080882f6c36860b6ba35c610c98ce87d4e2f26f # v2.10.2
with:
egress-policy: audit

- name: Set up Kubernetes
uses: azure/setup-kubectl@3e0aec4d80787158d308d7b364cb1b702e7feb7f # v4.0.0
with:
version: 'latest'

- name: "install k3d"
run: "curl -s https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash"
shell: bash

- name: Install The Latest Release Version of Zarf
uses: defenseunicorns/setup-zarf@10e539efed02f75ec39eb8823e22a5c795f492ae #v1.0.1
with:
download-init-package: true

- name: clone pepr
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
repository: defenseunicorns/pepr
path: pepr

- name: setup node
uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4.1.0
with:
node-version: 22
cache: "npm"
cache-dependency-path: pepr

- name: "set env: PEPR"
run: echo "PEPR=${GITHUB_WORKSPACE}/pepr" >> "$GITHUB_ENV"

- name: Install Pepr Dependencies
run: |
cd "$PEPR"
npm ci
- name: Build Pepr Package + Image
run: |
cd "$PEPR"
npm run build:image
- name: "set env: MOD_PATH"
run: |
echo "MOD_PATH=${PEPR}/${MOD_NAME}" >> "$GITHUB_ENV"
- name: Init Pepr Module
run: |
cd "$PEPR"
npx pepr init --name "$MOD_NAME" --description "$MOD_NAME" --skip-post-init --confirm
sed -i 's/uuid": ".*",/uuid": "'$MOD_NAME'",/g' "$MOD_PATH/package.json"
- name: Build Pepr Module
run: |
cd "$MOD_PATH"
npm install "${PEPR}/pepr-0.0.0-development.tgz"
npx pepr build --custom-image pepr:dev
- name: "set env: CLUSTER"
run: echo "CLUSTER=$MOD_NAME" >> "$GITHUB_ENV"

- name: Prepare Test Cluster
run: |
k3d cluster create "$CLUSTER"
k3d image import pepr:dev --cluster "$CLUSTER"
- name: "set env: KUBECONFIG"
run: echo "KUBECONFIG=$(k3d kubeconfig write "$CLUSTER")" >> "$GITHUB_ENV"


- name: Initialize Zarf
run: |
cd "$MOD_PATH"
zarf init --confirm
- name: Package Pepr Module with Zarf
run: |
cd "$MOD_PATH"
zarf package create --confirm "dist/"
- name: Deploy Pepr Module with Zarf
run: |
cd "$MOD_PATH"
zarf package deploy --confirm zarf-package-pepr-pepr-test-zarf-amd64-0.0.1.tar.zst
- name: Check Deployment Readiness
timeout-minutes: 5
run: |
${PEPR}/.github/workflows/scripts/check-deployment-readiness.sh pepr-$MOD_NAME
48 changes: 48 additions & 0 deletions .github/workflows/scripts/check-deployment-readiness.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

set -euo pipefail

check_deployment_readiness() {
local deployment_name=$1
local namespace=$2
local expected_ready_replicas=$3
local timeout=${4:-300} # Timeout in seconds (default: 5 minutes)
local interval=${5:-5} # Interval between checks in seconds
local elapsed=0

echo "$(date '+%Y-%m-%d %H:%M:%S') - Checking readiness for deployment '$deployment_name' in namespace '$namespace'..."
echo "$(date '+%Y-%m-%d %H:%M:%S') - Using timeout: ${timeout}s, interval: ${interval}s"

while [ "$elapsed" -lt "$timeout" ]; do
ready_replicas=$(kubectl get deploy "$deployment_name" -n "$namespace" -o jsonpath='{.status.readyReplicas}' 2>/dev/null || echo "0")
ready_replicas=${ready_replicas:-0} # Default to 0 if null

if [ "$ready_replicas" == "$expected_ready_replicas" ]; then
echo "$(date '+%Y-%m-%d %H:%M:%S') - Deployment '$deployment_name' is ready with $ready_replicas replicas."
return 0
fi

echo "$(date '+%Y-%m-%d %H:%M:%S') - Waiting for deployment '$deployment_name' to be ready. Ready replicas: ${ready_replicas:-0}/${expected_ready_replicas}."
kubectl get deploy -n "$namespace"
sleep "$interval"
elapsed=$((elapsed + interval))
done

echo "$(date '+%Y-%m-%d %H:%M:%S') - Timeout reached while waiting for deployment '$deployment_name' to be ready."
return 1
}

# Define success criteria
expected_pepr_replicas=2
expected_watcher_replicas=1
module_name=${1:-}
namespace=${2:-pepr-system} # Default to 'pepr-system' if null

if [ -z "$module_name" ]; then
echo "Error: Module name MUST be provided as the first argument."
exit 1
fi

check_deployment_readiness "$module_name" "$namespace" $expected_pepr_replicas || exit 1 # Check readiness for the first deployment

check_deployment_readiness "$module_name-watcher" "$namespace" $expected_watcher_replicas || exit 1 # Check readiness for the watcher deployment
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ node_modules/
stats.html
.vscode
insecure-tls*
pepr-test-module
pepr-test-*
pepr-upgrade-test
*.tar
*.tgz
Expand Down

0 comments on commit 35bf589

Please sign in to comment.