Skip to content

Commit

Permalink
Merge pull request #219 from microsoft/user/nijos/hcisdncni
Browse files Browse the repository at this point in the history
add networkinterface spec with the ability to add multiple ip configurations for a vm
  • Loading branch information
nijosmsft authored Sep 6, 2023
2 parents 4bd259d + 1e306a3 commit 6bb88cc
Show file tree
Hide file tree
Showing 11 changed files with 459 additions and 8 deletions.
3 changes: 3 additions & 0 deletions api/v1beta1/azurestackhcimachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ type AzureStackHCIMachineSpec struct {
AllocatePublicIP bool `json:"allocatePublicIP,omitempty"`

AdditionalSSHKeys []string `json:"additionalSSHKeys,omitempty"`

// +optional
NetworkInterfaces NetworkInterfaces `json:"networkInterfaces,omitempty"`
}

// AzureStackHCIMachineStatus defines the observed state of AzureStackHCIMachine
Expand Down
3 changes: 3 additions & 0 deletions api/v1beta1/azurestackhcivirtualmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ type AzureStackHCIVirtualMachineSpec struct {
BackendPoolNames []string `json:"backendPoolNames,omitempty"`

AdditionalSSHKeys []string `json:"additionalSSHKeys,omitempty"`

// +optional
NetworkInterfaces NetworkInterfaces `json:"networkInterfaces,omitempty"`
}

// AzureStackHCIVirtualMachineStatus defines the observed state of AzureStackHCIVirtualMachine
Expand Down
35 changes: 35 additions & 0 deletions api/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,41 @@ func (s Subnets) ToMap() map[string]*SubnetSpec {
return res
}

type IPAllocationMethod int32

const (
IPAllocationMethod_Invalid IPAllocationMethod = 0
IPAllocationMethod_Dynamic IPAllocationMethod = 1
IPAllocationMethod_Static IPAllocationMethod = 2
)

type IpConfigurationSpec struct {
Name string `json:"name,omitempty"`
// +optional
Primary bool `json:"primary,omitempty"`
// +optional
Allocation IPAllocationMethod `json:"allocation,omitempty"`
// below fields are unused, but adding for completeness
// +optional
IpAddress string `json:"ipAddress,omitempty"`
// +optional
PrefixLength string `json:"prefixLength,omitempty"`
// +optional
SubnetId string `json:"subnetId,omitempty"`
// +optional
Gateway string `json:"gateway,omitempty"`
}
type IpConfigurations []*IpConfigurationSpec

type NetworkInterfaceSpec struct {
// +optional
Name string `json:"name,omitempty"`
// +optional
IPConfigurations IpConfigurations `json:"ipConfigurations,omitempty"`
}

type NetworkInterfaces []*NetworkInterfaceSpec

const (
// OSVersionLabelName is the label set on resources to identify their os version
OSVersionLabelName = "msft.microsoft/os-version"
Expand Down
113 changes: 113 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions cloud/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ func GenerateNICName(machineName string) string {
return fmt.Sprintf("%s-nic", machineName)
}

// GenerateIPConfigName generates the name of an ipconfiguration based on the nic name.
func GenerateIPConfigName(nicName string, index int) string {
return fmt.Sprintf("%s-ipconfig-%d", nicName, index)
}

// GenerateOSDiskName generates the name of an OS disk based on the name of a VM.
func GenerateOSDiskName(machineName string) string {
return fmt.Sprintf("%s_OSDisk", machineName)
Expand Down
45 changes: 39 additions & 6 deletions cloud/services/networkinterfaces/networkinterfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ import (
"k8s.io/klog/v2"
)

// Spec specification for ip configuration
type IPConfiguration struct {
Name string
Primary bool
}

type IPConfigurations []*IPConfiguration

// Spec specification for network interface
type Spec struct {
Name string
Expand All @@ -36,6 +44,7 @@ type Spec struct {
StaticIPAddress string
MacAddress string
BackendPoolNames []string
IPConfigurations IPConfigurations
}

// Get provides information about a network interface.
Expand Down Expand Up @@ -87,15 +96,39 @@ func (s *Service) Reconcile(ctx context.Context, spec interface{}) error {
EnableIPForwarding: to.BoolPtr(true),
EnableMACSpoofing: to.BoolPtr(true),
MacAddress: &nicSpec.MacAddress,
IPConfigurations: &[]network.InterfaceIPConfiguration{
{
Name: to.StringPtr("pipConfig"),
InterfaceIPConfigurationPropertiesFormat: nicConfig,
},
},
IPConfigurations: &[]network.InterfaceIPConfiguration{},
},
}

if len(nicSpec.IPConfigurations) > 0 {
klog.V(2).Infof("Adding %d ipconfigurations to nic %s", len(nicSpec.IPConfigurations), nicSpec.Name)
for _, ipconfig := range nicSpec.IPConfigurations {

networkIpConfig := network.InterfaceIPConfiguration{
Name: &ipconfig.Name,
InterfaceIPConfigurationPropertiesFormat: &network.InterfaceIPConfigurationPropertiesFormat{
Primary: &ipconfig.Primary,
Subnet: &network.APIEntityReference{
ID: to.StringPtr(nicSpec.VnetName),
},
},
}

if ipconfig.Primary {
networkIpConfig.LoadBalancerBackendAddressPools = &backendAddressPools
}

*networkInterface.IPConfigurations = append(*networkInterface.IPConfigurations, networkIpConfig)
}
} else {
networkIpConfig := network.InterfaceIPConfiguration{
Name: to.StringPtr("pipConfig"),
InterfaceIPConfigurationPropertiesFormat: nicConfig,
}

*networkInterface.IPConfigurations = append(*networkInterface.IPConfigurations, networkIpConfig)
}

_, err := s.Client.CreateOrUpdate(ctx,
s.Scope.GetResourceGroup(),
nicSpec.Name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,32 @@ spec:
type: object
location:
type: string
networkInterfaces:
items:
properties:
ipConfigurations:
items:
properties:
allocation:
format: int32
type: integer
gateway:
type: string
ipaddress:
description: below fields are unused, but adding for completeness
type: string
name:
type: string
prefixlength:
type: string
primary:
type: boolean
subnetid:
type: string
type: object
type: array
type: object
type: array
osDisk:
properties:
diskSizeGB:
Expand Down Expand Up @@ -306,6 +332,32 @@ spec:
type: object
location:
type: string
networkInterfaces:
items:
properties:
ipConfigurations:
items:
properties:
allocation:
format: int32
type: integer
gateway:
type: string
ipaddress:
description: below fields are unused, but adding for completeness
type: string
name:
type: string
prefixlength:
type: string
primary:
type: boolean
subnetid:
type: string
type: object
type: array
type: object
type: array
osDisk:
properties:
diskSizeGB:
Expand Down Expand Up @@ -524,6 +576,32 @@ spec:
type: object
location:
type: string
networkInterfaces:
items:
properties:
ipConfigurations:
items:
properties:
allocation:
format: int32
type: integer
gateway:
type: string
ipaddress:
description: below fields are unused, but adding for completeness
type: string
name:
type: string
prefixlength:
type: string
primary:
type: boolean
subnetid:
type: string
type: object
type: array
type: object
type: array
osDisk:
properties:
diskSizeGB:
Expand Down
Loading

0 comments on commit 6bb88cc

Please sign in to comment.