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

     1  /*
     2  Copyright 2023 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 groups
    18  
    19  import (
    20  	"context"
    21  
    22  	asoresourcesv1 "github.com/Azure/azure-service-operator/v2/api/resources/v1api20200601"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  	"k8s.io/utils/ptr"
    25  	infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
    26  	"sigs.k8s.io/cluster-api-provider-azure/azure/services/aso"
    27  )
    28  
    29  // GroupSpec defines the specification for a Resource Group.
    30  type GroupSpec struct {
    31  	Name           string
    32  	AzureName      string
    33  	Location       string
    34  	ClusterName    string
    35  	AdditionalTags infrav1.Tags
    36  }
    37  
    38  // ResourceRef implements aso.ResourceSpecGetter.
    39  func (s *GroupSpec) ResourceRef() *asoresourcesv1.ResourceGroup {
    40  	return &asoresourcesv1.ResourceGroup{
    41  		ObjectMeta: metav1.ObjectMeta{
    42  			Name: s.Name,
    43  		},
    44  	}
    45  }
    46  
    47  // Parameters implements aso.ResourceSpecGetter.
    48  func (s *GroupSpec) Parameters(ctx context.Context, existing *asoresourcesv1.ResourceGroup) (*asoresourcesv1.ResourceGroup, error) {
    49  	if existing != nil {
    50  		return existing, nil
    51  	}
    52  
    53  	resourceGroup := &asoresourcesv1.ResourceGroup{
    54  		Spec: asoresourcesv1.ResourceGroup_Spec{
    55  			Location: ptr.To(s.Location),
    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  	if s.AzureName != "" {
    66  		resourceGroup.Spec.AzureName = s.AzureName
    67  	}
    68  	return resourceGroup, nil
    69  }
    70  
    71  // WasManaged implements azure.ASOResourceSpecGetter.
    72  func (s *GroupSpec) WasManaged(resource *asoresourcesv1.ResourceGroup) bool {
    73  	return infrav1.Tags(resource.Status.Tags).HasOwned(s.ClusterName)
    74  }
    75  
    76  var _ aso.TagsGetterSetter[*asoresourcesv1.ResourceGroup] = (*GroupSpec)(nil)
    77  
    78  // GetAdditionalTags implements aso.TagsGetterSetter.
    79  func (s *GroupSpec) GetAdditionalTags() infrav1.Tags {
    80  	return s.AdditionalTags
    81  }
    82  
    83  // GetDesiredTags implements aso.TagsGetterSetter.
    84  func (*GroupSpec) GetDesiredTags(resource *asoresourcesv1.ResourceGroup) infrav1.Tags {
    85  	return resource.Spec.Tags
    86  }
    87  
    88  // SetTags implements aso.TagsGetterSetter.
    89  func (*GroupSpec) SetTags(resource *asoresourcesv1.ResourceGroup, tags infrav1.Tags) {
    90  	resource.Spec.Tags = tags
    91  }