I am incorporating HPA creation based on certain toggle-able environment variables passed to our Jenkins bash deploy script.
There are 3 conditions:
- Correct HPA variables are found
- run 'get hpa' in project and delete it, before re-adding incase max/min pods or cpu thresholds have changed
- HPA is set to 'toggle off'
- run 'get hpa' in project, and if found, delete it
- HPA Env Variables are not present
- run 'get hpa' in project and if found, delete it
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:
- Is there a way to suppress this error message?
- If I do find a way to suppress the message, will the variable I am setting the result of this command to still be populated in either case?
Thanks
CodePudding user response:
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.