# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
{{- if .Values.features.cilium.enabled }}

---

apiVersion: "cilium.io/v2"
kind: CiliumNetworkPolicy
metadata:
  name: "recommendation"
  namespace: {{ .Values.ciliumServices.namespace }}
spec:
  endpointSelector:
    matchLabels:
      app: recommendation
  ingress:
    - fromEndpoints:
        - matchLabels:
            "k8s:app": rating
        - matchLabels:
            "k8s:app": songs
        - matchLabels:
            "k8s:app": gateway
        - matchLabels:
            "k8s:app": demo
            "k8s:component": satellite
  egress:
    - toEndpoints:
        - matchLabels:
            "k8s:io.kubernetes.pod.namespace": kube-system
            "k8s:k8s-app": kube-dns
      toPorts:
        - ports:
            - port: "53"
              protocol: ANY
          rules:
            dns:
              - matchPattern: "*"
    - toEndpoints:
        - matchLabels:
            "k8s:app": rating
        - matchLabels:
            "k8s:app": songs
        - matchLabels:
            "k8s:app": gateway
        - matchLabels:
            "k8s:component": satellite
      toPorts:
        - ports:
            - port: "80"
              protocol: TCP
          rules:
            http:
              - method: ""
                path: ""

---

apiVersion: "cilium.io/v2"
kind: CiliumNetworkPolicy
metadata:
  name: "gateway"
  namespace: {{ .Values.ciliumServices.namespace }}
spec:
  endpointSelector:
    matchLabels:
      app: gateway
  ingress:
    - fromEndpoints:
        - matchLabels:
            "k8s:app": recommendation
        - matchLabels:
            "k8s:app": songs
        - matchLabels:
            "k8s:app": app
        - matchLabels:
            "k8s:app": demo
            "k8s:component": satellite
  egress:
    - toEndpoints:
        - matchLabels:
            "k8s:io.kubernetes.pod.namespace": kube-system
            "k8s:k8s-app": kube-dns
      toPorts:
        - ports:
            - port: "53"
              protocol: ANY
          rules:
            dns:
              - matchPattern: "*"
    - toEndpoints:
        - matchLabels:
            "k8s:app": recommendation
        - matchLabels:
            "k8s:app": songs
        - matchLabels:
            "k8s:app": app
        - matchLabels:
            "k8s:component": satellite
      toPorts:
        - ports:
            - port: "80"
              protocol: TCP
          rules:
            http:
              - method: ""
                path: ""

---

apiVersion: "cilium.io/v2"
kind: CiliumNetworkPolicy
metadata:
  name: "app"
  namespace: {{ .Values.ciliumServices.namespace }}
spec:
  endpointSelector:
    matchLabels:
      app: app
  ingress:
    - fromEndpoints:
        - matchLabels:
            "k8s:app": frontend
        - matchLabels:
            "k8s:app": gateway
        - matchLabels:
            "k8s:app": demo
            "k8s:component": satellite
        - matchLabels:
            "k8s:app": demo
            "k8s:component": oap
  egress:
    - toEndpoints:
        - matchLabels:
            "k8s:io.kubernetes.pod.namespace": kube-system
            "k8s:k8s-app": kube-dns
      toPorts:
        - ports:
            - port: "53"
              protocol: ANY
          rules:
            dns:
              - matchPattern: "*"
    - toEndpoints:
        - matchLabels:
            "k8s:app": frontend
        - matchLabels:
            "k8s:app": gateway
        - matchLabels:
            "k8s:component": satellite
        - matchLabels:
            "k8s:app": demo
            "k8s:component": oap
      toPorts:
        - ports:
            - port: "80"
              protocol: TCP
          rules:
            http:
              - method: ""
                path: ""
        - ports:
            - port: "11800"
              protocol: TCP

---

apiVersion: "cilium.io/v2"
kind: CiliumNetworkPolicy
metadata:
  name: "loadgen"
  namespace: {{ .Values.ciliumServices.namespace }}
spec:
  endpointSelector:
    matchLabels:
      app: loadgen
  ingress:
    - fromEndpoints:
        - matchLabels:
            "k8s:app": frontend
        - matchLabels:
            "k8s:app": demo
            "k8s:component": satellite
        - matchLabels:
            "k8s:app": demo
            "k8s:component": oap
  egress:
    - toEndpoints:
        - matchLabels:
            "k8s:io.kubernetes.pod.namespace": kube-system
            "k8s:k8s-app": kube-dns
      toPorts:
        - ports:
            - port: "53"
              protocol: ANY
          rules:
            dns:
              - matchPattern: "*"
    - toEndpoints:
        - matchLabels:
            "k8s:app": frontend
        - matchLabels:
            "k8s:component": satellite
        - matchLabels:
            "k8s:component": oap
      toPorts:
        - ports:
            - port: "80"
              protocol: TCP
          rules:
            http:
              - method: ""
                path: ""
        - ports:
            - port: "11800"
              protocol: TCP
        - ports:
            - port: "12800"
              protocol: TCP
          rules:
            http:
              - method: ""
                path: ""

---

apiVersion: "cilium.io/v2"
kind: CiliumNetworkPolicy
metadata:
  name: "frontend"
  namespace: {{ .Values.ciliumServices.namespace }}
spec:
  endpointSelector:
    matchLabels:
      app: frontend
  ingress:
    - fromEndpoints:
        - matchLabels:
            "k8s:app": loadgen
        - matchLabels:
            "k8s:app": app
        - matchLabels:
            "k8s:app": demo
            "k8s:component": satellite
        - matchLabels:
            "k8s:app": demo
            "k8s:component": oap
        - matchLabels:
            "k8s:app.kubernetes.io/name": opentelemetry-collector
            "k8s:component": standalone-collector
  egress:
    - toEndpoints:
        - matchLabels:
            "k8s:io.kubernetes.pod.namespace": kube-system
            "k8s:k8s-app": kube-dns
      toPorts:
        - ports:
            - port: "53"
              protocol: ANY
          rules:
            dns:
              - matchPattern: "*"
    - toEndpoints:
        - matchLabels:
            "k8s:app": loadgen
        - matchLabels:
            "k8s:app": app
        - matchLabels:
            "k8s:app": demo
            "k8s:component": satellite
        - matchLabels:
            "k8s:app": demo
            "k8s:component": oap
        - matchLabels:
            "k8s:app.kubernetes.io/name": opentelemetry-collector
            "k8s:component": standalone-collector
      toPorts:
        - ports:
            - port: "80"
              protocol: TCP
          rules:
            http:
              - method: ""
                path: ""
        - ports:
            - port: "11800"
              protocol: TCP
            - port: "12800"
              protocol: TCP
            - port: "4318"
              protocol: TCP

{{- end }}

