sigs.k8s.io/cluster-api-provider-azure@v1.17.0/util/azure/azure.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package azure
    18  
    19  import (
    20  	"context"
    21  	"strings"
    22  	"time"
    23  
    24  	"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
    25  	"github.com/pkg/errors"
    26  	expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
    27  	"sigs.k8s.io/controller-runtime/pkg/client"
    28  )
    29  
    30  // AzureSystemNodeLabelPrefix is a standard node label prefix for Azure features, e.g., kubernetes.azure.com/scalesetpriority.
    31  const AzureSystemNodeLabelPrefix = "kubernetes.azure.com"
    32  
    33  const (
    34  	// ProviderIDPrefix will be appended to the beginning of Azure resource IDs to form the Kubernetes Provider ID.
    35  	// NOTE: this format matches the 2 slashes format used in cloud-provider and cluster-autoscaler.
    36  	ProviderIDPrefix = "azure://"
    37  )
    38  
    39  // IsAzureSystemNodeLabelKey is a helper function that determines whether a node label key is an Azure "system" label.
    40  func IsAzureSystemNodeLabelKey(labelKey string) bool {
    41  	return strings.HasPrefix(labelKey, AzureSystemNodeLabelPrefix)
    42  }
    43  
    44  // FindParentMachinePool finds the parent MachinePool for the AzureMachinePool.
    45  func FindParentMachinePool(ampName string, cli client.Client) (*expv1.MachinePool, error) {
    46  	ctx := context.Background()
    47  	machinePoolList := &expv1.MachinePoolList{}
    48  	if err := cli.List(ctx, machinePoolList); err != nil {
    49  		return nil, errors.Wrapf(err, "failed to list MachinePools for %s", ampName)
    50  	}
    51  	for _, mp := range machinePoolList.Items {
    52  		if mp.Spec.Template.Spec.InfrastructureRef.Name == ampName {
    53  			return &mp, nil
    54  		}
    55  	}
    56  	return nil, errors.Errorf("failed to get MachinePool for %s", ampName)
    57  }
    58  
    59  // FindParentMachinePoolWithRetry finds the parent MachinePool for the AzureMachinePool with retry.
    60  func FindParentMachinePoolWithRetry(ampName string, cli client.Client, maxAttempts int) (*expv1.MachinePool, error) {
    61  	for i := 1; ; i++ {
    62  		p, err := FindParentMachinePool(ampName, cli)
    63  		if err != nil {
    64  			if i >= maxAttempts {
    65  				return nil, errors.Wrap(err, "failed to find parent MachinePool")
    66  			}
    67  			time.Sleep(1 * time.Second)
    68  			continue
    69  		}
    70  		return p, nil
    71  	}
    72  }
    73  
    74  // ParseResourceID parses a string to an *arm.ResourceID, first removing any "azure://" prefix.
    75  func ParseResourceID(id string) (*arm.ResourceID, error) {
    76  	return arm.ParseResourceID(strings.TrimPrefix(id, ProviderIDPrefix))
    77  }