I’ve written an API in Golang, and the production image is built using a multistage Dockerx build. The output of the Dockerx build is an image that is FROM scratch
, and the compiled Go executable inside of it is statically compiled (my understanding is that it wouldn’t work with FROM scratch
if it were dynamically linked).
As a result of the multistage build being FROM scratch
, the output image is incredibly small (2-4MB), the Snyk analysis also finds no security issues with the image (I’m assuming since the attack surface of the image is so small).
The issue is that the image doesn’t use gzip to compress the responses - so my responses are theoretically larger than they need to be. If I were working with another language, I’d typically be using Nginx within the image to act as a reverse proxy - so I’d just configure it to enable gzip (something like this), but in this case, adding in Nginx would result in a larger image (with more vulnerabilities).
I’ve thought about it, and my other two options are:
- Add compression directly into the Go API by using the gzip package. This would involve writing code to handle the compression based on response type, and adding in testing for the functionality. This is my least favourite option because I would eventually have to create a bespoke solution.
- Add Istio to my cluster, inject it as a sidecar into the pods in my Go API service, then set up an Istio compression filter that will gzip any responses to requests made through the ingress controller (nginx ingress) - this would theoretically allow me to control the compression depending on the source of the request, and I wouldn’t have to alter/redeploy my image in order for any gzip config changes to take effect. So far this is my favourite option.
Are there any other factors/options to consider here?
EDIT: The reason I was leaning towards adding it at the service mesh (Istio) level as opposed to the Ingress layer is because there's a possibility of switching/using multiple ingress controllers down the line (which would mean multiple disparate gzip implementations). Also as I said "[Istio] would theoretically allow me to control the compression depending on the source of the request" - this would theoretically allow me to add compression for service-to-service requests later on.