2 #SPDX-license-identifier: Apache-2.0
3 ##############################################################################
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
16 INSTALLER_DIR="$(readlink -f "$(dirname "${BASH_SOURCE[0]}")")"
18 function install_prerequisites {
19 #install package for docker images
20 echo "Removing ppa for jonathonf/python-3.6"
21 ls /etc/apt/sources.list.d/ || true
22 find /etc/apt/sources.list.d -maxdepth 1 -name '*jonathonf*' -delete || true
24 apt-get install -y curl vim wget git \
25 software-properties-common python-pip sudo gettext-base
26 add-apt-repository -y ppa:longsleep/golang-backports
28 apt-get install -y golang-go rsync
31 # _install_ansible() - Install and Configure Ansible program
32 function _install_ansible {
33 local version=$(grep "ansible_version" ${kud_playbooks}/kud-vars.yml |
34 awk -F ': ' '{print $2}')
35 mkdir -p /etc/ansible/
36 pip install --no-cache-dir ansible==$version
39 function install_kubespray {
40 echo "Deploying kubernetes"
41 version=$(grep "kubespray_version" ${kud_playbooks}/kud-vars.yml | \
42 awk -F ': ' '{print $2}')
43 local_release_dir=$(grep "local_release_dir" \
44 $kud_inventory_folder/group_vars/k8s-cluster.yml | \
45 awk -F "\"" '{print $2}')
46 local tarball=v$version.tar.gz
47 # install make to run mitogen target & unzip is mitogen playbook dependency
48 apt-get install -y sshpass make unzip
50 wget https://github.com/kubernetes-incubator/kubespray/archive/$tarball
51 tar -C $dest_folder -xzf $tarball
52 chown -R root:root $dest_folder/kubespray-$version
53 mkdir -p ${local_release_dir}/containers
56 pushd $dest_folder/kubespray-$version/
57 pip install --no-cache-dir -r ./requirements.txt
60 rm -f $kud_inventory_folder/group_vars/all.yml 2> /dev/null
61 if [[ -n "${verbose:-}" ]]; then
62 echo "kube_log_level: 5" | tee \
63 $kud_inventory_folder/group_vars/all.yml
65 echo "kube_log_level: 2" | tee \
66 $kud_inventory_folder/group_vars/all.yml
68 echo "kubeadm_enabled: true" | \
69 tee --append $kud_inventory_folder/group_vars/all.yml
70 if [[ -n "${http_proxy:-}" ]]; then
71 echo "http_proxy: \"$http_proxy\"" | tee --append \
72 $kud_inventory_folder/group_vars/all.yml
74 if [[ -n "${https_proxy:-}" ]]; then
75 echo "https_proxy: \"$https_proxy\"" | tee --append \
76 $kud_inventory_folder/group_vars/all.yml
80 # install_k8s() - Install Kubernetes using kubespray tool including Kata
81 function install_k8s {
83 ansible-playbook $verbose -i \
84 $kud_inventory $kud_playbooks/preconfigure-kubespray.yml \
85 --become --become-user=root | \
86 tee $cluster_log/setup-kubernetes.log
87 if [ "$container_runtime" == "docker" ]; then
88 echo "Docker will be used as the container runtime interface"
89 ansible-playbook $verbose -i \
90 $kud_inventory $dest_folder/kubespray-$version/cluster.yml \
91 -e cluster_name=$cluster_name --become --become-user=root | \
92 tee $cluster_log/setup-kubernetes.log
93 elif [ "$container_runtime" == "containerd" ]; then
94 echo "Containerd will be used as the container runtime interface"
95 ansible-playbook $verbose -i \
96 $kud_inventory $dest_folder/kubespray-$version/cluster.yml \
97 -e $kud_kata_override_variables -e cluster_name=$cluster_name \
98 --become --become-user=root | \
99 tee $cluster_log/setup-kubernetes.log
100 #Install Kata Containers in containerd scenario
101 ansible-playbook $verbose -i \
102 $kud_inventory -e "base_dest=$HOME" \
103 $kud_playbooks/configure-kata.yml | \
104 tee $cluster_log/setup-kata.log
106 echo "Only Docker or Containerd are supported container runtimes"
110 # Configure environment
111 # Requires kubeconfig_localhost and kubectl_localhost to be true
112 # in inventory/group_vars/k8s-cluster.yml
114 cp $kud_inventory_folder/artifacts/admin.conf $HOME/.kube/config
115 if !(which kubectl); then
116 cp $kud_inventory_folder/artifacts/kubectl /usr/local/bin/
120 # install_addons() - Install Kubenertes AddOns
121 function install_addons {
123 local plugins_name="$1"
124 echo "additional addons plugins $1"
126 local plugins_name=""
127 echo "no additional addons pluigns"
130 source /etc/environment
131 echo "Installing Kubernetes AddOns"
132 ansible-galaxy install $verbose -r \
133 $kud_infra_folder/galaxy-requirements.yml --ignore-errors
135 ansible-playbook $verbose -i \
136 $kud_inventory -e "base_dest=$HOME" $kud_playbooks/configure-kud.yml \
137 | tee $cluster_log/setup-kud.log
139 kud_addons="${KUD_ADDONS:-} ${plugins_name}"
141 for addon in ${kud_addons}; do
142 echo "Deploying $addon using configure-$addon.yml playbook.."
143 ansible-playbook $verbose -i \
144 $kud_inventory -e "base_dest=$HOME" \
145 $kud_playbooks/configure-${addon}.yml | \
146 tee $cluster_log/setup-${addon}.log
149 echo "Run the test cases if testing_enabled is set to true."
150 if [[ "${testing_enabled}" == "true" ]]; then
152 # Run Kata test first if Kata was installed
153 if [ "$container_runtime" == "containerd" ]; then
154 #Install Kata webhook for test pods
155 ansible-playbook $verbose -i $kud_inventory -e "base_dest=$HOME" \
156 -e "kata_webhook_runtimeclass=$kata_webhook_runtimeclass" \
157 $kud_playbooks/configure-kata-webhook.yml \
158 --become --become-user=root | \
159 sudo tee $cluster_log/setup-kata-webhook.log
160 kata_webhook_deployed=true
162 bash kata.sh || failed_kud_tests="${failed_kud_tests} kata"
165 #Run other plugin tests
166 for addon in ${kud_addons}; do
168 bash ${addon}.sh || failed_kud_tests="${failed_kud_tests} ${addon}"
171 echo "Test the onap4k8s plugin installation"
172 for functional_test in plugin_edgex plugin_fw plugin_eaa; do
173 bash ${functional_test}.sh --external || failed_kud_tests="${failed_kud_tests} ${functional_test}"
177 echo "Test the emco plugin installation"
178 # TODO plugin_fw_v2 requires virtlet and a patched multus to succeed
179 # for functional_test in plugin_fw_v2; do
180 # bash ${functional_test}.sh --external || failed_kud_tests="${failed_kud_tests} ${functional_test}"
186 # Remove Kata webhook if user didn't want it permanently installed
187 if ! [ "$enable_kata_webhook" == "true" ] && [ "$kata_webhook_deployed" == "true" ]; then
188 ansible-playbook $verbose -i $kud_inventory -e "base_dest=$HOME" \
189 -e "kata_webhook_runtimeclass=$kata_webhook_runtimeclass" \
190 $kud_playbooks/configure-kata-webhook-reset.yml \
191 --become --become-user=root | \
192 sudo tee $cluster_log/kata-webhook-reset.log
193 kata_webhook_deployed=false
195 if [[ ! -z "$failed_kud_tests" ]]; then
196 echo "Test cases failed:${failed_kud_tests}"
201 # Check if Kata webhook should be installed and isn't already installed
202 if [ "$enable_kata_webhook" == "true" ] && ! [ "$kata_webhook_deployed" == "true" ]; then
203 ansible-playbook $verbose -i $kud_inventory -e "base_dest=$HOME" \
204 -e "kata_webhook_runtimeclass=$kata_webhook_runtimeclass" \
205 $kud_playbooks/configure-kata-webhook.yml \
206 --become --become-user=root | \
207 sudo tee $cluster_log/setup-kata-webhook.log
210 echo "Add-ons deployment complete..."
214 kubectl config view --minify -o jsonpath='{.clusters[0].cluster.server}' | awk -F '[:/]' '{print $4}'
217 # Copy installation artifacts to be usable in host running Ansible
218 function install_host_artifacts {
219 local -r cluster_name=$1
220 local -r host_dir="/opt/kud/multi-cluster"
221 local -r host_addons_dir="${host_dir}/addons"
222 local -r host_artifacts_dir="${host_dir}/${cluster_name}/artifacts"
224 for addon in cdi cdi-operator cpu-manager kubevirt kubevirt-operator multus-cni node-feature-discovery ovn4nfv qat-device-plugin sriov-network sriov-network-operator; do
225 mkdir -p ${host_addons_dir}/${addon}/{helm,profile}
226 cp -r ${kud_infra_folder}/helm/${addon} ${host_addons_dir}/${addon}/helm
227 cp -r ${kud_infra_folder}/profiles/${addon}/* ${host_addons_dir}/${addon}/profile
228 tar -czf ${host_addons_dir}/${addon}.tar.gz -C ${host_addons_dir}/${addon}/helm .
229 tar -czf ${host_addons_dir}/${addon}_profile.tar.gz -C ${host_addons_dir}/${addon}/profile .
232 mkdir -p ${host_addons_dir}/tests
233 for test in _common _common_test _functions topology-manager-sriov multus ovn4nfv nfd sriov-network qat cmk; do
234 cp ${kud_tests}/${test}.sh ${host_addons_dir}/tests
237 mkdir -p ${host_artifacts_dir}
238 cp -rf ${kud_inventory_folder}/artifacts/* ${host_artifacts_dir}
240 mkdir -p ${host_artifacts_dir}/addons
241 for yaml in ${kud_infra_folder}/emco/examples/*.yaml; do
242 cp ${yaml} ${host_artifacts_dir}/addons
244 for template in addons/*.tmpl; do
245 CLUSTER_NAME="${cluster_name}" \
246 HOST_IP="$(master_ip)" \
247 KUBE_PATH="${host_artifacts_dir}/admin.conf" \
248 PACKAGES_PATH="${host_addons_dir}" \
249 envsubst <${template} >${host_artifacts_dir}/${template%.tmpl}
253 # _print_kubernetes_info() - Prints the login Kubernetes information
254 function _print_kubernetes_info {
255 if ! $(kubectl version &>/dev/null); then
259 # Expose Dashboard using NodePort
261 KUBE_EDITOR="sed -i \"s|type\: ClusterIP|type\: NodePort|g\"" \
262 kubectl -n kube-system edit service kubernetes-dashboard
263 KUBE_EDITOR="sed -i \"s|nodePort\: .*|nodePort\: $node_port|g\"" \
264 kubectl -n kube-system edit service kubernetes-dashboard
266 printf "Kubernetes Info\n===============\n" > $k8s_info_file
267 echo "Dashboard URL: https://$(master_ip):$node_port" >> $k8s_info_file
268 echo "Admin user: kube" >> $k8s_info_file
269 echo "Admin password: secret" >> $k8s_info_file
273 if [[ -n "${KUD_DEBUG:-}" ]]; then
278 # Configuration values
280 kud_folder=${INSTALLER_DIR}
281 kud_infra_folder=$kud_folder/../../deployment_infra
282 kud_playbooks=$kud_infra_folder/playbooks
283 kud_tests=$kud_folder/../../tests
284 k8s_info_file=$kud_folder/k8s_info.log
285 testing_enabled=${KUD_ENABLE_TESTS:-false}
286 container_runtime=${CONTAINER_RUNTIME:-docker}
287 enable_kata_webhook=${ENABLE_KATA_WEBHOOK:-false}
288 kata_webhook_runtimeclass=${KATA_WEBHOOK_RUNTIMECLASS:-kata-qemu}
289 kata_webhook_deployed=false
290 # For containerd the etcd_deployment_type: docker is the default and doesn't work.
291 # You have to use either etcd_kubeadm_enabled: true or etcd_deployment_type: host
292 # See https://github.com/kubernetes-sigs/kubespray/issues/5713
293 kud_kata_override_variables="container_manager=containerd \
294 -e etcd_deployment_type=host -e kubelet_cgroup_driver=cgroupfs"
297 export CSAR_DIR=/opt/csar
299 function install_pkg {
300 # Install dependencies
302 install_prerequisites
306 function install_cluster {
307 version=$(grep "kubespray_version" ${kud_playbooks}/kud-vars.yml | \
308 awk -F ': ' '{print $2}')
309 export ANSIBLE_CONFIG=$dest_folder/kubespray-$version/ansible.cfg
312 echo "install default addons and $2"
317 echo "installed the addons"
319 install_host_artifacts $1
321 _print_kubernetes_info
325 echo "installer usage:"
326 echo "./installer.sh --install_pkg - Install the required softwarepackage"
327 echo "./installer.sh --cluster <cluster name> \
328 - Install k8s cluster with default plugins"
329 echo "./installer.sh --cluster <cluster name> \
330 --plugins <plugin_1 plugin_2> - Install k8s cluster with default plugins \
331 and additional plugins such as onap4k8s."
334 if [ $# -eq 0 ]; then
335 echo "Error: No arguments supplied"
341 echo "Error: Null argument passed"
346 if [ "$1" == "--install_pkg" ]; then
347 export kud_inventory_folder=$kud_folder/inventory
348 kud_inventory=$kud_inventory_folder/hosts.ini
354 if [ "$1" == "--cluster" ]; then
355 if [ -z "${2-}" ]; then
356 echo "Error: Cluster name is null"
362 kud_multi_cluster_path=/opt/kud/multi-cluster
363 cluster_path=$kud_multi_cluster_path/$cluster_name
365 if [ ! -d "${cluster_path}" ]; then
366 echo "Error: cluster_path ${cluster_path} doesn't exit"
371 cluster_log=$kud_multi_cluster_path/$cluster_name/log
372 export kud_inventory_folder=$kud_folder/inventory/$cluster_name
373 kud_inventory=$kud_inventory_folder/hosts.ini
375 mkdir -p $kud_inventory_folder
376 mkdir -p $cluster_log
377 cp $kud_multi_cluster_path/$cluster_name/hosts.ini $kud_inventory_folder/
378 cp -rf $kud_folder/inventory/group_vars $kud_inventory_folder/
381 if [ "$3" == "--plugins" ]; then
382 if [ -z "${4-}" ]; then
383 echo "Error: plugins arguments is null; Refer the usage"
387 plugins_name=${@:4:$#}
388 install_cluster $cluster_name "$plugins_name"
391 echo "Error: cluster argument should have plugins; \
397 install_cluster $cluster_name
402 echo "Error: Refer the installer usage"