Skip to content

Commit f1ad24b

Browse files
committed
Remove the assumption of pod cidr of /24 in the gce window node start up script.
1 parent 0c6ce58 commit f1ad24b

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

cluster/gce/windows/k8s-node-setup.psm1

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,40 @@ function Add_InitialHnsNetwork {
636636
-Verbose
637637
}
638638

639+
# Get the network in uint32 for the given cidr
640+
function Get_NetworkDecimal_From_CIDR([string] $cidr) {
641+
$network, [int]$subnetlen = $cidr.Split('/')
642+
$decimal_network = ConvertTo_DecimalIP($network)
643+
return $decimal_network
644+
}
645+
646+
# Get gateway ip string (the first address) based on pod cidr.
647+
# For Windows nodes the pod gateway IP address is the first address in the pod
648+
# CIDR for the host.
649+
function Get_Gateway_From_CIDR([string] $cidr) {
650+
$network=Get_NetworkDecimal_From_CIDR($cidr)
651+
$gateway=ConvertTo_DottedDecimalIP($network+1)
652+
return $gateway
653+
}
654+
655+
# Get endpoint gateway ip string (the second address) based on pod cidr.
656+
# For Windows nodes the pod gateway IP address is the first address in the pod
657+
# CIDR for the host, but from inside containers it's the second address.
658+
function Get_Endpoint_Gateway_From_CIDR([string] $cidr) {
659+
$network=Get_NetworkDecimal_From_CIDR($cidr)
660+
$gateway=ConvertTo_DottedDecimalIP($network+2)
661+
return $gateway
662+
}
663+
664+
# Get pod IP range start based (the third address) on pod cidr
665+
# We reserve the first two in the cidr range for gateways. Start the cidr
666+
# range from the third so that IPAM does not allocate those IPs to pods.
667+
function Get_PodIP_Range_Start([string] $cidr) {
668+
$network=Get_NetworkDecimal_From_CIDR($cidr)
669+
$start=ConvertTo_DottedDecimalIP($network+3)
670+
return $start
671+
}
672+
639673
# Configures HNS on the Windows node to enable Kubernetes networking:
640674
# - Creates the "management" interface associated with an initial HNS network.
641675
# - Creates the HNS network $env:KUBE_NETWORK for pod networking.
@@ -651,12 +685,8 @@ function Configure-HostNetworkingService {
651685

652686
Add_InitialHnsNetwork
653687

654-
# For Windows nodes the pod gateway IP address is the .1 address in the pod
655-
# CIDR for the host, but from inside containers it's the .2 address.
656-
$pod_gateway = `
657-
${env:POD_CIDR}.substring(0, ${env:POD_CIDR}.lastIndexOf('.')) + '.1'
658-
$pod_endpoint_gateway = `
659-
${env:POD_CIDR}.substring(0, ${env:POD_CIDR}.lastIndexOf('.')) + '.2'
688+
$pod_gateway = Get_Gateway_From_CIDR(${env:POD_CIDR})
689+
$pod_endpoint_gateway = Get_Endpoint_Gateway_From_CIDR(${env:POD_CIDR})
660690
Log-Output ("Setting up Windows node HNS networking: " +
661691
"podCidr = ${env:POD_CIDR}, podGateway = ${pod_gateway}, " +
662692
"podEndpointGateway = ${pod_endpoint_gateway}")
@@ -833,10 +863,7 @@ function Configure-CniNetworking {
833863
Log-Output ("using mgmt IP ${mgmt_ip} and mgmt subnet ${mgmt_subnet} for " +
834864
"CNI config")
835865

836-
# We reserve .1 and .2 for gateways. Start the CIDR range from ".3" so that
837-
# IPAM does not allocate those IPs to pods.
838-
$cidr_range_start = `
839-
${env:POD_CIDR}.substring(0, ${env:POD_CIDR}.lastIndexOf('.')) + '.3'
866+
$cidr_range_start = Get_PodIP_Range_Start(${env:POD_CIDR})
840867

841868
# Explanation of the CNI config values:
842869
# CLUSTER_CIDR: the cluster CIDR from which pod CIDRs are allocated.

0 commit comments

Comments
 (0)