Created
October 16, 2022 00:12
-
-
Save jgreat/cfbf8747491107f031f30166beaa0d71 to your computer and use it in GitHub Desktop.
How to create idempotent values in bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based off of https://stackoverflow.com/a/33379756/7554392