# services/hermes-chat/networkpolicy.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: hermes-chat-isolation
  namespace: hermes-chat
spec:
  podSelector:
    matchLabels:
      app: hermes-chat
  policyTypes:
    - Ingress
    - Egress
  ingress:
    - from:
        - namespaceSelector:
            matchLabels:
              kubernetes.io/metadata.name: traefik
          podSelector:
            matchLabels:
              app.kubernetes.io/name: traefik
      ports:
        - protocol: TCP
          port: 9119
  egress:
    - to:
        - namespaceSelector:
            matchLabels:
              kubernetes.io/metadata.name: kube-system
          podSelector:
            matchLabels:
              k8s-app: kube-dns
      ports:
        - protocol: UDP
          port: 53
        - protocol: TCP
          port: 53
    - to:
        - namespaceSelector:
            matchLabels:
              kubernetes.io/metadata.name: hermes
          podSelector:
            matchLabels:
              app: hermes-model-gate
      ports:
        - protocol: TCP
          port: 8080
    # Atlas OIDC uses the public sso.bstein.dev issuer, which resolves to the
    # in-cluster Traefik load balancer for pods on the Atlas network.
    - to:
        - namespaceSelector:
            matchLabels:
              kubernetes.io/metadata.name: traefik
          podSelector:
            matchLabels:
              app: traefik
      ports:
        - protocol: TCP
          port: 443
    - to:
        - ipBlock:
            cidr: 10.43.0.1/32
      ports:
        - protocol: TCP
          port: 443
    # K3s applies egress policy after Service DNAT, so admit only the fixed API
    # server endpoints as well as the kubernetes Service IP above.
    - to:
        - ipBlock:
            cidr: 192.168.22.11/32
        - ipBlock:
            cidr: 192.168.22.12/32
        - ipBlock:
            cidr: 192.168.22.13/32
      ports:
        - protocol: TCP
          port: 6443
    - to:
        - ipBlock:
            cidr: 0.0.0.0/0
            except:
              - 10.0.0.0/8
              - 100.64.0.0/10
              - 127.0.0.0/8
              - 169.254.0.0/16
              - 172.16.0.0/12
              - 192.168.0.0/16
