Unverified Commit bc361fae authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

Merge pull request #58276 from feiskyer/vmss-name

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Convert ComputerName to lower case **What this PR does / why we need it**: When instances count is greater than 10, azure will generate instance hostname with upper cases. But kubelet always converts hostname to lower case. So azure cloud provider should also covert OsProfile.ComputerName to lower case. **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: Fixes #58274 **Special notes for your reviewer**: This PR also adds more unit tests for scale sets. **Release note**: ```release-note NONE ```
parents b04c28e8 2e646b0e
...@@ -12,6 +12,7 @@ go_library( ...@@ -12,6 +12,7 @@ go_library(
"azure.go", "azure.go",
"azure_backoff.go", "azure_backoff.go",
"azure_blobDiskController.go", "azure_blobDiskController.go",
"azure_cache.go",
"azure_client.go", "azure_client.go",
"azure_controllerCommon.go", "azure_controllerCommon.go",
"azure_fakes.go", "azure_fakes.go",
...@@ -21,12 +22,11 @@ go_library( ...@@ -21,12 +22,11 @@ go_library(
"azure_managedDiskController.go", "azure_managedDiskController.go",
"azure_metrics.go", "azure_metrics.go",
"azure_routes.go", "azure_routes.go",
"azure_standard.go",
"azure_storage.go", "azure_storage.go",
"azure_storageaccount.go", "azure_storageaccount.go",
"azure_util.go",
"azure_util_cache.go",
"azure_util_vmss.go",
"azure_vmsets.go", "azure_vmsets.go",
"azure_vmss.go",
"azure_wrap.go", "azure_wrap.go",
"azure_zones.go", "azure_zones.go",
], ],
...@@ -64,11 +64,11 @@ go_library( ...@@ -64,11 +64,11 @@ go_library(
go_test( go_test(
name = "go_default_test", name = "go_default_test",
srcs = [ srcs = [
"azure_cache_test.go",
"azure_loadbalancer_test.go", "azure_loadbalancer_test.go",
"azure_metrics_test.go", "azure_metrics_test.go",
"azure_test.go", "azure_test.go",
"azure_util_cache_test.go", "azure_vmss_test.go",
"azure_util_test.go",
"azure_wrap_test.go", "azure_wrap_test.go",
], ],
embed = [":go_default_library"], embed = [":go_default_library"],
......
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package azure
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetScaleSetVMInstanceID(t *testing.T) {
tests := []struct {
msg string
machineName string
expectError bool
expectedInstanceID string
}{{
msg: "invalid vmss instance name",
machineName: "vmvm",
expectError: true,
},
{
msg: "valid vmss instance name",
machineName: "vm00000Z",
expectError: false,
expectedInstanceID: "35",
},
}
for i, test := range tests {
instanceID, err := getScaleSetVMInstanceID(test.machineName)
if test.expectError {
assert.Error(t, err, fmt.Sprintf("TestCase[%d]: %s", i, test.msg))
} else {
assert.Equal(t, test.expectedInstanceID, instanceID, fmt.Sprintf("TestCase[%d]: %s", i, test.msg))
}
}
}
...@@ -121,7 +121,7 @@ func (ss *scaleSet) updateCache() error { ...@@ -121,7 +121,7 @@ func (ss *scaleSet) updateCache() error {
for _, vm := range vms { for _, vm := range vms {
nodeName := "" nodeName := ""
if vm.OsProfile != nil && vm.OsProfile.ComputerName != nil { if vm.OsProfile != nil && vm.OsProfile.ComputerName != nil {
nodeName = *vm.OsProfile.ComputerName nodeName = strings.ToLower(*vm.OsProfile.ComputerName)
} }
vmSize := "" vmSize := ""
...@@ -190,10 +190,12 @@ func (ss *scaleSet) getCachedVirtualMachine(nodeName string) (scaleSetVMInfo, er ...@@ -190,10 +190,12 @@ func (ss *scaleSet) getCachedVirtualMachine(nodeName string) (scaleSetVMInfo, er
} }
// Update cache and try again. // Update cache and try again.
glog.V(10).Infof("vmss cache before updateCache: %v", ss.cache)
if err := ss.updateCache(); err != nil { if err := ss.updateCache(); err != nil {
glog.Errorf("updateCache failed with error: %v", err) glog.Errorf("updateCache failed with error: %v", err)
return scaleSetVMInfo{}, err return scaleSetVMInfo{}, err
} }
glog.V(10).Infof("vmss cache after updateCache: %v", ss.cache)
vm, found = getVMFromCache(nodeName) vm, found = getVMFromCache(nodeName)
if found { if found {
return vm, nil return vm, nil
......
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package azure
import (
"fmt"
"testing"
"github.com/Azure/azure-sdk-for-go/arm/compute"
"github.com/stretchr/testify/assert"
)
func newTestScaleSet() *scaleSet {
ss := newScaleSet(getTestCloud())
return ss.(*scaleSet)
}
func setTestVirtualMachineScaleSets(ss *scaleSet, scaleSetName string, vmList []string) {
virtualMachineScaleSetsClient := newFakeVirtualMachineScaleSetsClient()
scaleSets := make(map[string]map[string]compute.VirtualMachineScaleSet)
scaleSets["rg"] = map[string]compute.VirtualMachineScaleSet{
scaleSetName: {
Name: &scaleSetName,
},
}
virtualMachineScaleSetsClient.setFakeStore(scaleSets)
virtualMachineScaleSetVMsClient := newFakeVirtualMachineScaleSetVMsClient()
ssVMs := make(map[string]map[string]compute.VirtualMachineScaleSetVM)
ssVMs["rg"] = make(map[string]compute.VirtualMachineScaleSetVM)
for i := range vmList {
ID := fmt.Sprintf("azure:///subscriptions/script/resourceGroups/rg/providers/Microsoft.Compute/virtualMachineScaleSets/%s/virtualMachines/%d", scaleSetName, i)
nodeName := vmList[i]
instanceID := fmt.Sprintf("%d", i)
vmKey := fmt.Sprintf("%s-%s", scaleSetName, nodeName)
networkInterfaces := []compute.NetworkInterfaceReference{
{
ID: &nodeName,
},
}
ssVMs["rg"][vmKey] = compute.VirtualMachineScaleSetVM{
VirtualMachineScaleSetVMProperties: &compute.VirtualMachineScaleSetVMProperties{
OsProfile: &compute.OSProfile{
ComputerName: &nodeName,
},
NetworkProfile: &compute.NetworkProfile{
NetworkInterfaces: &networkInterfaces,
},
},
ID: &ID,
InstanceID: &instanceID,
Location: &ss.Cloud.Location,
}
}
virtualMachineScaleSetVMsClient.setFakeStore(ssVMs)
ss.Cloud.VirtualMachineScaleSetsClient = virtualMachineScaleSetsClient
ss.Cloud.VirtualMachineScaleSetVMsClient = virtualMachineScaleSetVMsClient
}
func TestGetScaleSetVMInstanceID(t *testing.T) {
tests := []struct {
msg string
machineName string
expectError bool
expectedInstanceID string
}{{
msg: "invalid vmss instance name",
machineName: "vmvm",
expectError: true,
},
{
msg: "valid vmss instance name",
machineName: "vm00000Z",
expectError: false,
expectedInstanceID: "35",
},
}
for i, test := range tests {
instanceID, err := getScaleSetVMInstanceID(test.machineName)
if test.expectError {
assert.Error(t, err, fmt.Sprintf("TestCase[%d]: %s", i, test.msg))
} else {
assert.Equal(t, test.expectedInstanceID, instanceID, fmt.Sprintf("TestCase[%d]: %s", i, test.msg))
}
}
}
func TestGetInstanceIDByNodeName(t *testing.T) {
ss := newTestScaleSet()
testCases := []struct {
description string
scaleSet string
vmList []string
nodeName string
expected string
expectError bool
}{
{
description: "scaleSet should get instance by node name",
scaleSet: "ss",
vmList: []string{"vm1", "vm2"},
nodeName: "vm1",
expected: "azure:///subscriptions/script/resourceGroups/rg/providers/Microsoft.Compute/virtualMachineScaleSets/ss/virtualMachines/0",
},
{
description: "scaleSet should get instance by node name with upper cases hostname",
scaleSet: "ss",
vmList: []string{"VM1", "vm2"},
nodeName: "vm1",
expected: "azure:///subscriptions/script/resourceGroups/rg/providers/Microsoft.Compute/virtualMachineScaleSets/ss/virtualMachines/0",
},
{
description: "scaleSet should not get instance for non-exist nodes",
scaleSet: "ss",
vmList: []string{"VM1", "vm2"},
nodeName: "vm3",
expectError: true,
},
}
for _, test := range testCases {
setTestVirtualMachineScaleSets(ss, test.scaleSet, test.vmList)
real, err := ss.GetInstanceIDByNodeName(test.nodeName)
if test.expectError {
assert.Error(t, err, test.description)
continue
}
assert.NoError(t, err, test.description)
assert.Equal(t, test.expected, real, test.description)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment