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