From 0d59d8d9071d61a45e7ffbc58156f9ab749aa7ba Mon Sep 17 00:00:00 2001 From: OZG-Cloud Team <noreply@ozg-sh.de> Date: Thu, 13 Jul 2023 21:03:17 +0200 Subject: [PATCH] OZG-3880 - add network policy --- run_helm_test.sh | 7 ++ src/main/helm/templates/_helpers.tpl | 1 + src/main/helm/templates/network_policy.yaml | 37 ++++++++ src/test/helm/deployment_test.yaml | 6 ++ src/test/helm/network_policy_test.yaml | 96 +++++++++++++++++++++ 5 files changed, 147 insertions(+) create mode 100755 run_helm_test.sh create mode 100644 src/main/helm/templates/network_policy.yaml create mode 100644 src/test/helm/network_policy_test.yaml diff --git a/run_helm_test.sh b/run_helm_test.sh new file mode 100755 index 00000000..7324e7d1 --- /dev/null +++ b/run_helm_test.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +set -e + +helm template ./src/main/helm/ -f src/test/unit-values.yaml +helm lint -f src/test/unit-values.yaml ./src/main/helm/ +cd src/main/helm && helm unittest --helm3 -f '../../test/helm/*.yaml' -v '../../test/unit-values.yaml' . \ No newline at end of file diff --git a/src/main/helm/templates/_helpers.tpl b/src/main/helm/templates/_helpers.tpl index 10b786e5..a4be54ca 100644 --- a/src/main/helm/templates/_helpers.tpl +++ b/src/main/helm/templates/_helpers.tpl @@ -35,6 +35,7 @@ app.kubernetes.io/part-of: kop app.kubernetes.io/version: {{ include "app.version" . }} app.kubernetes.io/namespace: {{ include "app.namespace" . }} helm.sh/chart: {{ include "app.chart" . }} +ozg-component: eingangsadapter {{- end -}} {{- define "app.matchLabels" }} diff --git a/src/main/helm/templates/network_policy.yaml b/src/main/helm/templates/network_policy.yaml new file mode 100644 index 00000000..23a609a0 --- /dev/null +++ b/src/main/helm/templates/network_policy.yaml @@ -0,0 +1,37 @@ +{{- if not (.Values.network_policy).disabled }} +apiVersion: networking.k8s.io/v1 +kind: NetworkPolicy +metadata: + name: network-policy-eingangsadapter + namespace: {{ .Release.Namespace }} +spec: + podSelector: + matchLabels: + ozg-component: eingangsadapter + policyTypes: + - Ingress + - Egress + ingress: + - ports: + - port: 8080 + egress: + - to: + - podSelector: + matchLabels: + component: pluto + ports: + - port: 9090 + protocol: TCP + - to: + - namespaceSelector: + matchLabels: + kubernetes.io/metadata.name: kube-system + podSelector: + matchLabels: + k8s-app: kube-dns + ports: + - port: 53 + protocol: UDP + - port: 53 + protocol: TCP +{{- end }} \ No newline at end of file diff --git a/src/test/helm/deployment_test.yaml b/src/test/helm/deployment_test.yaml index 02ab7be0..ca8169b6 100644 --- a/src/test/helm/deployment_test.yaml +++ b/src/test/helm/deployment_test.yaml @@ -44,3 +44,9 @@ tests: - equal: path: spec.template.spec.containers[0].image value: docker.ozg-sh.de/intelliform-adapter:latest + - it: should have label ozg-component + asserts: + - equal: + path: metadata.labels.ozg-component + value: eingangsadapter + diff --git a/src/test/helm/network_policy_test.yaml b/src/test/helm/network_policy_test.yaml new file mode 100644 index 00000000..94af2675 --- /dev/null +++ b/src/test/helm/network_policy_test.yaml @@ -0,0 +1,96 @@ +# +# Copyright (C) 2022 Das Land Schleswig-Holstein vertreten durch den +# Ministerpräsidenten des Landes Schleswig-Holstein +# Staatskanzlei +# Abteilung Digitalisierung und zentrales IT-Management der Landesregierung +# +# Lizenziert unter der EUPL, Version 1.2 oder - sobald +# diese von der Europäischen Kommission genehmigt wurden - +# Folgeversionen der EUPL ("Lizenz"); +# Sie dürfen dieses Werk ausschließlich gemäß +# dieser Lizenz nutzen. +# Eine Kopie der Lizenz finden Sie hier: +# +# https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 +# +# Sofern nicht durch anwendbare Rechtsvorschriften +# gefordert oder in schriftlicher Form vereinbart, wird +# die unter der Lizenz verbreitete Software "so wie sie +# ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN - +# ausdrücklich oder stillschweigend - verbreitet. +# Die sprachspezifischen Genehmigungen und Beschränkungen +# unter der Lizenz sind dem Lizenztext zu entnehmen. +# + +suite: network policy test +release: + name: eingangsadapter + namespace: by-helm-test +templates: + - templates/network_policy.yaml +tests: + - it: should match apiVersion + asserts: + - isAPIVersion: + of: networking.k8s.io/v1 + - it: should match kind + asserts: + - isKind: + of: NetworkPolicy + - it: validate metadata + asserts: + - equal: + path: metadata + value: + name: network-policy-eingangsadapter + namespace: by-helm-test + - it: validate spec + asserts: + - equal: + path: spec + value: + podSelector: + matchLabels: + ozg-component: eingangsadapter + policyTypes: + - Ingress + - Egress + ingress: + - ports: + - port: 8080 + egress: + - to: + - podSelector: + matchLabels: + component: pluto + ports: + - port: 9090 + protocol: TCP + - to: + - namespaceSelector: + matchLabels: + kubernetes.io/metadata.name: kube-system + podSelector: + matchLabels: + k8s-app: kube-dns + ports: + - port: 53 + protocol: UDP + - port: 53 + protocol: TCP + + - it: test network policy disabled + set: + network_policy: + disabled: true + asserts: + - hasDocuments: + count: 0 + + - it: test network policy unset should be disabled + set: + network_policy: + disabled: false + asserts: + - hasDocuments: + count: 1 \ No newline at end of file -- GitLab