Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ 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"
existingNamespace = k8sNamespace
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
Expand Down Expand Up @@ -607,6 +608,7 @@ func TestInitRequestController(t *testing.T) {
IP: allocatedPodIP,
},
},
SubnetAddressSpace: subnetRange,
},
},
},
Expand Down
20 changes: 11 additions & 9 deletions cns/requestcontroller/kubecontroller/crdtranslator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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{
Expand Down
85 changes: 60 additions & 25 deletions cns/requestcontroller/kubecontroller/crdtranslator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -32,9 +33,10 @@ func TestStatusToNCRequestMalformedPrimaryIP(t *testing.T) {
IPAssignments: []nnc.IPAssignment{
{
Name: allocatedUUID,
IP: ipCIDR,
IP: testSecIp1,
},
},
SubnetAddressSpace: subnetAddressSpace,
},
},
}
Expand All @@ -56,14 +58,15 @@ func TestStatusToNCRequestMalformedIPAssignment(t *testing.T) {
status = nnc.NodeNetworkConfigStatus{
NetworkContainers: []nnc.NetworkContainer{
{
PrimaryIP: ipCIDR,
PrimaryIP: primaryIp,
ID: ncID,
IPAssignments: []nnc.IPAssignment{
{
Name: allocatedUUID,
IP: ipMalformed,
},
},
SubnetAddressSpace: subnetAddressSpace,
},
},
}
Expand All @@ -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
Expand All @@ -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,
},
},
}
Expand All @@ -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
},
},
}
Expand All @@ -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,
},
},
}
Expand All @@ -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 {
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -229,7 +264,7 @@ func TestSecondaryIPsToCRDSpecSuccess(t *testing.T) {

secondaryIPs = map[string]cns.SecondaryIPConfig{
allocatedUUID: {
IPAddress: ipCIDRString,
IPAddress: testSecIp1,
},
}

Expand Down
2 changes: 1 addition & 1 deletion cns/restserver/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
14 changes: 8 additions & 6 deletions cns/restserver/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down