handle the use of the AddressPrefixes field alongside AddressPrefix

improved ValidateCIDRRanges test

add vnet names to help with debugging if needed in the future

comment improvement
This commit is contained in:
Ari Lima 2021-11-22 09:31:17 -08:00
Родитель f45cba3634
Коммит 018851cf1f
2 изменённых файлов: 79 добавлений и 36 удалений

Просмотреть файл

@ -287,11 +287,22 @@ func (dv *dynamic) validateCIDRRanges(ctx context.Context, subnets []Subnet, add
return err
}
_, net, err := net.ParseCIDR(*s.AddressPrefix)
if err != nil {
return err
// Validate the CIDR of AddressPrefix or AddressPrefixes, whichever is defined
if s.AddressPrefix == nil {
for _, address := range *s.AddressPrefixes {
_, net, err := net.ParseCIDR(address)
if err != nil {
return err
}
CIDRArray = append(CIDRArray, net)
}
} else {
_, net, err := net.ParseCIDR(*s.AddressPrefix)
if err != nil {
return err
}
CIDRArray = append(CIDRArray, net)
}
CIDRArray = append(CIDRArray, net)
}
for _, c := range additionalCIDRs {

Просмотреть файл

@ -419,26 +419,56 @@ func TestValidateCIDRRanges(t *testing.T) {
},
}
vnet := mgmtnetwork.VirtualNetwork{
ID: &vnetID,
Location: to.StringPtr("eastus"),
VirtualNetworkPropertiesFormat: &mgmtnetwork.VirtualNetworkPropertiesFormat{
Subnets: &[]mgmtnetwork.Subnet{
{
ID: &masterSubnet,
SubnetPropertiesFormat: &mgmtnetwork.SubnetPropertiesFormat{
AddressPrefix: to.StringPtr("10.0.0.0/24"),
NetworkSecurityGroup: &mgmtnetwork.SecurityGroup{
ID: &masterNSGv1,
vnets := []mgmtnetwork.VirtualNetwork{
{
ID: &vnetID,
Location: to.StringPtr("eastus"),
Name: to.StringPtr("VNET With AddressPrefix"),
VirtualNetworkPropertiesFormat: &mgmtnetwork.VirtualNetworkPropertiesFormat{
Subnets: &[]mgmtnetwork.Subnet{
{
ID: &masterSubnet,
SubnetPropertiesFormat: &mgmtnetwork.SubnetPropertiesFormat{
AddressPrefix: to.StringPtr("10.0.0.0/24"),
NetworkSecurityGroup: &mgmtnetwork.SecurityGroup{
ID: &masterNSGv1,
},
},
},
{
ID: &workerSubnet,
SubnetPropertiesFormat: &mgmtnetwork.SubnetPropertiesFormat{
AddressPrefix: to.StringPtr("10.0.1.0/24"),
NetworkSecurityGroup: &mgmtnetwork.SecurityGroup{
ID: &workerNSGv1,
},
},
},
},
{
ID: &workerSubnet,
SubnetPropertiesFormat: &mgmtnetwork.SubnetPropertiesFormat{
AddressPrefix: to.StringPtr("10.0.1.0/24"),
NetworkSecurityGroup: &mgmtnetwork.SecurityGroup{
ID: &workerNSGv1,
},
},
{
ID: &vnetID,
Location: to.StringPtr("eastus"),
Name: to.StringPtr("VNET With AddressPrefixes"),
VirtualNetworkPropertiesFormat: &mgmtnetwork.VirtualNetworkPropertiesFormat{
Subnets: &[]mgmtnetwork.Subnet{
{
ID: &masterSubnet,
SubnetPropertiesFormat: &mgmtnetwork.SubnetPropertiesFormat{
AddressPrefixes: to.StringSlicePtr([]string{"10.0.0.0/24"}),
NetworkSecurityGroup: &mgmtnetwork.SecurityGroup{
ID: &masterNSGv1,
},
},
},
{
ID: &workerSubnet,
SubnetPropertiesFormat: &mgmtnetwork.SubnetPropertiesFormat{
AddressPrefixes: to.StringSlicePtr([]string{"10.0.1.0/24"}),
NetworkSecurityGroup: &mgmtnetwork.SecurityGroup{
ID: &workerNSGv1,
},
},
},
},
@ -450,23 +480,25 @@ func TestValidateCIDRRanges(t *testing.T) {
tt.modifyOC(oc)
}
vnetClient := mock_network.NewMockVirtualNetworksClient(controller)
if tt.vnetMocks != nil {
tt.vnetMocks(vnetClient, vnet)
}
for _, vnet := range vnets {
vnetClient := mock_network.NewMockVirtualNetworksClient(controller)
if tt.vnetMocks != nil {
tt.vnetMocks(vnetClient, vnet)
}
dv := &dynamic{
log: logrus.NewEntry(logrus.StandardLogger()),
virtualNetworks: vnetClient,
}
dv := &dynamic{
log: logrus.NewEntry(logrus.StandardLogger()),
virtualNetworks: vnetClient,
}
err := dv.validateCIDRRanges(ctx, []Subnet{
{ID: masterSubnet},
{ID: workerSubnet}},
oc.Properties.NetworkProfile.PodCIDR, oc.Properties.NetworkProfile.ServiceCIDR)
if err != nil && err.Error() != tt.wantErr ||
err == nil && tt.wantErr != "" {
t.Error(err)
err := dv.validateCIDRRanges(ctx, []Subnet{
{ID: masterSubnet},
{ID: workerSubnet}},
oc.Properties.NetworkProfile.PodCIDR, oc.Properties.NetworkProfile.ServiceCIDR)
if err != nil && err.Error() != tt.wantErr ||
err == nil && tt.wantErr != "" {
t.Error(*vnet.Name, err)
}
}
})
}