diff --git a/upi/vsphere/machine/main.tf b/upi/vsphere/machine/main.tf index 7f3394514af..7403f3cfa44 100644 --- a/upi/vsphere/machine/main.tf +++ b/upi/vsphere/machine/main.tf @@ -1,5 +1,7 @@ locals { ignition_encoded = "data:text/plain;charset=utf-8;base64,${base64encode(var.ignition)}" + mask = "${element(split("/", var.machine_cidr), 1)}" + gw = "${cidrhost(var.machine_cidr,1)}" } data "vsphere_datastore" "datastore" { @@ -29,6 +31,46 @@ data "ignition_file" "hostname" { } } +data "ignition_file" "static_ip" { + count = "${var.instance_count}" + + filesystem = "root" + path = "/etc/sysconfig/network-scripts/ifcfg-eth0" + mode = "420" + + content { + content = <&2 + exit 1 +} + +function check_deps() { + test -f $(which jq) || error_exit "jq command not detected in path, please install it" + test -f $(which ipcalc) || error_exit "ipcalc command not detected in path, please install it" + test -f $(which dig) || error_exit "dig command not detected in path, please install it" + +} + +function parse_input() { + # jq reads from stdin so we don't have to set up any inputs, but let's validate the outputs + eval "$(jq -r '@sh "export CIDR=\(.cidr) master_count=\(.master_count) worker_count=\(.worker_count) cluster_domain=\(.cluster_domain)"')" + if [[ -z "${CIDR}" ]]; then export CIDR=none; fi + if [[ -z "${master_count}" ]]; then export master_count=none; fi + if [[ -z "${worker_count}" ]]; then export worker_count=none; fi + if [[ -z "${cluster_domain}" ]]; then export cluster_domain=none; fi +} + +function produce_output() { + + cidr=$CIDR + + # range is bounded by network (-n) & broadcast (-b) addresses. + lo=$(ipcalc -n $cidr | cut -f2 -d=) + hi=$(ipcalc -b $cidr | cut -f2 -d=) + + read a b c d <<< $(echo $lo | tr . ' ') + read e f g h <<< $(echo $hi | tr . ' ') + IP_RANGE=$(eval echo {$a..$e}.{$b..$f}.{$c..$g}.{$d..$h}) + + bs_count=0 + m_count=0 + w_count=0 + + # check cluster_domain DNS first + for ETCD in 0 1 2 + do + + DNS_RECORDS+="$(dig +short etcd-$ETCD.${cluster_domain}) " + done + + for ENTRY in ${DNS_RECORDS} + do + ping -c1 -w1 $ENTRY > /dev/null 2>&1 + ping_rc=$? + if [[ $ping_rc -eq 0 ]] && [[ $m_count -ne $master_count ]] + then + bootstrap_ip="" + bs_count=1 + master_ips+="$ENTRY " + m_count=$((m_count+1)) + fi + done + + for IPADDR in ${IP_RANGE} + do + + if [ $IPADDR != $(ipcalc -n $cidr | cut -f2 -d=) ] && [ $IPADDR != $(ipcalc -b $cidr | cut -f2 -d=) ] + then + ping -c1 -w1 $IPADDR > /dev/null 2>&1 + ping_rc=$? + + if [[ $ping_rc -eq 1 ]] && [[ $bs_count -ne 1 ]] + then + bootstrap_ip+="$IPADDR" + bs_count=$((bs_count+1)) + elif [[ $ping_rc -eq 1 ]] && [[ $m_count -ne $master_count ]] + then + master_ips+="$IPADDR " + m_count=$((m_count+1)) + elif [[ $ping_rc -eq 1 ]] && [[ $w_count -ne $worker_count ]] + then + worker_ips+="$IPADDR " + w_count=$((w_count+1)) + elif [[ $bs_count -eq 1 ]] && [[ $m_count -eq $master_count ]] && [[ $w_count -eq $worker_count ]] + then + jq -n \ + --arg bootstrap_ip "$bootstrap_ip" \ + --arg master_ips "$master_ips" \ + --arg worker_ips "$worker_ips" \ + '{"bootstrap_ip":$bootstrap_ip,"master_ips":$master_ips,"worker_ips":$worker_ips}' + exit 0 + fi + fi + done +} + +# main() +check_deps +parse_input +produce_output diff --git a/upi/vsphere/network/main.tf b/upi/vsphere/network/main.tf new file mode 100644 index 00000000000..684c8cc60ca --- /dev/null +++ b/upi/vsphere/network/main.tf @@ -0,0 +1,10 @@ +data "external" "ping" { + program = ["bash", "${path.root}/network/cidr_to_ip.sh"] + + query = { + cidr = "${var.machine_cidr}" + master_count = "${var.master_count}" + worker_count = "${var.worker_count}" + cluster_domain = "${var.cluster_domain}" + } +} diff --git a/upi/vsphere/network/output.tf b/upi/vsphere/network/output.tf new file mode 100644 index 00000000000..6ac63e81078 --- /dev/null +++ b/upi/vsphere/network/output.tf @@ -0,0 +1,11 @@ +output "master_ips" { + value = "${split(" ", trimspace(data.external.ping.result.master_ips))}" +} + +output "worker_ips" { + value = "${split(" ", trimspace(data.external.ping.result.worker_ips))}" +} + +output "bootstrap_ip" { + value = "${split(" ", trimspace(data.external.ping.result.bootstrap_ip))}" +} diff --git a/upi/vsphere/network/variables.tf b/upi/vsphere/network/variables.tf new file mode 100644 index 00000000000..a2caf92bc2f --- /dev/null +++ b/upi/vsphere/network/variables.tf @@ -0,0 +1,19 @@ +variable "machine_cidr" { + type = "string" + description = "This is the public network netmask." +} + +variable "cluster_domain" { + type = "string" + description = "This is the cluster domain where the API record is created" +} + +variable "master_count" { + type = "string" + description = "The number of master IP addresses to obtain from the machine_cidr." +} + +variable "worker_count" { + type = "string" + description = "The number of worker IP addresses to obtain from the machine_cidr." +} diff --git a/upi/vsphere/route53/main.tf b/upi/vsphere/route53/main.tf index b14544a3625..62df63b5720 100644 --- a/upi/vsphere/route53/main.tf +++ b/upi/vsphere/route53/main.tf @@ -35,7 +35,7 @@ resource "aws_route53_record" "api" { zone_id = "${aws_route53_zone.cluster.zone_id}" name = "api.${var.cluster_domain}" set_identifier = "api" - records = "${compact(concat(list(var.bootstrap_ip), var.control_plane_ips))}" + records = ["${compact(concat(var.bootstrap_ip, var.control_plane_ips))}"] weighted_routing_policy { weight = 90 diff --git a/upi/vsphere/route53/variables.tf b/upi/vsphere/route53/variables.tf index 99c8fb6c3d5..61579f01f2a 100644 --- a/upi/vsphere/route53/variables.tf +++ b/upi/vsphere/route53/variables.tf @@ -4,7 +4,7 @@ variable "cluster_domain" { } variable "bootstrap_ip" { - type = "string" + type = "list" } variable "control_plane_ips" { diff --git a/upi/vsphere/variables.tf b/upi/vsphere/variables.tf index 2392f8882c4..59aed9435ed 100644 --- a/upi/vsphere/variables.tf +++ b/upi/vsphere/variables.tf @@ -85,6 +85,11 @@ variable "bootstrap_complete" { default = "false" } +variable "machine_cidr" { + type = "string" + description = "The IP space to provision with." +} + variable "bootstrap_ip" { type = "string" description = "The IP address in the machine_cidr to apply to the bootstrap."