This repository was archived by the owner on May 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-eip.sh
More file actions
executable file
·64 lines (55 loc) · 3.05 KB
/
get-eip.sh
File metadata and controls
executable file
·64 lines (55 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env bash
set -euo pipefail
########################################################################################################################
# FUNCTIONS
########################################################################################################################
# Typical die function for throwing errors
die() { echo "$*" 1>&2 ; exit 1; }
# Wait for the istio namespace so get svc doesn't puke
waitForIstioNamespace() {
until kubectl get ns istio-system 1>&- 2>&-
do
sleep 1
done
}
# Wait until at least one LoadBalancer is present to continue so we don't exit too quickly
waitUntilAtLeastOneLoadBalancerPresent() {
while [[ "$(kubectl -n istio-system get svc -o json | jq -e -r '.items[] | select(.spec.type=="LoadBalancer") | length')" -lt 1 ]] ; do
sleep 1
done
}
# wait for either ip or hostname to exist for each LoadBalancer present, then echo a json array with the info on ip and hostname
# Format: [{"name": "foo", "ip": "bar", "hostname": "baz"},{...}]
getLoadBalancerInfo() {
while true ; do
sleep 1
# Construct a base64 encoded JSON object containing the name, ip, and hostname of each LoadBalancer service present in the istio-system namespace.
JSONB64=$(kubectl -n istio-system get svc -o json | jq -e -r '.items[] | select(.spec.type=="LoadBalancer") | {"name": .metadata.name, "ip": .status.loadBalancer.ingress[0].ip, "hostname": .status.loadBalancer.ingress[0].hostname} | @base64')
# If the var is empty it means something went wrong and we weren't able to parse json with the format that we expect
[[ -z "${JSONB64}" ]] && die "Something went wrong, no LoadBalancer info was returned"
# We're not done until all objects have either an IP or a hostname
done=1
for row in ${JSONB64}; do
if [[ "$(echo "$row" | base64 -d | jq -e -r .ip)" = "null" ]] && [[ "$(echo "$row" | base64 -d | jq -e -r .hostname)" = "null" ]] ; then
done=0
fi
done
if [[ "$done" = "1" ]] ; then
break
fi
done
# echo as a json array
JSON=$(kubectl -n istio-system get svc -o json | jq -e '.items[] | select(.spec.type=="LoadBalancer") | {"name": .metadata.name, "ip": .status.loadBalancer.ingress[0].ip, "hostname": .status.loadBalancer.ingress[0].hostname}' | jq -e -s '.')
echo "${JSON}"
}
########################################################################################################################
# RUN
########################################################################################################################
waitForIstioNamespace
waitUntilAtLeastOneLoadBalancerPresent
LOAD_BALANCER_JSON="$(getLoadBalancerInfo)"
# If the json is empty, it means something went wrong, so we should exit with a nonzero exit code
[[ -z "${LOAD_BALANCER_JSON}" ]] && die "Something went wrong, no LoadBalancer info was returned"
# base64 encode the json and add it to a json key to keep Terraform happy
LOAD_BALANCER_JSON_ENCODED="$(jq -e -n --arg "in" "$(echo "${LOAD_BALANCER_JSON}" | base64)" '{"encoded": $in}')"
echo "${LOAD_BALANCER_JSON_ENCODED}"