sigs.k8s.io/cluster-api-provider-azure@v1.17.0/azure/services/virtualnetworks/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 virtualnetworks
    18  
    19  import (
    20  	"context"
    21  
    22  	asonetworkv1 "github.com/Azure/azure-service-operator/v2/api/network/v1api20201101"
    23  	"github.com/Azure/azure-service-operator/v2/pkg/genruntime"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    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"
    28  	"sigs.k8s.io/cluster-api-provider-azure/azure/converters"
    29  )
    30  
    31  // VNetSpec defines the specification for a Virtual Network.
    32  type VNetSpec struct {
    33  	ResourceGroup    string
    34  	Name             string
    35  	CIDRs            []string
    36  	Location         string
    37  	ExtendedLocation *infrav1.ExtendedLocationSpec
    38  	ClusterName      string
    39  	AdditionalTags   infrav1.Tags
    40  }
    41  
    42  // ResourceRef implements azure.ASOResourceSpecGetter.
    43  func (s *VNetSpec) ResourceRef() *asonetworkv1.VirtualNetwork {
    44  	return &asonetworkv1.VirtualNetwork{
    45  		ObjectMeta: metav1.ObjectMeta{
    46  			Name: azure.GetNormalizedKubernetesName(s.Name),
    47  		},
    48  	}
    49  }
    50  
    51  // Parameters implements azure.ASOResourceSpecGetter.
    52  func (s *VNetSpec) Parameters(ctx context.Context, existing *asonetworkv1.VirtualNetwork) (*asonetworkv1.VirtualNetwork, error) {
    53  	vnet := existing
    54  	if existing == nil {
    55  		vnet = &asonetworkv1.VirtualNetwork{
    56  			Spec: asonetworkv1.VirtualNetwork_Spec{
    57  				Tags: infrav1.Build(infrav1.BuildParams{
    58  					ClusterName: s.ClusterName,
    59  					Lifecycle:   infrav1.ResourceLifecycleOwned,
    60  					Name:        ptr.To(s.Name),
    61  					Role:        ptr.To(infrav1.CommonRole),
    62  					Additional:  s.AdditionalTags,
    63  				}),
    64  			},
    65  		}
    66  	}
    67  
    68  	vnet.Spec.AzureName = s.Name
    69  	vnet.Spec.Owner = &genruntime.KnownResourceReference{
    70  		Name: azure.GetNormalizedKubernetesName(s.ResourceGroup),
    71  	}
    72  	vnet.Spec.Location = ptr.To(s.Location)
    73  	vnet.Spec.ExtendedLocation = converters.ExtendedLocationToNetworkASO(s.ExtendedLocation)
    74  	vnet.Spec.AddressSpace = &asonetworkv1.AddressSpace{
    75  		AddressPrefixes: s.CIDRs,
    76  	}
    77  
    78  	return vnet, nil
    79  }
    80  
    81  // WasManaged implements azure.ASOResourceSpecGetter.
    82  func (s *VNetSpec) WasManaged(resource *asonetworkv1.VirtualNetwork) bool {
    83  	return infrav1.Tags(resource.Status.Tags).HasOwned(s.ClusterName)
    84  }