version: 1
id: gateway-api-http-route
title: Apply a Gateway API HTTPRoute
summary: Apply GatewayClass, Gateway, and HTTPRoute objects on a live k3d cluster that expose payments-api on api.payments.example.
difficulty: intermediate
estimatedMinutes: 40
prerequisites: [kubernetes-ingress, kubernetes-service]
image: alpine/k8s:1.31.4
shell: /bin/bash
runtime:
  type: k3d
  addons: [gateway]
setup:
  - "mkdir -p /workspace/gateway /workspace/docs"
  - "printf 'Ticket: migrate api.payments.example from Ingress to Gateway API.\\nNeed GatewayClass, Gateway listener, and HTTPRoute to Service payments-api:80.\\nGateway API CRDs are already installed — apply your manifests.\\n' > /workspace/gateway/TICKET.md"
  - "kubectl create deploy payments-api --image=nginx:1.27-alpine --port=80 >/dev/null"
  - "kubectl expose deploy payments-api --port=80 --target-port=80 >/dev/null"
  - "kubectl wait --for=condition=established crd/gateways.gateway.networking.k8s.io --timeout=120s"
tasks:
  - id: gateway-objects
    title: Apply GatewayClass and Gateway
    description: 'Create /workspace/gateway/gatewayclass.yaml with kind: GatewayClass and metadata.name payments-gatewayclass (controllerName can be example.com/payments-gateway). Create /workspace/gateway/gateway.yaml with kind: Gateway, metadata.name payments-gateway, gatewayClassName payments-gatewayclass, and a listener for hostname api.payments.example on port 80 (HTTP). Apply both with kubectl.'
    hints:
      - 'Tip code GW_CLASS: GatewayClass selects the controller implementation.'
      - 'Tip code GW_LISTENER: Gateway listeners declare hostname/port/protocol.'
      - 'kubectl apply -f /workspace/gateway/gatewayclass.yaml -f /workspace/gateway/gateway.yaml'
    checks:
      - type: file
        name: GatewayClass present
        path: /workspace/gateway/gatewayclass.yaml
        value: "kind: GatewayClass"
      - type: file
        name: GatewayClass named
        path: /workspace/gateway/gatewayclass.yaml
        value: payments-gatewayclass
      - type: file
        name: Gateway present
        path: /workspace/gateway/gateway.yaml
        value: "kind: Gateway"
      - type: file
        name: Gateway references class
        path: /workspace/gateway/gateway.yaml
        value: payments-gatewayclass
      - type: file
        name: Listener hostname set
        path: /workspace/gateway/gateway.yaml
        value: api.payments.example
      - type: kubernetes
        name: GatewayClass applied
        command: kubectl get gatewayclass payments-gatewayclass
      - type: kubernetes
        name: Gateway applied
        command: kubectl get gateway payments-gateway
  - id: httproute
    title: Route to payments-api
    description: 'Create /workspace/gateway/httproute.yaml with kind: HTTPRoute, metadata.name payments-api-route, parentRefs to payments-gateway, hostnames including api.payments.example, and a backendRef to Service payments-api port 80. Apply it. Write /workspace/docs/MIGRATION.md mentioning Ingress and Gateway API. Write /workspace/gateway/STATUS.txt containing GATEWAY_OK.'
    hints:
      - 'Tip code HTTP_ROUTE: HTTPRoute attaches to a Gateway via parentRefs and targets Services via backendRefs.'
      - 'Tip code MIGRATE_INGRESS: document how Gateway API replaces classic Ingress hosts.'
      - 'STATUS.txt should include GATEWAY_OK.'
    checks:
      - type: file
        name: HTTPRoute kind
        path: /workspace/gateway/httproute.yaml
        value: "kind: HTTPRoute"
      - type: file
        name: HTTPRoute named
        path: /workspace/gateway/httproute.yaml
        value: payments-api-route
      - type: file
        name: Parent ref to gateway
        path: /workspace/gateway/httproute.yaml
        value: payments-gateway
      - type: file
        name: Host configured
        path: /workspace/gateway/httproute.yaml
        value: api.payments.example
      - type: file
        name: Backend service named
        path: /workspace/gateway/httproute.yaml
        value: payments-api
      - type: command
        name: Backend port 80
        command: "grep -Eq 'port:[[:space:]]*80' /workspace/gateway/httproute.yaml"
      - type: kubernetes
        name: HTTPRoute applied
        command: kubectl get httproute payments-api-route
      - type: command
        name: Migration mentions both APIs
        command: "grep -qi 'Ingress' /workspace/docs/MIGRATION.md && grep -Eqi 'Gateway API|HTTPRoute' /workspace/docs/MIGRATION.md"
      - type: file
        name: Gateway status ok
        path: /workspace/gateway/STATUS.txt
        value: GATEWAY_OK
limits: {cpus: "1.0", memory: 512m, pids: 256, timeout: 3600, network: true}
