How can I test the validity of a haproxy config file without trying to resolve all addresses?
I'm running a haproxy in Kubernetes, getting its config file (haproxy.cfg) from a ConfigMap. When updating the ConfigMap with a new configuration, I'm testing the validity of the configuration before updating the config map.
I do the validation from the CI/CD environment, and here the DNS entries from the Kubernetes cluster will not resolve. So basically I will only learn if the configuration is syntaxial correct or not, but that is still an important check.
My problem is that instead on seconds, the validation of the haproxy configuration takes well over half an hour, because I have to wait for it to fail all the 120 backends (micro service) that are configured for our api-gateway... 120 error messages of type:
server service-a/k8s_service' : could not resolve address 'service-a.svc.cluster.local', disabling server.
I use a command like this (from the folder where my haproxy.cfg file resides):
docker run --rm -v $(pwd):/etc/haproxy/ haproxy:2.6-alpine haproxy -c -dr -f /etc/haproxy/haproxy.cfg
Triva
My full script to apply an update the haproxy config looks basically like this, although maybe not relevant for the actual question:
docker run --rm -v $(pwd):/etc/haproxy/ haproxy:2.6-alpine haproxy -c -dr -f /etc/haproxy/haproxy.cfg
if [ $? -ne 0 ]; then
echo "Invalid haproxy configuration in $(pwd)/haproxy.cfg"
exit 1
fi
kubectl create configmap api-gateway-config --from-file=haproxy.cfg -o yaml --dry-run=client | kubectl apply -f -
kubectl rollout restart deployment api-gateway
The Kubernetes deployment will define and mount a volume with the ConfigMap into the folder /etc/haproxy
of the running container.