'How to check if commands inside the deployment.yaml are working?
I'm trying to write some bash commands inside my deployment.yaml file. I want to execute this commands at the postStart.
How can I check if the commands are working well? And if possible, how to get the node IP address?
lifecycle:
postStart:
exec:
command:
- "sh"
- "-c"
- |
GATEWAY_HTTPS_NAME="${GATEWAY_SERVICE_NAME}_SERVICE_PORT_HTTPS"
GATEWAY_HTTPS_PORT=$(eval "echo \$$GATEWAY_HTTPS_NAME")
cat /app/gateway/ip
BOOL=true
while [ $BOOL ]; do
if [ "$GATEWAY_HTTPS_PORT" != '' ]; then
nohup sh -c "sleep 30; cat /app/gateway/ip | xargs -I[] curl -H 'Content-Type: application/json' -d '{}' -k https://[]/actuator/refresh" &
ROUTES=$(nohup sh -c "cat /app/gateway/ip | xargs -I[] curl -H 'Content-Type: application/json' -d '{}' -k https://[]/actuator/gateway/routes")
else
nohup sh -c "sleep 30; cat /app/gateway/ip | xargs -I[] curl -H 'Content-Type: application/json' -d '{}' -k http://[]/actuator/refresh" &
ROUTES=$(nohup sh -c "cat /app/gateway/ip | xargs -I[] curl -H 'Content-Type: application/json' -d '{}' -k http://[]/actuator/gateway/routes")
fi
NODE_IP=???
echo $ROUTES
if [ $ROUTES[*] =~ $NODE_IP ]; then
BOOL=$false
fi
BOOL=$false
sleep 10
done
Solution 1:[1]
You can validate the command using exit status, I usually do that by adding a double pipe which will output the exit status if something went wrong, here is an example:
ls /unexistant/dir &>/dev/null && echo Success, Code=$? || echo Something went wrong, Code=$?
This will obviously throw an error and run the commands after double pipe since the status code not equals 0 will return Something went wrong, Code=2
Instead, if we run this:
ls /existant/dir &>/dev/null && echo Success, Code=$? || echo Something went wrong, Code=$?
This will run the first command with success and exit status will be 0 which means success, this is the output: Success, Code=0
You can of course play around with this and read more about double pipe
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Affes Salem |
