I made an attempt for a simple shell script to view statefulset's images, and then replace them if desired.
I ran into this issue that the array values that are stored are not getting displayed properly.. here is the script
#!/bin/bash
##-chronograph3r
##Variables
read -p "Namespace : " NS
##### To find the Image tags in statefulset
app=($(kubectl get statefulset -n $NS -o=name | grep "myapp" | cut -c 18-) )
declare app
imgver=($(kubectl get statefulset -n $NS ${array[@]} -o yaml | egrep "image: asia.gcr.io" ))
declare imgver
#--- To display current image version
for i in $@
do
app[${#app[@]}]=$i
imglist[${#imgver[@]}]=$i
echo $i
done
for (( i==0; i < ${#app[@]}; i++ ))
do
echo "current image version for ${app[$i]} is ${imgver[$i]}"
done
the desired output should be,
current image version for myapp1 is image: asia.gcr.io/lucifer/myapp:latest
current image version for myapp2 is image: asia.gcr.io/lucifer/myapp:1.4.2
current image version for myapp3 is image: asia.gcr.io/lucifer/myapp:1.3.0
current image version for myapp4 is image: asia.gcr.io/lucifer/myapp:stable
the output I get is
current image version for myapp1 is image: asia.gcr.io/lucifer/myapp:latest
current image version for myapp2 is image:
current image version for myapp3 is image: asia.gcr.io/lucifer/myapp:1.3.0
current image version for myapp4 is image:
When I do " echo ${imgver[@]}"
it returns this
image: asia.gcr.io/lucifer/myapp:latest image: asia.gcr.io/lucifer/myapp:1.4.2 image: asia.gcr.io/lucifer/myapp:1.3.0 image: asia.gcr.io/lucifer/myapp:stable
I believe that concatenating the declared arrays is the troublemaker here. help me find the issue here or let me know if there are other ways to achieve the desired output.
${array[@]}and$@should be"${array[@]}"and"$@". Also, thedeclarecommands are not necessary here. By the way,app[${#app[@]}]=...could be abbreviated asapp+=(...)or evenapp+=("$@")if you leave out the loop.declare -p imgver?cmd's output into an array you can usemapfile -t array < <(cmd).