Skip to content

Commit 8853265

Browse files
Merge pull request #1120 from flavianmissi/OCPBUGS-42196
OCPBUGS-42196: pkg/storage/azure: use cluster-api tag key to discover vnet
2 parents 249d1a7 + 847b329 commit 8853265

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
@@ -676,8 +676,13 @@ func (d *driver) assurePrivateAccount(cfg *Azure, infra *configv1.Infrastructure
676676
}
677677

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

pkg/storage/azure/azureclient/azureclient.go

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

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

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

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

0 commit comments

Comments
 (0)