Bash Function to Check if a Kubernetes Pod Name is Valid

  • 时间:2020-09-17 10:57:36
  • 分类:网络文摘
  • 阅读:135 次

Kubernetes is getting popular these days – as it is a wonderful way to deploy your applications on the cloud and it scales easily (horizontally). The kubectl get pods is a most-common used command to list the current pods available (running) and their statuses.

For example, it might give the following:

1
2
3
# kubectl get pods
NAME                                                 READY   STATUS      RESTARTS   AGE
first-application1-747d698587-n6vrl                  1/1     Running     0          16m
# kubectl get pods
NAME                                                 READY   STATUS      RESTARTS   AGE
first-application1-747d698587-n6vrl                  1/1     Running     0          16m

If a pod crashes or dies for no reasons, the Kubernetes (management platform) will try to restart it for you. We define a pod name’s validity is that it appears in the list, although the pod could be terminating, completed or in other statuses.

Bash Function to Check if a Pod is Valid

We can define a bash function and return true (number 0) if it appears in the result list of kubectl get pods. First, we need to extract the first column and skip the first row – which is the header. We can do this easily by using the awk command line. (NR>1 skips the first line), and print $1 prints the first column by default the whitespace delimiter. Then we can use the for loop to iterate all the pod names and compare to the input parameter which is $1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#
# Bash function to check if the given pod name is valid
# Returns 0 to indicate that the pod is in the `kubectl get pods` list
# Returns 1 to indicate that the pod name is invalid (not found)
#
# How to use this function in bash script?
#   if ! is_pod_name_valid 'the-pod-name'; then
#     echo the-pod-name is found
#   else
#     echo the-pod-name is not found
#   fi
# 
function is_pod_name_valid {
  for pod in $(kubectl get pods | awk 'NR>1{print $1}'); do
    if [[ "$pod" == "$1" ]]; then
      return 0  # pod name found a match, then returns True
    fi
  done
  return 1  # pod not found then returns False
}
#
# Bash function to check if the given pod name is valid
# Returns 0 to indicate that the pod is in the `kubectl get pods` list
# Returns 1 to indicate that the pod name is invalid (not found)
#
# How to use this function in bash script?
#   if ! is_pod_name_valid 'the-pod-name'; then
#     echo the-pod-name is found
#   else
#     echo the-pod-name is not found
#   fi
# 
function is_pod_name_valid {
  for pod in $(kubectl get pods | awk 'NR>1{print $1}'); do
    if [[ "$pod" == "$1" ]]; then
      return 0  # pod name found a match, then returns True
    fi
  done
  return 1  # pod not found then returns False
}

We could put the above BASH function in a separate script e.g. is_pod_name_valid.sh Then we can write a bash wrapper script.

1
2
3
4
5
#!/bin/bash
 
source is_pod_name_valid.sh  # include the bash function
is_pod_name_valid $1         # invoke the function
echo $?                      # echo the return value of the function call
#!/bin/bash

source is_pod_name_valid.sh  # include the bash function
is_pod_name_valid $1         # invoke the function
echo $?                      # echo the return value of the function call

Example usage (assuming we rename the wrapper script pod_test.sh):

1
2
3
4
# ./pod_test.sh first-application1-747d698587-n6vrl 
0
# ./pod_test.sh some-random-pod-name
1
# ./pod_test.sh first-application1-747d698587-n6vrl 
0
# ./pod_test.sh some-random-pod-name
1

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
赵威后问齐使原文及翻译  冯谖客孟尝君原文及翻译  齐宣王见颜斶/颜斶说齐王原文及翻译  邹忌讽齐王纳谏原文及翻译  范雎说秦王原文及翻译  三个博客写作技巧坚持了10年 养活了他一家子!  百度搜索正式升级冰桶算法5.0  现在的建站公司都有哪些套路?真会吹!  百度反推算法,又一次站长和百度之间的较量  百度搜索低调改版搜索界面 
评论列表
添加评论