Interpret KUBERNETES_SERVICE_HOST as hostname if not parsed as IP address (#2285)

* Interpret KUBERNETES_SERVICE_HOST as hostname if not parsed as IP address

This allows correctly connecting to the Kubernetes API when the service host
is set as a hostname. This is at least the case on gardener.cloud . I'm
running the patched version in production.

Related to #2047 which describes the same problem I had.

* Reindent logical-backup/dump.sh

---------

Co-authored-by: Nick Douma <nick.douma@cyso.com>
This commit is contained in:
Nick Douma 2023-04-19 15:42:27 +02:00 committed by GitHub
parent ec701f6d30
commit 3f4e44de82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 47 additions and 43 deletions

View File

@ -12,14 +12,18 @@ DUMP_SIZE_COEFF=5
ERRORCOUNT=0
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
KUBERNETES_SERVICE_PORT=${KUBERNETES_SERVICE_PORT:-443}
if [ "$KUBERNETES_SERVICE_HOST" != "${KUBERNETES_SERVICE_HOST#*[0-9].[0-9]}" ]; then
echo "IPv4"
K8S_API_URL=https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_SERVICE_PORT/api/v1
elif [ "$KUBERNETES_SERVICE_HOST" != "${KUBERNETES_SERVICE_HOST#*:[0-9a-fA-F]}" ]; then
echo "IPv6"
K8S_API_URL=https://[$KUBERNETES_SERVICE_HOST]:$KUBERNETES_SERVICE_PORT/api/v1
elif [ -n "$KUBERNETES_SERVICE_HOST" ]; then
echo "Hostname"
K8S_API_URL=https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_SERVICE_PORT/api/v1
else
echo "Unrecognized IP format '$KUBERNETES_SERVICE_HOST'"
echo "KUBERNETES_SERVICE_HOST was not set"
fi
echo "API Endpoint: ${K8S_API_URL}"
CERT=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt