diff --git a/cns/requestcontroller/kubecontroller/crdrequestcontroller_test.go b/cns/requestcontroller/kubecontroller/crdrequestcontroller_test.go index 6a98def3e6..fd6490116a 100644 --- a/cns/requestcontroller/kubecontroller/crdrequestcontroller_test.go +++ b/cns/requestcontroller/kubecontroller/crdrequestcontroller_test.go @@ -23,7 +23,7 @@ const ( existingNNCName = "nodenetconfig_1" existingPodName = "pod_1" hostNetworkPodName = "pod_hostNet" - allocatedPodIP = "10.0.0.1/32" + allocatedPodIP = "10.0.0.2" allocatedUUID = "539970a2-c2dd-11ea-b3de-0242ac130004" allocatedUUID2 = "01a5dd00-cd5d-11ea-87d0-0242ac130003" networkContainerID = "24fcd232-0364-41b0-8027-6e6ef9aeabc6" @@ -31,7 +31,8 @@ const ( nonexistingNNCName = "nodenetconfig_nonexisting" nonexistingPodName = "pod_nonexisting" nonexistingNamespace = "namespace_nonexisting" - ncPrimaryIP = "10.0.0.1/32" + ncPrimaryIP = "10.0.0.1" + subnetRange = "10.0.0.0/24" ) // MockAPI is a mock of kubernete's API server @@ -607,6 +608,7 @@ func TestInitRequestController(t *testing.T) { IP: allocatedPodIP, }, }, + SubnetAddressSpace: subnetRange, }, }, }, diff --git a/cns/requestcontroller/kubecontroller/crdtranslator.go b/cns/requestcontroller/kubecontroller/crdtranslator.go index e3ff1ca526..815510712d 100644 --- a/cns/requestcontroller/kubecontroller/crdtranslator.go +++ b/cns/requestcontroller/kubecontroller/crdtranslator.go @@ -19,7 +19,7 @@ func CRDStatusToNCRequest(crdStatus nnc.NodeNetworkConfigStatus) (cns.CreateNetw err error ip net.IP ipNet *net.IPNet - bits int + size int numNCsSupported int numNCs int ) @@ -37,21 +37,23 @@ func CRDStatusToNCRequest(crdStatus nnc.NodeNetworkConfigStatus) (cns.CreateNetw ncRequest.NetworkContainerid = nc.ID ncRequest.NetworkContainerType = cns.Docker - // Convert "10.0.0.1/32" into "10.0.0.1" and prefix length - // Todo, this will be changed soon and only ipaddress will be passed - if ip, ipNet, err = net.ParseCIDR(nc.PrimaryIP); err != nil { - return ncRequest, err + if ip = net.ParseIP(nc.PrimaryIP); ip == nil { + return ncRequest, fmt.Errorf("Invalid PrimaryIP %s:", nc.PrimaryIP) } - _, bits = ipNet.Mask.Size() + if _, ipNet, err = net.ParseCIDR(nc.SubnetAddressSpace); err != nil { + return ncRequest, fmt.Errorf("Invalid SubnetAddressSpace %s:, err:%s", nc.SubnetAddressSpace, err) + } + + size, _ = ipNet.Mask.Size() ipSubnet.IPAddress = ip.String() - ipSubnet.PrefixLength = uint8(bits) + ipSubnet.PrefixLength = uint8(size) ncRequest.IPConfiguration.IPSubnet = ipSubnet ncRequest.IPConfiguration.GatewayIPAddress = nc.DefaultGateway for _, ipAssignment = range nc.IPAssignments { - if ip, _, err = net.ParseCIDR(ipAssignment.IP); err != nil { - return ncRequest, err + if ip = net.ParseIP(ipAssignment.IP); ip == nil { + return ncRequest, fmt.Errorf("Invalid SecondaryIP %s:", ipAssignment.IP) } secondaryIPConfig = cns.SecondaryIPConfig{ diff --git a/cns/requestcontroller/kubecontroller/crdtranslator_test.go b/cns/requestcontroller/kubecontroller/crdtranslator_test.go index 8d80358cb7..0f79d01462 100644 --- a/cns/requestcontroller/kubecontroller/crdtranslator_test.go +++ b/cns/requestcontroller/kubecontroller/crdtranslator_test.go @@ -8,14 +8,15 @@ import ( ) const ( - ncID = "160005ba-cd02-11ea-87d0-0242ac130003" - ipCIDR = "10.0.0.1/32" - ipCIDRString = "10.0.0.1" - ipCIDRMaskLength = 32 - ipNotCIDR = "10.0.0.1" - ipMalformed = "10.0.0.0.0" - defaultGateway = "10.0.0.2" - subnetName = "subnet1" + ncID = "160005ba-cd02-11ea-87d0-0242ac130003" + primaryIp = "10.0.0.1" + ipInCIDR = "10.0.0.1/32" + ipMalformed = "10.0.0.0.0" + defaultGateway = "10.0.0.2" + subnetName = "subnet1" + subnetAddressSpace = "10.0.0.0/24" + subnetPrefixLen = 24 + testSecIp1 = "10.0.0.2" ) func TestStatusToNCRequestMalformedPrimaryIP(t *testing.T) { @@ -32,9 +33,10 @@ func TestStatusToNCRequestMalformedPrimaryIP(t *testing.T) { IPAssignments: []nnc.IPAssignment{ { Name: allocatedUUID, - IP: ipCIDR, + IP: testSecIp1, }, }, + SubnetAddressSpace: subnetAddressSpace, }, }, } @@ -56,7 +58,7 @@ func TestStatusToNCRequestMalformedIPAssignment(t *testing.T) { status = nnc.NodeNetworkConfigStatus{ NetworkContainers: []nnc.NetworkContainer{ { - PrimaryIP: ipCIDR, + PrimaryIP: primaryIp, ID: ncID, IPAssignments: []nnc.IPAssignment{ { @@ -64,6 +66,7 @@ func TestStatusToNCRequestMalformedIPAssignment(t *testing.T) { IP: ipMalformed, }, }, + SubnetAddressSpace: subnetAddressSpace, }, }, } @@ -76,7 +79,7 @@ func TestStatusToNCRequestMalformedIPAssignment(t *testing.T) { } } -func TestStatusToNCRequestPrimaryIPNotCIDR(t *testing.T) { +func TestStatusToNCRequestPrimaryIPInCIDR(t *testing.T) { var ( status nnc.NodeNetworkConfigStatus err error @@ -85,14 +88,15 @@ func TestStatusToNCRequestPrimaryIPNotCIDR(t *testing.T) { status = nnc.NodeNetworkConfigStatus{ NetworkContainers: []nnc.NetworkContainer{ { - PrimaryIP: ipNotCIDR, + PrimaryIP: ipInCIDR, ID: ncID, IPAssignments: []nnc.IPAssignment{ { Name: allocatedUUID, - IP: ipCIDR, + IP: testSecIp1, }, }, + SubnetAddressSpace: subnetAddressSpace, }, }, } @@ -114,14 +118,45 @@ func TestStatusToNCRequestIPAssignmentNotCIDR(t *testing.T) { status = nnc.NodeNetworkConfigStatus{ NetworkContainers: []nnc.NetworkContainer{ { - PrimaryIP: ipCIDR, + PrimaryIP: primaryIp, ID: ncID, IPAssignments: []nnc.IPAssignment{ { Name: allocatedUUID, - IP: ipNotCIDR, + IP: ipInCIDR, }, }, + SubnetAddressSpace: subnetAddressSpace, + }, + }, + } + + // Test with ip assignment not in CIDR form + _, err = CRDStatusToNCRequest(status) + + if err == nil { + t.Fatalf("Expected translation of CRD status with ip assignment not CIDR, to fail.") + } +} + +func TestStatusToNCRequestWithIncorrectSubnetAddressSpace(t *testing.T) { + var ( + status nnc.NodeNetworkConfigStatus + err error + ) + + status = nnc.NodeNetworkConfigStatus{ + NetworkContainers: []nnc.NetworkContainer{ + { + PrimaryIP: primaryIp, + ID: ncID, + IPAssignments: []nnc.IPAssignment{ + { + Name: allocatedUUID, + IP: testSecIp1, + }, + }, + SubnetAddressSpace: "10.0.0.0", // not a cidr range }, }, } @@ -147,17 +182,17 @@ func TestStatusToNCRequestSuccess(t *testing.T) { status = nnc.NodeNetworkConfigStatus{ NetworkContainers: []nnc.NetworkContainer{ { - PrimaryIP: ipCIDR, + PrimaryIP: primaryIp, ID: ncID, IPAssignments: []nnc.IPAssignment{ { Name: allocatedUUID, - IP: ipCIDR, + IP: testSecIp1, }, }, SubnetName: subnetName, DefaultGateway: defaultGateway, - SubnetAddressSpace: "", // Not currently set by DNC Request Controller + SubnetAddressSpace: subnetAddressSpace, }, }, } @@ -169,12 +204,12 @@ func TestStatusToNCRequestSuccess(t *testing.T) { t.Fatalf("Expected translation of CRD status to succeed, got error :%v", err) } - if ncRequest.IPConfiguration.IPSubnet.IPAddress != ipCIDRString { - t.Fatalf("Expected ncRequest's ipconfiguration to have the ip %v but got %v", ipCIDRString, ncRequest.IPConfiguration.IPSubnet.IPAddress) + if ncRequest.IPConfiguration.IPSubnet.IPAddress != primaryIp { + t.Fatalf("Expected ncRequest's ipconfiguration to have the ip %v but got %v", primaryIp, ncRequest.IPConfiguration.IPSubnet.IPAddress) } - if ncRequest.IPConfiguration.IPSubnet.PrefixLength != uint8(ipCIDRMaskLength) { - t.Fatalf("Expected ncRequest's ipconfiguration prefix length to be %v but got %v", ipCIDRMaskLength, ncRequest.IPConfiguration.IPSubnet.PrefixLength) + if ncRequest.IPConfiguration.IPSubnet.PrefixLength != uint8(subnetPrefixLen) { + t.Fatalf("Expected ncRequest's ipconfiguration prefix length to be %v but got %v", subnetPrefixLen, ncRequest.IPConfiguration.IPSubnet.PrefixLength) } if ncRequest.IPConfiguration.GatewayIPAddress != defaultGateway { @@ -195,8 +230,8 @@ func TestStatusToNCRequestSuccess(t *testing.T) { t.Fatalf("Expected there to be a secondary ip with the key %v but found nothing", allocatedUUID) } - if secondaryIP.IPAddress != ipCIDRString { - t.Fatalf("Expected %v as the secondary IP config but got %v", ipCIDRString, secondaryIP.IPAddress) + if secondaryIP.IPAddress != testSecIp1 { + t.Fatalf("Expected %v as the secondary IP config but got %v", testSecIp1, secondaryIP.IPAddress) } } @@ -229,7 +264,7 @@ func TestSecondaryIPsToCRDSpecSuccess(t *testing.T) { secondaryIPs = map[string]cns.SecondaryIPConfig{ allocatedUUID: { - IPAddress: ipCIDRString, + IPAddress: testSecIp1, }, } diff --git a/cns/restserver/ipam.go b/cns/restserver/ipam.go index 10dff205c6..362a8d0071 100644 --- a/cns/restserver/ipam.go +++ b/cns/restserver/ipam.go @@ -145,7 +145,7 @@ func (service *HTTPRestService) releaseIPConfig(podInfo cns.KubernetesPodInfo) e if ipID != "" { if ipconfig, isExist := service.PodIPConfigState[ipID]; isExist { service.setIPConfigAsAvailable(ipconfig, podInfo) - logger.Printf("Released IP %+v for pod %+v", ipconfig.IPSubnet, podInfo) + logger.Printf("Released IP %+v for pod %+v", ipconfig.IPAddress, podInfo) } else { logger.Errorf("Failed to get release ipconfig. Pod to IPID exists, but IPID to IPConfig doesn't exist, CNS State potentially corrupt") diff --git a/cns/restserver/util.go b/cns/restserver/util.go index cad3521760..839580f07d 100644 --- a/cns/restserver/util.go +++ b/cns/restserver/util.go @@ -643,21 +643,23 @@ func (service *HTTPRestService) validateIpConfigRequest(ipConfigRequest cns.GetI func (service *HTTPRestService) populateIpConfigInfoUntransacted(ipConfigStatus ipConfigurationStatus, ipConfiguration *cns.IPConfiguration) error { var ( - ncStatus containerstatus - exists bool + ncStatus containerstatus + exists bool + primaryIpConfiguration cns.IPConfiguration ) if ncStatus, exists = service.state.ContainerStatus[ipConfigStatus.NCID]; !exists { return fmt.Errorf("Failed to get NC Configuration for NcId: %s", ipConfigStatus.NCID) } - ipConfiguration.DNSServers = ncStatus.CreateNetworkContainerRequest.IPConfiguration.DNSServers - ipConfiguration.GatewayIPAddress = ncStatus.CreateNetworkContainerRequest.IPConfiguration.GatewayIPAddress + primaryIpConfiguration = ncStatus.CreateNetworkContainerRequest.IPConfiguration + + ipConfiguration.DNSServers = primaryIpConfiguration.DNSServers + ipConfiguration.GatewayIPAddress = primaryIpConfiguration.GatewayIPAddress - // TODO: This will be changed soon, and prefix length will be set as Subnetprefix length ipConfiguration.IPSubnet = cns.IPSubnet{ IPAddress: ipConfigStatus.IPAddress, - PrefixLength: 32, + PrefixLength: primaryIpConfiguration.IPSubnet.PrefixLength, } return nil }