I am writing a bash script to mount openshift service accounts into kubernetes objects. The right secret
to use is highlighted in this text:
$ oc describe sa sa-build-webhook-realworld Name: sa-build-webhook-realworld Namespace: your-eng2 Labels: app=sa-build-webhook-realworld Annotations: <none> Image pull secrets: sa-build-webhook-realworld-dockercfg-4qz9g Mountable secrets: sa-build-webhook-realworld-token-bqtnw sa-build-webhook-realworld-dockercfg-4qz9g Tokens: sa-build-webhook-realworld-token-bqtnw sa-build-webhook-realworld-token-k7lq8 Events: <none>
I want the code to be reasonably robust so I am thinking of this as a job for awk where I need "anything in column 2 that is a 'Mountable secret' that isn't the docker secret". Here is the logic I have come up with:
MOUNTABLE_SECRETS='Mountable secrets:'
SECRET_NAME=$(\
oc describe sa sa-build-webhook-realworld \
| sed $(printf 's/./&,/%s' ${#MOUNTABLE_SECRETS}) \
| awk 'BEGIN{FS=OFS=","} {if ($1 ~ /^[ \t]*$/) $1=ch; else ch=$1} 1' \
| grep "$MOUNTABLE_SECRETS" \
| sed 's/[, ]*//g' \
| awk -F':' '{print $2}' \
| grep -v docker \
| grep token)
echo "SECRET_NAME=$SECRET_NAME"
Basically, I insert a character just beyond the width of that phrase to cut the table in half, copy cells on the left into blanks below, then select the second column then grep what I am looking for.
To my mind, it breaks things into pieces that can be understood. It works, but past experiences have taught me that someone looking to maintain that may not be best pleased. Since performance is not an issue what I am really aiming for is maintainability. I also want portability and I am looking to stick with to typical bash and coreutils tools.
How might I improve that script?