# Deploy a template with the catalog command

> Verify the release signature, prepare the cluster, and render and apply a template to complete the deployment.

After you verify the release signature, use the `catalog` command to render and apply a template. Then run the smoke check and your own tests to verify the deployment.

## Before you begin

- Install Go, `git`, `gh`, `cosign`, `kubectl` and `sha256sum` (on macOS, from GNU coreutils).
- Install every component listed in the template's `requires`, all at the same version.
- Use a CNI that enforces NetworkPolicy. The installer isolates each project's Namespace. Templates don't restrict outbound traffic.
- If the template requires a domain, prepare a cert-manager ClusterIssuer first.
- Install each template only once per Namespace. Object names within a template are fixed.

### Check the cluster

Templates don't specify a StorageClass, so volumes use the default. Without a default StorageClass, PersistentVolumeClaims remain `Pending`. List the StorageClasses:

```sh
kubectl get storageclass
```

One line has `(default)` after its name, such as `local-path (default)`. If none does, set a StorageClass as the default before you continue.

### Get the catalog command

Replace `vX.Y.Z` with the release tag you plan to deploy, check out its source, and run the `catalog` command from that checkout. Don't run unverified downloads.

```sh
tag=vX.Y.Z
git clone --depth 1 --branch "$tag" https://github.com/zeabur-oss/catalog
cd catalog
cargo run -p zeabur-catalog -- check
```

`check` prints nothing and exits with status 0 when the catalog has no problems.

### Verify the release

Download `bundle.tar.gz`, `SHA256SUMS` and `SHA256SUMS.sigstore.json` from the same release tag into a new private directory. Then verify the signature of `SHA256SUMS` against the workflow, the repository and the tag, and check the bundle against it:

```sh
dir=$(mktemp -d)
gh release download "$tag" --repo zeabur-oss/catalog --dir "$dir" \
  --pattern bundle.tar.gz --pattern SHA256SUMS --pattern SHA256SUMS.sigstore.json
(cd "$dir" &&
  cosign verify-blob SHA256SUMS --bundle SHA256SUMS.sigstore.json \
    --certificate-identity "https://github.com/zeabur-oss/catalog/.github/workflows/release.yml@refs/tags/$tag" \
    --certificate-oidc-issuer https://token.actions.githubusercontent.com &&
  sha256sum --check --ignore-missing SHA256SUMS)
```

`cosign` prints `Verified OK`, and `sha256sum` prints `bundle.tar.gz: OK`. Stop if either prints anything else.

Checking only the issuer or the workflow accepts signatures from other workflows or other zeabur-oss repositories. Extract the bundle only after signature verification succeeds, and reject absolute paths, `..`, and links.

### Render and apply the template

Read the license, inputs, `requires`, and security risks in `templates/<id>/template.yaml` before you render the objects:

```sh
go run ./tools/cmd/catalog template render -namespace app \
  -domain app.example.com -cluster-issuer letsencrypt -values inputs.json <id> > plan.yaml
```

`inputs.json` is a JSON object that provides the inputs the template requires as string values. Prepare object storage buckets and credentials beforehand.

Generate the template's passwords and keys as Secrets, then apply the objects:

```sh
umask 077
go run ./tools/cmd/catalog template inputs -namespace app <id> > secrets.json
kubectl create namespace app
kubectl -n app apply -f secrets.json
kubectl -n app apply -f plan.yaml
```

Don't commit `secrets.json` to version control. Each run of `inputs` generates new values. Run it only once per deployment, and reuse the existing Secrets during upgrades.

Check that every pod starts:

```sh
kubectl -n app get pods
```

Within a few minutes, each pod shows `Running` in `STATUS`, and both numbers in `READY` match, such as `1/1`. A pod of a finished Job shows `Completed`. Then run the checks listed under `smoke` in `template.yaml` and your own tests. For a template with a domain:

```sh
curl -sI https://app.example.com | head -n 1
```

The first line is the status line, such as `HTTP/2 200`.

### Upgrade and roll back

Record the version, Namespace, image references, inputs, and Secret names for each deployment. Before you upgrade, compare the rendered objects, read all upgrade notes between the current and target versions, complete a backup, and rehearse the upgrade on a copy.

To roll back the application, check database and storage compatibility first, then deploy the objects from a previously verified release. Database downgrades usually require a restore. Store backups outside the application's Namespace.

## Generated inputs

Generated inputs include `secret` (`name` and `key`) and a `validation.pattern` that must match the entire generated value.

| Field | Description |
| - | - |
| `generate.type` | `random`. |
| `generate.bytes` | Number of random bytes before encoding, from 12 to 64. Required. |
| `generate.encoding` | `base64url` (without padding) or lowercase `hex`. Required. |

User-provided inputs include `required` and don't include generation fields. Encoding 12 bytes as `base64url` produces 16 characters; encoding 16 or 32 bytes as `hex` produces 32 or 64 characters, respectively.

CloudNativePG creates its own Secret for application credentials, and the template reuses it directly.

## References

- [Cosign blob verification](https://docs.sigstore.dev/cosign/verifying/verify/)
- [GitHub OIDC](https://docs.github.com/en/actions/reference/security/oidc)
- [Kubernetes Secret best practices](https://kubernetes.io/docs/concepts/security/secrets-good-practices/)
- [Pod Security Standards](https://kubernetes.io/docs/concepts/security/pod-security-standards/)
