Skip to content

Commit

Permalink
Truncate lengthy PIP name
Browse files Browse the repository at this point in the history
If PIP prefix is lengthy, the PIP name may be longer than 80. If so,
the PIP name needs truncation.

Signed-off-by: Zhecheng Li <zhechengli@microsoft.com>
  • Loading branch information
lzhecheng committed Jan 9, 2024
1 parent 3b80247 commit 1da24a0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pkg/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ const (
FrontendIPConfigNameMaxLength = 80
// LoadBalancerRuleNameMaxLength is the max length of the load balancing rule
LoadBalancerRuleNameMaxLength = 80
// PIPPrefixNameMaxLength is the max length of the PIP prefix name
PIPPrefixNameMaxLength = 80
// IPFamilySuffixLength is the length of suffix length of IP family ("-IPv4", "-IPv6")
IPFamilySuffixLength = 5

Expand Down
10 changes: 9 additions & 1 deletion pkg/provider/azure_standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,15 @@ func (az *Cloud) getPublicIPName(clusterName string, service *v1.Service) string
}
pipName = fmt.Sprintf("%s-%s", pipName, prefixName)
}
return pipName

pipNameSegment := pipName
maxLength := consts.PIPPrefixNameMaxLength - consts.IPFamilySuffixLength
if len(pipName) > maxLength {
pipNameSegment = pipNameSegment[:maxLength]
klog.V(6).Infof("original PIP name is lengthy %q, truncate it to %q", pipName, pipNameSegment)
}

return pipNameSegment
}

func (az *Cloud) serviceOwnsRule(service *v1.Service, rule string) bool {
Expand Down
64 changes: 64 additions & 0 deletions pkg/provider/azure_standard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2301,3 +2301,67 @@ func TestGetSecurityRuleName(t *testing.T) {
})
}
}

func TestGetPublicIPName(t *testing.T) {
testcases := []struct {
desc string
svc *v1.Service
pips []network.PublicIPAddress
isIPv6 bool
expectedPIPName string
}{
{
desc: "common",
svc: &v1.Service{
ObjectMeta: meta.ObjectMeta{UID: types.UID("uid")},
Spec: v1.ServiceSpec{
IPFamilies: []v1.IPFamily{v1.IPv4Protocol},
},
},
isIPv6: false,
expectedPIPName: "azure-auid",
},
{
desc: "Service PIP prefix id",
svc: &v1.Service{
ObjectMeta: meta.ObjectMeta{
UID: types.UID("uid"),
Annotations: map[string]string{
consts.ServiceAnnotationPIPPrefixIDDualStack[false]: "prefix-id",
},
},
Spec: v1.ServiceSpec{
IPFamilies: []v1.IPFamily{v1.IPv4Protocol},
},
},
isIPv6: false,
expectedPIPName: "azure-auid-prefix-id",
},
{
desc: "Service PIP prefix id IPv6 lengthy",
svc: &v1.Service{
ObjectMeta: meta.ObjectMeta{
UID: types.UID("uid"),
Annotations: map[string]string{
consts.ServiceAnnotationPIPPrefixIDDualStack[false]: "prefix-id-ipv6-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
},
},
Spec: v1.ServiceSpec{
IPFamilies: []v1.IPFamily{v1.IPv6Protocol},
},
},
isIPv6: true,
expectedPIPName: "azure-auid-prefix-id-ipv6-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
},
}

ctrl := gomock.NewController(t)
defer ctrl.Finish()
for _, tc := range testcases {
t.Run(tc.desc, func(t *testing.T) {
az := GetTestCloud(ctrl)
name := az.getPublicIPName("azure", tc.svc)
assert.Equal(t, tc.expectedPIPName, name)
})
}
}

0 comments on commit 1da24a0

Please sign in to comment.