#!/usr/bin/env bash
# 文件: ingress.sh

source $(realpath -m ..)/.vars.conf

set -o pipefail

# 渲染 YAML 配置文件
for yaml in istio.yaml
do
    envsubst < $(realpath -m ..)/yamls/${yaml} >${install_dir}/${yaml}
done

# 安装 Ingress 组件
if [[ "${ingress_name}" == "istio" ]]
then

    printf "$info: [ ${bldgre}istio${txtrst} ] 安装中...\n"
    
    # 安装 icstioctl
    [[ -e /usr/bin/istioctl ]] || tar axvf $(realpath -m ..)/files/istioctl-${istio_version}-${ARCH}.tar.gz -C /usr/bin >/dev/null
    
    # 安装 istio Operator
    istioctl operator init --hub=${image_registry} --tag=${istio_version} >/dev/null
    
    # 创建 istio-system 命名空间
    kubectl create namespace istio-system --dry-run=client -oyaml | kubectl apply -f -  >/dev/null
    
    # 创建 istio 服务
    kubectl apply -f ${install_dir}/istio.yaml >/dev/null
    
    # 更新SVC类型
    #kubectl -n istio-system wait --for=condition=ready pod -l app=istio-ingressgateway --timeout=45s &&  kubectl -n istio-system patch svc istio-ingressgateway -p '{"spec": {"ports": [{"nodePort": 443, "port": 443,"targetPort": 8443,"name": "https"},{"nodePort": 80, "port": 80,"targetPort": 8080,"name": "http2"}],"type": "NodePort"}}'
    kubectl -n istio-system rollout status deploy/istio-ingressgateway -w &&  kubectl -n istio-system patch svc istio-ingressgateway -p '{"spec": {"ports": [{"nodePort": 443, "port": 443,"targetPort": 8443,"name": "https"},{"nodePort": 80, "port": 80,"targetPort": 8080,"name": "http2"}],"type": "NodePort"}}'
fi 
