Check a change locally
About 1322 words
backstage-cilium-render renders a cluster's network policy the way Backstage would, from local checkouts of your catalog repositories. Use it to see what a new relation or annotation does to the policy before you push it, to find the problems a change brings, or to get the manifests without a running Backstage.
It ships with the backend package.
Run it
Pass one or more directories that hold catalog files:
npx -p @spechtlabs/backstage-plugin-cilium-backend backstage-cilium-render \
~/src/acme/k8s-deployment ~/src/acme/catalogEach directory must be inside a git working tree, because the command lists files with git ls-files. It reads tracked and untracked files and skips the ones git ignores, so a new entity file you haven't committed yet counts.
Every .yaml or .yml document whose apiVersion starts with backstage.io/ or specht-labs.de/ is an entity; Location entities are skipped. An entity defined twice keeps its first definition, with a warning.
Read the output
For the example catalog in docs/examples/prod:
Production cluster: 29 entities, 13 endpoints (12 under the default deny), 45 flows, 15 policies, 864 lines
component:default/argocd argocd/app.kubernetes.io/part-of=argocd in 1 / out 4
component:default/blackbox-exporter monitoring/app.kubernetes.io/name=blackbox-exporter in 1 / out 3
component:default/cert-manager cert-manager/* in 3 / out 3
component:default/cluster-api {capi-system,capi-kubeadm-bootstrap-system,capi-kubeadm-control-plane-system}/* in 3 / out 3
component:default/cnpg-operator cnpg-system/app.kubernetes.io/name=cloudnative-pg in 1 / out 3
component:default/envoy-gateway envoy-gateway-system/app.kubernetes.io/name=envoy-gateway in 1 / out 2
(open) component:default/external-dns kube-system/app.kubernetes.io/name=external-dns in 1 / out 3
component:default/orders orders/app=orders in 4 / out 1
component:default/prometheus monitoring/app.kubernetes.io/name=prometheus in 1 / out 5
component:default/shop shop/app=shop in 4 / out 8
component:default/turn turn/app=coturn in 2 / out 1
resource:default/edge gateways/gateway.networking.k8s.io/gateway-name=edge in 2 / out 2
resource:default/shop-db shop/cnpg.io/cluster=shop-db in 4 / out 1The first line is about the policy: how many entities were read, how many endpoints there are and how many of them are under the default deny, how many flows (each endpoint's flow to its own pods included), how many CiliumClusterwideNetworkPolicy documents and lines the manifests have. Then one line per endpoint: (open) if its namespaces are left out of the default deny, its reference, the pods its policy selects (namespace/selector, with * for every namespace or every pod), and how many flows reach it and leave it.
--flows adds every flow but the ones within an endpoint, with its ports (* for every port) and the fact it comes from:
Production cluster: 6 entities, 3 endpoints (3 under the default deny), 5 flows, 4 policies, 172 lines
component:default/orders orders/app=orders in 2 / out 1
component:default/shop shop/app=shop in 1 / out 3
resource:default/shop-db shop/cnpg.io/cluster=shop-db in 2 / out 1
flows:
component:shop -> component:orders [tcp:9000] component:shop consumes api:orders-api
component:shop -> resource:shop-db [tcp:5432] component:shop depends on resource:shop-dbWhen the policy has problems, they come last. For the catalog in docs/examples/problems, which gets a few things wrong on purpose:
lab: 17 entities, 5 endpoints (5 under the default deny), 6 flows, 6 policies, 216 lines
component:default/backend backend/app=backend in 2 / out 1
component:default/node-agent {node-agent,kube-system}/app=node-agent in 1 / out 1
component:default/web web/* in 1 / out 2
component:default/worker */app=worker in 1 / out 1
resource:default/cache web/app=cache in 1 / out 1
problems:
error: cilium:default/stray: spec.target: no Cilium policy cilium:staging
error: cilium:default/lab: spec.metrics.scraper: component:default/grafana-agent has no pods on resource:default/lab
error: cilium:default/probe-everything: spec.from: no component:default/prober in the catalog
error: cilium:default/world-to-office: entity:world to cidr:192.0.2.0/24: one end must be pods on resource:default/lab
error: component:default/backend: specht-labs.de/cilium-ports: not ports: tcp:http
error: component:default/batch: specht-labs.de/cilium-selector: not a label requirement: app in (batch
error: component:default/legacy: not namespaces: Legacy_Apps
error: resource:default/cache: specht-labs.de/cilium-allow: entity:internet: not a Cilium entity (all, world, world-ipv4, world-ipv6, cluster, host, remote-node, kube-apiserver, ingress, health, init, unmanaged)
warning: cilium:default/probe-everything: spec.to: api:default/legacy-api has no pods on resource:default/lab and no specht-labs.de/cilium-dst
warning: component:default/backend: reached on every port by 1 flow: give it (or the APIs it provides) specht-labs.de/cilium-ports
warning: component:default/node-agent: only some of its namespaces are left out of the default deny (kube-system)
warning: component:default/web: consumes api:default/ghost-api, which isn't in the catalog
warning: component:default/web: consumes api:default/legacy-api, which doesn't run on resource:default/lab and has no specht-labs.de/cilium-dst: not allowed
warning: component:default/web: consumes api:default/remote-api, which doesn't run on resource:default/lab and has no specht-labs.de/cilium-dst (it is on the tailnet, but the policy has no tailnetEgress): not allowed
warning: component:default/web: depends on component:default/billing, which has pods (backstage.io/kubernetes-namespace) but doesn't depend on resource:default/lab: no flow
warning: component:default/worker: no backstage.io/kubernetes-namespace or specht-labs.de/cilium-namespaces: its policy selects app=worker in every namespace
info: component:default/web: depends on resource:default/queue, outside resource:default/lab: if it is reached over the network, give it specht-labs.de/cilium-dstErrors come first, then warnings, then notes (info). The first lines are the rule entities whose spec.target isn't a policy, whichever policy you render. Diagnostics explains each message.
Pick a policy and keep the output
| Flag | Effect |
|---|---|
--policy <name> | render the policy entity with this metadata.name; without it, the first policy by name |
--flows | list the flows |
--out <file> | write the rendered manifests |
--json <file> | write what GET /api/cilium/overview would serve, every policy included, without the publishing status |
Exit status
| Status | Meaning |
|---|---|
| 0 | the policy has no errors (warnings and notes don't count) |
| 1 | the policy, or a rule entity without a policy, has an error |
| 2 | bad arguments: no directory, a path that isn't a directory, an unknown --policy, or an unexpected failure such as a directory outside a git working tree |
Use it in CI
Because the exit status is 1 on any error, the command can guard a catalog repository: render the policy on every pull request and fail when a change brings an error. With the repository that holds the policy entity and the catalog checked out side by side:
- run: |
npx -p @spechtlabs/backstage-plugin-cilium-backend backstage-cilium-render \
--flows k8s-deployment catalogTo see what a pull request changes in the manifests, render with --out on both sides and diff the files. The output is stable: endpoints are sorted by policy name, and the same catalog always renders the same text.
Where it differs from Backstage
The command rebuilds the relations the catalog would add from the specs: consumesApis, providesApis, dependsOn, and the Cilium relations. It doesn't run the catalog's processors, so:
dependsOnentries without a kind default to Component, so writeresource:prodin full;- entities from other sources (a discovery provider, an ingestion from a cloud account) are missing unless they're in the directories you pass;
- it doesn't validate
kind: Ciliumentities the way the catalog module does. An entity the catalog would refuse may render here, or fail the command.
Publishing isn't part of it: there's no spec.publish status, and nothing is written to GitHub.
From the plugin's repository
In a checkout of the plugin's own repository, yarn cilium:render runs the command from source, with the same arguments. Relative paths resolve against the directory you started yarn in.
