CSIT-870 Kubernetes/Ligato integration
[csit.git] / resources / libraries / bash / shell / k8s_utils.sh
1 #!/bin/bash
2 # Copyright (c) 2018 Cisco and/or its affiliates.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 function k8s_utils.destroy {
16     # Destroy existing Kubernetes deployment
17     kubectl drain $HOSTNAME --delete-local-data --force --ignore-daemonsets
18     kubectl delete node $HOSTNAME
19
20     # Revert any changes made to this host by 'kubeadm init' or 'kubeadm join'
21     sudo kubeadm reset && sudo rm -rf $HOME/.kube || \
22         { echo "Failed to reset kubeadm"; exit 1; }
23 }
24
25 function k8s_utils.prepare {
26     # Sets up the Kubernetes master
27
28     # Disable swap
29     sudo swapoff --all
30
31     # Set up the Kubernetes master
32     sudo -E kubeadm init --token-ttl 0 ${1} || \
33         { echo "Failed to init kubeadm"; exit 1; }
34
35     # Make cgroup non-exclusive for CPU and MEM
36     sudo cgset -r cpuset.cpu_exclusive=0 /kubepods
37     sudo cgset -r cpuset.mem_exclusive=0 /kubepods
38
39     rm -rf $HOME/.kube
40     mkdir -p $HOME/.kube
41     sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
42     sudo chown $(id -u):$(id -g) $HOME/.kube/config
43 }
44
45 function k8s_utils.taint {
46     # Updates the taints
47     kubectl taint nodes --all node-role.kubernetes.io/master- || \
48         { echo "Failed to taint nodes"; exit 1; }
49 }
50
51 function k8s_utils.calico_deploy {
52     # Calico yaml URL or file
53     k8s_calico=$1
54
55     # Apply resources
56     kubectl apply -f ${k8s_calico}  || \
57         { echo "Failed to apply ${k8s_calico}"; exit 1; }
58
59     # Update the taints
60     k8s_utils.taint
61 }
62
63 function k8s_utils.contiv_vpp_deploy {
64     # Contiv yaml URL or file
65     k8s_contiv=$1
66     k8s_contiv_patch="kubecon.contiv-vpp-yaml-patch.diff"
67
68     # Pull the most recent Docker images
69     bash <(curl -s https://raw.githubusercontent.com/contiv/vpp/master/k8s/pull-images.sh)
70
71     # Apply resources
72     wget ${k8s_contiv}
73     patch contiv-vpp.yaml -i ${k8s_contiv_patch} -o - | kubectl apply -f - || \
74         { echo "Failed to apply Contiv resources"; exit 1; }
75     rm contiv-vpp.yaml
76
77     # Update the taints
78     k8s_utils.taint
79 }
80
81 function k8s_utils.cri_shim_install {
82     # Install the CRI Shim on host
83     sudo su root -c 'bash <(curl -s https://raw.githubusercontent.com/contiv/vpp/master/k8s/cri-install.sh)'
84 }
85
86 function k8s_utils.cri_shim_uninstall {
87     # Uninstall the CRI Shim on host
88     sudo su root -c 'bash <(curl -s https://raw.githubusercontent.com/contiv/vpp/master/k8s/cri-install.sh) --uninstall'
89 }
90
91 function k8s_utils.kube_proxy_install {
92     # Installing custom version of Kube-Proxy to enable Kubernetes services
93     bash <(curl -s https://raw.githubusercontent.com/contiv/vpp/master/k8s/proxy-install.sh)
94 }
95
96 function k8s_utils.apply {
97     # Resource yaml URL or file
98     k8s_resource=$1
99
100     # Apply resources
101     kubectl apply -f ${k8s_resource}  || \
102         { echo "Failed to apply ${k8s_resource}"; exit 1; }
103 }
104
105 function k8s_utils.resource_delete {
106     # Resource yaml URL or file
107     k8s_resource=$1
108
109     # Delete resources
110     kubectl delete -f ${k8s_resource}  || \
111         { echo "Failed to delete ${k8s_resource}"; exit 1; }
112 }
113
114 function k8s_utils.affinity_non_vpp {
115     # Set affinity for all non VPP docker containers to CPU 0
116     for i in `sudo docker ps --format "{{.ID}} {{.Names}}" | grep -v vpp | cut -d' ' -f1`; do
117         sudo docker update --cpuset-cpus 0 ${i}
118     done
119 }
120
121 function k8s_utils.dump_all {
122     # Dumps the kubernetes objects
123     kubectl get all --all-namespaces
124     kubectl describe nodes
125 }