Skip to content

Commit 3f4e44d

Browse files
LordGaavNick Douma
andauthored
Interpret KUBERNETES_SERVICE_HOST as hostname if not parsed as IP address (zalando#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 zalando#2047 which describes the same problem I had. * Reindent logical-backup/dump.sh --------- Co-authored-by: Nick Douma <[email protected]>
1 parent ec701f6 commit 3f4e44d

File tree

1 file changed

+41
-37
lines changed

1 file changed

+41
-37
lines changed

docker/logical-backup/dump.sh

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@ DUMP_SIZE_COEFF=5
1212
ERRORCOUNT=0
1313

1414
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
15+
KUBERNETES_SERVICE_PORT=${KUBERNETES_SERVICE_PORT:-443}
1516
if [ "$KUBERNETES_SERVICE_HOST" != "${KUBERNETES_SERVICE_HOST#*[0-9].[0-9]}" ]; then
16-
echo "IPv4"
17-
K8S_API_URL=https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_SERVICE_PORT/api/v1
17+
echo "IPv4"
18+
K8S_API_URL=https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_SERVICE_PORT/api/v1
1819
elif [ "$KUBERNETES_SERVICE_HOST" != "${KUBERNETES_SERVICE_HOST#*:[0-9a-fA-F]}" ]; then
19-
echo "IPv6"
20-
K8S_API_URL=https://[$KUBERNETES_SERVICE_HOST]:$KUBERNETES_SERVICE_PORT/api/v1
20+
echo "IPv6"
21+
K8S_API_URL=https://[$KUBERNETES_SERVICE_HOST]:$KUBERNETES_SERVICE_PORT/api/v1
22+
elif [ -n "$KUBERNETES_SERVICE_HOST" ]; then
23+
echo "Hostname"
24+
K8S_API_URL=https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_SERVICE_PORT/api/v1
2125
else
22-
echo "Unrecognized IP format '$KUBERNETES_SERVICE_HOST'"
26+
echo "KUBERNETES_SERVICE_HOST was not set"
2327
fi
2428
echo "API Endpoint: ${K8S_API_URL}"
2529
CERT=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt
@@ -59,42 +63,42 @@ function aws_delete_objects {
5963
export -f aws_delete_objects
6064

6165
function aws_delete_outdated {
62-
if [[ -z "$LOGICAL_BACKUP_S3_RETENTION_TIME" ]] ; then
63-
echo "no retention time configured: skip cleanup of outdated backups"
64-
return 0
65-
fi
66+
if [[ -z "$LOGICAL_BACKUP_S3_RETENTION_TIME" ]] ; then
67+
echo "no retention time configured: skip cleanup of outdated backups"
68+
return 0
69+
fi
6670

67-
# define cutoff date for outdated backups (day precision)
68-
cutoff_date=$(date -d "$LOGICAL_BACKUP_S3_RETENTION_TIME ago" +%F)
71+
# define cutoff date for outdated backups (day precision)
72+
cutoff_date=$(date -d "$LOGICAL_BACKUP_S3_RETENTION_TIME ago" +%F)
6973

70-
# mimic bucket setup from Spilo
71-
prefix="spilo/"$SCOPE$LOGICAL_BACKUP_S3_BUCKET_SCOPE_SUFFIX"/logical_backups/"
74+
# mimic bucket setup from Spilo
75+
prefix="spilo/"$SCOPE$LOGICAL_BACKUP_S3_BUCKET_SCOPE_SUFFIX"/logical_backups/"
7276

73-
args=(
74-
"--no-paginate"
75-
"--output=text"
76-
"--prefix=$prefix"
77-
"--bucket=$LOGICAL_BACKUP_S3_BUCKET"
78-
)
77+
args=(
78+
"--no-paginate"
79+
"--output=text"
80+
"--prefix=$prefix"
81+
"--bucket=$LOGICAL_BACKUP_S3_BUCKET"
82+
)
7983

80-
[[ ! -z "$LOGICAL_BACKUP_S3_ENDPOINT" ]] && args+=("--endpoint-url=$LOGICAL_BACKUP_S3_ENDPOINT")
81-
[[ ! -z "$LOGICAL_BACKUP_S3_REGION" ]] && args+=("--region=$LOGICAL_BACKUP_S3_REGION")
84+
[[ ! -z "$LOGICAL_BACKUP_S3_ENDPOINT" ]] && args+=("--endpoint-url=$LOGICAL_BACKUP_S3_ENDPOINT")
85+
[[ ! -z "$LOGICAL_BACKUP_S3_REGION" ]] && args+=("--region=$LOGICAL_BACKUP_S3_REGION")
8286

83-
# list objects older than the cutoff date
84-
aws s3api list-objects "${args[@]}" --query="Contents[?LastModified<='$cutoff_date'].[Key]" > /tmp/outdated-backups
87+
# list objects older than the cutoff date
88+
aws s3api list-objects "${args[@]}" --query="Contents[?LastModified<='$cutoff_date'].[Key]" > /tmp/outdated-backups
8589

86-
# spare the last backup
87-
sed -i '$d' /tmp/outdated-backups
90+
# spare the last backup
91+
sed -i '$d' /tmp/outdated-backups
8892

89-
count=$(wc -l < /tmp/outdated-backups)
90-
if [[ $count == 0 ]] ; then
91-
echo "no outdated backups to delete"
92-
return 0
93-
fi
94-
echo "deleting $count outdated backups created before $cutoff_date"
93+
count=$(wc -l < /tmp/outdated-backups)
94+
if [[ $count == 0 ]] ; then
95+
echo "no outdated backups to delete"
96+
return 0
97+
fi
98+
echo "deleting $count outdated backups created before $cutoff_date"
9599

96-
# deleted outdated files in batches with 100 at a time
97-
tr '\n' '\0' < /tmp/outdated-backups | xargs -0 -P1 -n100 bash -c 'aws_delete_objects "$@"' _
100+
# deleted outdated files in batches with 100 at a time
101+
tr '\n' '\0' < /tmp/outdated-backups | xargs -0 -P1 -n100 bash -c 'aws_delete_objects "$@"' _
98102
}
99103

100104
function aws_upload {
@@ -137,14 +141,14 @@ function get_pods {
137141
declare -r SELECTOR="$1"
138142

139143
curl "${K8S_API_URL}/namespaces/${POD_NAMESPACE}/pods?$SELECTOR" \
140-
--cacert $CERT \
141-
-H "Authorization: Bearer ${TOKEN}" | jq .items[].status.podIP -r
144+
--cacert $CERT \
145+
-H "Authorization: Bearer ${TOKEN}" | jq .items[].status.podIP -r
142146
}
143147

144148
function get_current_pod {
145149
curl "${K8S_API_URL}/namespaces/${POD_NAMESPACE}/pods?fieldSelector=metadata.name%3D${HOSTNAME}" \
146-
--cacert $CERT \
147-
-H "Authorization: Bearer ${TOKEN}"
150+
--cacert $CERT \
151+
-H "Authorization: Bearer ${TOKEN}"
148152
}
149153

150154
declare -a search_strategy=(

0 commit comments

Comments
 (0)