A list of nice findings for Kubernetes

  • List all images in Helm chart
1images=$(helm template -g $helm |yq -N '..|.image? | select(. == "*" and . != null)'|sort|uniq|grep ":"|egrep -v '*:[[:blank:]]' || echo "")
  • upload images listed in an Helm chart
 1load_helm_images(){
 2  # look in helm charts
 3  for helm in $(ls ../../roles/*/files/helm/*.tgz); do
 4    printf "\e[1;34m[INFO]\e[m Look for images in ${helm}...\n"
 5
 6    images=$(helm template -g $helm |yq -N '..|.image? | select(. == "*" and . != null)'|sort|uniq|grep ":"|egrep -v '*:[[:blank:]]' || echo "")
 7
 8    dir=$( dirname $helm | xargs dirname )
 9
10    echo "####"
11
12    if [ "$images" != "" ]; then
13      printf "\e[1;34m[INFO]\e[m Images found in the helm charts: ${images}\n"
14      printf "\e[1;34m[INFO]\e[m Create ${dir}/images images...\n"
15
16      mkdir -p ${dir}/images
17
18      while i= read -r image_name; do
19        archive_name=$(basename -a $(awk -F : '{print $1}'<<<${image_name}));
20        printf "\e[1;34m[INFO]\e[m Pull images...\n"
21        podman pull ${image_name};
22        printf "\e[1;34m[INFO]\e[m Push ${image_name} in ${dir}/images/${archive_name}\n"
23        podman save ${image_name} --format oci-archive -o ${dir}/images/${archive_name};
24      done <<< ${images}
25    else
26      printf "\e[1;34m[INFO]\e[m No Images found in the helm charts: $helm\n"
27    fi
28  done
29}
  • Check components version
1function checkComponentsInstall() {
2    componentsArray=("kubectl" "helm")
3    for i in "${componentsArray[@]}"; do
4      command -v "${i}" >/dev/null 2>&1 ||
5        { echo "[ERROR] ${i} is required, but it's not installed. Aborting." >&2; exit 1; }
6    done
7}
  • Version comparator
 1function checkK8sVersion() {
 2    currentK8sVersion=$(kubectl version --short | grep "Server Version" | awk '{gsub(/v/,$5)}1 {print $3}')
 3    testVersionComparator 1.20 "$currentK8sVersion" '<'
 4    if [[ $k8sVersion == "ok" ]]; then
 5      echo "current kubernetes version is ok"
 6    else
 7      minikube start --kubernetes-version=v1.22.4;
 8    fi
 9}
10
11
12# the comparator based on https://stackoverflow.com/a/4025065
13versionComparator () {
14    if [[ $1 == $2 ]]
15    then
16        return 0
17    fi
18    local IFS=.
19    local i ver1=($1) ver2=($2)
20    # fill empty fields in ver1 with zeros
21    for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
22    do
23        ver1[i]=0
24    done
25    for ((i=0; i<${#ver1[@]}; i++))
26    do
27        if [[ -z ${ver2[i]} ]]
28        then
29            # fill empty fields in ver2 with zeros
30            ver2[i]=0
31        fi
32        if ((10#${ver1[i]} > 10#${ver2[i]}))
33        then
34            return 1
35        fi
36        if ((10#${ver1[i]} < 10#${ver2[i]}))
37        then
38            return 2
39        fi
40    done
41    return 0
42}
43
44testVersionComparator () {
45    versionComparator $1 $2
46    case $? in
47        0) op='=';;
48        1) op='>';;
49        2) op='<';;
50    esac
51    if [[ $op != "$3" ]]
52    then
53        echo "Kubernetes test fail: Expected '$3', Actual '$op', Arg1 '$1', Arg2 '$2'"
54        k8sVersion="not ok"
55    else
56        echo "Kubernetes test pass: '$1 $op $2'"
57        k8sVersion="ok"
58    fi
59}