Skip to content

Commit

Permalink
Fix possibly incorrect last char of frontend PIP config name * Fronte…
Browse files Browse the repository at this point in the history
…nd PIP config name may end with invalid char like '-' * Add a UT and update an e2e testcase

Signed-off-by: Zhecheng Li <zhechengli@microsoft.com>
  • Loading branch information
lzhecheng authored and k8s-infra-cherrypick-robot committed Apr 12, 2022
1 parent 7ace44a commit 67f3b8f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
6 changes: 6 additions & 0 deletions pkg/provider/azure_standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strings"
"sync"
"time"
"unicode"

"sigs.k8s.io/cloud-provider-azure/pkg/consts"

Expand Down Expand Up @@ -393,6 +394,11 @@ func (az *Cloud) getDefaultFrontendIPConfigName(service *v1.Service) string {
// Azure lb front end configuration name must not exceed 80 characters
if len(ipcName) > consts.FrontendIPConfigNameMaxLength {
ipcName = ipcName[:consts.FrontendIPConfigNameMaxLength]
// Cutting the string may result in char like "-" as the string end.
// If the last char is not a letter or '_', replace it with "_".
if !unicode.IsLetter(rune(ipcName[len(ipcName)-1:][0])) && ipcName[len(ipcName)-1:] != "_" {
ipcName = ipcName[:len(ipcName)-1] + "_"
}
}
return ipcName
}
Expand Down
27 changes: 18 additions & 9 deletions pkg/provider/azure_standard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,13 @@ func TestGetFrontendIPConfigName(t *testing.T) {
useStandardLB: true,
expected: "a257b965551374ad2b091ef3f07043ad-shortsubnet",
},
{
description: "internal lb should have subnet name on the frontend ip configuration name but truncated to 80 characters, also not end with char like '-'",
subnetName: "a--------------------------------------------------z",
isInternal: true,
useStandardLB: true,
expected: "a257b965551374ad2b091ef3f07043ad-a---------------------------------------------_",
},
{
description: "internal standard lb should have subnet name on the frontend ip configuration name but truncated to 80 characters",
subnetName: "averylonnnngggnnnnnnnnnnnnnnnnnnnnnngggggggggggggggggggggggggggggggggggggsubet",
Expand Down Expand Up @@ -489,16 +496,18 @@ func TestGetFrontendIPConfigName(t *testing.T) {
}

for _, c := range cases {
if c.useStandardLB {
az.Config.LoadBalancerSku = consts.LoadBalancerSkuStandard
} else {
az.Config.LoadBalancerSku = consts.LoadBalancerSkuBasic
}
svc.Annotations[consts.ServiceAnnotationLoadBalancerInternalSubnet] = c.subnetName
svc.Annotations[consts.ServiceAnnotationLoadBalancerInternal] = strconv.FormatBool(c.isInternal)
t.Run(c.description, func(t *testing.T) {
if c.useStandardLB {
az.Config.LoadBalancerSku = consts.LoadBalancerSkuStandard
} else {
az.Config.LoadBalancerSku = consts.LoadBalancerSkuBasic
}
svc.Annotations[consts.ServiceAnnotationLoadBalancerInternalSubnet] = c.subnetName
svc.Annotations[consts.ServiceAnnotationLoadBalancerInternal] = strconv.FormatBool(c.isInternal)

ipconfigName := az.getDefaultFrontendIPConfigName(svc)
assert.Equal(t, c.expected, ipconfigName, c.description)
ipconfigName := az.getDefaultFrontendIPConfigName(svc)
assert.Equal(t, c.expected, ipconfigName, c)
})
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/e2e/network/service_annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ var _ = Describe("Service with annotation", func() {

It("should support service annotation 'service.beta.kubernetes.io/azure-load-balancer-internal-subnet'", func() {
By("creating environment")
subnetName := "lb-subnet"
// This subnetName verifies a bug fix in an issue: https://github.com/kubernetes-sigs/cloud-provider-azure/issues/1443
subnetName := "a--------------------------------------------------z"

vNet, err := tc.GetClusterVirtualNetwork()
Expect(err).NotTo(HaveOccurred())
Expand Down

0 comments on commit 67f3b8f

Please sign in to comment.