policy-generator/cmd/policygen/templates/netpol/pod/pod.yaml
Erik Brakkee eba04ec132 Ports specified at communications now override the default ports of
the application.

Also added some comments to the generated output.
2025-01-02 22:49:47 +01:00

73 lines
1.9 KiB
YAML

{{- define "ports" }}
{{- range $port := . }}
- port: {{ $port.Port }}
{{- if $port.Protocol }}
protocol: {{ $port.Protocol }}
{{- end }}
{{- end }}
{{- end }}
{{- define "peers" }}
{{- range .Applications }}
# {{ .Application.Namespace.Name }}/{{ .Application.Name }}
- podSelector:
matchLabels: {{ .Application.MatchLabels | toYaml | nindent 12 }}
namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: {{ .Application.Namespace.Name }}
{{- if .Ports }}
ports:
{{- template "ports" .Ports }}
{{- end }}
{{- end }}
{{- end }}
{{- define "networks" }}
{{- range .Networks }}
# {{ .Network.Name }}
- ipBlock:
cidr: {{ .Network.CIDR}}
except:
{{- range $except := .Network.Except }}
- {{ $except }}
{{- end }}
{{- if .Ports }}
ports:
{{- template "ports" .Ports }}
{{- end }}
{{- end }}
{{- end }}
{{- if not .app.Namespace.Open }}
---
kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
name: "{{.app.Name}}"
namespace: "{{.app.Namespace.Name }}"
labels: {{ .labels | toYaml | nindent 4 }}
spec:
# {{ .app.Namespace.Name }}/{{ .app.Name }}
podSelector: {{ .app.MatchLabels | toYaml | nindent 4 }}
policyTypes:
{{- if or .ingress.Applications .ingress.Networks }}
- Ingress
{{- end }}
{{- if or .egress.Applications .egress.Networks }}
- Egress
{{- end }}
{{- if or .ingress.Applications .ingress.Networks }}
ingress:
from:
{{- template "peers" .ingress }}
{{- template "networks" .ingress }}
{{- end }}
{{- if or .egress.Applications .egress.Networks }}
egress:
to:
{{- template "peers" .egress }}
{{- template "networks" .egress }}
{{- end }}
{{- end }}