Skip to content

Commit 8ad4166

Browse files
author
Barkha Choithani
committed
added network policy config and tests
1 parent 68efbb8 commit 8ad4166

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

charts/templates/networkPolicy.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{{- if .Values.networkPolicy.enabled }}
2+
apiVersion: networking.k8s.io/v1
3+
kind: NetworkPolicy
4+
metadata:
5+
name: {{ include "marklogic.fullname" . }}-network-policy
6+
namespace: {{ .Release.Namespace }}
7+
spec:
8+
podSelector:
9+
matchLabels:
10+
{{- include "marklogic.selectorLabels" . | nindent 6 }}
11+
policyTypes:
12+
- Ingress
13+
ingress:
14+
{{- if .Values.networkPolicy.customRules }}
15+
- from:
16+
{{- toYaml .Values.networkPolicy.customRules | nindent 8 }}
17+
{{- end }}
18+
- ports:
19+
{{- toYaml .Values.networkPolicy.ports | nindent 8 }}
20+
{{- end }}

charts/values.yaml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,18 @@ serviceAccount:
100100
# If not set and create is true, a name is generated using the fullname template
101101
name: ""
102102

103-
103+
# Configure network options
104+
networkPolicy:
105+
enabled: true
106+
customRules: {}
107+
ports:
108+
- port: 8000
109+
protocol: TCP
110+
- port: 8001
111+
protocol: TCP
112+
- port: 8002
113+
protocol: TCP
114+
104115
# Below are the advanced configurations, please understand read the reference before you make changes
105116

106117
# Configure options for liveness probe

test/template/network_templ_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package template_test
2+
3+
import (
4+
"path/filepath"
5+
"strings"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
netv1 "k8s.io/api/networking/v1"
10+
11+
"github.com/gruntwork-io/terratest/modules/helm"
12+
"github.com/gruntwork-io/terratest/modules/k8s"
13+
"github.com/gruntwork-io/terratest/modules/random"
14+
)
15+
16+
func TestChartTemplateNetworkPolicyEnabled(t *testing.T) {
17+
t.Parallel()
18+
19+
// Path to the helm chart we will test
20+
helmChartPath, err := filepath.Abs("../../charts")
21+
releaseName := "marklogic-network-test"
22+
t.Log(helmChartPath, releaseName)
23+
require.NoError(t, err)
24+
25+
// Set up the namespace; confirm that the template renders the expected value for the namespace.
26+
namespaceName := "marklogic-" + strings.ToLower(random.UniqueId()) + "-network-policy"
27+
t.Logf("Namespace: %s\n", namespaceName)
28+
29+
// Setup the args for helm install
30+
options := &helm.Options{
31+
SetValues: map[string]string{
32+
"image.repository": "marklogicdb/marklogic-db",
33+
"image.tag": "latest",
34+
"persistence.enabled": "false",
35+
"networkPolicy.enabled": "true",
36+
},
37+
KubectlOptions: k8s.NewKubectlOptions("", "", namespaceName),
38+
}
39+
output := helm.RenderTemplate(t, options, helmChartPath, releaseName, []string{"templates/networkPolicy.yaml"})
40+
41+
var networkpolicy netv1.NetworkPolicy
42+
helm.UnmarshalK8SYaml(t, output, &networkpolicy)
43+
44+
// Verify the name and namespace matches
45+
require.Equal(t, namespaceName, networkpolicy.Namespace)
46+
47+
// Verify the network policy type matches
48+
networkPolicies := networkpolicy.Spec
49+
expectedPolicyTypes := "Ingress"
50+
require.Equal(t, string(networkPolicies.PolicyTypes[0]), expectedPolicyTypes)
51+
}
52+

0 commit comments

Comments
 (0)