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:
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
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; }
25 function k8s_utils.prepare {
26 # Sets up the Kubernetes master
31 # Set up the Kubernetes master
32 sudo -E kubeadm init --token-ttl 0 ${1} || \
33 { echo "Failed to init kubeadm"; exit 1; }
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
41 sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
42 sudo chown $(id -u):$(id -g) $HOME/.kube/config
45 function k8s_utils.taint {
47 kubectl taint nodes --all node-role.kubernetes.io/master- || \
48 { echo "Failed to taint nodes"; exit 1; }
51 function k8s_utils.calico_deploy {
52 # Calico yaml URL or file
56 kubectl apply -f ${k8s_calico} || \
57 { echo "Failed to apply ${k8s_calico}"; exit 1; }
63 function k8s_utils.contiv_vpp_deploy {
64 # Contiv yaml URL or file
66 k8s_contiv_patch="kubecon.contiv-vpp-yaml-patch.diff"
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}")
74 patch contiv-vpp.yaml -i ${k8s_contiv_patch} -o - | kubectl apply -f - || \
75 { echo "Failed to apply Contiv resources"; exit 1; }
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}')"
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"
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}")
101 function k8s_utils.apply {
102 # Resource yaml URL or file
106 kubectl apply -f ${k8s_resource} || \
107 { echo "Failed to apply ${k8s_resource}"; exit 1; }
110 function k8s_utils.resource_delete {
111 # Resource yaml URL or file
115 kubectl delete -f ${k8s_resource} || \
116 { echo "Failed to delete ${k8s_resource}"; exit 1; }
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}
128 function k8s_utils.dump_all {
129 # Dumps the kubernetes objects
130 kubectl get all --all-namespaces
131 kubectl describe nodes