Skip to content

Commit 64a9c1c

Browse files
committed
fix: Consider ipv6 single stack case when checking pip name and pip prefix id
1 parent b294bce commit 64a9c1c

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

pkg/provider/azure_loadbalancer_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2453,6 +2453,8 @@ func TestDeterminePublicIPName(t *testing.T) {
24532453
expectedPIPName string
24542454
expectedError bool
24552455
isIPv6 bool
2456+
serviceIPv6 bool
2457+
annotations map[string]string
24562458
}{
24572459
{
24582460
desc: "determinePublicIpName shall get public IP from az.getPublicIPName if no specific " +
@@ -2481,12 +2483,22 @@ func TestDeterminePublicIPName(t *testing.T) {
24812483
expectedPIPName: "pipName",
24822484
expectedError: false,
24832485
},
2486+
{
2487+
desc: "determinePublicIpName shall use IPv6 pip annotation for IPv6 single stack service",
2488+
annotations: map[string]string{
2489+
consts.ServiceAnnotationPIPNameDualStack[true]: "service-lb-public-IP3dbe-v6",
2490+
},
2491+
expectedPIPName: "service-lb-public-IP3dbe-v6",
2492+
isIPv6: true,
2493+
serviceIPv6: true,
2494+
expectedError: false,
2495+
},
24842496
}
24852497

24862498
for _, test := range testCases {
24872499
t.Run(test.desc, func(t *testing.T) {
24882500
az := GetTestCloud(ctrl)
2489-
service := getTestService("test1", v1.ProtocolTCP, nil, false, 80)
2501+
service := getTestService("test1", v1.ProtocolTCP, test.annotations, test.serviceIPv6, 80)
24902502
setServiceLoadBalancerIP(&service, test.loadBalancerIP)
24912503

24922504
mockPIPsClient := az.NetworkClientFactory.GetPublicIPAddressClient().(*mock_publicipaddressclient.MockInterface)

pkg/provider/azure_utils.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,12 @@ func getServicePIPName(service *v1.Service, isIPv6 bool) string {
377377
}
378378

379379
if !isServiceDualStack(service) {
380+
v4Enabled, v6Enabled := getIPFamiliesEnabled(service)
381+
if isIPv6 && v6Enabled && !v4Enabled {
382+
if name := service.Annotations[consts.ServiceAnnotationPIPNameDualStack[true]]; name != "" {
383+
return name
384+
}
385+
}
380386
return service.Annotations[consts.ServiceAnnotationPIPNameDualStack[false]]
381387
}
382388

@@ -397,6 +403,12 @@ func getServicePIPPrefixID(service *v1.Service, isIPv6 bool) string {
397403
}
398404

399405
if !isServiceDualStack(service) {
406+
v4Enabled, v6Enabled := getIPFamiliesEnabled(service)
407+
if isIPv6 && v6Enabled && !v4Enabled {
408+
if id := service.Annotations[consts.ServiceAnnotationPIPPrefixIDDualStack[true]]; id != "" {
409+
return id
410+
}
411+
}
400412
return service.Annotations[consts.ServiceAnnotationPIPPrefixIDDualStack[false]]
401413
}
402414

pkg/provider/azure_utils_test.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ func TestGetServicePIPName(t *testing.T) {
794794
&v1.Service{
795795
ObjectMeta: metav1.ObjectMeta{
796796
Annotations: map[string]string{
797-
consts.ServiceAnnotationPIPNameDualStack[false]: "pip-name-ipv6",
797+
consts.ServiceAnnotationPIPNameDualStack[true]: "pip-name-ipv6",
798798
},
799799
},
800800
Spec: v1.ServiceSpec{
@@ -836,6 +836,21 @@ func TestGetServicePIPName(t *testing.T) {
836836
true,
837837
"pip-name-ipv6",
838838
},
839+
{
840+
"From ServiceAnnotationPIPName IPv6 single stack fallback",
841+
&v1.Service{
842+
ObjectMeta: metav1.ObjectMeta{
843+
Annotations: map[string]string{
844+
consts.ServiceAnnotationPIPNameDualStack[false]: "pip-name-ipv6",
845+
},
846+
},
847+
Spec: v1.ServiceSpec{
848+
IPFamilies: []v1.IPFamily{v1.IPv6Protocol},
849+
},
850+
},
851+
true,
852+
"pip-name-ipv6",
853+
},
839854
}
840855
for _, tc := range testcases {
841856
t.Run(tc.desc, func(t *testing.T) {
@@ -872,7 +887,7 @@ func TestGetServicePIPPrefixID(t *testing.T) {
872887
&v1.Service{
873888
ObjectMeta: metav1.ObjectMeta{
874889
Annotations: map[string]string{
875-
consts.ServiceAnnotationPIPPrefixIDDualStack[false]: "pip-prefix-id-ipv6",
890+
consts.ServiceAnnotationPIPPrefixIDDualStack[true]: "pip-prefix-id-ipv6",
876891
},
877892
},
878893
Spec: v1.ServiceSpec{
@@ -914,6 +929,21 @@ func TestGetServicePIPPrefixID(t *testing.T) {
914929
true,
915930
"pip-prefix-id-ipv6",
916931
},
932+
{
933+
"From ServiceAnnotationPIPPrefixIDDualStack IPv6 single stack fallback",
934+
&v1.Service{
935+
ObjectMeta: metav1.ObjectMeta{
936+
Annotations: map[string]string{
937+
consts.ServiceAnnotationPIPPrefixIDDualStack[false]: "pip-prefix-id-ipv6",
938+
},
939+
},
940+
Spec: v1.ServiceSpec{
941+
IPFamilies: []v1.IPFamily{v1.IPv6Protocol},
942+
},
943+
},
944+
true,
945+
"pip-prefix-id-ipv6",
946+
},
917947
}
918948
for _, tc := range testcases {
919949
t.Run(tc.desc, func(t *testing.T) {
@@ -923,6 +953,22 @@ func TestGetServicePIPPrefixID(t *testing.T) {
923953
}
924954
}
925955

956+
func TestGetServicePIPNames(t *testing.T) {
957+
svc := &v1.Service{
958+
ObjectMeta: metav1.ObjectMeta{
959+
Annotations: map[string]string{
960+
consts.ServiceAnnotationPIPNameDualStack[true]: "pip-name-ipv6",
961+
},
962+
},
963+
Spec: v1.ServiceSpec{
964+
IPFamilies: []v1.IPFamily{v1.IPv6Protocol},
965+
},
966+
}
967+
968+
names := getServicePIPNames(svc)
969+
assert.Equal(t, []string{"", "pip-name-ipv6"}, names)
970+
}
971+
926972
func TestGetResourceByIPFamily(t *testing.T) {
927973
testcases := []struct {
928974
desc string

0 commit comments

Comments
 (0)