Skip to content

Commit 77bfb10

Browse files
committed
Added developer script for updating Amazon Route 53
1 parent e4c0653 commit 77bfb10

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

hack/update_route_53.sh

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#!/bin/bash
2+
set -eu
3+
4+
zone_name=
5+
profile="default"
6+
7+
help() {
8+
echo "Continually queries Kubernetes for control plane machines and adds their IP address to an Amazon Route 53"
9+
echo "recordset. The recordset name will be cp, and it will be created in the specified zone. If a recordset"
10+
echo "already exists with that name, it will first be deleted."
11+
echo
12+
echo "The Route 53 zone has to already exist. You can create one in the AWS console."
13+
echo
14+
echo "Before running this script, configure kubectl with the proper kubeconfig and namespace so it can get the"
15+
echo "cluster machines."
16+
echo
17+
echo "This script is not intended for production use."
18+
echo
19+
echo "USAGE: $0 -z <zone name> [-p <AWS profile name>]"
20+
}
21+
22+
if [[ $# -eq 0 ]]
23+
then
24+
help
25+
exit 2
26+
fi
27+
28+
short_opts='z:p:h'
29+
long_opts='zone:,profile:,help'
30+
parsed_opts=$(getopt 'z:p:h' $*)
31+
eval set -- $parsed_opts
32+
33+
while true
34+
do
35+
case "$1" in
36+
-z)
37+
zone_name="$2"
38+
shift 2
39+
;;
40+
-p)
41+
profile="$2"
42+
shift 2
43+
;;
44+
-h)
45+
shift
46+
help
47+
exit 0
48+
;;
49+
--)
50+
shift
51+
break
52+
;;
53+
*)
54+
echo "Impossible value found. This is a bug."
55+
exit 1
56+
;;
57+
esac
58+
done
59+
60+
if [[ -z $zone_name ]]
61+
then
62+
echo "Missing zone name"
63+
exit 1
64+
fi
65+
66+
# Zone name must end with a period, but the user doesn't need to know that. Add one if it's missing.
67+
if [[ ! $zone_name =~ [.]$ ]]
68+
then
69+
zone_name=$zone_name.
70+
fi
71+
72+
recordset_name="cp.$zone_name"
73+
74+
zone_id=$(aws route53 list-hosted-zones --profile "$profile" | jq -r '.HostedZones[] | select(.Name == "'"$zone_name"'").Id | split("/")[2]')
75+
if [[ -n $zone_id ]]
76+
then
77+
echo "Found zone $zone_name"
78+
else
79+
echo "Zone $zone_name not found. Please create it first."
80+
exit 1
81+
fi
82+
83+
get_recordset() {
84+
aws route53 list-resource-record-sets --profile "$profile" --hosted-zone-id "$zone_id" | jq -r '.ResourceRecordSets[] | select(.Name == "'"$recordset_name"'")'
85+
}
86+
87+
upsert_addresses() {
88+
local addresses=$1
89+
echo "Replacing old records"
90+
local recordset='{"Name":"'"$recordset_name"'","Type":"A","TTL":10,"ResourceRecords":[]}'
91+
for address in $addresses
92+
do
93+
echo "Adding $address"
94+
recordset=$(echo "$recordset" | jq -r --arg a "$address" '.ResourceRecords += [{"Value":$a}]')
95+
done
96+
local batch=$(jq -r -n --argjson rs "$recordset" '{"Changes":[{"Action":"UPSERT","ResourceRecordSet":$rs}]}')
97+
aws route53 change-resource-record-sets --profile "$profile" --hosted-zone-id "$zone_id" --change-batch "$batch" > /dev/null
98+
}
99+
100+
# If the recordset exists from a previous run, delete it.
101+
old_recordset=$(get_recordset)
102+
if [[ -n $old_recordset ]]
103+
then
104+
echo "Deleting recordset $recordset_name"
105+
aws route53 change-resource-record-sets --profile "$profile" --hosted-zone-id "$zone_id" --change-batch '{"Changes":[{"Action":"DELETE","ResourceRecordSet":'"$old_recordset"'}]}' > /dev/null
106+
fi
107+
108+
echo "Watching for control plane machines..."
109+
old_addresses=
110+
while true
111+
do
112+
addresses=$(kubectl get machine -o json | jq -r '.items[] | select(.metadata.labels."cluster.x-k8s.io/control-plane" != null) | .status | select(.addresses!=null) | .addresses[].address')
113+
if [[ $addresses != "$old_addresses" ]]
114+
then
115+
upsert_addresses "$addresses"
116+
fi
117+
old_addresses=$addresses
118+
sleep 5
119+
done

0 commit comments

Comments
 (0)