#!/bin/bash

# Setup kubernetes cluster
sudo kubeadm reset   # Delete existing master
rm $HOME/.kube/config
sudo rm -rf /etc/cni/net.d
sudo swapoff -a     # Swapoff
sudo kubeadm init --pod-network-cidr=10.244.0.0/16  # Initialize cluster
mkdir -p $HOME/.kube    
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl apply -f https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml     # Use flannel for networking
kubectl taint nodes ub-10 node-role.kubernetes.io/master-   # Allow device plugins and pods to run on master
kubectl label node ub-10 mps-gpu-enabled=true   # Add device plugin label

# Delete daemonset
# kubectl delete daemonset gpu-device-plugin-daemonset
# kubectl delete clusterrolebinding gpu-device-plugin-manager-role

# Attach daemonset again
# kubectl create namespace gpu-device-plugin-namespace
kubectl create sa mps-device-plugin-manager -n kube-system
kubectl create clusterrolebinding mps-device-plugin-manager-role --clusterrole=cluster-admin --serviceaccount=kube-system:mps-device-plugin-manager
kubectl apply -f device_plugin/mps-manager.yaml

# Start scheduler extender
# sudo cp $HOME/.kube/config /root/.kube/config
# sudo cp /etc/kubernetes/pki/ca.crt /root/.kube/ca.crt
# sudo cp gpu_scheduler_extender/gpu-scheduler-extender-plugin-config.conf /etc/kubernetes/
# sudo cp gpu_scheduler_extender/scheduler_config.yaml /etc/kubernetes/
# sudo cp gpu_scheduler_extender/kube-scheduler.yaml /etc/kubernetes/manifests/
# kubectl apply -f gpu_scheduler_extender/scheduler_extender.yaml
# kubectl apply -f gpu_scheduler_extender/scheduler_config.yaml