Skip to content
dan-pettyPublic

About

IaC templates

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

309 Commits

Folders and files

Repository files navigation

infra

The shared infrastructure templates of the repositories of dan-petty, and the checks that test them. A template that two or more consumers use, or a platform stack that any service can use, is here. A template of one consumer stays with that consumer (devops-cli ADR 0032, "Infrastructure templates live in four homes"). The reusable GitHub Actions workflows and the Copier template of a new repository are in dan-petty/github-workflows, which releases only when they change.

infra holds no private data

infra is public. It holds no secret and no value of one environment: no real domain, no host or node name, no private address, no device path, no inventory, no Secret and no token. Templates, tests and documents use the placeholder <domain>, the domain example.com and the RFC 5737 addresses 192.0.2.0/24, 198.51.100.0/24 and 203.0.113.0/24. The consumer gives its own values. The private-data guard of CI refuses each of these values ("Private-data guard").

Layout

Folder Content
k8s/bases/ Kustomize bases.
k8s/components/ Kustomize components, with their Prometheus rules in rules/.
k8s/tests/ The test overlay of each base and component.
charts/ The Helm charts that infra writes, with their test values and unit tests.
helm/ The values templates of upstream Helm charts, each with a test chart that pins the upstream chart.
ansible/ The Ansible collection dan_petty.infra: galaxy.yml, roles/, playbooks/ and its tests/.
tf/modules/ OpenTofu modules.
tf/examples/ An example for each OpenTofu module.
tf/github/ The OpenTofu roots of the GitHub settings of the owner account, which devops tf converge applies from main after the owner merges a change (README).
renovate/ Renovate presets that other repositories extend.
docs/ Documents that apply to more than one template.
.github/workflows/ CI (ci.yml) and the callers of the reusable workflows of dan-petty/github-workflows: the title check (pr-title-caller.yml) and the releases (release-caller.yml).

The README of each folder tells its content and the consumers of each template. Each folder that a CI job checks has an example fixture, so CI proves that each check runs; the test chart of each values template is the fixture of helm/. The real templates so far are the Renovate presets in renovate/ (#6), the chart charts/service-tenant (#10, with its secret store from #12), the Ansible roles common, data_disk, nvidia, gvisor and cpu_tuning, and the generic plays (#7, #8), the base k8s/bases/valkey with its components valkey-persistence, valkey-exporter and valkey-acl (#13), the base k8s/bases/service-repos (#11, docs/service-repos.md), the base k8s/bases/cloudflared (#16; the upstream chart cloudflare-tunnel-remote lacks the required 2-replica default with anti-affinity, fixed Secret name cloudflared-token, and PodDisruptionBudget), the values template helm/external-secrets of External Secrets Operator (#12), the values templates helm/cert-manager and helm/traefik (#16), the OpenTofu modules aws-eks, azure-aks and gcp-gke with their examples (#9), the OpenTofu module github-repository with its example (#240), the root tf/github/dan-petty that calls it, (#241). More templates come with later items, such as #14 to #19 (Kubernetes bases and components, and Helm values templates).

Pin a release

A consumer pins infra at a release tag vX.Y.Z, never at main. Renovate updates each pin.

Form Pin
Kustomize base or component https://github.com/dan-petty/infra//k8s/bases/<base>?ref=vX.Y.Z
Argo CD source repoURL: https://github.com/dan-petty/infra.git with targetRevision: vX.Y.Z
Ansible collection name: https://github.com/dan-petty/infra.git#/ansible/, type: git, version: vX.Y.Z
OpenTofu module git::https://github.com/dan-petty/infra.git//tf/modules/<module>?ref=vX.Y.Z
Reusable workflow uses: dan-petty/github-workflows/.github/workflows/<workflow>.yml@<sha> # vX.Y.Z, a release of dan-petty/github-workflows (its README)
Copier template copier copy --vcs-ref vX.Y.Z https://github.com/dan-petty/github-workflows.git <dir>, a release of dan-petty/github-workflows (its README)

A caller of a reusable workflow pins <sha>, the commit of the tag (git rev-parse vX.Y.Z^{commit}), with the tag in a comment: the github-actions manager of Renovate writes this form for a digest pin (pinDigests of renovate/base.json), and a repository with a supply-chain rule refuses a bare tag. Each pin has one owner. The template of dan-petty/github-workflows renders the callers .github/workflows/pr-title.yml and .github/workflows/release.yml of a repository, and the copier update of the template moves their pins. Renovate moves each other pin. infra holds no render of the template: its callers in ci.yml, pr-title-caller.yml and release-caller.yml are its own files, and Renovate moves their pins. A release of infra holds no workflow and no template, so Renovate leaves a caller of a workflow of infra and a copier render of infra alone (renovate/README.md).

Releases

release-please makes each release. On each push to main, .github/workflows/release-caller.yml calls the reusable workflow release.yml of dan-petty/github-workflows ("Call the reusable workflows"), which opens or updates one release pull request. It holds the next version in version.txt, .release-please-manifest.json and ansible/galaxy.yml, the next tag vX.Y.Z in k8s/bases/service-repos/applicationsets.yaml, and the new entry of CHANGELOG.md. The workflow turns on auto-merge and requests review (dan-petty by default) on the release pull request, and GitHub merges it when its required checks pass; a person can also merge it. The next run of release-please creates the tag vX.Y.Z and its GitHub release. .github/CODEOWNERS also names dan-petty as the owner of each file, so GitHub asks dan-petty to review each pull request on public repositories. With the repository setting immutable releases, nobody can move or delete a published tag, so a pin always gets the same content.

release-please reads the type of each commit on main since the last release. The largest change gives the next version. The merge settings of the repository decide if the pull request title or the commit subjects of the branch land on main. Thus write the pull request title and each commit subject as a Conventional Commit. .github/workflows/pr-title-caller.yml calls the reusable workflow pr-title.yml of dan-petty/github-workflows, which checks the title of each pull request into main. It checks no commit subject: github/repository.yaml gives the squash commit the pull request title (squash_merge_commit_title PR_TITLE). The scope is optional. A scope that a title gives must be in the input scopes of the caller, which holds the name after scope/ of each scope/* label of .github/labels.yml, and ci, deps, docs, main and spec of every repository. The workflow reads no file, so a pull request that adds a scope label adds the scope to the caller too. The next version comes from the types:

Commit Next version
The first release v0.1.0 (initial-version of release-please-config.json)
A breaking change: ! after the type, or a BREAKING CHANGE: footer The next major version. Renovate opens a major update as a separate pull request.
feat The next minor version
fix, perf or revert The next patch version
fix(deps) update of an image under k8s/bases/ The next patch version
build, chore, ci, docs, refactor, style or test No release of its own

A change of a template input that a consumer must follow is a breaking change.

extra-files of the package . in release-please-config.json lists the other files that get the new version:

  • ansible/galaxy.yml has the type yaml and the path $.version, so the release pull request writes the new version into the key version of the collection. This updater writes the whole file again with js-yaml: it removes each comment and can change the quotes of a string. Thus ansible/galaxy.yml has no comments, and each string has the quotes that js-yaml writes.
  • To keep a file that names its own release tag current, put the comment x-release-please-version on that line and add {"type": "generic", "path": "<file>"} to extra-files. The release pull request then writes the new version on that line only. A YAML file also needs the type generic: for a path with no type, release-please also sets a key version of the file with the updater yaml, which removes the comments. k8s/bases/service-repos/applicationsets.yaml pins the chart charts/service-tenant this way, so each release tag of the base names the chart of the same tag. tests/test-service-repos.sh checks that the pin is v and the version of version.txt.

The release App gives the token of the job, so the checks of ci.yml run on the release pull request. A pull request that GITHUB_TOKEN opens starts no workflow. The job runs in the environment release, which holds the variable RELEASE_APP_CLIENT_ID and the secret RELEASE_APP_PRIVATE_KEY of the App. When one of them is missing, the job stops with an error that names it.

Call the reusable workflows

infra calls the reusable workflows of dan-petty/github-workflows at the commit of a release tag, as each other repository calls them ("Pin a release"). Its README gives the inputs, the outputs and the permissions of each workflow.

Caller Workflow Check
The job changes of ci.yml changes.yml: the path filter of the check jobs Changes / Changes
The job private-data of ci.yml private-data.yml: gitleaks with the shared rules Private-data guard / Scan
The job k8s-validate of ci.yml k8s-validate.yml: the Kustomize roots, Helm charts and Prometheus rules of infra, with the judgments of helm/.trivyignore.yaml and the excluded kube-linter checks of the jobs k8s-validate-mattermost-operator and k8s-validate-pyroscope (helm/README.md), and the tests of the service-repos base and the base browser K8s run / Validate, K8s run mattermost-operator / Validate, K8s run pyroscope / Validate and K8s run / Repository checks
The job tofu of ci.yml opentofu.yml: tofu fmt, tflint, trivy config and tofu validate of tf/, trivy config and tofu validate of each folder of tf/examples/ and of tf/github/dan-petty OpenTofu / OpenTofu
The job ansible of ci.yml ansible.yml: ansible-lint, the build of the collection and tests/test-ansible-collection.sh Ansible / Ansible
The job workflows of ci.yml lint.yml: actionlint on the workflows, shellcheck on each tracked *.sh file, zizmor on .github/, yamllint and the Renovate config validator Workflows / Lint
The job repository-checks of ci.yml k8s-validate.yml, job Repository checks: tests/test-private-data-scan.sh, tests/test-renovate-presets.sh, tests/test-github-repository-module.sh, tests/test-tf-github-root.sh and tests/test-tf-github-resources.sh Repository checks / Repository checks
pr-title-caller.yml pr-title.yml PR title / Semantic PR title
release-caller.yml release.yml, in the environment release Release / Release Please

The zizmor step of lint.yml checks the workflows: each action and each reusable workflow has a pin to a commit SHA, no job gets more permissions than it needs and no call passes secrets: inherit.

Tools

The repository pins no tool. Each called workflow runs in the devtools image of dan-petty/github-workflows, which holds the tools at the versions of the image tag of the pin of the workflow ("Call the reusable workflows"). A local run uses the same tools from the devtools image on PATH, such as the devcontainer of the repository. The command of the job K8s run uses yq of the image to read the input helm-charts of ci.yml. To run a check with the image alone: podman run --rm -v "$PWD":/w -w /w ghcr.io/dan-petty/devtools:<tag> <command>.

Checks

.github/workflows/ci.yml runs on each pull request, each push to main and each manual run (workflow_dispatch). The Private-data guard, the job Workflows and the job Repository checks run on each of these events. Each other check job runs only when a change touches the files of its tool. A manual run runs every check job.

The check CI gate needs each check job and the job Changes. It always reports. It fails when one of these jobs failed or was cancelled; a skipped job passes. In github/repository.yaml, the ruleset main requires the status checks CI gate and PR title / Semantic PR title. Thus add each new check job to needs of the job gate.

To run a job locally, run its command from the repository root. The job K8s run runs the script scripts/k8s-validate.sh of dan-petty/github-workflows at the tag of the pin of ci.yml, so a local run takes it from a clone of that tag next to infra: git clone --depth 1 --branch vX.Y.Z https://github.com/dan-petty/github-workflows.git ../github-workflows.

Job Command
Private-data guard scripts/private-data-scan.sh dir .
K8s run KUSTOMIZE_ROOTS="$(ls -d k8s/tests/*/)" HELM_CHARTS="$(yq -r '.jobs["k8s-validate"].with["helm-charts"]' .github/workflows/ci.yml)" PROMTOOL_RULES=k8s/components/example/rules/example.rules.yaml TRIVY_IGNORE_FILE=helm/.trivyignore.yaml ../github-workflows/scripts/k8s-validate.sh all, then tests/test-service-repos.sh and tests/test-browser-base.sh
OpenTofu tofu fmt -check -recursive tf/, tflint --recursive --config "$PWD/.tflint.hcl", in each folder of tf/examples/: trivy config --exit-code 1 --severity HIGH,CRITICAL --ignorefile tf/.trivyignore.yaml --skip-dirs '**/.terraform' --tf-vars <folder>/<name>.tfvars.example <folder> (with no --tf-vars for a folder that has no such file; a bare trivy config tf/ gets no variable values and reports GCP-0061), then tofu init -backend=false and tofu validate; the same trivy config, tofu init -backend=false and tofu validate in tf/github/dan-petty
Ansible ansible-lint and ansible-galaxy collection build in ansible/, and tests/test-ansible-collection.sh
Workflows / Lint actionlint, git ls-files -z '*.sh' | xargs -0 -r shellcheck, zizmor --offline .github/ and git ls-files -z '*.yaml' '*.yml' | xargs -0 -r yamllint --strict
Repository checks tests/test-private-data-scan.sh, tests/test-renovate-presets.sh, tests/test-github-repository-module.sh, tests/test-tf-github-root.sh and tests/test-tf-github-resources.sh

Private-data guard

scripts/private-data-scan.sh runs gitleaks with .gitleaks.toml: the default rules of gitleaks for secrets, and rules for RFC 1918, CGNAT and link-local addresses, for names that end in .lan or .local, and for device paths. The allowlist admits the k3s default cluster CIDR 10.42.0.0/16 and Service CIDR 10.43.0.0/16, the block of each private range (such as 10.0.0.0/8), the Kubernetes cluster domain and the pseudo-devices such as /dev/null. tests/test-private-data-scan.sh holds a case for each rule.

The guard also refuses each domain of the environment variable PRIVATE_DOMAINS (one domain on each line) and each subdomain of it. In CI, the value comes from the Actions secret PRIVATE_DOMAINS, so no file holds a real domain. When the secret is not set, the job writes a warning in its log and its summary, and runs the other rules.

Pre-commit hooks

Run pre-commit install once in each clone. The hooks of .pre-commit-config.yaml refuse a commit on main and run the private-data guard on the staged changes.

License

MIT.

About

IaC templates

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages