# Two edges the base's default-deny policy blocks, and the capstone's health
# needs both. Only the hardened overlay gets these four policies. The vulnerable
# overlay stays flat, so a policy that selects a Pod here cannot cut its
# traffic there.
#
# Redis: the catalog's readiness handler pings Redis (app/catalog/main.go,
# handleReadyz). Without the two Redis policies the ping fails, the catalog
# never becomes Ready, and it never joins its Service. Both ends need an allow,
# because egress and ingress are both default-denied.
#
# Migration: the db-migrate Job's Pods carry app: db-migrate. No base policy
# names that label, so under the base policies the migration cannot reach
# postgres. The base the lab ran against had the same gap, and this overlay
# closed it for the capstone only. The current base (Part 02 ch.06) allows both
# edges in allow-data-tier-ingress and allow-backend-egress, so on it these four
# policies repeat rules. NetworkPolicies are additive, so the repeat is harmless.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-catalog-to-redis
  namespace: bookstore
  labels:
    app.kubernetes.io/part-of: bookstore
spec:
  podSelector:
    matchLabels:
      app: redis
  policyTypes: ["Ingress"]
  ingress:
    - from:
        - podSelector:
            matchLabels:
              app: catalog
      ports:
        - { protocol: TCP, port: 6379 }
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-catalog-egress-to-redis
  namespace: bookstore
  labels:
    app.kubernetes.io/part-of: bookstore
spec:
  podSelector:
    matchLabels:
      app: catalog
  policyTypes: ["Egress"]
  egress:
    - to:
        - podSelector:
            matchLabels:
              app: redis
      ports:
        - { protocol: TCP, port: 6379 }
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-db-migrate-to-postgres
  namespace: bookstore
  labels:
    app.kubernetes.io/part-of: bookstore
spec:
  podSelector:
    matchLabels:
      app: postgres
  policyTypes: ["Ingress"]
  ingress:
    - from:
        - podSelector:
            matchLabels:
              app: db-migrate
      ports:
        - { protocol: TCP, port: 5432 }
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-db-migrate-egress
  namespace: bookstore
  labels:
    app.kubernetes.io/part-of: bookstore
spec:
  podSelector:
    matchLabels:
      app: db-migrate
  policyTypes: ["Egress"]
  egress:
    - to:
        - podSelector:
            matchLabels:
              app: postgres
      ports:
        - { protocol: TCP, port: 5432 }
