Skip to content

Commit 6bbfe54

Browse files
Merge pull request #67 from barkhachoithani/feature/CLD-595
CLD-595: Add support for networkPolicy in the helm chart
2 parents 68efbb8 + 44f9b76 commit 6bbfe54

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: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,19 @@ serviceAccount:
100100
# If not set and create is true, a name is generated using the fullname template
101101
name: ""
102102

103-
103+
# Configure options for network policy
104+
# ref: https://kubernetes.io/docs/concepts/services-networking/network-policies
105+
networkPolicy:
106+
enabled: false
107+
customRules: {}
108+
ports:
109+
- port: 8000
110+
protocol: TCP
111+
- port: 8001
112+
protocol: TCP
113+
- port: 8002
114+
protocol: TCP
115+
104116
# Below are the advanced configurations, please understand read the reference before you make changes
105117

106118
# Configure options for liveness probe

test/template/network_templ_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
}

0 commit comments

Comments
 (0)