#!/usr/bin/env bash

knativeserving() {
  cat <<EOF
apiVersion: operator.knative.dev/v1beta1
kind: KnativeServing
metadata:
  name: knative-serving
  namespace: knative-serving
spec:
  config:
    logging:
      loglevel.activator: info
      loglevel.autoscaler: info
      loglevel.controller: info
      loglevel.domainmapping: info
      loglevel.domainmapping-webhook: info
      loglevel.hpaautoscaler: info
      loglevel.queueproxy: info
      loglevel.webhook: info
  controller-custom-certs:
    name: ""
    type: ""
  registry: {}
EOF
}

knativeeventing() {
  cat <<EOF
apiVersion: operator.knative.dev/v1beta1
kind: KnativeEventing
metadata:
  name: knative-eventing
  namespace: knative-eventing
spec:
  config:
    logging:
      loglevel.controller: "info"
      loglevel.webhook: "info"
      loglevel.kafkachannel-dispatcher: "info"
      loglevel.kafkachannel-controller: "info"
      loglevel.kafkachannel-webhook: "info"
      loglevel.inmemorychannel-dispatcher: "info"
      loglevel.mt-broker-controller: "info"
      loglevel.mt_broker_filter: "info"
      loglevel.mt_broker_ingress: "info"
EOF
}

knativekafka() {
  cat <<EOF
apiVersion: operator.serverless.openshift.io/v1alpha1
kind: KnativeKafka
metadata:
  name: knative-kafka
  namespace: knative-eventing
spec:
  broker:
    defaultConfig:
      authSecretName: ""
      bootstrapServers: my-cluster-kafka-bootstrap.kafka:9092
      numPartitions: 10
      replicationFactor: 3
    enabled: true
  channel:
    authSecretName: ""
    authSecretNamespace: ""
    bootstrapServers: my-cluster-kafka-bootstrap.kafka:9092
    enabled: false
  high-availability:
    replicas: 1
  logging:
    level: INFO
  sink:
    enabled: true
  source:
    enabled: true
EOF
}

create() {
  oc create -f -
}

wait_for_serving_readiness() {
  oc wait --for=condition=Ready=true knativeserving knative-serving -n knative-serving --timeout=5m
}

wait_for_eventing_readiness() {
  oc wait --for=condition=Ready=true knativeeventing knative-eventing -n knative-eventing --timeout=5m
}

wait_for_knativekafka_readiness() {
  oc wait --for=condition=Ready=true knativekafka knative-kafka -n knative-eventing --timeout=5m
}

wait_for_serving_nonexistence() {
  while true
  do
    oc get pod -n knative-serving -l app=webhook --no-headers=true | grep -q webhook || oc get pod -n knative-serving -l app=controller --no-headers=true | grep -q controller || break
  done
}

wait_for_eventing_nonexistence() {
  while true
  do
    oc get pod -n knative-eventing -l role=eventing-webhook --no-headers=true | grep -q webhook || oc get pod -n knative-eventing -l app=eventing-controller --no-headers=true | grep -q controller || break
  done
}

wait_for_knativekafka_nonexistence() {
  while true
  do
    oc get pod -n knative-eventing -l role=kafka-webhook --no-headers=true | grep -q webhook || oc get pod -n knative-eventing -l control-plane=kafka-controller-manager --no-headers=true | grep -q controller || break
  done
}

serving_delete_create_wait_delete_create() {
  oc delete knativeserving -n knative-serving knative-serving
  wait_for_serving_nonexistence
  knativeserving | create && wait_for_serving_readiness
  oc delete knativeserving -n knative-serving knative-serving
  knativeserving | create
}

eventing_delete_create_wait_delete_create() {
  oc delete knativekafka -n knative-eventing knative-kafka
  wait_for_knativekafka_nonexistence
  oc delete knativeeventing -n knative-eventing knative-eventing
  wait_for_eventing_nonexistence
  knativeeventing | create && wait_for_eventing_readiness
  oc delete knativeeventing -n knative-eventing knative-eventing
  knativeeventing | create
  knativekafka | create
}

knativeserving | create && wait_for_serving_readiness
knativeeventing | create && wait_for_eventing_readiness
knativekafka | create && wait_for_knativekafka_readiness

i=0
while true
do
  i=$(($i+1))
  echo "Iteration $i"
  serving_delete_create_wait_delete_create &
  eventing_delete_create_wait_delete_create &
  wait
  wait_for_eventing_readiness || break
  wait_for_knativekafka_readiness || break
  wait_for_serving_readiness || break
done


