mirror of
https://github.com/edgelesssys/constellation.git
synced 2025-05-02 22:34:56 -04:00
Refactor provider metadata
This commit is contained in:
parent
32f1f5fd3e
commit
09e86e6c5d
36 changed files with 198 additions and 1340 deletions
|
@ -7,17 +7,10 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
azureVMProviderIDRegexp = regexp.MustCompile(`^azure:///subscriptions/([^/]+)/resourceGroups/([^/]+)/providers/Microsoft.Compute/virtualMachines/([^/]+)$`)
|
||||
azureVMSSProviderIDRegexp = regexp.MustCompile(`^azure:///subscriptions/([^/]+)/resourceGroups/([^/]+)/providers/Microsoft.Compute/virtualMachineScaleSets/([^/]+)/virtualMachines/([^/]+)$`)
|
||||
)
|
||||
var azureVMSSProviderIDRegexp = regexp.MustCompile(`^azure:///subscriptions/([^/]+)/resourceGroups/([^/]+)/providers/Microsoft.Compute/virtualMachineScaleSets/([^/]+)/virtualMachines/([^/]+)$`)
|
||||
|
||||
// BasicsFromProviderID extracts subscriptionID and resourceGroup from both types of valid azure providerID.
|
||||
func BasicsFromProviderID(providerID string) (subscriptionID, resourceGroup string, err error) {
|
||||
subscriptionID, resourceGroup, _, err = VMInformationFromProviderID(providerID)
|
||||
if err == nil {
|
||||
return subscriptionID, resourceGroup, nil
|
||||
}
|
||||
subscriptionID, resourceGroup, _, _, err = ScaleSetInformationFromProviderID(providerID)
|
||||
if err == nil {
|
||||
return subscriptionID, resourceGroup, nil
|
||||
|
@ -29,7 +22,7 @@ func BasicsFromProviderID(providerID string) (subscriptionID, resourceGroup stri
|
|||
// suffix at the resource group, e.g., resource-group-J18dB
|
||||
// J18dB is the UID.
|
||||
func UIDFromProviderID(providerID string) (string, error) {
|
||||
_, resourceGroup, err := BasicsFromProviderID(providerID)
|
||||
_, resourceGroup, _, _, err := ScaleSetInformationFromProviderID(providerID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -38,17 +31,6 @@ func UIDFromProviderID(providerID string) (string, error) {
|
|||
return parts[len(parts)-1], nil
|
||||
}
|
||||
|
||||
// VMInformationFromProviderID splits a provider's id belonging to a single azure instance into core components.
|
||||
// A providerID for individual VMs is build after the following schema:
|
||||
// - 'azure:///subscriptions/<subscription-id>/resourceGroups/<resource-group>/providers/Microsoft.Compute/virtualMachines/<instance-name>'
|
||||
func VMInformationFromProviderID(providerID string) (subscriptionID, resourceGroup, instanceName string, err error) {
|
||||
matches := azureVMProviderIDRegexp.FindStringSubmatch(providerID)
|
||||
if len(matches) != 4 {
|
||||
return "", "", "", errors.New("error splitting providerID")
|
||||
}
|
||||
return matches[1], matches[2], matches[3], nil
|
||||
}
|
||||
|
||||
// ScaleSetInformationFromProviderID splits a provider's id belonging to an azure scaleset into core components.
|
||||
// A providerID for scale set VMs is build after the following schema:
|
||||
// - 'azure:///subscriptions/<subscription-id>/resourceGroups/<resource-group>/providers/Microsoft.Compute/virtualMachineScaleSets/<scale-set-name>/virtualMachines/<instance-id>'
|
||||
|
|
|
@ -2,7 +2,6 @@ package metadata
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
|
@ -36,24 +35,30 @@ type metadataAPI interface {
|
|||
Supported() bool
|
||||
}
|
||||
|
||||
// TODO(katexochen): Rename to InitEndpoints
|
||||
func CoordinatorEndpoints(ctx context.Context, api metadataAPI) ([]string, error) {
|
||||
if !api.Supported() {
|
||||
return nil, errors.New("retrieving instances list from cloud provider is not yet supported")
|
||||
}
|
||||
instances, err := api.List(ctx)
|
||||
type InstanceSelfer interface {
|
||||
// Self retrieves the current instance.
|
||||
Self(ctx context.Context) (InstanceMetadata, error)
|
||||
}
|
||||
|
||||
type InstanceLister interface {
|
||||
// List retrieves all instances belonging to the current constellation.
|
||||
List(ctx context.Context) ([]InstanceMetadata, error)
|
||||
}
|
||||
|
||||
func InitServerEndpoints(ctx context.Context, lister InstanceLister) ([]string, error) {
|
||||
instances, err := lister.List(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("retrieving instances list from cloud provider: %w", err)
|
||||
}
|
||||
coordinatorEndpoints := []string{}
|
||||
initServerEndpoints := []string{}
|
||||
for _, instance := range instances {
|
||||
// check if role of instance is "Coordinator"
|
||||
if instance.Role == role.Coordinator {
|
||||
for _, ip := range instance.PrivateIPs {
|
||||
coordinatorEndpoints = append(coordinatorEndpoints, net.JoinHostPort(ip, strconv.Itoa(constants.CoordinatorPort)))
|
||||
initServerEndpoints = append(initServerEndpoints, net.JoinHostPort(ip, strconv.Itoa(constants.CoordinatorPort)))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return coordinatorEndpoints, nil
|
||||
return initServerEndpoints, nil
|
||||
}
|
||||
|
|
|
@ -50,7 +50,6 @@ const (
|
|||
//
|
||||
// Filenames.
|
||||
//
|
||||
|
||||
StateFilename = "constellation-state.json"
|
||||
ClusterIDsFileName = "constellation-id.json"
|
||||
ConfigFilename = "constellation-conf.yaml"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue