Skip to content

Commit b9da08a

Browse files
authored
Merge pull request kubernetes#92793 from feiskyer/fix-node-name
Fix throttling issues when Azure VM computer name prefix is different from VMSS name
2 parents 9e70d6f + e1bbcd8 commit b9da08a

File tree

2 files changed

+17
-58
lines changed

2 files changed

+17
-58
lines changed

staging/src/k8s.io/legacy-cloud-providers/azure/azure_instances.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ package azure
2121
import (
2222
"context"
2323
"fmt"
24-
"strconv"
24+
"os"
2525
"strings"
2626

2727
v1 "k8s.io/api/core/v1"
@@ -36,6 +36,10 @@ const (
3636
vmPowerStateStopped = "stopped"
3737
vmPowerStateDeallocated = "deallocated"
3838
vmPowerStateDeallocating = "deallocating"
39+
40+
// nodeNameEnvironmentName is the environment variable name for getting node name.
41+
// It is only used for out-of-tree cloud provider.
42+
nodeNameEnvironmentName = "NODE_NAME"
3943
)
4044

4145
var (
@@ -310,17 +314,20 @@ func (az *Cloud) InstanceMetadataByProviderID(ctx context.Context, providerID st
310314
}
311315

312316
func (az *Cloud) isCurrentInstance(name types.NodeName, metadataVMName string) (bool, error) {
317+
var err error
313318
nodeName := mapNodeNameToVMName(name)
314319

320+
// VMSS vmName is not same with hostname, use hostname instead.
315321
if az.VMType == vmTypeVMSS {
316-
// VMSS vmName is not same with hostname, construct the node name "{computer-name-prefix}{base-36-instance-id}".
317-
// Refer https://docs.microsoft.com/en-us/azure/virtual-machine-scale-sets/virtual-machine-scale-sets-instance-ids#scale-set-vm-computer-name.
318-
if ssName, instanceID, err := extractVmssVMName(metadataVMName); err == nil {
319-
instance, err := strconv.ParseInt(instanceID, 10, 64)
320-
if err != nil {
321-
return false, fmt.Errorf("failed to parse VMSS instanceID %q: %v", instanceID, err)
322-
}
323-
metadataVMName = fmt.Sprintf("%s%06s", ssName, strconv.FormatInt(instance, 36))
322+
metadataVMName, err = os.Hostname()
323+
if err != nil {
324+
return false, err
325+
}
326+
327+
// Use name from env variable "NODE_NAME" if it is set.
328+
nodeNameEnv := os.Getenv(nodeNameEnvironmentName)
329+
if nodeNameEnv != "" {
330+
metadataVMName = nodeNameEnv
324331
}
325332
}
326333

staging/src/k8s.io/legacy-cloud-providers/azure/azure_instances_test.go

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,6 @@ func TestInstanceID(t *testing.T) {
173173
useInstanceMetadata: true,
174174
expectedErrMsg: fmt.Errorf("failure of getting instance metadata"),
175175
},
176-
{
177-
name: "NodeAddresses should report error if VMSS instanceID is invalid",
178-
nodeName: "vm123456",
179-
metadataName: "vmss_$123",
180-
vmType: vmTypeVMSS,
181-
useInstanceMetadata: true,
182-
expectedErrMsg: fmt.Errorf("failed to parse VMSS instanceID %q: strconv.ParseInt: parsing %q: invalid syntax", "$123", "$123"),
183-
},
184176
{
185177
name: "NodeAddresses should report error if cloud.vmSet is nil",
186178
nodeName: "vm1",
@@ -452,14 +444,6 @@ func TestNodeAddresses(t *testing.T) {
452444
useInstanceMetadata: true,
453445
expectedErrMsg: fmt.Errorf("getError"),
454446
},
455-
{
456-
name: "NodeAddresses should report error if VMSS instanceID is invalid",
457-
nodeName: "vm123456",
458-
metadataName: "vmss_$123",
459-
vmType: vmTypeVMSS,
460-
useInstanceMetadata: true,
461-
expectedErrMsg: fmt.Errorf("failed to parse VMSS instanceID %q: strconv.ParseInt: parsing %q: invalid syntax", "$123", "$123"),
462-
},
463447
{
464448
name: "NodeAddresses should report error if cloud.vmSet is nil",
465449
nodeName: "vm1",
@@ -769,14 +753,6 @@ func TestInstanceMetadataByProviderID(t *testing.T) {
769753
useCustomImsCache: true,
770754
expectedErrMsg: fmt.Errorf("getError"),
771755
},
772-
{
773-
name: "InstanceMetadataByProviderID should report error if VMSS instanceID is invalid",
774-
metadataName: "vmss_$123",
775-
providerID: providerID,
776-
vmType: vmTypeVMSS,
777-
useInstanceMetadata: true,
778-
expectedErrMsg: fmt.Errorf("failed to parse VMSS instanceID %q: strconv.ParseInt: parsing %q: invalid syntax", "$123", "$123"),
779-
},
780756
{
781757
name: "InstanceMetadataByProviderID should get metadata from Azure API if cloud.UseInstanceMetadata is false",
782758
metadataName: "vm1",
@@ -920,7 +896,7 @@ func TestInstanceMetadataByProviderID(t *testing.T) {
920896
func TestIsCurrentInstance(t *testing.T) {
921897
cloud := &Cloud{
922898
Config: Config{
923-
VMType: vmTypeVMSS,
899+
VMType: vmTypeStandard,
924900
},
925901
}
926902
testcases := []struct {
@@ -939,22 +915,6 @@ func TestIsCurrentInstance(t *testing.T) {
939915
metadataVMName: "node2",
940916
expected: false,
941917
},
942-
{
943-
nodeName: "vmss000001",
944-
metadataVMName: "vmss_1",
945-
expected: true,
946-
},
947-
{
948-
nodeName: "vmss_2",
949-
metadataVMName: "vmss000000",
950-
expected: false,
951-
},
952-
{
953-
nodeName: "vmss123456",
954-
metadataVMName: "vmss_$123",
955-
expected: false,
956-
expectedErrMsg: fmt.Errorf("failed to parse VMSS instanceID %q: strconv.ParseInt: parsing %q: invalid syntax", "$123", "$123"),
957-
},
958918
}
959919

960920
for _, test := range testcases {
@@ -1048,14 +1008,6 @@ func TestInstanceTypeByProviderID(t *testing.T) {
10481008
useCustomImsCache: true,
10491009
expectedErrMsg: fmt.Errorf("getError"),
10501010
},
1051-
{
1052-
name: "NodeAddresses should report error if VMSS instanceID is invalid",
1053-
nodeName: "vm123456",
1054-
metadataName: "vmss_$123",
1055-
providerID: "azure:///subscriptions/subscription/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm1",
1056-
vmType: vmTypeVMSS,
1057-
expectedErrMsg: fmt.Errorf("failed to parse VMSS instanceID %q: strconv.ParseInt: parsing %q: invalid syntax", "$123", "$123"),
1058-
},
10591011
}
10601012

10611013
for _, test := range testcases {

0 commit comments

Comments
 (0)