diff --git a/.github/workflows/karpenter-chart-release.yaml b/.github/workflows/karpenter-chart-release.yaml new file mode 100644 index 0000000..015ace3 --- /dev/null +++ b/.github/workflows/karpenter-chart-release.yaml @@ -0,0 +1,81 @@ +name: Release Karpenter Helm Chart + +on: + push: + tags: + - 'karpenter-chart/v*' + workflow_dispatch: + inputs: + version: + description: 'Chart version override (e.g. 0.2.0). Leave empty to use Chart.yaml version.' + required: false + type: string + +env: + REGISTRY: ghcr.io + CHART_NAME: karpenter + +jobs: + release: + runs-on: ubuntu-latest + permissions: + contents: write + packages: write + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Helm + uses: azure/setup-helm@v4 + with: + version: v3.17.0 + + - name: Determine chart version + id: version + run: | + if [[ -n "${{ inputs.version }}" ]]; then + VERSION="${{ inputs.version }}" + elif [[ "$GITHUB_REF" == refs/tags/karpenter-chart/v* ]]; then + VERSION="${GITHUB_REF#refs/tags/karpenter-chart/v}" + else + VERSION=$(grep '^version:' karpenter/charts/karpenter/Chart.yaml | awk '{print $2}') + fi + echo "version=${VERSION}" >> "$GITHUB_OUTPUT" + echo "Chart version: ${VERSION}" + + - name: Log in to GHCR + run: | + echo "${{ secrets.GITHUB_TOKEN }}" | helm registry login ${{ env.REGISTRY }} \ + --username ${{ github.actor }} \ + --password-stdin + + - name: Package and push Helm chart + run: | + karpenter/hack/release-chart.sh \ + --version "${{ steps.version.outputs.version }}" \ + --registry "oci://${{ env.REGISTRY }}/${{ github.repository }}/charts" \ + --push + + - name: Create GitHub Release + if: startsWith(github.ref, 'refs/tags/karpenter-chart/v') + uses: softprops/action-gh-release@v2 + with: + name: "Karpenter Helm Chart v${{ steps.version.outputs.version }}" + body: | + ## Karpenter Helm Chart v${{ steps.version.outputs.version }} + + ### Install from OCI registry + ```bash + helm install karpenter oci://${{ env.REGISTRY }}/${{ github.repository }}/charts/${{ env.CHART_NAME }} \ + --version ${{ steps.version.outputs.version }} \ + --namespace kube-system + ``` + + ### Install from downloaded archive + ```bash + helm install karpenter ./${{ env.CHART_NAME }}-${{ steps.version.outputs.version }}.tgz \ + --namespace kube-system + ``` + files: .helm-packages/${{ env.CHART_NAME }}-${{ steps.version.outputs.version }}.tgz + fail_on_unmatched_files: true diff --git a/.gitignore b/.gitignore index 7e8a5d4..39e6f61 100644 --- a/.gitignore +++ b/.gitignore @@ -432,4 +432,7 @@ dist/ .env .kube -*.kubeconfig \ No newline at end of file +*.kubeconfig + +# Helm chart packaging output +.helm-packages/ \ No newline at end of file diff --git a/karpenter/Makefile b/karpenter/Makefile index ecca763..81ef3aa 100644 --- a/karpenter/Makefile +++ b/karpenter/Makefile @@ -150,6 +150,20 @@ build-installer: manifests generate kustomize ## Generate a consolidated YAML wi mkdir -p dist $(KUSTOMIZE) build config/default > dist/install.yaml +##@ Helm Chart + +.PHONY: helm-lint +helm-lint: ## Lint the Karpenter Helm chart. + helm lint charts/karpenter + +.PHONY: helm-package +helm-package: ## Lint and package the Karpenter Helm chart into a .tgz archive. + hack/release-chart.sh + +.PHONY: helm-push +helm-push: ## Lint, package, and push the Karpenter Helm chart to an OCI registry. Requires HELM_REGISTRY (e.g. oci://ghcr.io/azure/aks-flex/charts). + hack/release-chart.sh --registry $(HELM_REGISTRY) --push + ##@ Deployment ifndef ignore-not-found diff --git a/karpenter/hack/release-chart.sh b/karpenter/hack/release-chart.sh new file mode 100755 index 0000000..16f2896 --- /dev/null +++ b/karpenter/hack/release-chart.sh @@ -0,0 +1,92 @@ +#!/usr/bin/env bash +# Build, package, and optionally push the Karpenter Helm chart. +# +# Usage: +# ./hack/release-chart.sh [options] +# +# Options: +# --version VERSION Override chart version (default: read from Chart.yaml) +# --registry URI OCI registry to push to (e.g. oci://ghcr.io/azure/aks-flex/charts) +# --output DIR Output directory for the .tgz package (default: .helm-packages) +# --push Push the chart to the OCI registry after packaging +# --help Show this help message +# +# Examples: +# # Lint and package only (local dev) +# ./hack/release-chart.sh +# +# # Package with a specific version +# ./hack/release-chart.sh --version 0.2.0 +# +# # Package and push to GHCR +# ./hack/release-chart.sh --version 0.2.0 --registry oci://ghcr.io/azure/aks-flex/charts --push + +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +CHART_DIR="${SCRIPT_DIR}/../charts/karpenter" + +# Defaults +VERSION="" +REGISTRY="" +OUTPUT_DIR="${SCRIPT_DIR}/../../.helm-packages" +PUSH=false + +usage() { + sed -n '2,/^$/s/^# \{0,1\}//p' "$0" + exit 0 +} + +while [[ $# -gt 0 ]]; do + case "$1" in + --version) VERSION="$2"; shift 2 ;; + --registry) REGISTRY="$2"; shift 2 ;; + --output) OUTPUT_DIR="$2"; shift 2 ;; + --push) PUSH=true; shift ;; + --help|-h) usage ;; + *) echo "Unknown option: $1" >&2; usage ;; + esac +done + +# --------------------------------------------------------------------------- +# Resolve chart version +# --------------------------------------------------------------------------- +if [[ -z "$VERSION" ]]; then + VERSION=$(grep '^version:' "$CHART_DIR/Chart.yaml" | awk '{print $2}') +fi +echo "Chart version: ${VERSION}" + +# --------------------------------------------------------------------------- +# Step 1: Lint +# --------------------------------------------------------------------------- +echo "==> Linting chart..." +helm lint "$CHART_DIR" + +# --------------------------------------------------------------------------- +# Step 2: Package +# --------------------------------------------------------------------------- +mkdir -p "$OUTPUT_DIR" +echo "==> Packaging chart..." +helm package "$CHART_DIR" --version "$VERSION" --destination "$OUTPUT_DIR" + +CHART_PACKAGE="${OUTPUT_DIR}/karpenter-${VERSION}.tgz" +if [[ ! -f "$CHART_PACKAGE" ]]; then + echo "Error: expected package not found: $CHART_PACKAGE" >&2 + exit 1 +fi +echo "Package created: ${CHART_PACKAGE}" + +# --------------------------------------------------------------------------- +# Step 3: Push (optional) +# --------------------------------------------------------------------------- +if [[ "$PUSH" == true ]]; then + if [[ -z "$REGISTRY" ]]; then + echo "Error: --registry is required when --push is set" >&2 + exit 1 + fi + echo "==> Pushing chart to ${REGISTRY}..." + helm push "$CHART_PACKAGE" "$REGISTRY" + echo "Chart pushed successfully." +fi + +echo "Done."