2022-03-22 11:03:15 -04:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute"
|
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork"
|
|
|
|
"github.com/edgelesssys/constellation/coordinator/role"
|
2022-06-10 07:18:30 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/azureshared"
|
2022-06-28 10:08:05 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/metadata"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
2022-05-24 04:04:42 -04:00
|
|
|
var (
|
|
|
|
coordinatorScaleSetRegexp = regexp.MustCompile(`constellation-scale-set-coordinators-[0-9a-zA-Z]+$`)
|
|
|
|
nodeScaleSetRegexp = regexp.MustCompile(`constellation-scale-set-nodes-[0-9a-zA-Z]+$`)
|
|
|
|
)
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
// getScaleSetVM tries to get an azure vm belonging to a scale set.
|
2022-06-28 10:08:05 -04:00
|
|
|
func (m *Metadata) getScaleSetVM(ctx context.Context, providerID string) (metadata.InstanceMetadata, error) {
|
2022-06-10 07:18:30 -04:00
|
|
|
_, resourceGroup, scaleSet, instanceID, err := azureshared.ScaleSetInformationFromProviderID(providerID)
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
2022-06-28 10:08:05 -04:00
|
|
|
return metadata.InstanceMetadata{}, err
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
vmResp, err := m.virtualMachineScaleSetVMsAPI.Get(ctx, resourceGroup, scaleSet, instanceID, nil)
|
|
|
|
if err != nil {
|
2022-06-28 10:08:05 -04:00
|
|
|
return metadata.InstanceMetadata{}, err
|
2022-05-24 04:04:42 -04:00
|
|
|
}
|
|
|
|
networkInterfaces, err := m.getScaleSetVMInterfaces(ctx, vmResp.VirtualMachineScaleSetVM, resourceGroup, scaleSet, instanceID)
|
|
|
|
if err != nil {
|
2022-06-28 10:08:05 -04:00
|
|
|
return metadata.InstanceMetadata{}, err
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-05-24 04:04:42 -04:00
|
|
|
publicIPAddresses, err := m.getScaleSetVMPublicIPAddresses(ctx, resourceGroup, scaleSet, instanceID, networkInterfaces)
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
2022-06-28 10:08:05 -04:00
|
|
|
return metadata.InstanceMetadata{}, err
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-05-24 04:04:42 -04:00
|
|
|
return convertScaleSetVMToCoreInstance(scaleSet, vmResp.VirtualMachineScaleSetVM, networkInterfaces, publicIPAddresses)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// listScaleSetVMs lists all scale set VMs in the current resource group.
|
2022-06-28 10:08:05 -04:00
|
|
|
func (m *Metadata) listScaleSetVMs(ctx context.Context, resourceGroup string) ([]metadata.InstanceMetadata, error) {
|
|
|
|
instances := []metadata.InstanceMetadata{}
|
2022-03-22 11:03:15 -04:00
|
|
|
scaleSetPager := m.scaleSetsAPI.List(resourceGroup, nil)
|
|
|
|
for scaleSetPager.NextPage(ctx) {
|
|
|
|
for _, scaleSet := range scaleSetPager.PageResponse().Value {
|
|
|
|
if scaleSet == nil || scaleSet.Name == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
vmPager := m.virtualMachineScaleSetVMsAPI.List(resourceGroup, *scaleSet.Name, nil)
|
|
|
|
for vmPager.NextPage(ctx) {
|
|
|
|
for _, vm := range vmPager.PageResponse().Value {
|
|
|
|
if vm == nil || vm.InstanceID == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
interfaces, err := m.getScaleSetVMInterfaces(ctx, *vm, resourceGroup, *scaleSet.Name, *vm.InstanceID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-24 04:04:42 -04:00
|
|
|
instance, err := convertScaleSetVMToCoreInstance(*scaleSet.Name, *vm, interfaces, nil)
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
instances = append(instances, instance)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return instances, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertScaleSetVMToCoreInstance converts an azure scale set virtual machine with interface configurations into a core.Instance.
|
2022-06-28 10:08:05 -04:00
|
|
|
func convertScaleSetVMToCoreInstance(scaleSet string, vm armcompute.VirtualMachineScaleSetVM, networkInterfaces []armnetwork.Interface, publicIPAddresses []string) (metadata.InstanceMetadata, error) {
|
2022-03-22 11:03:15 -04:00
|
|
|
if vm.ID == nil {
|
2022-06-28 10:08:05 -04:00
|
|
|
return metadata.InstanceMetadata{}, errors.New("retrieving instance from armcompute API client returned no instance ID")
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
if vm.Properties == nil || vm.Properties.OSProfile == nil || vm.Properties.OSProfile.ComputerName == nil {
|
2022-06-28 10:08:05 -04:00
|
|
|
return metadata.InstanceMetadata{}, errors.New("retrieving instance from armcompute API client returned no computer name")
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
var sshKeys map[string][]string
|
|
|
|
if vm.Properties.OSProfile.LinuxConfiguration == nil || vm.Properties.OSProfile.LinuxConfiguration.SSH == nil {
|
|
|
|
sshKeys = map[string][]string{}
|
|
|
|
} else {
|
|
|
|
sshKeys = extractSSHKeys(*vm.Properties.OSProfile.LinuxConfiguration.SSH)
|
|
|
|
}
|
2022-06-28 10:08:05 -04:00
|
|
|
return metadata.InstanceMetadata{
|
2022-03-22 11:03:15 -04:00
|
|
|
Name: *vm.Properties.OSProfile.ComputerName,
|
|
|
|
ProviderID: "azure://" + *vm.ID,
|
|
|
|
Role: extractScaleSetVMRole(scaleSet),
|
2022-05-24 04:04:42 -04:00
|
|
|
PrivateIPs: extractPrivateIPs(networkInterfaces),
|
|
|
|
PublicIPs: publicIPAddresses,
|
2022-03-22 11:03:15 -04:00
|
|
|
SSHKeys: sshKeys,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractScaleSetVMRole extracts the constellation role of a scale set using its name.
|
|
|
|
func extractScaleSetVMRole(scaleSet string) role.Role {
|
|
|
|
if coordinatorScaleSetRegexp.MatchString(scaleSet) {
|
|
|
|
return role.Coordinator
|
|
|
|
}
|
|
|
|
if nodeScaleSetRegexp.MatchString(scaleSet) {
|
|
|
|
return role.Node
|
|
|
|
}
|
|
|
|
return role.Unknown
|
|
|
|
}
|