Report: Add rls data
[csit.git] / resources / libraries / bash / shell / k8s_utils.sh
1 #!/bin/bash
2 # Copyright (c) 2021 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 --force && 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     url="https://raw.githubusercontent.com/contiv/vpp/master/k8s/pull-images.sh"
70     bash <(curl -s "${url}")
71
72     # Apply resources
73     wget ${k8s_contiv}
74     patch contiv-vpp.yaml -i ${k8s_contiv_patch} -o - | kubectl apply -f - || \
75         { echo "Failed to apply Contiv resources"; exit 1; }
76     rm contiv-vpp.yaml
77
78     # Update the taints
79     k8s_utils.taint
80 }
81
82 function k8s_utils.cri_shim_install {
83     # Install the CRI Shim on host
84     url"https://raw.githubusercontent.com/contiv/vpp/master/k8s/cri-install.sh"
85     sudo su root -c "bash <(curl -s '${url}')"
86 }
87
88 function k8s_utils.cri_shim_uninstall {
89     # Uninstall the CRI Shim on host
90     url="https://raw.githubusercontent.com/contiv/vpp/master/k8s/cri-install.sh"
91     sudo su root -c "bash <(curl -s '${url}') --uninstall"
92 }
93
94 function k8s_utils.kube_proxy_install {
95     # Installing custom version of Kube-Proxy to enable Kubernetes services
96     url="https://raw.githubusercontent.com/contiv/vpp/master/k8s/"
97     url+="proxy-install.sh"
98     bash <(curl -s "${url}")
99 }
100
101 function k8s_utils.apply {
102     # Resource yaml URL or file
103     k8s_resource=$1
104
105     # Apply resources
106     kubectl apply -f ${k8s_resource}  || \
107         { echo "Failed to apply ${k8s_resource}"; exit 1; }
108 }
109
110 function k8s_utils.resource_delete {
111     # Resource yaml URL or file
112     k8s_resource=$1
113
114     # Delete resources
115     kubectl delete -f ${k8s_resource}  || \
116         { echo "Failed to delete ${k8s_resource}"; exit 1; }
117 }
118
119 function k8s_utils.affinity_non_vpp {
120     # Set affinity for all non VPP docker containers to CPU 0
121     command='sudo docker ps --format "{{.ID}} {{.Names}}"'
122     command+=" | grep -v vpp | cut -d' ' -f1"
123     for i in $(${command}); do
124         sudo docker update --cpuset-cpus 0 ${i}
125     done
126 }
127
128 function k8s_utils.dump_all {
129     # Dumps the kubernetes objects
130     kubectl get all --all-namespaces
131     kubectl describe nodes
132 }