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