{{- include "nplus.init" $ -}}

{{- if ((.this.security).cni).createNetworkPolicy }}
kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
  name: {{ .component.fullName }}
  {{- if .this.utils.includeNamespace }}
  namespace: {{ .Release.Namespace }}
  {{- end }}
  labels:
    {{- include "nplus.instanceLabels" . | nindent 4 }}
  annotations:
    {{- include "nplus.argoWave" . | nindent 4 }}
    {{- include "nplus.annotations" . | nindent 4 }}
    {{- include "nplus.securityAnnotations" . | nindent 4 }}
spec:
  podSelector:
    matchLabels:
    {{- include "nplus.selectorLabels" . | nindent 6 }}
  policyTypes:
  - Ingress
  - Egress
  ingress:
  - from:
    - podSelector:
        matchLabels:
          nplus/group: {{ .instance.group }}
          nplus/type: core
    {{- if ((.this.security).cni).excludeUnusedPorts }}
    ports:
    {{- include "nplus.defaultPolicyPorts" . | nindent 4 }}
    {{- end }}

  {{- include "nplus.networkpolicy.allowFromAdmin" . | nindent 2 }}
  {{- include "nplus.networkpolicy.allowFromMon" . | nindent 2 }}

  egress:

  #
  # access to other storage-layer in the same instance
  #
  - to:
    - podSelector:
        matchLabels:
          nplus/group: {{ .instance.group }}
          nplus/type: nstl

  {{- if ((.this.security).cni).nstlIpRange }}
  #
  # Allow access to out-of-cluster Storage Layer
  #
  - to:
    - ipBlock:
        cidr: {{ .this.security.cni.nstlIpRange | quote }}
  {{- end }}

{{- end }}