sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/availabilitysets/spec.go (about)

     1  /*
     2  Copyright 2021 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 availabilitysets
    18  
    19  import (
    20  	"context"
    21  	"strconv"
    22  
    23  	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
    24  	"github.com/pkg/errors"
    25  	"k8s.io/utils/ptr"
    26  	infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
    27  	"sigs.k8s.io/cluster-api-provider-azure/azure/converters"
    28  	"sigs.k8s.io/cluster-api-provider-azure/azure/services/resourceskus"
    29  )
    30  
    31  // AvailabilitySetSpec defines the specification for an availability set.
    32  type AvailabilitySetSpec struct {
    33  	Name           string
    34  	ResourceGroup  string
    35  	ClusterName    string
    36  	Location       string
    37  	SKU            *resourceskus.SKU
    38  	AdditionalTags infrav1.Tags
    39  }
    40  
    41  // ResourceName returns the name of the availability set.
    42  func (s *AvailabilitySetSpec) ResourceName() string {
    43  	return s.Name
    44  }
    45  
    46  // ResourceGroupName returns the name of the resource group.
    47  func (s *AvailabilitySetSpec) ResourceGroupName() string {
    48  	return s.ResourceGroup
    49  }
    50  
    51  // OwnerResourceName is a no-op for availability sets.
    52  func (s *AvailabilitySetSpec) OwnerResourceName() string {
    53  	return ""
    54  }
    55  
    56  // Parameters returns the parameters for the availability set.
    57  func (s *AvailabilitySetSpec) Parameters(ctx context.Context, existing interface{}) (params interface{}, err error) {
    58  	if existing != nil {
    59  		if _, ok := existing.(armcompute.AvailabilitySet); !ok {
    60  			return nil, errors.Errorf("%T is not an armcompute.AvailabilitySet", existing)
    61  		}
    62  		// availability set already exists
    63  		return nil, nil
    64  	}
    65  
    66  	if s.SKU == nil {
    67  		return nil, errors.New("unable to get required availability set SKU from machine cache")
    68  	}
    69  
    70  	var faultDomainCount *int32
    71  	faultDomainCountStr, ok := s.SKU.GetCapability(resourceskus.MaximumPlatformFaultDomainCount)
    72  	if !ok {
    73  		return nil, errors.Errorf("unable to get required availability set SKU capability %s", resourceskus.MaximumPlatformFaultDomainCount)
    74  	}
    75  	count, err := strconv.ParseInt(faultDomainCountStr, 10, 32)
    76  	if err != nil {
    77  		return nil, errors.Wrapf(err, "unable to parse availability set fault domain count")
    78  	}
    79  	faultDomainCount = ptr.To[int32](int32(count))
    80  
    81  	asParams := armcompute.AvailabilitySet{
    82  		SKU: &armcompute.SKU{
    83  			Name: ptr.To(string(armcompute.AvailabilitySetSKUTypesAligned)),
    84  		},
    85  		Properties: &armcompute.AvailabilitySetProperties{
    86  			PlatformFaultDomainCount: faultDomainCount,
    87  		},
    88  		Tags: converters.TagsToMap(infrav1.Build(infrav1.BuildParams{
    89  			ClusterName: s.ClusterName,
    90  			Lifecycle:   infrav1.ResourceLifecycleOwned,
    91  			Name:        ptr.To(s.Name),
    92  			Role:        ptr.To(infrav1.CommonRole),
    93  			Additional:  s.AdditionalTags,
    94  		})),
    95  		Location: ptr.To(s.Location),
    96  	}
    97  
    98  	return asParams, nil
    99  }