aboutsummaryrefslogtreecommitdiffstats
path: root/resources/libraries/bash/shell/k8s_utils.sh
blob: b96ec8df6c6bb78120cd261dba67b9dc22d8de05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash
# Copyright (c) 2021 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

function k8s_utils.destroy {
    # Destroy existing Kubernetes deployment
    kubectl drain $HOSTNAME --delete-local-data --force --ignore-daemonsets
    kubectl delete node $HOSTNAME

    # Revert any changes made to this host by 'kubeadm init' or 'kubeadm join'
    sudo kubeadm reset --force && sudo rm -rf $HOME/.kube || \
        { echo "Failed to reset kubeadm"; exit 1; }
}

function k8s_utils.prepare {
    # Sets up the Kubernetes master

    # Disable swap
    sudo swapoff --all

    # Set up the Kubernetes master
    sudo -E kubeadm init --token-ttl 0 ${1} || \
        { echo "Failed to init kubeadm"; exit 1; }

    # Make cgroup non-exclusive for CPU and MEM
    sudo cgset -r cpuset.cpu_exclusive=0 /kubepods
    sudo cgset -r cpuset.mem_exclusive=0 /kubepods

    rm -rf $HOME/.kube
    mkdir -p $HOME/.kube
    sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
    sudo chown $(id -u):$(id -g) $HOME/.kube/config
}

function k8s_utils.taint {
    # Updates the taints
    kubectl taint nodes --all node-role.kubernetes.io/master- || \
        { echo "Failed to taint nodes"; exit 1; }
}

function k8s_utils.calico_deploy {
    # Calico yaml URL or file
    k8s_calico=$1

    # Apply resources
    kubectl apply -f ${k8s_calico}  || \
        { echo "Failed to apply ${k8s_calico}"; exit 1; }

    # Update the taints
    k8s_utils.taint
}

function k8s_utils.contiv_vpp_deploy {
    # Contiv yaml URL or file
    k8s_contiv=$1
    k8s_contiv_patch="kubecon.contiv-vpp-yaml-patch.diff"

    # Pull the most recent Docker images
    url="https://raw.githubusercontent.com/contiv/vpp/master/k8s/pull-images.sh"
    bash <(curl -s "${url}")

    # Apply resources
    wget ${k8s_contiv}
    patch contiv-vpp.yaml -i ${k8s_contiv_patch} -o - | kubectl apply -f - || \
        { echo "Failed to apply Contiv resources"; exit 1; }
    rm contiv-vpp.yaml

    # Update the taints
    k8s_utils.taint
}

function k8s_utils.cri_shim_install {
    # Install the CRI Shim on host
    url"https://raw.githubusercontent.com/contiv/vpp/master/k8s/cri-install.sh"
    sudo su root -c "bash <(curl -s '${url}')"
}

function k8s_utils.cri_shim_uninstall {
    # Uninstall the CRI Shim on host
    url="https://raw.githubusercontent.com/contiv/vpp/master/k8s/cri-install.sh"
    sudo su root -c "bash <(curl -s '${url}') --uninstall"
}

function k8s_utils.kube_proxy_install {
    # Installing custom version of Kube-Proxy to enable Kubernetes services
    url="https://raw.githubusercontent.com/contiv/vpp/master/k8s/"
    url+="proxy-install.sh"
    bash <(curl -s "${url}")
}

function k8s_utils.apply {
    # Resource yaml URL or file
    k8s_resource=$1

    # Apply resources
    kubectl apply -f ${k8s_resource}  || \
        { echo "Failed to apply ${k8s_resource}"; exit 1; }
}

function k8s_utils.resource_delete {
    # Resource yaml URL or file
    k8s_resource=$1

    # Delete resources
    kubectl delete -f ${k8s_resource}  || \
        { echo "Failed to delete ${k8s_resource}"; exit 1; }
}

function k8s_utils.affinity_non_vpp {
    # Set affinity for all non VPP docker containers to CPU 0
    command='sudo docker ps --format "{{.ID}} {{.Names}}"'
    command+=" | grep -v vpp | cut -d' ' -f1"
    for i in $(${command}); do
        sudo docker update --cpuset-cpus 0 ${i}
    done
}

function k8s_utils.dump_all {
    # Dumps the kubernetes objects
    kubectl get all --all-namespaces
    kubectl describe nodes
}