Repository navigation
ci: add k8s stack PR preview via labeled deploy + Cloudflare Tunnel #205
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
Merged
Merged
Changes from 103 commits
Commits
Show all changes
115 commits
Select commit
Hold shift + click to select a range
1ef138b
ci: add k8s stack PR preview via labeled deploy + Cloudflare Tunnel
aktech 72dab46
ci: temporarily trigger smoketest on this PR for iteration
aktech 52d9f86
fix: move cloudflared --no-autoupdate flag before the run subcommand
aktech 2818f48
ci: use single-level github.fyi hostnames for the preview tunnel
aktech fedb68f
ci: drop temporary pull_request trigger from smoketest workflow
aktech b22f710
fix: fetch chart dependencies before helm install in preview workflow
aktech 1c7618a
ci: post smoketest URL as a PR comment via optional pr_number input
aktech 0a75683
fix: register the jupyterhub chart repo before dependency build
aktech 1eec127
fix: opt preview namespace into nic-operator management, add failure …
aktech 17f723b
fix: disable nebariapp for preview deploy, hub crash-loops otherwise
aktech a43dfb7
fix: set jupyterhub.custom.external-url to the tunnel hostname
aktech 57f1b00
Merge branch 'main' into add-k8s-preview-workflow
aktech a146616
ci: show deploy/expiry timestamps in preview PR comments
aktech c0ca336
Merge branch 'add-k8s-preview-workflow' of github.com:nebari-dev/neba…
aktech 65651b4
fix: make tunnel creation idempotent, retries reuse the run_id
aktech 774c0d6
Merge branch 'main' into add-k8s-preview-workflow
aktech 74d3d49
ci: source preview domain from a repo Variable instead of hardcoding it
aktech 71817f5
ci: switch preview domain to openteams.app, flatten per-PR hostname
aktech 4919f73
ci: migrate k8s-preview to action-nebari-sandbox v3 (kind, not k3d)
aktech c849c85
ci: bump kind CLI to v0.33.0 in k8s-preview for containerd config v4
aktech cec54be
ci: always dump hub logs in k8s-preview to diagnose jhub-apps startup
aktech f577f6a
ci: smoke-test jhub-apps directly in k8s-preview before opening the t…
aktech 10fa371
ci: capture hub logs after the tunnel closes, not just at startup
aktech 26f7b65
ci: cache docker build, enable Keycloak auth, always start tmate debug
aktech d15efbc
ci: wait for the operator's Keycloak secret instead of blocking helm …
aktech 2b6f1a9
ci: cover the keycloak namespace in the deploy-failure debug dump
aktech 494eee4
ci: label the preview namespace so nic-operator actually reconciles it
aktech be49af8
docs: update k8s-preview comments to match the current auth setup
aktech daba573
ci: diagnose why the operator's issuer-url secret key comes back empty
aktech 47e6606
ci: wait for issuer-url specifically, not just the secret's existence
aktech 612f07d
ci: retry the hub restart until it actually picks up the operator secret
aktech b0c12f0
ci: fix the actual bug -- hub was mounting the wrong secret name
aktech 0d041ce
ci: use --set-json for extraVolumes, not indexed --set
aktech 0b2e76e
ci: expose Keycloak's own login page through a second tunnel route
aktech f925800
ci: point Keycloak's own hostname at the public route instead of rewr…
aktech c85f9b4
ci: fix Keycloak hostname via GitOps instead of a direct kubectl patch
aktech 78b0086
ci: create a simple reviewer/admin test user in Keycloak
aktech 6c96912
ci: route hub's Keycloak backchannel calls in-cluster, not through Ac…
aktech d5c27c7
fix: point jhub-apps' hub-API client at localhost, not the hub Service
aktech fdec180
fix: the env-dict override for JUPYTERHUB_API_URL doesn't survive spawn
aktech 3ca96ef
ci: pre-fill email/name on the reviewer test user
aktech 928adec
fix: the JUPYTERHUB_API_URL rewrite for jhub-apps never actually ran
aktech ba3fd45
ci: bound the tunnel step to 20min instead of the job timeout
aktech d284543
ci: add a GitHub Deployment for the preview, not just a comment
aktech 044a28e
ci: fix the tunnel step's exit-124 handling under bash -e
aktech 7371cc1
ci: replace the preview comment's prose with a Vercel-style table
aktech f2896bc
ci: fix the deployment table's Project column
aktech 7b1df6a
ci: show a live-updating expiry time instead of a static UTC string
aktech 26bbfe1
ci: update the preview comment again when it expires
aktech f6c9f4a
ci: extend the preview's lifetime in place via an extend-preview label
aktech 6bdfafb
Merge branch 'main' into add-k8s-preview-workflow
aktech 3693d08
ci: don't let extend-preview cancel the run it's meant to extend
aktech 9ab529d
refactor: add a testable Python foundation for k8s-preview.yaml's CI …
aktech 748e6d5
refactor: move Cloudflare Tunnel/DNS logic into scripts/preview/cloud…
aktech 6844087
refactor: move Keycloak + k8s readiness logic into scripts/preview/
aktech 037c993
refactor: move remaining GitHub API/comment logic into scripts/preview/
aktech 9305abe
refactor: move the extend-preview tunnel loop into scripts/preview/tu…
aktech df6d007
ci: dump ArgoCD Application status on deploy failure
aktech 148323a
ci: also dump cloudnative-pg's own ArgoCD Application status on failure
aktech 30d7f6a
fix: update the PR comment's expiry when extend-preview actually exte…
aktech 4a3dec5
fix: the comment-timer fix computed the new expiry from the wrong clock
aktech f1809b0
ci: trim every workflow comment and cap each action at ~4 lines
aktech f492690
test: remove 4 ceremonial CLI passthrough tests
aktech 9256f2f
ci: remove the standalone tunnel smoketest workflow
aktech ed70518
ci: cut contrastive "X, not Y" padding from workflow comments
aktech 2f76800
fix: "for 20 more minutes" read as cumulative, it isn't
aktech f2661e7
build: bump actions/checkout to v7.0.1, helm/kind-action to v1.15.0
aktech 0d9d3e6
docs: trim redundant '(not cumulative)' aside in k8s-preview.yaml
aktech d3d3a2d
docs: move k8s-preview.yaml's comments into a PR Preview Environments…
aktech 7ab009d
docs: lead with a How it works diagram, drop generic debugging sectio…
aktech 23f8fa5
docs: cut implementation-detail aside from the intro paragraph
aktech 269924d
feat: point preview deploys at the PR's own JupyterLab image build
aktech e50d1d3
docs: explain why the namespace label is manual here (ArgoCD does it …
aktech 644529b
docs: frame the manual namespace label as a known gap, not a permanen…
aktech eec0b14
docs: explain why kind/kindnet can't hairpin a pod back to its own Se…
aktech 4603e0d
docs: trim the hairpin-NAT comment down, no fluff
aktech 1ef4c3f
docs: shrink the hairpin-NAT comment to a tweet, fix hub_connect_url …
aktech c751720
fix: disable prePuller.hook for preview deploys, it blocks install on…
aktech bd9dfc2
docs: state the exec-time-rewrite reason directly instead of via what…
aktech 35ab2ae
docs: attribute the jhub-apps hub-API timeout to the hub NetworkPolic…
aktech 11b307f
fix: put prePuller override at the jupyterhub level, it had swallowed…
aktech 85b2fc3
fix: label the hub pod network-access-hub so jhub-apps can reach the …
aktech 55980ee
refactor: use gh for the workflow's GitHub API steps, drop unused tun…
aktech 4044005
test: drop tests that pin request shapes and constants instead of beh…
aktech 839d712
test: drop the create-tunnel success test, it only pinned the request…
aktech 91522fd
test: remove the scripts/preview unit tests and their injection seams
aktech 7606a08
chore: drop unread tunnel_id output, gha path param, and unused PREVI…
aktech 42ad288
feat: show reviewer credentials in the PR comment and docs; tunnel.py…
aktech 8e3e646
docs: one-line why on the Keycloak hostname repoint
aktech d98ff4d
chore: drop the debug-dump script and its failure step, tmate covers it
aktech 8d17fa9
chore: drop tmate and hub log-dump steps
aktech 0c57d21
Merge branch 'main' into add-k8s-preview-workflow
aktech e1232ea
feat: keep main's pinned image as a second profile choice in previews
aktech 00f666a
feat: deploying status comment with CI run link, Keycloak admin creds…
aktech c1b6816
refactor: tunnel.py talks to the GitHub API directly, run() split int…
aktech 6e30d58
docs: deploying comment says ready in a few minutes
aktech 3487d66
feat: remove the deploy-preview label when the preview ends
aktech cbc4cfd
fix: cascade-delete the tunnel and warn instead of silently swallowin…
aktech 3c8e1bb
chore: rename the workflow to Deploy Preview
aktech 6512f5e
chore: declare pr_image.py's dependency inline (PEP 723), run it with…
aktech 101a7cb
fix: pr_image.py puts the repo root on sys.path itself, pipx run drop…
aktech bd6dcf2
docs: clarify the preview Keycloak reviewer account, drop implementat…
aktech 8a4b8df
fix: pass the Keycloak admin password via env, post a failed-preview …
aktech 5a57bb4
ci: skip preview deploys for fork PRs, drop the fork plumbing
aktech bd6e30d
docs: state the fork-PR skip with GitHub's own wording on secrets
aktech 780e76c
docs: plain wording for why fork PRs are skipped
aktech db04576
ci: keep the pinned JupyterLab image when the PR has no pr-N tag
aktech a84bfd3
ci: preview profiles sized for the 4 vCPU runner, log resource summary
aktech 699d8c7
ci: preview profile limits match their descriptions
aktech 518c347
ci: preview profile limits and guarantees in the same ballpark
aktech 9c66433
ci: let a newer preview run take over the comment and label
aktech 6b300cf
ci: preview profiles get their image selector from the single pin
aktech 081c220
ci: preview profiles get their image selector from the single pin
aktech 992d8fe
Merge branch 'main' into add-k8s-preview-workflow
aktech 6cdc581
ci: keep preview port-forwards alive, stop label events cancelling th…
aktech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| --- | ||
| title: PR Preview Environments | ||
| description: How the k8s-preview CI workflow deploys and exposes an ephemeral per-PR environment. | ||
| --- | ||
|
|
||
| The `k8s-preview` GitHub Actions workflow | ||
| (`.github/workflows/k8s-preview.yaml`) deploys this PR's chart into an | ||
| ephemeral kind cluster running the full Nebari platform stack (Keycloak + | ||
| Nebari Operator + Envoy Gateway, via `nebari-dev/action-nebari-sandbox`), | ||
| then exposes it through a per-PR Cloudflare Tunnel behind Cloudflare Access | ||
| (GitHub SSO). | ||
|
|
||
| ## How it works | ||
|
|
||
| ```mermaid | ||
| flowchart TB | ||
| Label(["deploy-preview label added"]) --> Runner | ||
| subgraph Runner["GitHub Actions runner"] | ||
| Build["Build hub image\nfrom this PR"] --> Kind["kind cluster +\nKeycloak, Operator, Envoy Gateway"] | ||
| Kind --> Deploy["helm install\nthis PR's chart"] | ||
| Deploy --> PF["port-forward hub + Keycloak\nto runner localhost"] | ||
| PF --> Tunnel["cloudflared tunnel"] | ||
| end | ||
| Tunnel --> DNS["Cloudflare DNS record\npr-<n>-....<domain>"] | ||
| DNS --> Access["Cloudflare Access\n(GitHub SSO gate)"] | ||
| Runner -. posts .-> Comment["PR comment +\nGitHub Deployment"] | ||
| Reviewer(["Reviewer's browser"]) -->|opens the link| Access --> Tunnel | ||
| ``` | ||
|
|
||
| 1. Labeling the PR `deploy-preview` starts the `deploy-preview` job. | ||
| 2. It builds the hub image from this PR and boots a kind cluster running | ||
| Keycloak, the Nebari Operator, and Envoy Gateway | ||
| (`nebari-dev/action-nebari-sandbox`), then side-loads the built image | ||
| into it. | ||
| 3. For a non-fork PR, it points the chart's JupyterLab image at this PR's | ||
| own build (see "JupyterLab image" below), then installs the chart with | ||
| `helm upgrade --install`, patches Keycloak's own hostname to match the | ||
| public route, and waits for the Operator to finish provisioning the | ||
| OIDC client. | ||
| 4. It port-forwards the hub and Keycloak services from the cluster to the | ||
| runner's `localhost`, starts a `cloudflared` tunnel mapping the public | ||
| preview hostnames to those local ports, and points a Cloudflare DNS | ||
| record at the tunnel. | ||
| 5. It posts the preview URL as a GitHub Deployment and a PR comment. | ||
| 6. A reviewer opening the link authenticates through Cloudflare Access | ||
| (GitHub SSO) before any request reaches the tunnel. Past Access, traffic | ||
| flows tunnel -> runner port-forward -> cluster service -> pod, and the | ||
| hub runs its own OAuth flow against Keycloak so it knows who signed in. | ||
| 7. After 20 minutes (or on `extend-preview`/label removal), the tunnel | ||
| closes, the DNS record and tunnel are deleted, and the runner (with its | ||
| kind cluster) is torn down when the job ends. | ||
|
|
||
| ## Triggering it | ||
|
|
||
| Add the `deploy-preview` label to a PR. That label gates who can trigger a | ||
| deploy (GitHub label permissions), so a labeled fork PR still runs, using | ||
| that fork's own code. Removing the label runs the `cleanup-preview` job, | ||
| which cancels any in-flight deploy for that PR, marks the GitHub deployment | ||
| inactive, and posts a "stopped" comment. | ||
|
|
||
| ## Lifetime and the `extend-preview` label | ||
|
|
||
| A preview is live for 20 minutes by default (the tunnel step's own | ||
| timeout, kept under the job's 90-minute timeout so it self-ends cleanly | ||
| instead of GitHub reporting a false "cancelled" run). Adding the | ||
| `extend-preview` label resets the deadline to 20 minutes from that moment; | ||
| it can be added any number of times, but each add is a reset, not an | ||
| addition on top of what's left. The label is polled from inside the running | ||
| tunnel step (`scripts/preview/tunnel.py`), not re-triggered as a new | ||
| workflow run, so extending is excluded from the workflow's own | ||
| `cancel-in-progress` concurrency group. | ||
|
|
||
| ## Hostnames and TLS | ||
|
|
||
| Preview hostnames stay single-level | ||
| (`pr-<number>-data-science-pack.<domain>`): the free Cloudflare Universal | ||
| SSL certificate on the preview zone only covers the zone itself plus one | ||
| wildcard level. | ||
|
|
||
| ## Authentication | ||
|
|
||
| `nebariapp.enabled=true` routes login through a real Operator-provisioned | ||
| Keycloak client, the same auth path production deployments use. The preview | ||
| Keycloak is a fresh realm with no users and no link to any external identity | ||
| provider, so the workflow seeds one account, `reviewer` / `admin`, for the | ||
| hub login. Cloudflare Access (GitHub sign-in) in front of the tunnel is the | ||
| actual security boundary, so a simple known password for that account is | ||
| acceptable. | ||
|
|
||
| ## JupyterLab image | ||
|
|
||
| The hub image is built and side-loaded locally by this workflow, but the | ||
| JupyterLab image users actually spawn is built separately by | ||
| `build-images.yaml` (multi-arch, pushed to ghcr.io/quay.io on the same PR | ||
| trigger). For a non-fork PR, `scripts/preview/pr_image.py` rewrites | ||
| `values.yaml`'s singleuser and per-profile image refs, in the ephemeral | ||
| checkout only, to that PR's `pr-<number>` tag before the chart deploys. | ||
|
|
||
| Fork PRs are skipped: `build-images.yaml` never pushes images for a fork | ||
| (no credentials to do so), so the preview keeps the chart's default pinned | ||
| JupyterLab image instead of pointing at a tag that doesn't exist. | ||
|
|
||
| The two builds aren't ordered against each other. A pod only pulls the | ||
| image when a reviewer actually spawns a server, by which point | ||
| `build-images.yaml` has usually finished; if not, kubelet retries the pull | ||
| automatically once the tag exists; no action is needed either way. | ||
|
|
||
| ## Tunnel and GitHub visibility | ||
|
|
||
| - **Named Tunnel**: each PR gets its own named Cloudflare Tunnel sitting | ||
| behind Access, rather than an anonymous quick tunnel. | ||
| - **GitHub Deployment**: a Vercel-style deployment box is created via the | ||
| GitHub Deployments API, separate from the sticky PR comment (which | ||
| doesn't move once posted). `required_contexts: []` keeps this preview | ||
| link from gating on unrelated checks like lint or test. | ||
| - **Comment timestamps**: rendered with GitHub's `<relative-time>` web | ||
| component, so "Expires in 12 minutes" keeps ticking client-side with no | ||
| manual timezone math or re-editing needed. | ||
| - **On expiry**: the deployment is marked inactive and, if a "ready" | ||
| comment was posted, it's re-rendered to the expired state. | ||
| - **On failure**: if the deploy dies before a "ready" comment exists, the | ||
| "deploying" comment is re-rendered to a failed state linking the run, | ||
| instead of pointing at a failed run as "Deploying" forever. | ||
|
|
||
| ## One-time Cloudflare setup | ||
|
|
||
| Configured once in the Cloudflare Zero Trust dashboard for this repository: | ||
|
|
||
| - Repository variable `PREVIEW_DOMAIN`: the zone name (e.g. | ||
| `openteams.app`). | ||
| - A Cloudflare Access application for `*.<PREVIEW_DOMAIN>`, GitHub as the | ||
| identity provider, and a policy scoped to this org. | ||
| - Secret `CLOUDFLARE_TUNNEL_ACCOUNT_ID`: that Cloudflare account's ID. | ||
| - Secret `CLOUDFLARE_TUNNEL_API_TOKEN`: `Tunnel:Edit` + `Zone:Read` + | ||
| `DNS:Edit`, scoped to the `PREVIEW_DOMAIN` zone/account only. This is | ||
| separate from `CLOUDFLARE_API_TOKEN`, which belongs to the Pages account | ||
| used by `docs.yml`. | ||
|
|
||
| ## Security model | ||
|
|
||
| - **Public exposure is gated before it reaches the cluster**: Cloudflare | ||
| Access sits in front of the tunnel and requires a GitHub SSO login plus | ||
| an org policy match before any request reaches `cloudflared`. Nothing in | ||
| the preview cluster is reachable without passing that gate. | ||
| - **kind shares the runner's Docker daemon**: a container escaping its pod | ||
| gets host-level Docker access on that ephemeral runner only, not on any | ||
| shared or production infrastructure, and the runner is destroyed with | ||
| the job. | ||
| - **A labeled fork PR deploys the fork's own code**: it gets the same | ||
| Cloudflare Access gate, but the hub image built and running is | ||
| unreviewed. The PR comment flags this on every fork deploy. | ||
| - **Token scope**: `GITHUB_TOKEN` is limited to `contents:read`, | ||
| `pull-requests:write`, `issues:write`, `deployments:write` for this job. | ||
| The Cloudflare API token can only edit Tunnels and DNS and read the zone, | ||
| scoped to the single `PREVIEW_DOMAIN` zone, not account-wide. | ||
| - **Nothing outlives the run**: the tunnel, its DNS record, the Keycloak | ||
| reviewer account, and the kind cluster all exist only for the job's | ||
| lifetime, or until the preview expires or is stopped early. | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.