Get VM Scale Set NIC (#13283)
Calling `*armnetwork.InterfacesClient.Get()` doesn't work for Scale Set VM NIC, because these use a different Resource ID format. Use `*armnetwork.InterfacesClient.GetVirtualMachineScaleSetNetworkInterface()` instead. This needs both the scale set name and the instance ID, so add an `InstanceID` field to the `virtualMachine` struct. `InstanceID` is empty for a VM that isn't a ScaleSetVM. Signed-off-by: Daniel Nicholls <daniel.nicholls@resdiary.com>
This commit is contained in:
parent
d0c2d9c0b9
commit
103133124a
|
@ -95,7 +95,7 @@ func CloudConfigurationFromName(name string) (cloud.Configuration, error) {
|
||||||
name = strings.ToUpper(name)
|
name = strings.ToUpper(name)
|
||||||
env, ok := environments[name]
|
env, ok := environments[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return env, fmt.Errorf("There is no cloud configuration matching the name %q", name)
|
return env, fmt.Errorf("there is no cloud configuration matching the name %q", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
return env, nil
|
return env, nil
|
||||||
|
@ -308,6 +308,7 @@ type virtualMachine struct {
|
||||||
Location string
|
Location string
|
||||||
OsType string
|
OsType string
|
||||||
ScaleSet string
|
ScaleSet string
|
||||||
|
InstanceID string
|
||||||
Tags map[string]*string
|
Tags map[string]*string
|
||||||
NetworkInterfaces []string
|
NetworkInterfaces []string
|
||||||
Size string
|
Size string
|
||||||
|
@ -408,7 +409,8 @@ func (d *Discovery) refresh(ctx context.Context) ([]*targetgroup.Group, error) {
|
||||||
networkInterface = v
|
networkInterface = v
|
||||||
d.cacheHitCount.Add(1)
|
d.cacheHitCount.Add(1)
|
||||||
} else {
|
} else {
|
||||||
networkInterface, err = client.getNetworkInterfaceByID(ctx, nicID)
|
if vm.ScaleSet == "" {
|
||||||
|
networkInterface, err = client.getVMNetworkInterfaceByID(ctx, nicID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, errorNotFound) {
|
if errors.Is(err, errorNotFound) {
|
||||||
level.Warn(d.logger).Log("msg", "Network interface does not exist", "name", nicID, "err", err)
|
level.Warn(d.logger).Log("msg", "Network interface does not exist", "name", nicID, "err", err)
|
||||||
|
@ -419,6 +421,19 @@ func (d *Discovery) refresh(ctx context.Context) ([]*targetgroup.Group, error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
d.addToCache(nicID, networkInterface)
|
d.addToCache(nicID, networkInterface)
|
||||||
|
} else {
|
||||||
|
networkInterface, err = client.getVMScaleSetVMNetworkInterfaceByID(ctx, nicID, vm.ScaleSet, vm.InstanceID)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, errorNotFound) {
|
||||||
|
level.Warn(d.logger).Log("msg", "Network interface does not exist", "name", nicID, "err", err)
|
||||||
|
} else {
|
||||||
|
ch <- target{labelSet: nil, err: err}
|
||||||
|
}
|
||||||
|
// Get out of this routine because we cannot continue without a network interface.
|
||||||
|
return
|
||||||
|
}
|
||||||
|
d.addToCache(nicID, networkInterface)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if networkInterface.Properties == nil {
|
if networkInterface.Properties == nil {
|
||||||
|
@ -626,6 +641,7 @@ func mapFromVMScaleSetVM(vm armcompute.VirtualMachineScaleSetVM, scaleSetName st
|
||||||
Location: *(vm.Location),
|
Location: *(vm.Location),
|
||||||
OsType: osType,
|
OsType: osType,
|
||||||
ScaleSet: scaleSetName,
|
ScaleSet: scaleSetName,
|
||||||
|
InstanceID: *(vm.InstanceID),
|
||||||
Tags: tags,
|
Tags: tags,
|
||||||
NetworkInterfaces: networkInterfaces,
|
NetworkInterfaces: networkInterfaces,
|
||||||
Size: size,
|
Size: size,
|
||||||
|
@ -634,9 +650,9 @@ func mapFromVMScaleSetVM(vm armcompute.VirtualMachineScaleSetVM, scaleSetName st
|
||||||
|
|
||||||
var errorNotFound = errors.New("network interface does not exist")
|
var errorNotFound = errors.New("network interface does not exist")
|
||||||
|
|
||||||
// getNetworkInterfaceByID gets the network interface.
|
// getVMNetworkInterfaceByID gets the network interface.
|
||||||
// If a 404 is returned from the Azure API, `errorNotFound` is returned.
|
// If a 404 is returned from the Azure API, `errorNotFound` is returned.
|
||||||
func (client *azureClient) getNetworkInterfaceByID(ctx context.Context, networkInterfaceID string) (*armnetwork.Interface, error) {
|
func (client *azureClient) getVMNetworkInterfaceByID(ctx context.Context, networkInterfaceID string) (*armnetwork.Interface, error) {
|
||||||
r, err := newAzureResourceFromID(networkInterfaceID, client.logger)
|
r, err := newAzureResourceFromID(networkInterfaceID, client.logger)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("could not parse network interface ID: %w", err)
|
return nil, fmt.Errorf("could not parse network interface ID: %w", err)
|
||||||
|
@ -648,7 +664,27 @@ func (client *azureClient) getNetworkInterfaceByID(ctx context.Context, networkI
|
||||||
if errors.As(err, &responseError) && responseError.StatusCode == http.StatusNotFound {
|
if errors.As(err, &responseError) && responseError.StatusCode == http.StatusNotFound {
|
||||||
return nil, errorNotFound
|
return nil, errorNotFound
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("Failed to retrieve Interface %v with error: %w", networkInterfaceID, err)
|
return nil, fmt.Errorf("failed to retrieve Interface %v with error: %w", networkInterfaceID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &resp.Interface, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getVMScaleSetVMNetworkInterfaceByID gets the network interface.
|
||||||
|
// If a 404 is returned from the Azure API, `errorNotFound` is returned.
|
||||||
|
func (client *azureClient) getVMScaleSetVMNetworkInterfaceByID(ctx context.Context, networkInterfaceID, scaleSetName, instanceID string) (*armnetwork.Interface, error) {
|
||||||
|
r, err := newAzureResourceFromID(networkInterfaceID, client.logger)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("could not parse network interface ID: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.nic.GetVirtualMachineScaleSetNetworkInterface(ctx, r.ResourceGroupName, scaleSetName, instanceID, r.Name, &armnetwork.InterfacesClientGetVirtualMachineScaleSetNetworkInterfaceOptions{Expand: to.Ptr("IPConfigurations/PublicIPAddress")})
|
||||||
|
if err != nil {
|
||||||
|
var responseError *azcore.ResponseError
|
||||||
|
if errors.As(err, &responseError) && responseError.StatusCode == http.StatusNotFound {
|
||||||
|
return nil, errorNotFound
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("failed to retrieve Interface %v with error: %w", networkInterfaceID, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &resp.Interface, nil
|
return &resp.Interface, nil
|
||||||
|
|
|
@ -142,6 +142,7 @@ func TestMapFromVMScaleSetVMWithEmptyTags(t *testing.T) {
|
||||||
vmSize := armcompute.VirtualMachineSizeTypes(size)
|
vmSize := armcompute.VirtualMachineSizeTypes(size)
|
||||||
osType := armcompute.OperatingSystemTypesLinux
|
osType := armcompute.OperatingSystemTypesLinux
|
||||||
vmType := "type"
|
vmType := "type"
|
||||||
|
instanceID := "123"
|
||||||
location := "westeurope"
|
location := "westeurope"
|
||||||
computerName := "computer_name"
|
computerName := "computer_name"
|
||||||
networkProfile := armcompute.NetworkProfile{
|
networkProfile := armcompute.NetworkProfile{
|
||||||
|
@ -166,6 +167,7 @@ func TestMapFromVMScaleSetVMWithEmptyTags(t *testing.T) {
|
||||||
ID: &id,
|
ID: &id,
|
||||||
Name: &name,
|
Name: &name,
|
||||||
Type: &vmType,
|
Type: &vmType,
|
||||||
|
InstanceID: &instanceID,
|
||||||
Location: &location,
|
Location: &location,
|
||||||
Tags: nil,
|
Tags: nil,
|
||||||
Properties: properties,
|
Properties: properties,
|
||||||
|
@ -182,6 +184,7 @@ func TestMapFromVMScaleSetVMWithEmptyTags(t *testing.T) {
|
||||||
Tags: map[string]*string{},
|
Tags: map[string]*string{},
|
||||||
NetworkInterfaces: []string{},
|
NetworkInterfaces: []string{},
|
||||||
ScaleSet: scaleSet,
|
ScaleSet: scaleSet,
|
||||||
|
InstanceID: instanceID,
|
||||||
Size: size,
|
Size: size,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,6 +200,7 @@ func TestMapFromVMScaleSetVMWithTags(t *testing.T) {
|
||||||
vmSize := armcompute.VirtualMachineSizeTypes(size)
|
vmSize := armcompute.VirtualMachineSizeTypes(size)
|
||||||
osType := armcompute.OperatingSystemTypesLinux
|
osType := armcompute.OperatingSystemTypesLinux
|
||||||
vmType := "type"
|
vmType := "type"
|
||||||
|
instanceID := "123"
|
||||||
location := "westeurope"
|
location := "westeurope"
|
||||||
computerName := "computer_name"
|
computerName := "computer_name"
|
||||||
tags := map[string]*string{
|
tags := map[string]*string{
|
||||||
|
@ -224,6 +228,7 @@ func TestMapFromVMScaleSetVMWithTags(t *testing.T) {
|
||||||
ID: &id,
|
ID: &id,
|
||||||
Name: &name,
|
Name: &name,
|
||||||
Type: &vmType,
|
Type: &vmType,
|
||||||
|
InstanceID: &instanceID,
|
||||||
Location: &location,
|
Location: &location,
|
||||||
Tags: tags,
|
Tags: tags,
|
||||||
Properties: properties,
|
Properties: properties,
|
||||||
|
@ -240,6 +245,7 @@ func TestMapFromVMScaleSetVMWithTags(t *testing.T) {
|
||||||
Tags: tags,
|
Tags: tags,
|
||||||
NetworkInterfaces: []string{},
|
NetworkInterfaces: []string{},
|
||||||
ScaleSet: scaleSet,
|
ScaleSet: scaleSet,
|
||||||
|
InstanceID: instanceID,
|
||||||
Size: size,
|
Size: size,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue