# Exercise 1: HPA under load (medium)
#
# Prereqs: kind + metrics-server
#   kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
#   (on kind add the arg --kubelet-insecure-tls to the metrics-server Deployment)
# Run: kubectl apply -n lab -f this-file.yaml
#
# The `php-apache` Deployment below burns CPU on every request.
#   TODO 1: give the container requests cpu 200m / memory 64Mi and a memory limit 128Mi
#           (why is the CPU request essential for the HPA? answer in a comment)
#   TODO 2: Service php-apache port 80
#   TODO 3: HPA (autoscaling/v2) min 1, max 8, target 50% CPU utilization
#   TODO 4: behavior: scale up at most 4 Pods per 15 s; scale down at most 1 Pod per
#           60 s with a 120 s stabilization window
#
# Generate load:
#   kubectl run -n lab load --rm -it --image=busybox:1.36 -- sh -c \
#     "while true; do wget -q -O- http://php-apache; done"
# Watch: kubectl get hpa php-apache -n lab -w
#
# CHECKLIST:
# [ ] kubectl get hpa shows a real CPU % (not <unknown>).
# [ ] Under load replicas grow (≤ 4 per 15 s) and stabilize near 50%.
# [ ] After stopping load, replicas shrink 1 per minute after the window.
# [ ] Comment: compute desired replicas if 3 Pods average 150% of target 50%.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: php-apache
spec:
  selector:
    matchLabels:
      app: php-apache
  template:
    metadata:
      labels:
        app: php-apache
    spec:
      containers:
        - name: php-apache
          image: registry.k8s.io/hpa-example
          ports:
            - containerPort: 80
          # TODO 1: resources
---
# TODO 2: Service
# ---
# TODO 3 + 4: HorizontalPodAutoscaler
