Skip to content

Instantly share code, notes, and snippets.

@jgreat
Created October 16, 2022 00:12
Show Gist options
  • Save jgreat/cfbf8747491107f031f30166beaa0d71 to your computer and use it in GitHub Desktop.
Save jgreat/cfbf8747491107f031f30166beaa0d71 to your computer and use it in GitHub Desktop.
How to create idempotent values in bash
#!/bin/bash
# Generate Pod CIDR based on 10.240.0.0/14 address space
# generate a md5 from hostname to get a generated but idempotent hex value.
idempotent_hex=$(hostname | md5sum | cut -c 1-4)
# turn this hex value into an integer "16# means base 16"
idempotent_int=$((16#$idempotent_hex))
# second octet - limit to 0-4 and add 240
b=$(( idempotent_int % 4 + 240 ))
# third octet - limit to 0-255
c=$(( idempotent_int % 255 ))
# fourth octet - limit 0 or 1 (1 being 128)
d=$(( idempotent_int % 1 ))
if [[ $d -eq 1 ]]
then
d=128
fi
echo "Generated Pod CIDR 10.$b.$c.$d/25"
@jgreat
Copy link
Author

jgreat commented Oct 16, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment