Skip to content

Commit 30d294a

Browse files
authored
Merge pull request #1297 from CecileRobertMichon/cherry-pick-1293
[backport] Fix VM provider ID to match node ID
2 parents 5a264be + 18ae8fe commit 30d294a

File tree

8 files changed

+24
-18
lines changed

8 files changed

+24
-18
lines changed

cloud/converters/identity.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package converters
1919
import (
2020
"strings"
2121

22+
azure "sigs.k8s.io/cluster-api-provider-azure/cloud"
23+
2224
"github.com/Azure/azure-sdk-for-go/profiles/latest/compute/mgmt/compute"
2325
"github.com/pkg/errors"
2426

@@ -59,7 +61,7 @@ func UserAssignedIdentitiesToVMSSSDK(identities []infrav1.UserAssignedIdentity)
5961
return userIdentitiesMap, nil
6062
}
6163

62-
// sanitized removes "azure:///" prefix from the given id
64+
// sanitized removes "azure://" prefix from the given id
6365
func sanitized(id string) string {
64-
return strings.TrimPrefix(id, "azure:///")
66+
return strings.TrimPrefix(id, azure.ProviderIDPrefix)
6567
}

cloud/converters/identity_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ var sampleSubjectFactory = []infrav1.UserAssignedIdentity{
3838
}
3939

4040
var expectedVMSDKObject = map[string]*compute.VirtualMachineIdentityUserAssignedIdentitiesValue{
41-
"foo": {},
42-
"bar": {},
41+
"/foo": {},
42+
"/bar": {},
4343
"/without/prefix": {},
4444
}
4545

4646
var expectedVMSSSDKObject = map[string]*compute.VirtualMachineScaleSetIdentityUserAssignedIdentitiesValue{
47-
"foo": {},
48-
"bar": {},
47+
"/foo": {},
48+
"/bar": {},
4949
"/without/prefix": {},
5050
}
5151

cloud/defaults.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ const (
6565
ControlPlaneNodeGroup = "control-plane"
6666
)
6767

68+
const (
69+
// ProviderIDPrefix will be appended to the beginning of Azure resource IDs to form the Kubernetes Provider ID.
70+
// NOTE: this format matches the 2 slashes format used in cloud-provider and cluster-autoscaler.
71+
ProviderIDPrefix = "azure://"
72+
)
73+
6874
// GenerateBackendAddressPoolName generates a load balancer backend address pool name.
6975
func GenerateBackendAddressPoolName(lbName string) string {
7076
return fmt.Sprintf("%s-%s", lbName, "backendPool")

cloud/scope/machinepool.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package scope
1919
import (
2020
"context"
2121
"encoding/base64"
22-
"fmt"
2322

2423
"github.com/Azure/go-autorest/autorest/to"
2524
"github.com/go-logr/logr"
@@ -168,7 +167,7 @@ func (m *MachinePoolScope) UpdateInstanceStatuses(ctx context.Context, instances
168167

169168
providerIDs := make([]string, len(instances))
170169
for i, instance := range instances {
171-
providerIDs[i] = fmt.Sprintf("azure://%s", instance.ID)
170+
providerIDs[i] = azure.ProviderIDPrefix + instance.ID
172171
}
173172

174173
nodeStatusByProviderID, err := m.getNodeStatusByProviderID(ctx, providerIDs)
@@ -180,7 +179,7 @@ func (m *MachinePoolScope) UpdateInstanceStatuses(ctx context.Context, instances
180179
instanceStatuses := make([]*infrav1exp.AzureMachinePoolInstanceStatus, len(instances))
181180
for i, instance := range instances {
182181
instanceStatuses[i] = &infrav1exp.AzureMachinePoolInstanceStatus{
183-
ProviderID: fmt.Sprintf("azure://%s", instance.ID),
182+
ProviderID: azure.ProviderIDPrefix + instance.ID,
184183
InstanceID: instance.InstanceID,
185184
InstanceName: instance.Name,
186185
ProvisioningState: &instance.State,

cloud/services/scalesets/scalesets.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (s *Service) Reconcile(ctx context.Context) error {
117117
}
118118
default:
119119
// just in case, set the provider ID if the instance exists
120-
s.Scope.SetProviderID(fmt.Sprintf("azure://%s", fetchedVMSS.ID))
120+
s.Scope.SetProviderID(azure.ProviderIDPrefix + fetchedVMSS.ID)
121121
}
122122

123123
// Try to get the VMSS to update status if we have created a long running operation. If the VMSS is still in a long
@@ -193,7 +193,7 @@ func (s *Service) patchVMSSIfNeeded(ctx context.Context, infraVMSS *infrav1exp.V
193193
ctx, span := tele.Tracer().Start(ctx, "scalesets.Service.patchVMSSIfNeeded")
194194
defer span.End()
195195

196-
s.Scope.SetProviderID(fmt.Sprintf("azure://%s", infraVMSS.ID))
196+
s.Scope.SetProviderID(azure.ProviderIDPrefix + infraVMSS.ID)
197197

198198
spec := s.Scope.ScaleSetSpec()
199199
result, err := s.buildVMSSFromSpec(ctx, spec)

cloud/services/scalesets/scalesets_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package scalesets
1818

1919
import (
2020
"context"
21-
"fmt"
2221
"net/http"
2322
"testing"
2423

@@ -258,7 +257,7 @@ func TestReconcileVMSS(t *testing.T) {
258257
createdVMSS := newDefaultVMSS()
259258
instances := newDefaultInstances()
260259
createdVMSS = setupDefaultVMSSInProgressOperationDoneExpectations(g, s, m, createdVMSS, instances)
261-
s.SetProviderID(fmt.Sprintf("azure://%s", *createdVMSS.ID))
260+
s.SetProviderID(azure.ProviderIDPrefix + *createdVMSS.ID)
262261
s.SetLongRunningOperationState(nil)
263262
s.SetProvisioningState(infrav1.VMStateSucceeded)
264263
s.NeedsK8sVersionUpdate().Return(false)
@@ -279,7 +278,7 @@ func TestReconcileVMSS(t *testing.T) {
279278
vmss := newDefaultVMSS()
280279
instances := newDefaultInstances()
281280
vmss = setupDefaultVMSSInProgressOperationDoneExpectations(g, s, m, vmss, instances)
282-
s.SetProviderID(fmt.Sprintf("azure://%s", *vmss.ID))
281+
s.SetProviderID(azure.ProviderIDPrefix + *vmss.ID)
283282
s.SetProvisioningState(infrav1.VMStateUpdating)
284283

285284
// create a VMSS patch with an updated hash to match the spec
@@ -387,7 +386,7 @@ func TestReconcileVMSS(t *testing.T) {
387386
spec.Identity = infrav1.VMIdentityUserAssigned
388387
spec.UserAssignedIdentities = []infrav1.UserAssignedIdentity{
389388
{
390-
ProviderID: "azure:////subscriptions/123/resourcegroups/456/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id1",
389+
ProviderID: "azure:///subscriptions/123/resourcegroups/456/providers/Microsoft.ManagedIdentity/userAssignedIdentities/id1",
391390
},
392391
}
393392
s.ScaleSetSpec().Return(spec).AnyTimes()
@@ -1009,7 +1008,7 @@ func setupDefaultVMSSExpectations(s *mock_scalesets.MockScaleSetScopeMockRecorde
10091008

10101009
func setupDefaultVMSSUpdateExpectations(s *mock_scalesets.MockScaleSetScopeMockRecorder) {
10111010
setupDefaultVMSSExpectations(s)
1012-
s.SetProviderID("azure://vmss-id")
1011+
s.SetProviderID(azure.ProviderIDPrefix + "vmss-id")
10131012
s.SetProvisioningState(infrav1.VMStateUpdating)
10141013
s.GetLongRunningOperationState().Return(nil)
10151014
}

cloud/services/virtualmachines/virtualmachines.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (s *Service) Reconcile(ctx context.Context) error {
9393
return errors.Wrapf(err, "failed to get VM %s", vmSpec.Name)
9494
case err == nil:
9595
// VM already exists, update the spec and skip creation.
96-
s.Scope.SetProviderID(fmt.Sprintf("azure:///%s", existingVM.ID))
96+
s.Scope.SetProviderID(azure.ProviderIDPrefix + existingVM.ID)
9797
s.Scope.SetAnnotation("cluster-api-provider-azure", "true")
9898
s.Scope.SetAddresses(existingVM.Addresses)
9999
s.Scope.SetVMState(existingVM.State)

exp/controllers/azuremanagedmachinepool_reconciler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (s *azureManagedMachinePoolService) Reconcile(ctx context.Context, scope *s
152152

153153
var providerIDs = make([]string, len(instances))
154154
for i := 0; i < len(instances); i++ {
155-
providerIDs[i] = fmt.Sprintf("azure://%s", *instances[i].ID)
155+
providerIDs[i] = azure.ProviderIDPrefix + *instances[i].ID
156156
}
157157

158158
scope.InfraMachinePool.Spec.ProviderIDList = providerIDs

0 commit comments

Comments
 (0)