Skip to content

Commit 7ae1801

Browse files
committed
Refactor GET calls for tags to use tags.GetAtScope
1 parent 7de91f0 commit 7ae1801

File tree

11 files changed

+410
-198
lines changed

11 files changed

+410
-198
lines changed

azure/defaults.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,16 @@ func AvailabilitySetID(subscriptionID, resourceGroup, availabilitySetName string
281281
return fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/availabilitySets/%s", subscriptionID, resourceGroup, availabilitySetName)
282282
}
283283

284+
// PrivateDNSZoneID returns the azure resource ID for a given private DNS zone.
285+
func PrivateDNSZoneID(subscriptionID, resourceGroup, privateDNSZoneName string) string {
286+
return fmt.Sprintf("subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/privateDnsZones/%s", subscriptionID, resourceGroup, privateDNSZoneName)
287+
}
288+
289+
// VirtualNetworkLinkID returns the azure resource ID for a given virtual network link.
290+
func VirtualNetworkLinkID(subscriptionID, resourceGroup, privateDNSZoneName, virtualNetworkLinkName string) string {
291+
return fmt.Sprintf("subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/privateDnsZones/%s/virtualNetworkLinks/%s", subscriptionID, resourceGroup, privateDNSZoneName, virtualNetworkLinkName)
292+
}
293+
284294
// GetBootstrappingVMExtension returns the CAPZ Bootstrapping VM extension.
285295
// The CAPZ Bootstrapping extension is a simple clone of https://github.com/Azure/custom-script-extension-linux for Linux or
286296
// https://docs.microsoft.com/en-us/azure/virtual-machines/extensions/custom-script-windows for Windows.

azure/services/async/interfaces.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package async
1919
import (
2020
"context"
2121

22+
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-10-01/resources"
2223
azureautorest "github.com/Azure/go-autorest/autorest/azure"
2324
"sigs.k8s.io/cluster-api-provider-azure/azure"
2425
)
@@ -41,6 +42,11 @@ type Getter interface {
4142
Get(ctx context.Context, spec azure.ResourceSpecGetter) (result interface{}, err error)
4243
}
4344

45+
// TagsGetter is an interface that can get a tags resource.
46+
type TagsGetter interface {
47+
GetAtScope(ctx context.Context, scope string) (result resources.TagsResource, err error)
48+
}
49+
4450
// Creator is a client that can create or update a resource asynchronously.
4551
type Creator interface {
4652
FutureHandler

azure/services/async/mock_async/async_mock.go

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

azure/services/privatedns/privatedns.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ package privatedns
1919
import (
2020
"context"
2121

22-
"github.com/Azure/azure-sdk-for-go/services/privatedns/mgmt/2018-09-01/privatedns"
2322
"github.com/pkg/errors"
2423
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
2524
"sigs.k8s.io/cluster-api-provider-azure/azure"
2625
"sigs.k8s.io/cluster-api-provider-azure/azure/converters"
2726
"sigs.k8s.io/cluster-api-provider-azure/azure/services/async"
27+
"sigs.k8s.io/cluster-api-provider-azure/azure/services/tags"
2828
"sigs.k8s.io/cluster-api-provider-azure/util/reconciler"
2929
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
3030
)
@@ -42,8 +42,7 @@ type Scope interface {
4242
// Service provides operations on Azure resources.
4343
type Service struct {
4444
Scope Scope
45-
zoneGetter async.Getter
46-
vnetLinkGetter async.Getter
45+
TagsGetter async.TagsGetter
4746
zoneReconciler async.Reconciler
4847
vnetLinkReconciler async.Reconciler
4948
recordReconciler async.Reconciler
@@ -54,10 +53,10 @@ func New(scope Scope) *Service {
5453
zoneClient := newPrivateZonesClient(scope)
5554
vnetLinkClient := newVirtualNetworkLinksClient(scope)
5655
recordSetsClient := newRecordSetsClient(scope)
56+
tagsClient := tags.NewClient(scope)
5757
return &Service{
5858
Scope: scope,
59-
zoneGetter: zoneClient,
60-
vnetLinkGetter: vnetLinkClient,
59+
TagsGetter: tagsClient,
6160
zoneReconciler: async.New(scope, zoneClient, zoneClient),
6261
vnetLinkReconciler: async.New(scope, vnetLinkClient, vnetLinkClient),
6362
recordReconciler: async.New(scope, recordSetsClient, recordSetsClient),
@@ -136,17 +135,18 @@ func (s *Service) Delete(ctx context.Context) error {
136135
// isVnetLinkManaged returns true if the vnet link has an owned tag with the cluster name as value,
137136
// meaning that the vnet link lifecycle is managed.
138137
func (s *Service) isVnetLinkManaged(ctx context.Context, spec azure.ResourceSpecGetter) (bool, error) {
139-
result, err := s.vnetLinkGetter.Get(ctx, spec)
138+
scope := azure.VirtualNetworkLinkID(s.Scope.SubscriptionID(), spec.ResourceGroupName(), spec.OwnerResourceName(), spec.ResourceName())
139+
result, err := s.TagsGetter.GetAtScope(ctx, scope)
140140
if err != nil {
141141
return false, err
142142
}
143143

144-
link, ok := result.(privatedns.VirtualNetworkLink)
145-
if !ok {
146-
return false, errors.Errorf("%T is not a privatedns.VirtualNetworkLink", link)
144+
tagsMap := make(map[string]*string)
145+
if result.Properties != nil && result.Properties.Tags != nil {
146+
tagsMap = result.Properties.Tags
147147
}
148148

149-
tags := converters.MapToTags(link.Tags)
149+
tags := converters.MapToTags(tagsMap)
150150
return tags.HasOwned(s.Scope.ClusterName()), nil
151151
}
152152

@@ -158,15 +158,17 @@ func (s *Service) IsManaged(ctx context.Context) (bool, error) {
158158
return false, errors.Errorf("no private dns zone spec available")
159159
}
160160

161-
result, err := s.zoneGetter.Get(ctx, zoneSpec)
161+
scope := azure.PrivateDNSZoneID(s.Scope.SubscriptionID(), zoneSpec.ResourceGroupName(), zoneSpec.ResourceName())
162+
result, err := s.TagsGetter.GetAtScope(ctx, scope)
162163
if err != nil {
163164
return false, err
164165
}
165-
zone, ok := result.(privatedns.PrivateZone)
166-
if !ok {
167-
return false, errors.Errorf("%T is not a privatedns.PrivateZone", zone)
166+
167+
tagsMap := make(map[string]*string)
168+
if result.Properties != nil && result.Properties.Tags != nil {
169+
tagsMap = result.Properties.Tags
168170
}
169171

170-
tags := converters.MapToTags(zone.Tags)
172+
tags := converters.MapToTags(tagsMap)
171173
return tags.HasOwned(s.Scope.ClusterName()), nil
172174
}

0 commit comments

Comments
 (0)