Skip to content

Commit 7345b8f

Browse files
authored
Merge pull request #3626 from k8s-infra-cherrypick-robot/cherry-pick-3616-to-release-1.9
[release-1.9] Allow azure:// prefix when parsing resource IDs
2 parents 6ada2b5 + 50168f8 commit 7345b8f

File tree

11 files changed

+71
-27
lines changed

11 files changed

+71
-27
lines changed

azure/defaults.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ package azure
1919
import (
2020
"fmt"
2121
"net/http"
22+
"strings"
2223

24+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
2325
"github.com/Azure/go-autorest/autorest"
2426
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
2527
"sigs.k8s.io/cluster-api-provider-azure/version"
@@ -361,3 +363,8 @@ func msCorrelationIDSendDecorator(snd autorest.Sender) autorest.Sender {
361363
return snd.Do(r)
362364
})
363365
}
366+
367+
// ParseResourceID parses a string to an *arm.ResourceID, first removing any "azure://" prefix.
368+
func ParseResourceID(id string) (*arm.ResourceID, error) {
369+
return arm.ParseResourceID(strings.TrimPrefix(id, ProviderIDPrefix))
370+
}

azure/defaults_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,55 @@ func TestMSCorrelationIDSendDecorator(t *testing.T) {
117117
receivedReq.Header.Get(string(tele.CorrIDKeyVal)),
118118
).To(Equal(string(corrID)))
119119
}
120+
121+
func TestParseResourceID(t *testing.T) {
122+
g := NewWithT(t)
123+
124+
tests := []struct {
125+
name string
126+
id string
127+
expectedName string
128+
errExpected bool
129+
}{
130+
{
131+
name: "invalid",
132+
id: "invalid",
133+
expectedName: "",
134+
errExpected: true,
135+
},
136+
{
137+
name: "invalid: must start with slash",
138+
id: "subscriptions/123/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm",
139+
expectedName: "",
140+
errExpected: true,
141+
},
142+
{
143+
name: "invalid: must start with subscriptions or providers",
144+
id: "/prescriptions/123/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm",
145+
expectedName: "",
146+
errExpected: true,
147+
},
148+
{
149+
name: "valid",
150+
id: "/subscriptions/123/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm",
151+
expectedName: "vm",
152+
},
153+
{
154+
name: "valid with provider prefix",
155+
id: "azure:///subscriptions/123/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm",
156+
expectedName: "vm",
157+
},
158+
}
159+
160+
for _, tt := range tests {
161+
t.Run(tt.name, func(t *testing.T) {
162+
resourceID, err := ParseResourceID(tt.id)
163+
if tt.errExpected {
164+
g.Expect(err).To(HaveOccurred())
165+
} else {
166+
g.Expect(err).NotTo(HaveOccurred())
167+
g.Expect(resourceID.Name).To(Equal(tt.expectedName))
168+
}
169+
})
170+
}
171+
}

azure/scope/machinepool.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"io"
2525
"strings"
2626

