maxcloud
Deploy apps and functions to my homelab, the way you would to Cloud Run or
Firebase. maxcloud is Knative on the homelab cluster, deployed through Flux
from maxemiliang/homelab.
This repo holds:
mcloud, the command line (Go, cmd/mcloud).
@max-cloud/functions, the Node.js/TypeScript SDK for writing HTTP,
scheduled and queue functions (packages/functions, see its README).
@max-cloud/nuxt, a Nuxt module: runtimeConfig from what the platform
gives the app, and the Nuxt server as a maxcloud function
(packages/nuxt, see its README).
github.com/maxemiliang/maxcloud/functions, the same for Go (functions/,
see its README).
maxcloud-controller (controller/), which gives apps their S3
buckets and Postgres databases, keeps their secrets safe through a
cluster rebuild, and describes the platform to mcloud. See its README.
Getting started
mise use -g github:maxemiliang/maxcloud # or: go install github.com/maxemiliang/maxcloud/cmd/mcloud@latest
mcloud login # once per machine: log in, and set up cluster access
mcloud doctor # if anything's off: says what, and how to fix it
mkdir shop && cd shop && git init
mcloud init # a starter functions app (--lang go for Go)
npm install
mcloud dev # run it here: http://hello.localhost:8080/
gh repo create --source . --push --private
mcloud deploy # register it, push, follow the build and the rollout
How an app gets deployed
-
Set up the repo. mcloud init writes a starter functions app (or
adds what an existing Node.js or Go app needs), its deploy workflow and
a maxcloud.yaml: the whole app's definition, like a wrangler.toml.
-
Register the app. The first mcloud deploy does it, from
maxcloud.yaml. It adds the app's entry to the registry in the cluster,
with your login: no commit to the homelab repo. From that entry the
platform creates the app's namespace with its quota and guardrails, its
buckets and database, and a reconciler for the app's deploy/k8s. After
that, a pushed change to maxcloud.yaml reaches the entry by itself (the
controller reads it from Flux), within the limits a developer may ask
for; mcloud deploy does it too. The maxcloud controller keeps a copy of
every entry in Object Storage and puts them back after a cluster
rebuild.
-
Push to deploy. Every push to main runs the deploy workflow
(.github/workflows/run-deploy.yaml in this repo, which app repos call).
It's mcloud build --ci:
- it builds the image (the repo's Dockerfile, else Paketo buildpacks)
and pushes it to
registry.gronblom.dev/<app>/<app> as the repo
itself (GitHub's OIDC token; no secrets);
- it asks the image which functions it has and writes their manifests
to
deploy/k8s;
- it pins the image digest and commits.
Flux then deploys that commit. mcloud deploy pushes and follows all
of it until the services are ready. If Flux can't read a new repo (the
homelab's Flux GitHub App doesn't have it yet), it says so and opens the
page where you add the repo to the app, then carries on once Flux can.
Each kind of function becomes:
| Function |
Runs as |
onRequest |
a Knative service: scales to zero, private unless public: true |
onSchedule |
a CronJob |
onQueue |
a Deployment that KEDA scales from 0 on the queue's backlog |
Queues keep their jobs in the app's Postgres database: db: true in
maxcloud.yaml (mcloud says so when an app with a queue has none).
The homelab README's "Run an app" covers the platform side, and its
templates/run-app is the starting point for apps that aren't functions;
they use the same workflow without functions: true.
A Nuxt app can be a function too: @max-cloud/nuxt builds its server as
one HTTP function (and its scheduled tasks as scheduled ones), so the
deploy workflow writes its manifests (packages/nuxt/README.md).
maxcloud.yaml
app: shop
quota: {cpu: 500m, memory: 2Gi}
buckets: [uploads, assets:public]
db: {size: 2Gi} # or db: true
domains: # hostnames of your own, within what an admin allowed
- {host: shop.gronblom.dev, service: web, proxied: true}
# source: worker # build the app from this folder, not the repo's root
# deploy: direct # mcloud deploy builds and applies from here, no git
It owns what it declares: a push (or mcloud deploy) applies it, and
mcloud apps set refuses to change those fields, since the next push would
undo them. What it leaves out stays as it is. Changing deploy: switches
the app between GitOps and direct on the next mcloud deploy, which asks
first.
source: is the folder the app lives in, relative to maxcloud.yaml
(default: the folder maxcloud.yaml is in). Use it when the repo's root is
something else, e.g. a Go CLI at the root with the web app in worker/:
buildpacks would otherwise detect Go. Everything that builds or runs the
app uses it: mcloud build, direct deploys (local and --build cluster,
which streams only that folder), the deploy workflow (unless its context
input says otherwise), mcloud generate and mcloud dev. A Dockerfile and
project.toml are looked for there. It has to be a directory inside the
repo, without ... The manifests stay where they are (deploy/k8s at the
root).
Local development
mcloud dev runs the whole app on your machine, with what it gets on
maxcloud:
- every HTTP function at
http://<name>.localhost:8080/ (a list at
http://localhost:8080/), the queue workers, and scheduled functions on
POST http://localhost:8080/_run/<name>;
DATABASE_URL: a local Postgres in Docker (kept between runs; the
default when maxcloud.yaml has db:), or a branch of the app's
database through a port-forward (--db branch:NAME), with the pending
migrations applied;
MAXCLOUD_APP, and secrets from .env (keep it out of git), or the
app's own (--secrets remote); the app's real buckets only with
--buckets.
TypeScript runs from source and restarts on changes; a Nuxt app (a
nuxt.config) runs nuxt dev on the port instead. To poke the deployed
app: mcloud call api /health sends a request to any function, private
ones too, and mcloud run nightly runs a scheduled function now.
Direct deploys
For quick functions and experiments there's no need for git at all:
mcloud init --deploy direct # a starter with deploy: direct in maxcloud.yaml
mcloud deploy # register, build here, push, apply, wait until ready
mcloud deploy builds the working tree, uncommitted changes included, with
mcloud build, the same build CI runs: with the Dockerfile, or with
buildpacks (pack, or pack in a container if only docker is installed).
Without docker, an app without a Dockerfile builds in the cluster instead
(--build cluster, chosen by itself): a buildpacks pod in the app's
namespace, with the source streamed to it. The image goes to the maxcloud
registry with a token of the app's push ServiceAccount that lasts an hour
(whoever may manage the app's namespace may push its images); mcloud is
docker's credential helper while it builds, so there's no docker login and
nothing is written to ~/.docker. Then it asks the image for its
functions and applies their manifests. Functions you removed are deleted;
the database never is.
What you give up: nothing is in git, so a cluster rebuild doesn't bring a
direct app back (run mcloud deploy again), and there's no history to
revert to. Use GitOps (the default) for anything that matters: remove
deploy: direct from maxcloud.yaml and mcloud deploy.
image: in maxcloud.yaml pushes somewhere else instead (log in to it
yourself); the cluster then needs to be able to pull from there.
Buckets
An app gets S3 buckets (in the homelab's UpCloud Object Storage, the way a
Worker gets R2) from buckets: in maxcloud.yaml. The
controller turns each into a bucket
mc-shop--uploads and puts the app's credentials in the Secret
maxcloud-s3, which every function gets as environment variables. The
SDKs' bucketName("uploads") gives the full name; so does the variable
MAXCLOUD_BUCKET_UPLOADS (for frameworks configured by environment; a pod
started before the bucket existed doesn't have it). Removing a bucket from
the list keeps it and its objects.
Public buckets
Add :public to a bucket's name in the list, and anyone can read its
objects (not list them) at a hostname of its own:
buckets: [uploads, assets:public]
https://assets--shop.gronblom.dev/logo.png # object logo.png in mc-shop--assets
- It's for files you'd put on a CDN: images, downloads, built front-end
assets. Only GET and HEAD go through; writing still takes the app's
credentials (from a function, or a presigned URL it hands out).
- Cloudflare serves the hostname and caches what it serves, by file
extension and the object's
Cache-Control. A changed object can be
served stale until that runs out, so give changed files new keys
(app.3f2a91.js), or set Cache-Control when you upload.
- The same objects are also readable straight from Object Storage, at
<AWS_ENDPOINT_URL_S3>/mc-shop--assets/logo.png (not cached).
kubectl get maxcloud -n shop shows each bucket's URL.
- Removing
:public makes the bucket private again: its hostname and the
public read go, its objects stay.
Custom domains
A function (or any Knative service of the app) can have hostnames besides
<service>-<app>.gronblom.dev, from domains: in maxcloud.yaml:
domains:
- {host: paja.lankkupelit.fi, service: web, proxied: true}
- An admin allows them first: the homelab repo's
infrastructure/configs/run/domains.yaml lists the hostnames each app
may have. Admission refuses a registry entry (from mcloud deploy, or a
pushed maxcloud.yaml) with any other, and refuses any route in the
app's namespace with a hostname that isn't the app's.
- Zones the cluster has certificates for: one level below
gronblom.dev, lab.gronblom.dev or lankkupelit.fi. external-dns
makes the DNS record. proxied: true puts it behind Cloudflare (WAF,
caching, Cloudflare Access), not for *.lab.gronblom.dev.
- The service can stay private. The hostname reaches its revisions
directly, scale-to-zero included, and the app sees the
Host the
client sent (paja.lankkupelit.fi, not a cluster-local name).
kubectl get maxcloud -n <app> shows each Domain. One that isn't Ready
says why: not on the admin's list, no certificate covers it, the
service isn't deployed yet, or another route already has the hostname
(it takes over once that one is gone).
- Removing a domain from the list removes its route and DNS record.
Databases
An app gets a Postgres database (the way a Worker gets D1, but Postgres)
from db: in maxcloud.yaml. The controller runs it with CloudNativePG,
backs it up continuously to the bucket mc-shop--db, and restores it from
there by itself if its cluster is lost. Every function gets
DATABASE_URL. An app set up before this may have a database.yaml (what
mcloud used to write for queue functions, with no backups): it keeps
working, but the controller won't take it over. Moving it is a dump and
restore (homelab docs/runbooks/move-database-to-registry.md).
mcloud db migrate # apply migrations/*.sql (see Migrations)
mcloud db psql # psql in it (--branch NAME for a branch)
mcloud db proxy # on localhost, with a DATABASE_URL to use
mcloud db restore --at 15m # as it was 15 minutes ago, in place
mcloud db branch create try # a throwaway copy (--at for a point in time)
restore and branch restore from the backups, so they reach back as far
as the retention (7 days by default).
Migrations
SQL migrations go in migrations/ in the app's folder (next to
maxcloud.yaml, or in source:), one file per
change, named NUMBER_DESCRIPTION.sql with numbers of one width:
migrations/0001_init.sql
migrations/0002_add_orders.sql
Each file is applied once, in name order, in its own transaction together
with its row in the table maxcloud_migrations (name, SHA-256 checksum,
time applied). A file that fails leaves nothing behind. Don't put BEGIN
or COMMIT in them, and statements that can't run in a transaction
(CREATE INDEX CONCURRENTLY) can't be migrations. A file edited after it
was applied stops every run until you put the change in a new file
instead. A file that sorts before one already applied (from a merged
branch) is still applied. Only one run applies at a time (a Postgres
advisory lock); the others wait.
They're applied in three places, all the same way and to the same table:
- On deploy, by the app itself. Before it runs a function, the SDK's
runtime applies the pending migrations to
DATABASE_URL. A new Knative
revision takes no traffic until it has, so new code never serves an old
schema, and a migration that fails stops the pod while the old revision
keeps serving. TypeScript apps read ./migrations (buildpacks keep it
in the image; with a Dockerfile, copy it in next to package.json); Go
apps embed it (//go:embed migrations, functions.Migrations(...)).
When the image doesn't have every file in migrations/, the deploy
warns. Keep them quick: every starting pod waits for them.
mcloud dev applies them to its database before it starts the app
(--migrate=false doesn't).
mcloud db migrate, by hand: to the deployed database through a
port-forward as the app's user (it asks first), a branch (--branch),
or any Postgres (--database-url). --dry-run lists what's applied and
what's pending.
mcloud db migrate --dry-run # applied, pending, changed
mcloud db migrate --branch try # try them on a branch first
Coming from D1: the files wrangler d1 migrations create writes
(0000_init.sql) keep their names, but their SQL has to become Postgres
(INTEGER PRIMARY KEY AUTOINCREMENT is bigserial PRIMARY KEY, and so
on).
Secrets and key-value storage
mcloud secrets set NAME stores an app's secret, like wrangler secret put. Every app keeps them in the Secret app-secrets in its namespace:
nothing to commit and no keys to hold, and the app's HTTP and queue
functions restart so they have it right away. Every function gets the
secrets as environment variables; the SDKs' secret("NAME") reads one. The
maxcloud controller keeps an encrypted copy and puts it back after a
cluster rebuild. A GitOps app that still has
deploy/k8s/secrets.sops.yaml (how secrets were kept before) moves off it
with mcloud secrets migrate, which takes over the Secret Flux already
decrypted.
For KV, the SDKs' kv stores JSON values in the app's database.
Logging in
mcloud (and kubectl) log in to the cluster through the homelab's Authelia,
and act with what your groups allow. mcloud login is all the setup:
- it reads the platform's discovery document
(
https://maxcloud.gronblom.dev/.well-known/maxcloud.json,
$MCLOUD_PLATFORM_URL): the cluster's address and CA, and the oldest
mcloud that works with it (older ones warn);
- it logs in: a device code you approve in any browser (
--browser: this
machine's);
- it writes mcloud's kubeconfig,
~/.config/mcloud/kubeconfig, which
mcloud uses from then on. For kubectl: set -gx KUBECONFIG ~/.config/mcloud/kubeconfig.
The kubeconfig runs mcloud credential for a token: the ID token lasts an
hour and is refreshed by itself; logging in again is needed after 12 hours
or mcloud logout. mcloud picks its kubeconfig from --kubeconfig, else
$MCLOUD_KUBECONFIG, else its own, else $KUBECONFIG. If Authelia or the
cluster is down, get an admin kubeconfig with upctl kubernetes config
(the homelab's break-glass) and pass it with --kubeconfig.
GitOps deploys follow the build on GitHub with $GH_TOKEN, else your
GitHub CLI login (gh auth login).
Commands
mcloud login # set up and log in (mcloud doctor checks it)
mcloud init # in a new repo: a functions app (--lang go for Go)
mcloud dev # run it here
mcloud deploy # register, push, follow build, Flux and rollout
mcloud status # deploy, services and URLs, workloads, databases, problems
mcloud logs -f # logs of every pod; mcloud logs shop web -f for one service
mcloud call api /health # a request to a function, private ones too
mcloud run nightly # a scheduled function, now
mcloud secrets set STRIPE_KEY # asks for the value; restarts the functions
mcloud secrets list # names only
mcloud secrets unset STRIPE_KEY
mcloud db migrate # apply migrations/*.sql (--dry-run lists them)
mcloud db psql # and db proxy, restore, recover, branch
mcloud queue ls # jobs per queue and state, latest failed ones
mcloud queue retry 41 42 # run failed jobs again (or --failed)
mcloud apps list # registered apps, did the last deploy apply
mcloud apps set shop --deploy direct # by hand: what maxcloud.yaml leaves out
mcloud apps set shop --domains shop.gronblom.dev:web:proxied
mcloud apps delete shop # remove it, namespace and data included
mcloud build # build and push the image (what deploys run)
mcloud generate # what deploy/k8s would become (asks the built app)
On a terminal mcloud shows its progress with spinners and colors and asks
with interactive prompts (mcloud init asks for the name, language and
deploy mode). In pipes, in CI, with NO_COLOR or MCLOUD_PLAIN=1, or with
--plain, it prints plain lines instead.
Run inside an app's repo, commands take the app from its maxcloud.yaml
(or $MCLOUD_APP). apps create, set and delete change the registry
in the cluster (no commit anywhere), and show the change and ask first.
Pass --dry-run to only see it, or --yes to skip the question. delete
asks you to type the app's name. Apps registered in the homelab repo itself
(the platform's own, like run-demo) change there instead.
Releasing
release-please (.github/workflows/release-please.yaml) keeps one release
PR open on main. It holds the next version of each part that has
changes, worked out from the conventional commits under that part's
directory (feat a minor version, fix a patch; while below 1.0.0 a
breaking change is a minor too), with changelogs and the versions in files.
Merging the PR releases those parts:
| Part |
Directory |
Tag |
Released by |
| mcloud |
everything else |
v0.12.0 |
release.yaml: GoReleaser adds the binaries to the GitHub release |
| controller |
controller/ |
controller/v0.11.0 |
release-controller.yaml: the image (the PR bumps newTag in config/kustomization.yaml) |
| Go SDK |
functions/ |
functions/v0.5.0 |
the tag (Go modules) |
@max-cloud/functions |
packages/functions/ |
functions-js/v0.5.0 |
release-npm.yaml (trusted publishing, no token) |
@max-cloud/nuxt |
packages/nuxt/ |
nuxt-js/v0.1.0 |
release-npm.yaml, after @max-cloud/functions |
- Order: the npm SDK goes before
@max-cloud/nuxt (it depends on it)
and before mcloud: run-deploy.yaml installs the newest mcloud, and
mcloud init writes the newest SDK into new apps. The PR also bumps
sdkFallback (internal/cli/sdkversion.go, for when npm can't be
asked) with each @max-cloud/functions release.
- Merge the release PR as it is, and other PRs with a merge commit or a
squash whose message is a conventional commit: the message decides the
versions and the changelog.
- A version of your choice:
Release-As: 1.0.0 in a commit message's
footer, for the parts that commit touches.
- Settings it needs: "Allow GitHub Actions to create and approve pull
requests" (Settings, Actions, General). On npmjs.com, each package's
trusted publisher is the workflow
release-please.yaml (npm checks the
calling workflow). The release PR is opened with GITHUB_TOKEN, so CI
doesn't run on it by itself.
- By hand, if release-please is stuck: pushing a
v* or
controller/v* tag still runs its workflow. npm packages only publish
through release-please.
Config: release-please-config.json, and .release-please-manifest.json
with the last released versions.
Development
go test ./...
go run ./cmd/mcloud --help
npm ci
npm run build --workspaces # @max-cloud/nuxt uses the functions SDK's build
# The queue tests need a Postgres; without TEST_DATABASE_URL they're skipped.
docker run -d --rm --name pg -p 127.0.0.1:55432:5432 -e POSTGRES_PASSWORD=test postgres:18
set -x TEST_DATABASE_URL postgres://postgres:test@127.0.0.1:55432/postgres
npm test --workspaces