Merge pull request #103470 from nilo19/bug/cherry-pick-679

fix: return empty VMAS name if using standalone VM
This commit is contained in:
Kubernetes Prow Robot 2021-07-08 14:02:01 -07:00 committed by GitHub
commit 103212febc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 0 deletions

View File

@ -1034,6 +1034,11 @@ func (as *availabilitySet) EnsureBackendPoolDeleted(service *v1.Service, backend
}
func getAvailabilitySetNameByID(asID string) (string, error) {
// for standalone VM
if asID == "" {
return "", nil
}
matches := vmasIDRE.FindStringSubmatch(asID)
if len(matches) != 2 {
return "", fmt.Errorf("getAvailabilitySetNameByID: failed to parse the VMAS ID %s", asID)

View File

@ -1747,3 +1747,25 @@ func TestStandardGetNodeNameByIPConfigurationID(t *testing.T) {
assert.Equal(t, "k8s-agentpool1-00000000-0", nodeName)
assert.Equal(t, "agentpool1-availabilityset-00000000", asName)
}
func TestGetAvailabilitySetNameByID(t *testing.T) {
t.Run("getAvailabilitySetNameByID should return empty string if the given ID is empty", func(t *testing.T) {
vmasName, err := getAvailabilitySetNameByID("")
assert.Nil(t, err)
assert.Empty(t, vmasName)
})
t.Run("getAvailabilitySetNameByID should report an error if the format of the given ID is wrong", func(t *testing.T) {
asID := "illegal-id"
vmasName, err := getAvailabilitySetNameByID(asID)
assert.Equal(t, fmt.Errorf("getAvailabilitySetNameByID: failed to parse the VMAS ID illegal-id"), err)
assert.Empty(t, vmasName)
})
t.Run("getAvailabilitySetNameByID should extract the VMAS name from the given ID", func(t *testing.T) {
asID := "/subscriptions/sub/resourceGroups/rg/providers/Microsoft.Compute/availabilitySets/as"
vmasName, err := getAvailabilitySetNameByID(asID)
assert.Nil(t, err)
assert.Equal(t, "as", vmasName)
})
}