Publish to GitHub
About 1081 words
Backstage can write a policy's rendered manifests to a GitHub repository: as one pull request it keeps up to date, or as a direct push. Applying them to the cluster stays with that repository and whatever syncs it, such as Argo CD or Flux. This guide sets that up.
1. Say where the manifests go
Add spec.publish to the policy entity:
apiVersion: specht-labs.de/v1alpha1
kind: Cilium
metadata:
name: prod
spec:
type: policy
owner: group:platform
cluster: resource:prod
publish:
repo: acme/k8s-deployment
path: cilium/rendered/prod.yaml
flow: pullRequest| Field | Required | Meaning |
|---|---|---|
repo | yes | owner/name of a repository on GitHub.com |
path | yes | the file in that repository, such as cilium/rendered/prod.yaml |
flow | no | pullRequest (the default) or push |
branch | no | the branch the file lands on; the repository's default branch if unset |
pullRequestBranch | no | for pullRequest, the branch Backstage owns and force-pushes; backstage/cilium-<policy name> if unset |
Point your GitOps tool at the file, or at its directory. It's a single YAML file with one document per policy.
2. Allow the repository in Backstage
The catalog says where each policy goes. Backstage's own config says which repositories it may write to at all:
# app-config.yaml
cilium:
allowedRepos:
- acme/k8s-deploymentThe comparison ignores case. A policy whose repository isn't listed isn't published: the console shows Not published: acme/k8s-deployment is not in Backstage's cilium.allowedRepos, and the backend logs a warning on every run. This keeps someone who can edit a policy entity from pointing Backstage at a repository you didn't mean it to write to.
3. Give Backstage credentials
The backend uses the credentials of Backstage's GitHub integration for github.com. A GitHub App is the better choice, since its access is limited to the repositories you install it on:
# app-config.yaml
integrations:
github:
- host: github.com
apps:
- $include: github-app-backstage-credentials.yamlThe App needs these repository permissions on the target repository:
| Permission | Access | Used for |
|---|---|---|
| Contents | Read and write | reading the current file, creating the commit, updating the branch |
| Pull requests | Read and write | finding, opening, updating and closing the pull request |
| Metadata | Read | always required by GitHub; reading the default branch |
With flow: push, Contents is enough, but keep Pull requests if you might switch flows. A personal access token works too (token: in place of apps:); a fine-grained one needs the same permissions.
GitHub Enterprise Server isn't supported. The backend calls https://api.github.com directly.
4. Wait for the hour, or run it now
The backend schedules a task, cilium-publish, that runs every 60 minutes, starting three minutes after Backstage starts so the catalog can settle. Each run reads the catalog fresh, renders every policy with spec.publish, and publishes each one. A run that takes longer than five minutes is stopped.
To run it right away, trigger cilium-publish from the Scheduled Tasks view of Backstage's DevTools plugin, if your Backstage has it.
The console's header shows where each policy goes, and the Publishing card on the overview has the repository, the file, the branch, and the outcome and link of the last run. The last run's outcome is kept in memory: a restart clears it, and with several backend replicas only the one that ran the task knows it.
Errors stop publishing
A policy with an error isn't published. The console shows Not published: the policy has errors, and the backend logs a warning. Warnings and notes don't stop it. Errors are things like a malformed annotation or a rule naming an entity that doesn't exist; until they're fixed, the manifests could allow less, or more, than the catalog means. Diagnostics lists which messages are errors.
What pullRequest does
Each run:
- Reads the file at
pathon the base branch (branch, or the default branch). If it already matches the rendered manifests, Backstage closes its open pull request, if there is one, and stops. - If
pullRequestBranchdoesn't hold the rendered manifests yet, Backstage builds one commit with the file on top of the base branch's head and pointspullRequestBranchat it, creating the branch if needed. The branch is force-pushed, so it always holds exactly one commit and the pull request shows exactly what the catalog would change. - Updates the title and description of the open pull request from
pullRequestBranch, or opens one.
Don't push your own commits to pullRequestBranch: the next change replaces them. Change the catalog instead.
The pull request's title counts the rendered policies and the flows (each endpoint's flow to its own pods included):
feat(cilium): render resource:prod network policies (15 policies, 45 flows)The description says where the manifests come from, counts the flows by the catalog fact they come from, lists every error and warning under Problems (none here), and links entities when app.baseUrl is set:
The network policies for [`resource:prod`](https://backstage.example.com/catalog/default/resource/prod), as Backstage renders them from [`cilium:prod`](https://backstage.example.com/catalog/default/cilium/prod) and the catalog's relations: a default deny (outside `kube-system`), and one policy per Component or Resource that allows its flows.
| Flows from | Count |
|---|---:|
| consumesApi | 14 |
| dependsOn | 6 |
| specht-labs.de/cilium-allow | 6 |
| metrics scraping | 3 |
| rule entities | 3 |
| within an endpoint | 13 |
Change the catalog, not this file: Backstage keeps this pull request up to date.The commit message is the title followed by Rendered by Backstage from the Cilium entities and relations in its catalog.
What push does
Each run reads the file on branch (or the default branch). If it differs from the rendered manifests, Backstage commits the file on top of the branch's head and moves the branch to that commit, without force. If someone pushed in between, the update fails, the run logs the error, and the next run tries again on top of their commit.
A branch protection rule that blocks direct pushes makes every run fail. Use pullRequest for protected branches.
Applying the manifests
Backstage stops at the repository. Have your GitOps tool sync the file to the cluster, and review the pull request before you merge it: it's the last place a person sees a change before Cilium enforces it. A new endpoint, or one whose selector changed, is worth a look in the console's Endpoints first. For the first rollout, follow Turn on the default deny.
