2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-07-18 04:18:29 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-07-29 09:00:15 -04:00
|
|
|
"strings"
|
2022-07-18 04:18:29 -04:00
|
|
|
|
2022-08-30 09:15:51 -04:00
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
|
2022-07-18 04:18:29 -04:00
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetScalingGroupImage returns the image URI of the scaling group.
|
|
|
|
func (c *Client) GetScalingGroupImage(ctx context.Context, scalingGroupID string) (string, error) {
|
|
|
|
_, resourceGroup, scaleSet, err := splitVMSSID(scalingGroupID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
res, err := c.scaleSetsAPI.Get(ctx, resourceGroup, scaleSet, nil)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if res.Properties == nil ||
|
|
|
|
res.Properties.VirtualMachineProfile == nil ||
|
|
|
|
res.Properties.VirtualMachineProfile.StorageProfile == nil ||
|
|
|
|
res.Properties.VirtualMachineProfile.StorageProfile.ImageReference == nil ||
|
2022-08-30 09:15:51 -04:00
|
|
|
res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.ID == nil && res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.CommunityGalleryImageID == nil {
|
2022-07-18 04:18:29 -04:00
|
|
|
return "", fmt.Errorf("scalet set %q does not have valid image reference", scalingGroupID)
|
|
|
|
}
|
2022-08-30 09:15:51 -04:00
|
|
|
if res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.ID != nil {
|
|
|
|
return *res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.ID, nil
|
|
|
|
}
|
2022-10-05 09:02:46 -04:00
|
|
|
return *res.Properties.VirtualMachineProfile.StorageProfile.ImageReference.CommunityGalleryImageID, nil
|
2022-07-18 04:18:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetScalingGroupImage sets the image URI of the scaling group.
|
|
|
|
func (c *Client) SetScalingGroupImage(ctx context.Context, scalingGroupID, imageURI string) error {
|
|
|
|
_, resourceGroup, scaleSet, err := splitVMSSID(scalingGroupID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
poller, err := c.scaleSetsAPI.BeginUpdate(ctx, resourceGroup, scaleSet, armcompute.VirtualMachineScaleSetUpdate{
|
|
|
|
Properties: &armcompute.VirtualMachineScaleSetUpdateProperties{
|
|
|
|
VirtualMachineProfile: &armcompute.VirtualMachineScaleSetUpdateVMProfile{
|
|
|
|
StorageProfile: &armcompute.VirtualMachineScaleSetUpdateStorageProfile{
|
2022-08-30 09:15:51 -04:00
|
|
|
ImageReference: imageReferenceFromImage(imageURI),
|
2022-07-18 04:18:29 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := poller.PollUntilDone(ctx, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-07-29 09:00:15 -04:00
|
|
|
|
2022-09-15 10:48:32 -04:00
|
|
|
// GetScalingGroupName retrieves the name of a scaling group, as expected by Kubernetes.
|
|
|
|
// This keeps the casing of the original name, but Kubernetes requires the name to be lowercase,
|
|
|
|
// so use strings.ToLower() on the result if using the name in a Kubernetes context.
|
|
|
|
func (c *Client) GetScalingGroupName(scalingGroupID string) (string, error) {
|
2022-07-29 09:00:15 -04:00
|
|
|
_, _, scaleSet, err := splitVMSSID(scalingGroupID)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("getting scaling group name: %w", err)
|
|
|
|
}
|
2022-09-15 10:48:32 -04:00
|
|
|
return scaleSet, nil
|
|
|
|
}
|
|
|
|
|
2022-11-09 09:57:54 -05:00
|
|
|
// GetAutoscalingGroupName retrieves the name of a scaling group as needed by the cluster-autoscaler.
|
2022-09-15 10:48:32 -04:00
|
|
|
func (c *Client) GetAutoscalingGroupName(scalingGroupID string) (string, error) {
|
|
|
|
return c.GetScalingGroupName(scalingGroupID)
|
2022-07-29 09:00:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListScalingGroups retrieves a list of scaling groups for the cluster.
|
|
|
|
func (c *Client) ListScalingGroups(ctx context.Context, uid string) (controlPlaneGroupIDs []string, workerGroupIDs []string, err error) {
|
2022-10-24 10:58:21 -04:00
|
|
|
pager := c.scaleSetsAPI.NewListPager(c.config.ResourceGroup, nil)
|
|
|
|
|
|
|
|
for pager.More() {
|
|
|
|
page, err := pager.NextPage(ctx)
|
2022-07-29 09:00:15 -04:00
|
|
|
if err != nil {
|
2022-10-24 10:58:21 -04:00
|
|
|
return nil, nil, fmt.Errorf("paging scale sets: %w", err)
|
2022-07-29 09:00:15 -04:00
|
|
|
}
|
2022-10-24 10:58:21 -04:00
|
|
|
for _, scaleSet := range page.Value {
|
|
|
|
if scaleSet == nil || scaleSet.ID == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if scaleSet.Tags == nil || scaleSet.Tags["constellation-uid"] == nil || *scaleSet.Tags["constellation-uid"] != uid {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("getting scaling group name: %w", err)
|
|
|
|
}
|
|
|
|
switch *scaleSet.Tags["constellation-role"] {
|
|
|
|
case "control-plane", "controlplane":
|
|
|
|
controlPlaneGroupIDs = append(controlPlaneGroupIDs, *scaleSet.ID)
|
|
|
|
case "worker":
|
|
|
|
workerGroupIDs = append(workerGroupIDs, *scaleSet.ID)
|
|
|
|
}
|
2022-07-29 09:00:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return controlPlaneGroupIDs, workerGroupIDs, nil
|
|
|
|
}
|
2022-08-30 09:15:51 -04:00
|
|
|
|
|
|
|
func imageReferenceFromImage(img string) *armcompute.ImageReference {
|
|
|
|
ref := &armcompute.ImageReference{}
|
|
|
|
|
|
|
|
if strings.HasPrefix(img, "/CommunityGalleries") {
|
|
|
|
ref.CommunityGalleryImageID = to.Ptr(img)
|
|
|
|
} else {
|
|
|
|
ref.ID = to.Ptr(img)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ref
|
|
|
|
}
|