apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: purple-lab
  labels:
    app: purple-lab
spec:
  podSelector:
    matchLabels:
      app: purple-lab
  policyTypes:
    - Ingress
    - Egress
  ingress:
    # Allow traffic from ingress controller
    - from:
        - namespaceSelector:
            matchLabels:
              kubernetes.io/metadata.name: ingress-nginx
      ports:
        - port: 57375
          protocol: TCP
  egress:
    # Allow DNS resolution
    - to:
        - namespaceSelector: {}
      ports:
        - port: 53
          protocol: UDP
        - port: 53
          protocol: TCP
    # Allow connections to PostgreSQL
    - to:
        - podSelector:
            matchLabels:
              app: postgres-purple-lab
      ports:
        - port: 5432
          protocol: TCP
    # Allow outbound HTTPS (GitHub OAuth, external APIs)
    - to:
        - ipBlock:
            cidr: 0.0.0.0/0
            except:
              - 10.0.0.0/8
              - 172.16.0.0/12
              - 192.168.0.0/16
      ports:
        - port: 443
          protocol: TCP
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: postgres-purple-lab
  labels:
    app: postgres-purple-lab
spec:
  podSelector:
    matchLabels:
      app: postgres-purple-lab
  policyTypes:
    - Ingress
    - Egress
  ingress:
    # Only allow connections from purple-lab pods
    - from:
        - podSelector:
            matchLabels:
              app: purple-lab
      ports:
        - port: 5432
          protocol: TCP
  egress:
    # Allow DNS resolution only
    - to:
        - namespaceSelector: {}
      ports:
        - port: 53
          protocol: UDP
        - port: 53
          protocol: TCP
