|
| 1 | +// |
| 2 | +//Copyright (c) Microsoft Corporation. |
| 3 | +//Licensed under the MIT license. |
| 4 | + |
| 5 | +// Package azure provides Azure-specific utilities and services. |
| 6 | +package azure |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "k8s.io/apimachinery/pkg/api/resource" |
| 14 | + "k8s.io/klog/v2" |
| 15 | + |
| 16 | + clusterv1beta1 "go.goms.io/fleet/apis/cluster/v1beta1" |
| 17 | + placementv1beta1 "go.goms.io/fleet/apis/placement/v1beta1" |
| 18 | + computev1 "go.goms.io/fleet/apis/protos/azure/compute/v1" |
| 19 | + "go.goms.io/fleet/pkg/clients/azure/compute" |
| 20 | +) |
| 21 | + |
| 22 | +// defaultAzureCapacityService is the default implementation of AzureCapacityService. |
| 23 | +type defaultAzureCapacityService struct { |
| 24 | + // ComputeClient is the Azure compute client used to validate capacity requirements. |
| 25 | + ComputeClient compute.AttributeBasedVMSizeRecommenderClient |
| 26 | +} |
| 27 | + |
| 28 | +// Compile-time check to ensure defaultAzureCapacityService implements AzureCapacityService |
| 29 | +var _ AzureCapacityService = &defaultAzureCapacityService{} |
| 30 | + |
| 31 | +// NewAzureCapacityService creates a new default Azure capacity service |
| 32 | +func NewAzureCapacityService(computeClient compute.AttributeBasedVMSizeRecommenderClient) AzureCapacityService { |
| 33 | + return &defaultAzureCapacityService{ |
| 34 | + ComputeClient: computeClient, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// ValidateCapacityRequirement validates a capacity requirement against Azure APIs. |
| 39 | +func (s *defaultAzureCapacityService) ValidateCapacityRequirement( |
| 40 | + cluster *clusterv1beta1.MemberCluster, |
| 41 | + req placementv1beta1.PropertySelectorRequirement, |
| 42 | +) (bool, error) { |
| 43 | + subID, location := extractAzureInfoFromLabels(cluster) |
| 44 | + if subID == "" || location == "" { |
| 45 | + return false, fmt.Errorf("cluster %s does not have required Azure labels", cluster.Name) |
| 46 | + } |
| 47 | + capacity, sku, err := extractCapacityRequirements(req) |
| 48 | + if err != nil { |
| 49 | + return false, fmt.Errorf("failed to extract capacity requirement: %w", err) |
| 50 | + } |
| 51 | + |
| 52 | + request := &computev1.GenerateAttributeBasedRecommendationsRequest{ |
| 53 | + SubscriptionId: subID, |
| 54 | + Location: location, |
| 55 | + RegularPriorityProfile: &computev1.RegularPriorityProfile{ |
| 56 | + CapacityUnitType: computev1.CapacityUnitType_CAPACITY_UNIT_TYPE_VM_INSTANCE_COUNT, |
| 57 | + TargetCapacity: uint32(capacity.Value()), //nolint:gosec // safe: capacity is derived from user input should be relatively small |
| 58 | + }, |
| 59 | + ResourceProperties: &computev1.ResourceProperties{ |
| 60 | + VmAttributes: &computev1.VMAttributes{ |
| 61 | + AllowedVmSizes: []string{sku}, |
| 62 | + }, |
| 63 | + }, |
| 64 | + RecommendationProperties: &computev1.RecommendationProperties{ |
| 65 | + RestrictionsFilter: computev1.RecommendationProperties_RESTRICTIONS_FILTER_QUOTA_AND_OFFER_RESTRICTIONS, |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + respObj, err := s.ComputeClient.GenerateAttributeBasedRecommendations(context.Background(), request) |
| 70 | + if err != nil { |
| 71 | + return false, fmt.Errorf("failed to generate VM size recommendations from Azure: %w", err) |
| 72 | + } |
| 73 | + |
| 74 | + available := false |
| 75 | + for _, vm := range respObj.RecommendedVmSizes.RegularVmSizes { |
| 76 | + if vm.Name == sku { |
| 77 | + available = true |
| 78 | + klog.V(2).Infof("SKU %s is available in cluster %s", sku, cluster.Name) |
| 79 | + break |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return available, nil |
| 84 | +} |
| 85 | + |
| 86 | +// extractAzureInfoFromLabels extracts subscription ID and location from MemberCluster labels. |
| 87 | +// Returns the subscription ID and location if found, empty strings otherwise. |
| 88 | +func extractAzureInfoFromLabels(cluster *clusterv1beta1.MemberCluster) (subscriptionID, location string) { |
| 89 | + if cluster == nil || cluster.Labels == nil { |
| 90 | + return "", "" |
| 91 | + } |
| 92 | + |
| 93 | + subscriptionID = cluster.Labels[AzureSubscriptionIDLabelKey] |
| 94 | + location = cluster.Labels[AzureLocationLabelKey] |
| 95 | + |
| 96 | + return subscriptionID, location |
| 97 | +} |
| 98 | + |
| 99 | +// extractCapacityRequirements extracts the capacity value from a PropertySelectorRequirement. |
| 100 | +// This function is specifically designed for Azure SKU capacity properties that follow the pattern: |
| 101 | +// "kubernetes.azure.com/vm-size/{sku}/capacity" |
| 102 | +// Returns the capacity as a resource.Quantity and the SKU name if the requirement is valid, |
| 103 | +// or an error if the requirement is invalid or not a capacity property. |
| 104 | +func extractCapacityRequirements(req placementv1beta1.PropertySelectorRequirement) (*resource.Quantity, string, error) { |
| 105 | + // Extract SKU from the property name |
| 106 | + // Expected format: "kubernetes.azure.com/vm-size/{sku}/capacity" |
| 107 | + if !strings.HasSuffix(req.Name, ".capacity") { |
| 108 | + return nil, "", fmt.Errorf("invalid Azure SKU capacity property format: %q", req.Name) |
| 109 | + } |
| 110 | + |
| 111 | + // Remove prefix and suffix to get the SKU |
| 112 | + sku := strings.TrimSuffix(strings.TrimPrefix(req.Name, SkuCapacityPropertyPrefix+"."), ".capacity") |
| 113 | + if sku == "" { |
| 114 | + return nil, "", fmt.Errorf("cannot extract SKU from property name: %q", req.Name) |
| 115 | + } |
| 116 | + |
| 117 | + // Validate that we have exactly one value |
| 118 | + if len(req.Values) != 1 { |
| 119 | + return nil, "", fmt.Errorf("azure SKU capacity property must have exactly one value, got %d", len(req.Values)) |
| 120 | + } |
| 121 | + |
| 122 | + // Parse the capacity value |
| 123 | + capacity, err := resource.ParseQuantity(req.Values[0]) |
| 124 | + if err != nil { |
| 125 | + return nil, "", fmt.Errorf("failed to parse capacity value %q: %w", req.Values[0], err) |
| 126 | + } |
| 127 | + |
| 128 | + return &capacity, sku, nil |
| 129 | +} |
0 commit comments