# #!/bin/sh

Install homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

# Install virtualbox
brew cask install virtualbox

# Install kubectl
brew install kubectl

# Install minikube
curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.25.0/minikube-darwin-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/

# Install Helm
brew install kubernetes-helm

minikube start \
 --memory 8129 \
 --cpus 2 \
 --disk-size 30g \
 --extra-config=controller-manager.ClusterSigningCertFile="/var/lib/localkube/certs/ca.crt" \
 --extra-config=controller-manager.ClusterSigningKeyFile="/var/lib/localkube/certs/ca.key" \
 --extra-config=apiserver.Admission.PluginNames=NamespaceLifecycle,LimitRanger,ServiceAccount,PersistentVolumeLabel,DefaultStorageClass,DefaultTolerationSeconds,MutatingAdmissionWebhook,ValidatingAdmissionWebhook,ResourceQuota \
 --kubernetes-version=v1.9.0

echo "Waiting for minikube to be ready..."
sleep 60

# Create kafka namespace
kubectl create namespace kafka

# Create develop namespace
kubectl create namespace develop


# Enable local urls in hosts file
echo "In order to use pulse locally in minikube you need to add the cluster ip to your /etc/hosts"
echo "$(minikube ip) api.pulse.local dashboard.pulse.local admin.pulse.local" | sudo tee -a /etc/hosts

echo "###########################################"
echo "API URL: http://api.pulse.local"
echo "DASHBOARD URL: http://dashboard.pulse.local"
echo "ADMIN URL: http://admin.pulse.local"
echo "###########################################"

kubectl create -f "$1/scripts/local-k8s/kube-configs/helm-rbac-config.yaml"

# Install the helm tiller inside your cluster
helm init

echo "Waiting for helm tiller to be ready..."
sleep 120

# Install schema-registry
helm repo add incubator http://storage.googleapis.com/kubernetes-charts-incubator
helm install --name kafka -f "$1/scripts/local-k8s/helm-values/registry-values.yaml" --namespace kafka incubator/schema-registry

echo "Waiting for kafka to be ready..."
sleep 60

# Install schema-ui
kubectl create -f "$1/scripts/local-k8s/kube-configs/schema-ui-deployment.yaml" -n kafka

echo "Setting up avro schema ui..."
sleep 30

# Launch minikube dashboard in browser
minikube dashboard

# kubectl apply -f $1/scripts/local-k8s/istio-0.7.1/install/kubernetes/istio.yaml
#
# kubectl api-versions | grep admissionregistration
#
# kubectl apply -f $1/scripts/local-k8s/istio-0.7.1/install/kubernetes/istio.yaml
#
# $1/scripts/local-k8s/istio-0.7.1/install/kubernetes/webhook-create-signed-cert.sh \
#     --service istio-sidecar-injector \
#     --namespace istio-system \
#     --secret sidecar-injector-certs
#
# kubectl apply -f $1/scripts/local-k8s/istio-0.7.1/install/kubernetes/istio-sidecar-injector-configmap-release.yaml
#
# cat $1/scripts/local-k8s/istio-0.7.1/install/kubernetes/istio-sidecar-injector.yaml | \
#      $1/scripts/local-k8s/istio-0.7.1/install/kubernetes/webhook-patch-ca-bundle.sh > \
#      $1/scripts/local-k8s/istio-0.7.1/install/kubernetes/istio-sidecar-injector-with-ca-bundle.yaml
#
# kubectl apply -f $1/scripts/local-k8s/istio-0.7.1/install/kubernetes/istio-sidecar-injector-with-ca-bundle.yaml
#
# kubectl label namespace develop istio-injection=enabled
#

kubectl create -f $1/scripts/local-k8s/kube-configs/ambassador.yaml

kubectl apply -f https://getambassador.io/yaml/ambassador/ambassador-rbac.yaml



# Launch shema registry ui in browser
#echo "Visit http://localhost:8000 to use your applications schema registry."
#kubectl port-forward $(kubectl get pods -n kafka -l "name=schema-ui" -o jsonpath="{.items[0].metadata.name}") 8000:8000 -n kafka

#sleep 20
#open http://localhost:8000
