@@ -636,6 +636,40 @@ function Add_InitialHnsNetwork {
636
636
- Verbose
637
637
}
638
638
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
+
639
673
# Configures HNS on the Windows node to enable Kubernetes networking:
640
674
# - Creates the "management" interface associated with an initial HNS network.
641
675
# - Creates the HNS network $env:KUBE_NETWORK for pod networking.
@@ -651,12 +685,8 @@ function Configure-HostNetworkingService {
651
685
652
686
Add_InitialHnsNetwork
653
687
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} )
660
690
Log- Output (" Setting up Windows node HNS networking: " +
661
691
" podCidr = ${env: POD_CIDR} , podGateway = ${pod_gateway} , " +
662
692
" podEndpointGateway = ${pod_endpoint_gateway} " )
@@ -833,10 +863,7 @@ function Configure-CniNetworking {
833
863
Log- Output (" using mgmt IP ${mgmt_ip} and mgmt subnet ${mgmt_subnet} for " +
834
864
" CNI config" )
835
865
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} )
840
867
841
868
# Explanation of the CNI config values:
842
869
# CLUSTER_CIDR: the cluster CIDR from which pod CIDRs are allocated.
0 commit comments