#!/usr/bin/env bash
# cold-admission.sh <image> <rounds> - restart Kyverno's admission controller (which empties
# its verification cache), wait until its image webhook is registered again, then time the
# first admission of <image> and the one right after it.
here="$(cd "$(dirname "$0")" && pwd)"
for r in $(seq 1 "${2:-5}"); do
  kubectl -n kyverno rollout restart deployment/kyverno-admission-controller >/dev/null
  kubectl -n kyverno rollout status deployment/kyverno-admission-controller --timeout=300s >/dev/null
  # ready = the webhook the guard looks for exists again
  until kubectl get validatingwebhookconfiguration kyverno-resource-validating-webhook-cfg -o json 2>/dev/null \
        | grep -q 'svc-fail-finegrained-verify-images'; do sleep 2; done
  sleep 5
  "$here/time-admission.sh" "round$r-cold" "$1" 1
  "$here/time-admission.sh" "round$r-warm" "$1" 1
done
