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:
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 bash <(curl -s https://raw.githubusercontent.com/contiv/vpp/master/k8s/pull-images.sh)
73 patch contiv-vpp.yaml -i ${k8s_contiv_patch} -o - | kubectl apply -f - || \
74 { echo "Failed to apply Contiv resources"; exit 1; }
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)'
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'
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)
96 function k8s_utils.apply {
97 # Resource yaml URL or file
101 kubectl apply -f ${k8s_resource} || \
102 { echo "Failed to apply ${k8s_resource}"; exit 1; }
105 function k8s_utils.resource_delete {
106 # Resource yaml URL or file
110 kubectl delete -f ${k8s_resource} || \
111 { echo "Failed to delete ${k8s_resource}"; exit 1; }
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}
121 function k8s_utils.dump_all {
122 # Dumps the kubernetes objects
123 kubectl get all --all-namespaces
124 kubectl describe nodes