sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/subnets/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 subnets
    18  
    19  import (
    20  	"context"
    21  	"strings"
    22  
    23  	asonetworkv1 "github.com/Azure/azure-service-operator/v2/api/network/v1api20201101"
    24  	"github.com/Azure/azure-service-operator/v2/pkg/genruntime"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/utils/ptr"
    27  	infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
    28  	"sigs.k8s.io/cluster-api-provider-azure/azure"
    29  )
    30  
    31  // SubnetSpec defines the specification for a Subnet.
    32  type SubnetSpec struct {
    33  	Name              string
    34  	ResourceGroup     string
    35  	SubscriptionID    string
    36  	CIDRs             []string
    37  	VNetName          string
    38  	VNetResourceGroup string
    39  	IsVNetManaged     bool
    40  	RouteTableName    string
    41  	SecurityGroupName string
    42  	NatGatewayName    string
    43  	ServiceEndpoints  infrav1.ServiceEndpoints
    44  }
    45  
    46  // ResourceRef implements azure.ASOResourceSpecGetter.
    47  func (s *SubnetSpec) ResourceRef() *asonetworkv1.VirtualNetworksSubnet {
    48  	return &asonetworkv1.VirtualNetworksSubnet{
    49  		ObjectMeta: metav1.ObjectMeta{
    50  			// s.Name isn't unique per-cluster, so combine with vnet name to avoid collisions.
    51  			// ToLower makes the name compatible with standard Kubernetes name requirements.
    52  			Name: s.VNetName + "-" + strings.ToLower(s.Name),
    53  		},
    54  	}
    55  }
    56  
    57  // Parameters implements azure.ASOResourceSpecGetter.
    58  func (s *SubnetSpec) Parameters(ctx context.Context, existing *asonetworkv1.VirtualNetworksSubnet) (parameters *asonetworkv1.VirtualNetworksSubnet, err error) {
    59  	subnet := existing
    60  	if subnet == nil {
    61  		subnet = &asonetworkv1.VirtualNetworksSubnet{}
    62  	}
    63  
    64  	subnet.Spec = asonetworkv1.VirtualNetworks_Subnet_Spec{
    65  		AzureName: s.Name,
    66  		Owner: &genruntime.KnownResourceReference{
    67  			Name: s.VNetName,
    68  		},
    69  		AddressPrefixes: s.CIDRs,
    70  	}
    71  	// workaround needed to avoid SubscriptionNotRegisteredForFeature for feature Microsoft.Network/AllowMultipleAddressPrefixesOnSubnet.
    72  	if len(s.CIDRs) == 1 {
    73  		subnet.Spec.AddressPrefix = &s.CIDRs[0]
    74  	}
    75  
    76  	if s.RouteTableName != "" {
    77  		subnet.Spec.RouteTable = &asonetworkv1.RouteTableSpec_VirtualNetworks_Subnet_SubResourceEmbedded{
    78  			Reference: &genruntime.ResourceReference{
    79  				ARMID: azure.RouteTableID(s.SubscriptionID, s.VNetResourceGroup, s.RouteTableName),
    80  			},
    81  		}
    82  	}
    83  
    84  	if s.NatGatewayName != "" {
    85  		subnet.Spec.NatGateway = &asonetworkv1.SubResource{
    86  			Reference: &genruntime.ResourceReference{
    87  				ARMID: azure.NatGatewayID(s.SubscriptionID, s.ResourceGroup, s.NatGatewayName),
    88  			},
    89  		}
    90  	}
    91  
    92  	if s.SecurityGroupName != "" {
    93  		subnet.Spec.NetworkSecurityGroup = &asonetworkv1.NetworkSecurityGroupSpec_VirtualNetworks_Subnet_SubResourceEmbedded{
    94  			Reference: &genruntime.ResourceReference{
    95  				ARMID: azure.SecurityGroupID(s.SubscriptionID, s.VNetResourceGroup, s.SecurityGroupName),
    96  			},
    97  		}
    98  	}
    99  
   100  	//nolint:prealloc // pre-allocating this slice isn't going to make any meaningful performance difference
   101  	// and makes it harder to keep this value nil when s.ServiceEndpoints is empty as is necessary.
   102  	var serviceEndpoints []asonetworkv1.ServiceEndpointPropertiesFormat
   103  	for _, se := range s.ServiceEndpoints {
   104  		serviceEndpoints = append(serviceEndpoints, asonetworkv1.ServiceEndpointPropertiesFormat{Service: ptr.To(se.Service), Locations: se.Locations})
   105  	}
   106  	subnet.Spec.ServiceEndpoints = serviceEndpoints
   107  
   108  	return subnet, nil
   109  }
   110  
   111  // WasManaged implements azure.ASOResourceSpecGetter.
   112  func (s *SubnetSpec) WasManaged(resource *asonetworkv1.VirtualNetworksSubnet) bool {
   113  	return s.IsVNetManaged
   114  }