27-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
2827
"github.com/pkg/errors"
2928
corev1 "k8s.io/api/core/v1"
3029
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -378,7 +377,7 @@ func (m *MachinePoolScope) createMachine(ctx context.Context, machine azure.VMSS
378377
ctx, _, done := tele.StartSpanWithLogger(ctx, "scope.MachinePoolScope.createMachine")
379378
defer done()
380379

381-
parsed, err := arm.ParseResourceID(machine.ID)
380+
parsed, err := azure.ParseResourceID(machine.ID)
382381
if err != nil {
383382
return errors.Wrap(err, fmt.Sprintf("failed to parse resource id %q", machine.ID))
384383
}

azure/services/identities/client.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package identities
1919
import (
2020
"context"
2121

22-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
2322
"github.com/Azure/azure-sdk-for-go/services/msi/mgmt/2018-11-30/msi"
2423
"github.com/Azure/go-autorest/autorest"
2524
"sigs.k8s.io/cluster-api-provider-azure/azure"
@@ -63,7 +62,7 @@ func (ac *AzureClient) GetClientID(ctx context.Context, providerID string) (stri
6362
ctx, _, done := tele.StartSpanWithLogger(ctx, "identities.GetClientID")
6463
defer done()
6564

66-
parsed, err := arm.ParseResourceID(providerID)
65+
parsed, err := azure.ParseResourceID(providerID)
6766
if err != nil {
6867
return "", err
6968
}

azure/services/natgateways/spec.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package natgateways
1919
import (
2020
"context"
2121

22-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
2322
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-08-01/network"
2423
"github.com/pkg/errors"
2524
"k8s.io/utils/pointer"
@@ -97,7 +96,7 @@ func hasPublicIP(natGateway network.NatGateway, publicIPName string) bool {
9796
}
9897

9998
for _, publicIP := range *natGateway.PublicIPAddresses {
100-
resource, err := arm.ParseResourceID(*publicIP.ID)
99+
resource, err := azure.ParseResourceID(*publicIP.ID)
101100
if err != nil {
102101
continue
103102
}

azure/services/scalesetvms/scalesetvms.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"strings"
2323
"time"
2424

25-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
2625
"github.com/go-logr/logr"
2726
"github.com/pkg/errors"
2827
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
@@ -147,7 +146,7 @@ func (s *Service) deleteVMSSFlexVM(ctx context.Context, resourceID string) error
147146
}
148147
}()
149148

150-
parsed, err := arm.ParseResourceID(resourceID)
149+
parsed, err := azure.ParseResourceID(resourceID)
151150
if err != nil {
152151
return errors.Wrap(err, fmt.Sprintf("failed to parse resource id %q", resourceID))
153152
}

azure/services/virtualmachines/client.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"fmt"
2424
"time"
2525

26-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
2726
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
2827
"github.com/Azure/go-autorest/autorest"
2928
azureautorest "github.com/Azure/go-autorest/autorest/azure"
@@ -90,7 +89,7 @@ func (ac *AzureClient) GetByID(ctx context.Context, resourceID string) (compute.
9089
ctx, log, done := tele.StartSpanWithLogger(ctx, "virtualmachines.AzureClient.GetByID")
9190
defer done()
9291

93-
parsed, err := arm.ParseResourceID(resourceID)
92+
parsed, err := azure.ParseResourceID(resourceID)
9493
if err != nil {
9594
return compute.VirtualMachine{}, errors.Wrap(err, fmt.Sprintf("failed parsing the VM resource id %q", resourceID))
9695
}

test/e2e/azure_edgezone.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ package e2e
2121

2222
import (
2323
"context"
24-
"strings"
2524

26-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
2725
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
2826
"github.com/Azure/go-autorest/autorest/azure/auth"
2927
. "github.com/onsi/ginkgo/v2"
@@ -87,8 +85,7 @@ func AzureEdgeZoneClusterSpec(ctx context.Context, inputGetter func() AzureEdgeZ
8785
vmClient.Authorizer = auth
8886

8987
// get the resource group name
90-
resourceID := strings.TrimPrefix(*machineList.Items[0].Spec.ProviderID, azure.ProviderIDPrefix)
91-
resource, err := arm.ParseResourceID(resourceID)
88+
resource, err := azure.ParseResourceID(*machineList.Items[0].Spec.ProviderID)
9289
Expect(err).NotTo(HaveOccurred())
9390

9491
vmListResults, err := vmClient.List(ctx, resource.ResourceGroupName, "")

test/e2e/azure_logcollector.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"strings"
2929
"time"
3030

31-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
3231
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
3332
"github.com/Azure/go-autorest/autorest/azure/auth"
3433
"github.com/pkg/errors"
@@ -401,8 +400,7 @@ func collectVMBootLog(ctx context.Context, am *infrav1.AzureMachine, outputPath
401400
return errors.New("AzureMachine provider ID is nil")
402401
}
403402

404-
resourceID := strings.TrimPrefix(*am.Spec.ProviderID, azure.ProviderIDPrefix)
405-
resource, err := arm.ParseResourceID(resourceID)
403+
resource, err := azure.ParseResourceID(*am.Spec.ProviderID)
406404
if err != nil {
407405
return errors.Wrap(err, "failed to parse resource id")
408406
}
@@ -432,7 +430,7 @@ func collectVMSSBootLog(ctx context.Context, providerID string, outputPath strin
432430
v := strings.Split(resourceID, "/")
433431
instanceID := v[len(v)-1]
434432
resourceID = strings.TrimSuffix(resourceID, "/virtualMachines/"+instanceID)
435-
resource, err := arm.ParseResourceID(resourceID)
433+
resource, err := azure.ParseResourceID(resourceID)
436434
if err != nil {
437435
return errors.Wrap(err, "failed to parse resource id")
438436
}

test/e2e/azure_privatecluster.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"path/filepath"
2727
"strings"
2828

29-
"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
3029
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
3130
"github.com/Azure/azure-sdk-for-go/services/msi/mgmt/2018-11-30/msi"
3231
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-08-01/network"
@@ -421,7 +420,7 @@ func SetupExistingVNet(ctx context.Context, vnetCidr string, cpSubnetCidrs, node
421420
}
422421

423422
func getAPIVersion(resourceID string) (string, error) {
424-
parsed, err := arm.ParseResourceID(resourceID)
423+
parsed, err := azure.ParseResourceID(resourceID)
425424
if err != nil {
426425
return "", errors.Wrap(err, fmt.Sprintf("unable to parse resource ID %q", resourceID))
427426
}
@@ -455,7 +454,7 @@ func getClientIDforMSI(resourceID string) string {
455454
msiClient := msi.NewUserAssignedIdentitiesClient(subscriptionID)
456455
msiClient.Authorizer = authorizer
457456

458-
parsed, err := arm.ParseResourceID(resourceID)
457+
parsed, err := azure.ParseResourceID(resourceID)
459458
Expect(err).NotTo(HaveOccurred())
460459

461460
id, err := msiClient.Get(context.TODO(), parsed.ResourceGroupName, parsed.Name)

0 commit comments

Comments
 (0)