I am incorporating HPA creation based on certain toggle-able environment variables passed to our Jenkins bash deploy script.
There are 3 conditions:
This is the actual line of bash I'm running:
hpaExists=`kubectl get hpa/${APP_NAME} --no-headers | awk '{print $1}'
The code is running fine, but if there is no hpa found, it shows this error in the console:
Error from server (NotFound): horizontalpodautoscalers.autoscaling "${APP_NAME}" not found
There is technically no issue with the error happening, but I know it will confuse developers and QA when running the Jenkins deploy jobs to see an error message and I would like to suppress it.
I have tried:
kubectl get hpa/${APP_NAME} --no-headers | awk '{print $1}' > /dev/null
kubectl get hpa/${APP_NAME} --no-headers | awk '{print $1}' > /dev/null 2>&1
kubectl get hpa/${APP_NAME} --no-headers | awk '{print $1}' 2> /dev/null
kubectl get hpa/${APP_NAME} --no-headers | awk '{print $1}' || true
kubectl get hpa/${APP_NAME} --no-headers | awk '{print $1}' &> /dev/null
All still print an error message.
So I have 2 questions:
Thanks
Assuming this error message comes from kubectl
, you need to redirect stderr on its side of the pipe.
kubectl get hpa/${APP_NAME} --no-headers 2>/dev/null | awk '{print $1}'
Each process in the pipeline has its own stdin, stdout, and stderr. The stdout of the first process is connected to the stdin of the second process, but stderr goes to the same place as it normally does unless you tell it to go somewhere else.