Update the network sdk and add more validations

This commit is contained in:
robbiezhang
2018-10-13 00:16:19 +00:00
parent d710e0391c
commit eb77d5686f
80 changed files with 16830 additions and 518 deletions

View File

@@ -45,7 +45,6 @@ const (
subnetsAction = "Microsoft.Network/virtualNetworks/subnets/action"
subnetDelegationService = "Microsoft.ContainerInstance/containerGroups"
networkProfileType = "Microsoft.Network/networkProfiles"
)
// DNS configuration settings
@@ -331,24 +330,42 @@ func (p *ACIProvider) setupNetworkProfile(auth *client.Authentication) error {
}
if err == nil {
if p.subnetCIDR == "" {
p.subnetCIDR = subnet.Properties.AddressPrefix
p.subnetCIDR = *subnet.SubnetPropertiesFormat.AddressPrefix
}
if p.subnetCIDR != subnet.Properties.AddressPrefix {
return fmt.Errorf("found subnet '%s' using different CIDR: '%s'. desired: '%s'", p.subnetName, subnet.Properties.AddressPrefix, p.subnetCIDR)
if p.subnetCIDR != *subnet.SubnetPropertiesFormat.AddressPrefix {
return fmt.Errorf("found subnet '%s' using different CIDR: '%s'. desired: '%s'", p.subnetName, *subnet.SubnetPropertiesFormat.AddressPrefix, p.subnetCIDR)
}
for _, d := range subnet.Properties.Delegations {
if d.Properties.ServiceName == subnetDelegationService {
createSubnet = false
break
if subnet.SubnetPropertiesFormat.NetworkSecurityGroup != nil {
return fmt.Errorf("unable to delegate subnet '%s' to Azure Container Instance since it references the network security group '%s'.", p.subnetName, *subnet.SubnetPropertiesFormat.NetworkSecurityGroup.ID)
}
if subnet.SubnetPropertiesFormat.RouteTable != nil {
return fmt.Errorf("unable to delegate subnet '%s' to Azure Container Instance since it references the route table '%s'.", p.subnetName, *subnet.SubnetPropertiesFormat.RouteTable.ID)
}
if subnet.SubnetPropertiesFormat.ServiceAssociationLinks != nil {
for _, l := range *subnet.SubnetPropertiesFormat.ServiceAssociationLinks {
if l.ServiceAssociationLinkPropertiesFormat != nil && *l.ServiceAssociationLinkPropertiesFormat.LinkedResourceType == subnetDelegationService {
createSubnet = false
break
}
return fmt.Errorf("unable to delegate subnet '%s' to Azure Container Instance as it is used by other Azure resource: '%v'.", p.subnetName, l)
}
} else {
if subnet.SubnetPropertiesFormat.IPConfigurationProfiles != nil && len(*subnet.SubnetPropertiesFormat.IPConfigurationProfiles) != 0 {
return fmt.Errorf("unable to delegate subnet '%s' to Azure Container Instance as its IP configuration profiles is not empty.", p.subnetName)
}
for _, d := range *subnet.SubnetPropertiesFormat.Delegations {
if d.ServiceDelegationPropertiesFormat != nil && *d.ServiceDelegationPropertiesFormat.ServiceName == subnetDelegationService {
createSubnet = false
break
}
}
}
}
if createSubnet {
if subnet == nil {
subnet = &network.Subnet{Name: p.subnetName}
}
populateSubnet(subnet, p.subnetCIDR)
subnet = network.NewSubnetWithContainerInstanceDelegation(p.subnetName, p.subnetCIDR)
subnet, err = c.CreateOrUpdateSubnet(p.vnetResourceGroup, p.vnetName, subnet)
if err != nil {
return fmt.Errorf("error creating subnet: %v", err)
@@ -360,68 +377,27 @@ func (p *ACIProvider) setupNetworkProfile(auth *client.Authentication) error {
return fmt.Errorf("error while looking up network profile: %v", err)
}
if err == nil {
for _, config := range profile.Properties.ContainerNetworkInterfaceConfigurations {
for _, ipConfig := range config.Properties.IPConfigurations {
if ipConfig.Properties.Subnet.ID == subnet.ID {
p.networkProfile = profile.ID
for _, config := range *profile.ProfilePropertiesFormat.ContainerNetworkInterfaceConfigurations {
for _, ipConfig := range *config.ContainerNetworkInterfaceConfigurationPropertiesFormat.IPConfigurations {
if *ipConfig.IPConfigurationProfilePropertiesFormat.Subnet.ID == *subnet.ID {
p.networkProfile = *profile.ID
return nil
}
}
}
return fmt.Errorf("found existing network profile but the profile is not linked to the subnet: %v, %v", profile, err)
}
// at this point, profile should be nil
profile = &network.Profile{
Name: p.nodeName,
Location: p.region,
Type: networkProfileType,
}
populateNetworkProfile(profile, subnet)
profile = network.NewNetworkProfile(p.nodeName, p.region, *subnet.ID)
profile, err = c.CreateOrUpdateProfile(p.resourceGroup, profile)
if err != nil {
return err
}
p.networkProfile = profile.ID
p.networkProfile = *profile.ID
return nil
}
func populateSubnet(s *network.Subnet, cidr string) {
if s.Properties == nil {
s.Properties = &network.SubnetProperties{
AddressPrefix: cidr,
}
}
s.Properties.Delegations = append(s.Properties.Delegations, network.Delegation{
Name: "aciDelegation",
Properties: network.DelegationProperties{
ServiceName: subnetDelegationService,
Actions: []string{subnetsAction},
},
})
}
func populateNetworkProfile(p *network.Profile, subnet *network.Subnet) {
p.Properties.ContainerNetworkInterfaceConfigurations = append(p.Properties.ContainerNetworkInterfaceConfigurations, network.InterfaceConfiguration{
Name: "eth0",
Properties: network.InterfaceConfigurationProperties{
IPConfigurations: []network.IPConfiguration{
{
Name: "ipconfigprofile1",
Properties: network.IPConfigurationProperties{
Subnet: network.ID{
ID: subnet.ID,
},
},
},
},
},
})
}
func getKubeProxyExtension(secretPath, masterURI, clusterCIDR string) (*aci.Extension, error) {
ca, err := ioutil.ReadFile(secretPath + "/ca.crt")
if err != nil {

View File

@@ -4,7 +4,7 @@ import (
"fmt"
"net/http"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-05-01/network"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-08-01/network"
"github.com/Azure/go-autorest/autorest/azure/auth"
azure "github.com/virtual-kubelet/virtual-kubelet/providers/azure/client"
"github.com/virtual-kubelet/virtual-kubelet/providers/azure/client/api"

View File

@@ -7,7 +7,7 @@ import (
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-05-01/network"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-08-01/network"
"github.com/Azure/go-autorest/autorest/azure/auth"
)

View File

@@ -6,6 +6,7 @@ import (
"net/http"
"net/url"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-08-01/network"
"github.com/pkg/errors"
"github.com/virtual-kubelet/virtual-kubelet/providers/azure/client/api"
)
@@ -14,50 +15,40 @@ const (
profilePath = "subscriptions/{{.subscriptionId}}/resourcegroups/{{.resourceGroupName}}/providers/Microsoft.Network/networkProfiles/{{.profileName}}"
)
// Profile represents an Azure network profile
type Profile struct {
Name string
ID string
ETag string `json:"etag"`
Type string
Location string
Properties ProfileProperties
}
var (
defaultNicName = "eth0"
defaultIPConfigName = "ipconfigprofile1"
)
// ProfileProperties stores the properties for network profiles
type ProfileProperties struct {
ContainerNetworkInterfaceConfigurations []InterfaceConfiguration
}
// NewNetworkProfile creates a new instance of network profile
func NewNetworkProfile(name, location, subnetID string ) *network.Profile {
p := network.Profile{
Name: &name,
Location: &location,
ProfilePropertiesFormat: &network.ProfilePropertiesFormat{
ContainerNetworkInterfaceConfigurations: &[]network.ContainerNetworkInterfaceConfiguration{
network.ContainerNetworkInterfaceConfiguration{
Name: &defaultNicName,
ContainerNetworkInterfaceConfigurationPropertiesFormat: &network.ContainerNetworkInterfaceConfigurationPropertiesFormat{
IPConfigurations: &[]network.IPConfigurationProfile{
network.IPConfigurationProfile{
Name: &defaultIPConfigName,
IPConfigurationProfilePropertiesFormat: &network.IPConfigurationProfilePropertiesFormat{
Subnet: &network.Subnet{ID: &subnetID},
},
},
},
},
},
},
},
}
// InterfaceConfiguration is a configuration for a network interface
type InterfaceConfiguration struct {
Name string
Properties InterfaceConfigurationProperties
}
// InterfaceConfigurationProperties is the properties for a network interface configuration
type InterfaceConfigurationProperties struct {
IPConfigurations []IPConfiguration
}
// IPConfiguration stores the configuration for an IP on a network profile
type IPConfiguration struct {
Name string
Properties IPConfigurationProperties
}
// IPConfigurationProperties stores the subnet for an IP configuration
type IPConfigurationProperties struct {
Subnet ID
}
// ID is a generic struct for objets with an ID
type ID struct {
ID string
return &p
}
// GetProfile gets the network profile with the provided name
func (c *Client) GetProfile(resourceGroup, name string) (*Profile, error) {
func (c *Client) GetProfile(resourceGroup, name string) (*network.Profile, error) {
urlParams := url.Values{
"api-version": []string{apiVersion},
}
@@ -97,7 +88,7 @@ func (c *Client) GetProfile(resourceGroup, name string) (*Profile, error) {
if resp.Body == nil {
return nil, errors.New("get network profile returned an empty body in the response")
}
var p Profile
var p network.Profile
if err := json.NewDecoder(resp.Body).Decode(&p); err != nil {
return nil, errors.Wrap(err, "decoding get network profile response body failed")
}
@@ -105,7 +96,7 @@ func (c *Client) GetProfile(resourceGroup, name string) (*Profile, error) {
}
// CreateOrUpdateProfile creates or updates an Azure network profile
func (c *Client) CreateOrUpdateProfile(resourceGroup string, p *Profile) (*Profile, error) {
func (c *Client) CreateOrUpdateProfile(resourceGroup string, p *network.Profile) (*network.Profile, error) {
urlParams := url.Values{
"api-version": []string{apiVersion},
}
@@ -129,7 +120,7 @@ func (c *Client) CreateOrUpdateProfile(resourceGroup string, p *Profile) (*Profi
if err := api.ExpandURL(req.URL, map[string]string{
"subscriptionId": c.auth.SubscriptionID,
"resourceGroupName": resourceGroup,
"profileName": p.Name,
"profileName": *p.Name,
}); err != nil {
return nil, errors.Wrap(err, "expanding URL with parameters failed")
}
@@ -151,7 +142,7 @@ func (c *Client) CreateOrUpdateProfile(resourceGroup string, p *Profile) (*Profi
return nil, errors.New("create network profile returned an empty body in the response")
}
var profile Profile
var profile network.Profile
if err := json.NewDecoder(resp.Body).Decode(&profile); err != nil {
return nil, errors.Wrap(err, "decoding create network profile response body failed")
}

View File

@@ -3,6 +3,8 @@ package network
import (
"path"
"testing"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-08-01/network"
)
func TestGetProfileNotFound(t *testing.T) {
@@ -23,42 +25,14 @@ func TestCreateGetProfile(t *testing.T) {
c := newTestClient(t)
ensureVnet(t, t.Name())
subnet := &Subnet{
Name: t.Name(),
Properties: &SubnetProperties{
AddressPrefix: "10.0.0.0/24",
},
}
subnet := NewSubnetWithContainerInstanceDelegation(t.Name(), "10.0.0.0/24")
subnet, err := c.CreateOrUpdateSubnet(resourceGroup, t.Name(), subnet)
if err != nil {
t.Fatal(err)
}
p := &Profile{
Name: t.Name(),
Type: "Microsoft.Network/networkProfiles",
Location: location,
Properties: ProfileProperties{
ContainerNetworkInterfaceConfigurations: []InterfaceConfiguration{
{
Name: "eth0",
Properties: InterfaceConfigurationProperties{
IPConfigurations: []IPConfiguration{
{
Name: "ipconfigprofile1",
Properties: IPConfigurationProperties{
Subnet: ID{
ID: subnet.ID,
},
},
},
},
},
},
},
},
}
p := NewNetworkProfile(t.Name(), location, *subnet.ID)
p1, err := c.CreateOrUpdateProfile(resourceGroup, p)
if err != nil {
@@ -67,23 +41,27 @@ func TestCreateGetProfile(t *testing.T) {
if p1 == nil {
t.Fatal("create profile should return profile")
}
if p1.ID == "" {
if p1.ID == nil || *p1.ID == "" {
t.Fatal("create profile should return profile.ID")
}
var p2 *Profile
p2, err = c.GetProfile(resourceGroup, p.Name)
var p2 *network.Profile
p2, err = c.GetProfile(resourceGroup, *p.Name)
if err != nil {
t.Fatal(err)
}
if len(p2.Properties.ContainerNetworkInterfaceConfigurations) != 1 {
t.Fatalf("got unexpected profile properties: %+v", p2.Properties)
if len(*p2.ProfilePropertiesFormat.ContainerNetworkInterfaceConfigurations) != 1 {
t.Fatalf("got unexpected profile properties: %+v", *p2.ProfilePropertiesFormat)
}
if len(p2.Properties.ContainerNetworkInterfaceConfigurations[0].Properties.IPConfigurations) != 1 {
t.Fatalf("got unexpected profile IP configuration: %+v", p2.Properties.ContainerNetworkInterfaceConfigurations[0].Properties.IPConfigurations)
containterNetworkInterfaceConfiguration := (*p2.ProfilePropertiesFormat.ContainerNetworkInterfaceConfigurations)[0]
if len(*containterNetworkInterfaceConfiguration.ContainerNetworkInterfaceConfigurationPropertiesFormat.IPConfigurations) != 1 {
t.Fatalf("got unexpected profile IP configuration: %+v", *containterNetworkInterfaceConfiguration.ContainerNetworkInterfaceConfigurationPropertiesFormat.IPConfigurations)
}
if p2.Properties.ContainerNetworkInterfaceConfigurations[0].Properties.IPConfigurations[0].Properties.Subnet.ID != subnet.ID {
ipConfiguration := (*containterNetworkInterfaceConfiguration.ContainerNetworkInterfaceConfigurationPropertiesFormat.IPConfigurations)[0]
if *ipConfiguration.IPConfigurationProfilePropertiesFormat.Subnet.ID != *subnet.ID {
t.Fatal("got unexpected subnet")
}
@@ -92,12 +70,12 @@ func TestCreateGetProfile(t *testing.T) {
t.Fatal(err)
}
if len(subnet.Properties.IPConfigurationProfiles) != 1 {
t.Fatalf("got unexpected subnet IP configuration profiles: %+v", subnet.Properties.IPConfigurationProfiles)
if len(*subnet.SubnetPropertiesFormat.IPConfigurationProfiles) != 1 {
t.Fatalf("got unexpected subnet IP configuration profiles: %+v", *subnet.SubnetPropertiesFormat.IPConfigurationProfiles)
}
expected := path.Join(p2.ID, "containerNetworkInterfaceConfigurations/eth0/ipConfigurations/ipconfigprofile1")
if subnet.Properties.IPConfigurationProfiles[0].ID != expected {
t.Fatalf("got unexpected profile, expected:\n\t%s, got:\n\t%s", expected, subnet.Properties.IPConfigurationProfiles[0].ID)
expected := path.Join(*p2.ID, "containerNetworkInterfaceConfigurations/eth0/ipConfigurations/ipconfigprofile1")
if *(*subnet.SubnetPropertiesFormat.IPConfigurationProfiles)[0].ID != expected {
t.Fatalf("got unexpected profile, expected:\n\t%s, got:\n\t%s", expected, *(*subnet.SubnetPropertiesFormat.IPConfigurationProfiles)[0].ID)
}
}

View File

@@ -6,66 +6,44 @@ import (
"net/http"
"net/url"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-05-01/network"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-08-01/network"
"github.com/pkg/errors"
"github.com/virtual-kubelet/virtual-kubelet/providers/azure/client/api"
)
const (
subnetPath = "subscriptions/{{.subscriptionId}}/resourcegroups/{{.resourceGroupName}}/providers/Microsoft.Network/virtualNetworks/{{.vnetName}}/subnets/{{.subnetName}}"
subnetAction = "Microsoft.Network/virtualNetworks/subnets/action"
)
// Subnet represents an Azure subnet
type Subnet struct {
Name string
ID string
Properties *SubnetProperties
}
var (
delegationName = "aciDelegation"
serviceName = "Microsoft.ContainerInstance/containerGroups"
)
// SubnetProperties are the properties for a subne
type SubnetProperties struct {
AddressPrefix string `json:"addressPrefix,omitempty"`
// NewSubnetWithContainerInstanceDelegation creates the subnet instance with ACI delegation
func NewSubnetWithContainerInstanceDelegation(name, addressPrefix string) *network.Subnet {
subnet := network.Subnet{
Name: &name,
SubnetPropertiesFormat: &network.SubnetPropertiesFormat{
AddressPrefix: &addressPrefix,
Delegations: &[]network.Delegation{
network.Delegation{
Name: &delegationName,
ServiceDelegationPropertiesFormat: &network.ServiceDelegationPropertiesFormat{
ServiceName: &serviceName,
Actions: &[]string{subnetAction},
},
},
},
},
}
// IPConfigurationProfiles and Delegations are new fields not available in the SDK yet
IPConfigurationProfiles []SubnetIPConfigurationProfile `json:"ipConfigurationProfiles"`
Delegations []Delegation
// copied from official go SDK, none of these are used here except to make sure we don't nil out some data on fetched objects.
// NetworkSecurityGroup - The reference of the NetworkSecurityGroup resource.
NetworkSecurityGroup *network.SecurityGroup `json:"networkSecurityGroup,omitempty"`
// RouteTable - The reference of the RouteTable resource.
RouteTable *network.RouteTable `json:"routeTable,omitempty"`
// ServiceEndpoints - An array of service endpoints.
ServiceEndpoints *[]network.ServiceEndpointPropertiesFormat `json:"serviceEndpoints,omitempty"`
// IPConfigurations - Gets an array of references to the network interface IP configurations using subnet.
IPConfigurations *[]network.IPConfiguration `json:"ipConfigurations,omitempty"`
// ResourceNavigationLinks - Gets an array of references to the external resources using subnet.
ResourceNavigationLinks *[]network.ResourceNavigationLink `json:"resourceNavigationLinks,omitempty"`
// ProvisioningState - The provisioning state of the resource.
ProvisioningState *string `json:"provisioningState,omitempty"`
}
// SubnetIPConfigurationProfile stores the ID for an assigned network profile
type SubnetIPConfigurationProfile struct {
ID string
}
// Delegation stores the subnet delegation details
type Delegation struct {
Name string
ID string
ETag string
Properties DelegationProperties
}
// DelegationProperties stores the properties for a delegation
type DelegationProperties struct {
ServiceName string
Actions []string
return &subnet
}
// GetSubnet gets the subnet from the specified resourcegroup/vnet
func (c *Client) GetSubnet(resourceGroup, vnet, name string) (*Subnet, error) {
func (c *Client) GetSubnet(resourceGroup, vnet, name string) (*network.Subnet, error) {
urlParams := url.Values{
"api-version": []string{apiVersion},
}
@@ -100,7 +78,7 @@ func (c *Client) GetSubnet(resourceGroup, vnet, name string) (*Subnet, error) {
return nil, err
}
var subnet Subnet
var subnet network.Subnet
if err := json.NewDecoder(resp.Body).Decode(&subnet); err != nil {
return nil, err
}
@@ -108,7 +86,7 @@ func (c *Client) GetSubnet(resourceGroup, vnet, name string) (*Subnet, error) {
}
// CreateOrUpdateSubnet creates a new or updates an existing subnet in the defined resourcegroup/vnet
func (c *Client) CreateOrUpdateSubnet(resourceGroup, vnet string, s *Subnet) (*Subnet, error) {
func (c *Client) CreateOrUpdateSubnet(resourceGroup, vnet string, s *network.Subnet) (*network.Subnet, error) {
urlParams := url.Values{
"api-version": []string{apiVersion},
}
@@ -132,7 +110,7 @@ func (c *Client) CreateOrUpdateSubnet(resourceGroup, vnet string, s *Subnet) (*S
if err := api.ExpandURL(req.URL, map[string]string{
"subscriptionId": c.auth.SubscriptionID,
"resourceGroupName": resourceGroup,
"subnetName": s.Name,
"subnetName": *s.Name,
"vnetName": vnet,
}); err != nil {
return nil, errors.Wrap(err, "expanding URL with parameters failed")
@@ -150,7 +128,7 @@ func (c *Client) CreateOrUpdateSubnet(resourceGroup, vnet string, s *Subnet) (*S
return nil, err
}
var subnet Subnet
var subnet network.Subnet
if err := json.NewDecoder(resp.Body).Decode(&subnet); err != nil {
return nil, err
}

View File

@@ -1,22 +1,16 @@
package network
import "testing"
import (
"testing"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-08-01/network"
)
func TestCreateGetSubnet(t *testing.T) {
c := newTestClient(t)
subnet := &Subnet{
Name: t.Name(),
Properties: &SubnetProperties{
AddressPrefix: "10.0.0.0/24",
Delegations: []Delegation{
{Name: "aciDelegation", Properties: DelegationProperties{
ServiceName: "Microsoft.ContainerInstance/containerGroups",
Actions: []string{"Microsoft.Network/virtualNetworks/subnets/action"},
}},
},
},
}
subnet := NewSubnetWithContainerInstanceDelegation(t.Name(), "10.0.0.0/24")
ensureVnet(t, t.Name())
s1, err := c.CreateOrUpdateSubnet(resourceGroup, t.Name(), subnet)
@@ -26,25 +20,25 @@ func TestCreateGetSubnet(t *testing.T) {
if s1 == nil {
t.Fatal("create subnet should return subnet")
}
if s1.ID == "" {
if s1.ID == nil || *s1.ID == "" {
t.Fatal("create subnet should return subnet.ID")
}
var s2 *Subnet
s2, err = c.GetSubnet(resourceGroup, t.Name(), subnet.Name)
var s2 *network.Subnet
s2, err = c.GetSubnet(resourceGroup, t.Name(), *subnet.Name)
if err != nil {
t.Fatal(err)
}
if s2.Name != subnet.Name {
if *s2.Name != *subnet.Name {
t.Fatal("got unexpected subnet")
}
if s2.Properties.AddressPrefix != subnet.Properties.AddressPrefix {
t.Fatalf("got unexpected address prefix: %s", s2.Properties.AddressPrefix)
if *s2.SubnetPropertiesFormat.AddressPrefix != *subnet.SubnetPropertiesFormat.AddressPrefix {
t.Fatalf("got unexpected address prefix: %s", *s2.SubnetPropertiesFormat.AddressPrefix)
}
if len(s2.Properties.Delegations) != 1 {
t.Fatalf("got unexpected delgations: %v", s2.Properties.Delegations)
if len(*s2.SubnetPropertiesFormat.Delegations) != 1 {
t.Fatalf("got unexpected delgations: %v", *s2.SubnetPropertiesFormat.Delegations)
}
if s2.Properties.Delegations[0].Name != subnet.Properties.Delegations[0].Name {
t.Fatalf("got unexpected delegation: %v", s2.Properties.Delegations[0])
if *(*s2.SubnetPropertiesFormat.Delegations)[0].Name != *(*subnet.SubnetPropertiesFormat.Delegations)[0].Name {
t.Fatalf("got unexpected delegation: %v", (*s2.SubnetPropertiesFormat.Delegations)[0])
}
}