github.com/kata-containers/tests@v0.0.0-20240307153542-772105b56064/integration/kubernetes/cleanup_bare_metal_env.sh (about)

     1  #!/bin/bash
     2  #
     3  # Copyright (c) 2019 ARM Limited
     4  #
     5  # SPDX-License-Identifier: Apache-2.0
     6  
     7  set -o errexit
     8  set -o nounset
     9  set -o pipefail
    10  
    11  SCRIPT_PATH=$(dirname "$(readlink -f "$0")")
    12  source "${SCRIPT_PATH}/../../lib/common.bash"
    13  
    14  info "Clean up bare metal env"
    15  keep_cni_bin="${1:-false}"
    16  iptables_cache="${KATA_TESTS_DATADIR}/iptables_cache"
    17  
    18  # The kubeadm reset process does not reset or clean up iptables rules
    19  # you must do it manually
    20  # Here, we restore the iptables based on the previously cached file.
    21  sudo iptables-restore < "$iptables_cache"
    22  # All chains were cleared, but we'll need Docker
    23  sudo iptables -N DOCKER
    24  
    25  # The kubeadm reset process does not clean your kubeconfig files.
    26  # you must remove them manually.
    27  sudo -E rm -rf "$HOME/.kube"
    28  
    29  # Remove existing CNI configurations and binaries.
    30  sudo sh -c 'rm -rf /var/lib/cni'
    31  if [ "${keep_cni_bin}" = "false" ]; then
    32  	sudo sh -c 'rm -rf /opt/cni/bin/*'
    33  fi
    34  
    35  #cleanup stale file under /run
    36  sudo sh -c 'rm -rf /run/flannel'
    37  
    38  # delete containers resource created by runc
    39  cri_runtime="${CRI_RUNTIME:-crio}"
    40  case "${cri_runtime}" in
    41  containerd)
    42          readonly runc_path=$(command -v runc)
    43          ;;
    44  crio)
    45          readonly runc_path="/usr/local/bin/crio-runc"
    46          ;;
    47  *)
    48          echo "Runtime ${cri_runtime} not supported"
    49  	exit 0
    50          ;;
    51  esac
    52  
    53  runc_container_union="$($runc_path list)"
    54  if [ -n "$runc_container_union" ]; then
    55  	while IFS='$\n' read runc_container; do
    56  		container_id="$(echo "$runc_container" | awk '{print $1}')"
    57  		if [ "$container_id" != "ID" ]; then
    58  			$runc_path delete -f $container_id
    59  		fi
    60  	done <<< "${runc_container_union}"
    61  fi
    62  
    63  # when pipeline consists of grep, it may fail unnecessarily
    64  # when no line selected.
    65  veth_interfaces_union=$(set +o pipefail; sudo ip link | grep "veth" | awk '{print $2}' | cut -d '@' -f1)
    66  
    67  # delete stale veth interfaces, which is named after vethXXX.
    68  if [ -n "$veth_interfaces_union" ]; then
    69  	while read veth_interface; do
    70  		sudo ip link set dev $veth_interface down
    71  		sudo ip link del $veth_interface
    72  	done <<< "$veth_interfaces_union"
    73  fi