#!/bin/bash

set -x

oc patch networks.operator.openshift.io cluster --type=merge -p '{"spec":{"defaultNetwork":{"ovnKubernetesConfig":{"gatewayConfig":{"routingViaHost": true}}}}}'
oc apply -f - <<EOF
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
  name: servicemeshoperator3
  namespace: openshift-operators
spec:
  channel: stable-3.2
  installPlanApproval: Automatic
  name: servicemeshoperator3
  source: redhat-operators
  sourceNamespace: openshift-marketplace
EOF
sleep 10
oc wait --for=jsonpath='{.status.state}'=AtLatestKnown subscription/servicemeshoperator3 -n openshift-operators --timeout=3m
oc create namespace istio-system
oc apply -f - <<EOF
apiVersion: sailoperator.io/v1
kind: Istio
metadata:
  name: default
spec:
  namespace: istio-system
  profile: ambient
  updateStrategy:
    inactiveRevisionDeletionGracePeriodSeconds: 30
    type: InPlace
  values:
    global:
      logging:
        level: debug
    pilot:
      env:
        COMPLIANCE_POLICY: ""
      trustedZtunnelNamespace: ztunnel
  version: v1.27-latest
EOF
oc wait --for=condition=Ready istios/default --timeout=3m
oc create namespace istio-cni
oc apply -f - <<EOF
apiVersion: sailoperator.io/v1
kind: IstioCNI
metadata:
  name: default
spec:
  namespace: istio-cni
  profile: ambient
  values:
    cni:
      ambient:
        reconcileIptablesOnStartup: true
EOF
oc wait --for=condition=Ready istios/default --timeout=3m
oc create namespace ztunnel
oc apply -f - <<EOF
apiVersion: sailoperator.io/v1
kind: ZTunnel
metadata:
  name: default
spec:
  namespace: ztunnel
  profile: ambient
  values:
    ztunnel:
      logLevel: debug
EOF
oc wait --for=condition=Ready ztunnel/default --timeout=3m
