#!/bin/bash

# install tidb operator crds
/usr/local/bin/kubectl apply -f https://raw.githubusercontent.com/pingcap/tidb-operator/master/manifests/crd.yaml --kubeconfig="/home/centos/.kube/config"
# install tidb operator
echo "Installing tidb operator ..."
/usr/local/bin/helm repo add pingcap https://charts.pingcap.org/ --kubeconfig="/home/centos/.kube/config"
/usr/local/bin/kubectl create namespace tidb-admin --kubeconfig="/home/centos/.kube/config"
/usr/local/bin/helm install --namespace tidb-admin tidb-operator pingcap/tidb-operator --version v1.2.3 --kubeconfig="/home/centos/.kube/config"
/usr/local/bin/kubectl wait --namespace tidb-admin \
  --for=condition=ready pod \
  --selector=app.kubernetes.io/component=controller-manager \
  --timeout=120s --kubeconfig="/home/centos/.kube/config"

# install tidb
/usr/local/bin/kubectl create namespace tidb-cluster --kubeconfig="/home/centos/.kube/config"
/usr/local/bin/kubectl apply -f /home/centos/tioperator/files_k8s/tidb-cluster.yaml.template -n tidb-cluster --kubeconfig="/home/centos/.kube/config" 
/usr/local/bin/kubectl apply -f /home/centos/tioperator/files_k8s/tidb-monitor.yaml.template -n tidb-cluster --kubeconfig="/home/centos/.kube/config"

# install nginx ingress and expose tidb dashboard
/usr/local/bin/kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.0.0/deploy/static/provider/aws/deploy.yaml --kubeconfig="/home/centos/.kube/config"
/usr/local/bin/kubectl wait --namespace ingress-nginx \
  --for=condition=ready pod \
  --selector=app.kubernetes.io/component=controller \
  --timeout=120s --kubeconfig="/home/centos/.kube/config"
/usr/local/bin/kubectl apply -f /home/centos/tioperator/files_k8s/tidb-dashboard.yaml.template -n tidb-cluster --kubeconfig="/home/centos/.kube/config"
