Skip to content

Commit

Permalink
Refactor tags service
Browse files Browse the repository at this point in the history
  • Loading branch information
Jont828 committed Oct 11, 2022
1 parent 724a4e7 commit fc006d5
Show file tree
Hide file tree
Showing 12 changed files with 691 additions and 498 deletions.
12 changes: 12 additions & 0 deletions azure/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package azure
import (
"context"

"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-10-01/resources"
"github.com/Azure/go-autorest/autorest"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
Expand Down Expand Up @@ -124,3 +125,14 @@ type ResourceSpecGetterWithHeaders interface {
// CustomHeaders returns the headers that should be added to Azure API calls.
CustomHeaders() map[string]string
}

type TagsSpecGetter interface {
// TagsScope returns the scope of a set of tags.
TagsScope() string
// MergeParameters returns the merge parameters for a set of tags.
MergeParameters(existing *resources.TagsResource) (*resources.TagsPatchResource, error)
// NewAnnotation returns the new annotation for a set of tags.
NewAnnotation(existing *resources.TagsResource) (map[string]interface{}, error)
// DeleteParameters returns the delete parameters for a set of tags.
DeleteParameters(existing *resources.TagsResource) (*resources.TagsPatchResource, error)
}
20 changes: 13 additions & 7 deletions azure/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"sigs.k8s.io/cluster-api-provider-azure/azure/services/routetables"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/securitygroups"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/subnets"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/tags"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/virtualnetworks"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/vnetpeerings"
"sigs.k8s.io/cluster-api-provider-azure/util/futures"
Expand Down Expand Up @@ -995,12 +996,17 @@ func (s *ClusterScope) SetAnnotation(key, value string) {
}

// TagsSpecs returns the tag specs for the AzureCluster.
func (s *ClusterScope) TagsSpecs() []azure.TagsSpec {
return []azure.TagsSpec{
{
Scope: azure.ResourceGroupID(s.SubscriptionID(), s.ResourceGroup()),
Tags: s.AdditionalTags(),
Annotation: azure.RGTagsLastAppliedAnnotation,
},
func (s *ClusterScope) TagsSpecs() ([]azure.TagsSpecGetter, error) {
annotationMap, err := s.AnnotationJSON(azure.RGTagsLastAppliedAnnotation)
if err != nil {
return nil, err
}

return []azure.TagsSpecGetter{
&tags.TagsSpec{
Scope: azure.ResourceGroupID(s.SubscriptionID(), s.ResourceGroup()),
Tags: s.AdditionalTags(),
LastAppliedTags: annotationMap,
},
}, nil
}
20 changes: 13 additions & 7 deletions azure/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"sigs.k8s.io/cluster-api-provider-azure/azure/services/publicips"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/resourceskus"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/roleassignments"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/tags"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/virtualmachineimages"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/virtualmachines"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/vmextensions"
Expand Down Expand Up @@ -177,14 +178,19 @@ func (m *MachineScope) VMSpec() azure.ResourceSpecGetter {
}

// TagsSpecs returns the tags for the AzureMachine.
func (m *MachineScope) TagsSpecs() []azure.TagsSpec {
return []azure.TagsSpec{
{
Scope: azure.VMID(m.SubscriptionID(), m.ResourceGroup(), m.Name()),
Tags: m.AdditionalTags(),
Annotation: azure.VMTagsLastAppliedAnnotation,
},
func (m *MachineScope) TagsSpecs() ([]azure.TagsSpecGetter, error) {
annotationMap, err := m.AnnotationJSON(azure.VMTagsLastAppliedAnnotation)
if err != nil {
return nil, err
}

return []azure.TagsSpecGetter{
&tags.TagsSpec{
Scope: azure.VMID(m.SubscriptionID(), m.ResourceGroup(), m.Name()),
Tags: m.AdditionalTags(),
LastAppliedTags: annotationMap,
},
}, nil
}

// PublicIPSpecs returns the public IP specs.
Expand Down
20 changes: 13 additions & 7 deletions azure/scope/managedcontrolplane.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"sigs.k8s.io/cluster-api-provider-azure/azure/services/groups"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/managedclusters"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/subnets"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/tags"
"sigs.k8s.io/cluster-api-provider-azure/azure/services/virtualnetworks"
infrav1exp "sigs.k8s.io/cluster-api-provider-azure/exp/api/v1beta1"
"sigs.k8s.io/cluster-api-provider-azure/util/futures"
Expand Down Expand Up @@ -642,12 +643,17 @@ func (s *ManagedControlPlaneScope) SetAnnotation(key, value string) {
}

// TagsSpecs returns the tag specs for the ManagedControlPlane.
func (s *ManagedControlPlaneScope) TagsSpecs() []azure.TagsSpec {
return []azure.TagsSpec{
{
Scope: azure.ResourceGroupID(s.SubscriptionID(), s.ResourceGroup()),
Tags: s.AdditionalTags(),
Annotation: azure.RGTagsLastAppliedAnnotation,
},
func (s *ManagedControlPlaneScope) TagsSpecs() ([]azure.TagsSpecGetter, error) {
annotationMap, err := s.AnnotationJSON(azure.RGTagsLastAppliedAnnotation)
if err != nil {
return nil, err
}

return []azure.TagsSpecGetter{
&tags.TagsSpec{
Scope: azure.ResourceGroupID(s.SubscriptionID(), s.ResourceGroup()),
Tags: s.AdditionalTags(),
LastAppliedTags: annotationMap,
},
}, nil
}
14 changes: 7 additions & 7 deletions azure/services/tags/client.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2020 The Kubernetes Authors.
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,8 +27,8 @@ import (

// client wraps go-sdk.
type client interface {
GetAtScope(context.Context, string) (resources.TagsResource, error)
UpdateAtScope(context.Context, string, resources.TagsPatchResource) (resources.TagsResource, error)
GetAtScope(context.Context, azure.TagsSpecGetter) (resources.TagsResource, error)
UpdateAtScope(context.Context, azure.TagsSpecGetter, resources.TagsPatchResource) (resources.TagsResource, error)
}

// azureClient contains the Azure go-sdk Client.
Expand All @@ -52,18 +52,18 @@ func newTagsClient(subscriptionID string, baseURI string, authorizer autorest.Au
}

// GetAtScope sends the get at scope request.
func (ac *azureClient) GetAtScope(ctx context.Context, scope string) (resources.TagsResource, error) {
func (ac *azureClient) GetAtScope(ctx context.Context, spec azure.TagsSpecGetter) (resources.TagsResource, error) {
ctx, _, done := tele.StartSpanWithLogger(ctx, "tags.AzureClient.GetAtScope")
defer done()

return ac.tags.GetAtScope(ctx, scope)
return ac.tags.GetAtScope(ctx, spec.TagsScope())
}

// UpdateAtScope this operation allows replacing, merging or selectively deleting tags on the specified resource or
// subscription.
func (ac *azureClient) UpdateAtScope(ctx context.Context, scope string, parameters resources.TagsPatchResource) (resources.TagsResource, error) {
func (ac *azureClient) UpdateAtScope(ctx context.Context, spec azure.TagsSpecGetter, parameters resources.TagsPatchResource) (resources.TagsResource, error) {
ctx, _, done := tele.StartSpanWithLogger(ctx, "tags.AzureClient.UpdateAtScope")
defer done()

return ac.tags.UpdateAtScope(ctx, scope, parameters)
return ac.tags.UpdateAtScope(ctx, spec.TagsScope(), parameters)
}
5 changes: 3 additions & 2 deletions azure/services/tags/mock_tags/client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions azure/services/tags/mock_tags/tags_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fc006d5

Please sign in to comment.