#!/usr/bin/env bash

oc create -f - <<EOF
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: autoscale
  annotations:
    serving.knative.openshift.io/enablePassthrough: "true"
spec:
  template:
    metadata:
      annotations:
        sidecar.istio.io/inject: "true"
        sidecar.istio.io/rewriteAppHTTPProbers: "true"
    spec:
      timeoutSeconds: 45
      containers:
      - image: registry.ci.openshift.org/openshift/knative-v0.23.1:knative-serving-test-autoscale
EOF

# workaround: add the following annotation to .spec.template.metadata.annotations
#       sidecar.maistra.io/proxyEnv: '{ "TERMINATION_DRAIN_DURATION_SECONDS": "45" }'

oc wait --for=condition=Ready ksvc autoscale --timeout=5m

url=$(oc get ksvc autoscale -o jsonpath='{.status.url}' | sed 's/^http/https/')

# wait a bit to let the ingress setup
sleep 10

curl -k "$url"

for i in $(seq 6)
do
#  curl -k -o /dev/null -s -w "%{http_code}\n" "$url/?sleep=12001" &
  curl -k "$url/?sleep=12001" &
  sleep 1
done

oc delete pod -l serving.knative.dev/service=autoscale

wait

oc delete ksvc autoscale
