A small CLI tool that spits out Kubernetes NetworkPolicy YAML so you don't have to hand-write it every time.
I got tired of copy-pasting the same NetworkPolicy boilerplate and tweaking labels/namespaces by hand, so this just generates it for you. Right now it supports building a policy by label selector, more match types are on the list.
Writing a NetworkPolicy manifest from scratch is one of those things that's simple but annoying, easy to typo the indentation, easy to forget policyTypes. This just handles that part so you can move on.
git clone https://github.com/jhash1/policygen.git
cd policygen
go build(You'll need Go installed — nothing fancy, whatever recent-ish version.)
go run policygen --name appnetworkpolicy --namespace testing --labels app=nginxThat gives you something like:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: appnetworkpolicy
namespace: testing
spec:
podSelector:
matchLabels:
app: nginx
policyTypes:
- Ingress
- Egress| Flag | What it does |
|---|---|
--name |
name of the NetworkPolicy |
--namespace |
namespace it lives in |
--labels |
label selector, e.g. app=nginx |
Pipe the output straight into kubectl apply -f - if you trust it, or redirect it to a file first if you want to eyeball it (probably the smarter move).
This is a personal project I'm actively poking at, not a polished tool with a stable API, flags and output might shift around. Label-based selection works today; more selector types and policy shapes are coming when I get to them.