Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

Commit

Permalink
fix: Network policies for pgsql clusters (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
lholota authored Nov 10, 2023
1 parent eb4f0cd commit 559d3d0
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 86 deletions.
4 changes: 2 additions & 2 deletions apps/common/templates/_labels.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ helm.sh/chart: {{ $.Chart.Name }}-{{ $.Chart.Version | replace "+" "_" }}
{{- end }}

{{- define "common.pod-labels" }}
app.kubernetes.io/name: {{ $.Chart.Name }}
app.kubernetes.io/instance: {{ $.Release.Name }}
app.kubernetes.io/name: {{ .Chart.Name }}
app.kubernetes.io/instance: {{ .Release.Name }}
{{- end }}
80 changes: 80 additions & 0 deletions apps/common/templates/_network-policies-postgresql.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{{- define "common.network-policy-postgresql-cluster" }}
{{- $appPodSelector := .AppPodSelector }}
{{- with .Root }}
{{- $clusterName := .Release.Name }}
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: {{ $clusterName }}-postgresql-cluster
labels:
{{- include "common.resource-labels" . | indent 4 }}
spec:
podSelector:
matchLabels:
cnpg.io/podRole: instance
cnpg.io/cluster: {{ $clusterName }}
policyTypes:
- Ingress
- Egress
egress:
# Allow cluster instances to talk to Kube API
{{- include "common.egress-kubeapi" . | indent 4 }}
ingress:
- ports:
- protocol: TCP
port: 5432
from:
# Accept traffic from postgresql jobs related to the same cluster
- podSelector:
matchExpressions:
- key: cnpg.io/jobRole
operator: Exists
- key: cnpg.io/cluster
operator: In
values:
- {{ $clusterName }}
# Accept traffic from other cluster instances
- podSelector:
matchLabels:
cnpg.io/podRole: instance
cnpg.io/cluster: {{ $clusterName }}

{{- if $appPodSelector }}
# Accept traffic from consuming app
- podSelector: {{ $appPodSelector | toYaml | nindent 12 }}
{{- end }}

# Accept traffic from operator in postgresql-system namespace
- ports:
- protocol: TCP
port: 8000
from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: postgresql-system
podSelector:
matchLabels:
app.kubernetes.io/name: cloudnative-pg
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: {{ $clusterName }}-postgresql-jobs
labels:
{{- include "common.resource-labels" . | indent 4 }}
spec:
podSelector:
matchExpressions:
- key: cnpg.io/jobRole
operator: Exists
- key: cnpg.io/cluster
operator: In
values:
- {{ $clusterName }}
policyTypes:
- Egress
egress:
# Allow jobs to talk to Kube API
{{- include "common.egress-kubeapi" . | indent 4 }}
{{- end }}
{{- end }}
2 changes: 1 addition & 1 deletion apps/cyberchef/templates/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Release.Name }}
namespace: {{ $.Release.Namespace }}
namespace: {{ .Release.Namespace }}
labels:
{{- include "common.resource-labels" . | indent 4 }}
spec:
Expand Down
2 changes: 1 addition & 1 deletion apps/cyberchef/templates/network-policy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: {{ .Release.Name }}
namespace: {{ $.Release.Namespace }}
namespace: {{ .Release.Namespace }}
labels:
{{- include "common.resource-labels" . | indent 4 }}
spec:
Expand Down

This file was deleted.

15 changes: 0 additions & 15 deletions apps/network-policies/templates/network-policy-postgresql-jobs.yml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,27 +1,2 @@
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: {{ .Release.Name }}-postgresql
namespace: {{ $.Release.Namespace }}
labels:
{{- include "common.resource-labels" . | indent 4 }}
spec:
podSelector:
matchExpressions:
- key: cnpg.io/cluster
operator: In
values:
- {{ $.Release.Name }}
policyTypes:
- Ingress
ingress:
# Accept traffic from pomerium
- ports:
- protocol: TCP
port: 5432
from:
- podSelector:
matchLabels:
app.kubernetes.io/name: pomerium
app.kubernetes.io/instance: {{ .Release.Name }}

{{- $appPodSelector := (dict "matchLabels" (include "common.pod-labels" . | fromYaml)) }}
{{ include "common.network-policy-postgresql-cluster" (dict "Root" . "AppPodSelector" $appPodSelector) }}
2 changes: 1 addition & 1 deletion apps/pomerium/values.lab.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ postgresql:
size: 10Gi
storageClass: local-path-provisioner-services-db
accessModes:
- ReadWriteMany
- ReadWriteMany

0 comments on commit 559d3d0

Please sign in to comment.