sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/publicips/spec.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 publicips
    18  
    19  import (
    20  	"context"
    21  	"strings"
    22  
    23  	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4"
    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  )
    29  
    30  // PublicIPSpec defines the specification for a Public IP.
    31  type PublicIPSpec struct {
    32  	Name             string
    33  	ResourceGroup    string
    34  	ClusterName      string
    35  	DNSName          string
    36  	IsIPv6           bool
    37  	Location         string
    38  	ExtendedLocation *infrav1.ExtendedLocationSpec
    39  	FailureDomains   []*string
    40  	AdditionalTags   infrav1.Tags
    41  	IPTags           []infrav1.IPTag
    42  }
    43  
    44  // ResourceName returns the name of the public IP.
    45  func (s *PublicIPSpec) ResourceName() string {
    46  	return s.Name
    47  }
    48  
    49  // ResourceGroupName returns the name of the resource group.
    50  func (s *PublicIPSpec) ResourceGroupName() string {
    51  	return s.ResourceGroup
    52  }
    53  
    54  // OwnerResourceName is a no-op for public IPs.
    55  func (s *PublicIPSpec) OwnerResourceName() string {
    56  	return ""
    57  }
    58  
    59  // Parameters returns the parameters for the public IP.
    60  func (s *PublicIPSpec) Parameters(ctx context.Context, existing interface{}) (params interface{}, err error) {
    61  	if existing != nil {
    62  		if _, ok := existing.(armnetwork.PublicIPAddress); !ok {
    63  			return nil, errors.Errorf("%T is not an armnetwork.PublicIPAddress", existing)
    64  		}
    65  		// public IP already exists
    66  		return nil, nil
    67  	}
    68  
    69  	addressVersion := armnetwork.IPVersionIPv4
    70  	if s.IsIPv6 {
    71  		addressVersion = armnetwork.IPVersionIPv6
    72  	}
    73  
    74  	// only set DNS properties if there is a DNS name specified
    75  	var dnsSettings *armnetwork.PublicIPAddressDNSSettings
    76  	if s.DNSName != "" {
    77  		dnsSettings = &armnetwork.PublicIPAddressDNSSettings{
    78  			DomainNameLabel: ptr.To(strings.Split(s.DNSName, ".")[0]),
    79  			Fqdn:            ptr.To(s.DNSName),
    80  		}
    81  	}
    82  
    83  	return armnetwork.PublicIPAddress{
    84  		Tags: converters.TagsToMap(infrav1.Build(infrav1.BuildParams{
    85  			ClusterName: s.ClusterName,
    86  			Lifecycle:   infrav1.ResourceLifecycleOwned,
    87  			Name:        ptr.To(s.Name),
    88  			Additional:  s.AdditionalTags,
    89  		})),
    90  		SKU:              &armnetwork.PublicIPAddressSKU{Name: ptr.To(armnetwork.PublicIPAddressSKUNameStandard)},
    91  		Name:             ptr.To(s.Name),
    92  		Location:         ptr.To(s.Location),
    93  		ExtendedLocation: converters.ExtendedLocationToNetworkSDK(s.ExtendedLocation),
    94  		Properties: &armnetwork.PublicIPAddressPropertiesFormat{
    95  			PublicIPAddressVersion:   &addressVersion,
    96  			PublicIPAllocationMethod: ptr.To(armnetwork.IPAllocationMethodStatic),
    97  			DNSSettings:              dnsSettings,
    98  			IPTags:                   converters.IPTagsToSDK(s.IPTags),
    99  		},
   100  		Zones: s.FailureDomains,
   101  	}, nil
   102  }