0

I am incorporating HPA creation based on certain toggle-able environment variables passed to our Jenkins bash deploy script.

There are 3 conditions:

  1. 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
  2. HPA is set to 'toggle off'
    • run 'get hpa' in project, and if found, delete it
  3. 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

  • 1
    Does this answer your question? [Kubernetes HPA disable scale down](https://stackoverflow.com/questions/60212727/kubernetes-hpa-disable-scale-down). Create the HPA, but disable scaling. – Daniel Mann Sep 30 '22 at 14:24
  • 1
    Have you considered using kustomize or helm to deploy k8s manifests with small differences? – jordanm Sep 30 '22 at 14:24
  • @jordanm unfortunately I work in a large enterprise setting and using helm or customize would require cutting through lots of red tape. I have brought it up to the archs though – stillmaticone17 Sep 30 '22 at 14:29
  • @DanielMann that would work, but I am building this automation enterprise-wide, and each service/app (100s) have their own special needs. I also don't want to have specialized yamls that require updating and customization for these different apps – stillmaticone17 Sep 30 '22 at 14:31
  • Both run locally, and don't require a server side component. They don't require setup. – jordanm Sep 30 '22 at 14:36
  • A version of kustomize is also built in to kubectl, so you even already have it on your jenkins server. – jordanm Sep 30 '22 at 14:37
  • If you're trying to build generic, reusable templates, that is **exactly** the use-case for Helm and/or Kustomize. – Daniel Mann Sep 30 '22 at 17:19

1 Answers1

1

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.

tjm3772
  • 2,346
  • 2
  • 10