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