Shell Script to find newly added pods in kubernetes on day basics Shell Script to find newly added pods in kubernetes on day basics kubernetes kubernetes

Shell Script to find newly added pods in kubernetes on day basics


Your script has numerous issues and inefficiencies. Repeatedly calling a somewhat heavy command like kubectl should be avoided; try to rearrange things so that you only run it once, and extract the information you need from it. I'm vaguely guessing you actually want something along the lines of

#!/bin/bash# Store pod names in an array  pods=($(kubectl get pods |    awk 'NR>1 { printf sep $1; sep=" "}'))if [ ${#pods[@]} -gt $n ]; then  # $n is still undefined!    for pod in "${pods[@]}"; do        kubectl describe pod "$pod" |        awk -v dt="$(date +"%a, %d %b %Y")" '            /SecretName:/ { next }            /Name:/ { name=$NF }            /Start Time:/ { t=$3 $4 $5 $6;                if (t==dt) print name                name="" }'    donefi

Once you run Awk anyway, it makes sense to refactor as much as the processing into Awk; it can do everything grep and cut and sed can do, and much more. Notice also how we use the $(command) command substitution syntax in preference over the obsolescent legacy `command` syntax.

kubectl with -o=json would probably be a lot easier and more straightforward to process programmatically so you should really look into that. I don't have a Kubernetes cluster to play around with so I'm only pointing this out as a direction for further improvement.