Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upi/vsphere: Make cidr_to_ip.sh more verbose #3821

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 23 additions & 15 deletions upi/vsphere/ipam/cidr_to_ip.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ function parse_input() {
}

is_ip_address() {
if [[ $1 =~ ^[0-9]{1,3}(\.[0-9]{1,3}){3}$ ]]
then
if [[ $1 =~ ^[0-9]{1,3}(\.[0-9]{1,3}){3}$ ]]; then
echo "true"
else
echo "false"
Expand All @@ -33,22 +32,24 @@ is_ip_address() {

get_reservation() {
reservation=$(curl -s "http://${ipam}/api/getIPs.php?apiapp=address&apitoken=${ipam_token}&domain=${hostname}")
if [[ "${reservation}" == "[]" ]]; then echo ""
if [[ "${reservation}" == "[]" ]]; then
echo "empty"
else
reserved_ip=$(echo "${reservation}" | jq -r ".\"${hostname}\"")
if [ "$(is_ip_address "${reserved_ip}")" == "false" ]; then echo ""
else echo "$reserved_ip"
if [ "$(is_ip_address "${reserved_ip}")" == "false" ]; then
echo "malformed data: ${ip_address}"
else
echo "$reserved_ip"
fi
fi
}

function produce_output() {
if [[ "${network}" == "null" ]]
then
if [[ "${network}" == "null" ]]; then
jq -n \
--arg ip_address "$(get_reservation)" \
'{"ip_address":$ip_address}'
exit 0
exit 0
fi

timeout=$((SECONDS + 60))
Expand All @@ -61,25 +62,32 @@ function produce_output() {
do
ip_address=$(curl -s "http://$ipam/api/getFreeIP.php?apiapp=address&apitoken=$ipam_token&subnet=${network}&host=${hostname}")

if [[ "$(is_ip_address "${ip_address}")" != "true" ]]; then error_exit "could not reserve an IP address: ${ip_address}"; fi
if [[ "$(is_ip_address "${ip_address}")" != "true" ]]; then
error_exit "could not reserve an IP address: malformed data: ${ip_address}"
fi

if [[ "$ip_address" == "$(get_reservation)" ]]
then
reservation=$(get_reservation)
if [[ "$ip_address" == "$reservation" ]]; then
jq -n \
--arg ip_address "$ip_address" \
'{"ip_address":$ip_address}'
exit 0
elif [[ "$reservation" == "empty" ]]; then
echo "received IP address reservation is empty. Retrying..."
elif [[ "$(is_ip_address "${reservation}")" != "true" ]]; then
echo "received invalid IP address reservation: $reservation. Retrying..."
else
echo "received IP address $ip_address does not equal reservation $reservation. Retrying..."
fi

sleep 3
done

# IPAM server responds with 0.0.0.0 when there are no available addresses
if [[ "${ip_address}" =~ ^0 ]]
then
error_exit "could not reserve an IP address: no available addresses"
if [[ "${ip_address}" =~ ^0 ]]; then
error_exit "could not reserve IP address $ip_address with reservation $reservation: no available addresses"
else
error_exit "could not reserve an IP address: timed out waiting for a reservation"
error_exit "could not reserve IP address $ip_address with reservation $reservation: timed out waiting for a reservation"
fi
}

Expand Down