Skip to content

Commit 847b329

Browse files
committed
pkg/storage/azure: also use cluster-api tag key to discover vnet
the previous tag key seems to no longer be added to clusters created using the cluster-api since 4.17. because the cluster-api (upstream) tag key might change, we keep the previous tag key as well (which will be added back by installer folks).
1 parent f6587e5 commit 847b329

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

pkg/storage/azure/azure.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,13 @@ func (d *driver) assurePrivateAccount(cfg *Azure, infra *configv1.Infrastructure
675675
}
676676

677677
if internalConfig.VNetName == "" {
678+
upstreamTagKey := fmt.Sprintf("sigs.k8s.io_cluster-api-provider-azure_cluster_%s", infra.Status.InfrastructureName)
678679
tagKey := fmt.Sprintf("kubernetes.io_cluster.%s", infra.Status.InfrastructureName)
679-
vnet, err := azclient.GetVNetByTag(d.Context, networkResourceGroup, tagKey, "owned", "shared")
680+
tagFilter := map[string][]string{
681+
upstreamTagKey: {"owned", "shared"},
682+
tagKey: {"owned", "shared"},
683+
}
684+
vnet, err := azclient.GetVNetByTags(d.Context, networkResourceGroup, tagFilter)
680685
if err != nil {
681686
return "", fmt.Errorf("failed to discover vnet name, please provide network details manually: %q", err)
682687
}

pkg/storage/azure/azureclient/azureclient.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,22 @@ func (c *Client) getStorageAccount(ctx context.Context, resourceGroupName, accou
149149
return resp.Account, nil
150150
}
151151

152-
func (c *Client) GetVNetByTag(ctx context.Context, resourceGroupName, tagKey string, tagValues ...string) (armnetwork.VirtualNetwork, error) {
152+
func (c *Client) vnetHasAnyTag(vnet armnetwork.VirtualNetwork, tagFilter map[string][]string) bool {
153+
for tagKey, tagValues := range tagFilter {
154+
tag, ok := vnet.Tags[tagKey]
155+
if !ok {
156+
continue
157+
}
158+
for _, tagValue := range tagValues {
159+
if *tag == tagValue {
160+
return true
161+
}
162+
}
163+
}
164+
return false
165+
}
166+
167+
func (c *Client) GetVNetByTags(ctx context.Context, resourceGroupName string, tagFilter map[string][]string) (armnetwork.VirtualNetwork, error) {
153168
client, err := armnetwork.NewVirtualNetworksClient(c.subscriptionID, c.creds, &arm.ClientOptions{
154169
ClientOptions: *c.clientOpts,
155170
})
@@ -164,19 +179,13 @@ func (c *Client) GetVNetByTag(ctx context.Context, resourceGroupName, tagKey str
164179
return armnetwork.VirtualNetwork{}, err
165180
}
166181
for _, vnet := range page.Value {
167-
tag, ok := vnet.Tags[tagKey]
168-
if !ok {
169-
continue
170-
}
171-
for _, tagValue := range tagValues {
172-
if *tag == tagValue {
173-
return *vnet, nil
174-
}
182+
if c.vnetHasAnyTag(*vnet, tagFilter) {
183+
return *vnet, nil
175184
}
176185
}
177186
}
178187

179-
return armnetwork.VirtualNetwork{}, fmt.Errorf("vnet with tag '%s: %v' not found", tagKey, tagValues)
188+
return armnetwork.VirtualNetwork{}, fmt.Errorf("vnet with tags '%+v' not found", tagFilter)
180189
}
181190

182191
func (c *Client) GetSubnetsByVNet(ctx context.Context, resourceGroupName, vnetName string) (armnetwork.Subnet, error) {

0 commit comments

Comments
 (0)