Skip to content

Commit

Permalink
Support dualstack for public IP in azure_loadbalancer.go
Browse files Browse the repository at this point in the history
* Feature code for public IP
* Add UT

Signed-off-by: Zhecheng Li <zhechengli@microsoft.com>
  • Loading branch information
lzhecheng committed Apr 3, 2023
1 parent ad1e555 commit 2ff52b2
Show file tree
Hide file tree
Showing 7 changed files with 816 additions and 291 deletions.
171 changes: 134 additions & 37 deletions pkg/provider/azure_loadbalancer.go

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions pkg/provider/azure_loadbalancer_backendpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ func TestEnsureHostsInPoolNodeIP(t *testing.T) {
lbClient.EXPECT().CreateOrUpdateBackendPools(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
az.LoadBalancerClient = lbClient

service := getTestService("svc-1", v1.ProtocolTCP, nil, false, 80)
makeTestServiceDualStack(&service)
service := getTestServiceDualStack("svc-1", v1.ProtocolTCP, nil, 80)
err := bi.EnsureHostsInPool(&service, nodes, "", "", "kubernetes", "kubernetes", tc.backendPool)
assert.NoError(t, err)
assert.Equal(t, tc.expectedBackendPool, tc.backendPool)
Expand Down
709 changes: 521 additions & 188 deletions pkg/provider/azure_loadbalancer_test.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/provider/azure_privatelinkservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func TestReconcilePrivateLinkService(t *testing.T) {
}
for i, test := range testCases {
az := GetTestCloud(ctrl)
service := getTestServiceWithAnnotation("test", test.annotations, 80)
service := getTestServiceWithAnnotation("test", test.annotations, false, 80)
fipConfig := &network.FrontendIPConfiguration{
Name: pointer.String("fipConfig"),
ID: pointer.String("fipConfigID"),
Expand Down
15 changes: 13 additions & 2 deletions pkg/provider/azure_standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,26 @@ func (az *Cloud) getRulePrefix(service *v1.Service) string {
return az.GetLoadBalancerName(context.TODO(), "", service)
}

func (az *Cloud) getPublicIPName(clusterName string, service *v1.Service, isIPv6 bool) string {
func (az *Cloud) getPublicIPName(clusterName string, service *v1.Service, pipResourceGroup string, pips *[]network.PublicIPAddress, isIPv6 bool) (string, error) {
// For IPv6, the PIP may not have IPv6 suffix because it may be one created before dual-stack support.
if isIPv6 {
pip, err := az.findMatchedPIPByIPFamilyAndServiceName(clusterName, service, pipResourceGroup, pips)
if err != nil {
return "", err
}
if pip != nil {
return pointer.StringDeref(pip.Name, ""), nil
}
}

pipName := fmt.Sprintf("%s-%s", clusterName, az.GetLoadBalancerName(context.TODO(), clusterName, service))
if id := getServicePIPPrefixID(service, isIPv6); id != "" {
id, err := getLastSegment(id, "/")
if err == nil {
pipName = fmt.Sprintf("%s-%s", pipName, id)
}
}
return getResourceByIPFamily(pipName, isIPv6)
return getResourceByIPFamily(pipName, isIPv6), nil
}

// TODO: UT
Expand Down
31 changes: 29 additions & 2 deletions pkg/provider/azure_standard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2310,6 +2310,7 @@ func TestGetPublicIPName(t *testing.T) {
testcases := []struct {
desc string
svc *v1.Service
pips *[]network.PublicIPAddress
isIPv6 bool
expectedPIPName string
}{
Expand All @@ -2321,6 +2322,7 @@ func TestGetPublicIPName(t *testing.T) {
IPFamilies: []v1.IPFamily{v1.IPv4Protocol},
},
},
pips: &[]network.PublicIPAddress{},
isIPv6: false,
expectedPIPName: "azure-auid",
},
Expand All @@ -2337,6 +2339,7 @@ func TestGetPublicIPName(t *testing.T) {
IPFamilies: []v1.IPFamily{v1.IPv4Protocol},
},
},
pips: &[]network.PublicIPAddress{},
isIPv6: false,
expectedPIPName: "azure-auid-prefix-id",
},
Expand All @@ -2354,6 +2357,7 @@ func TestGetPublicIPName(t *testing.T) {
IPFamilies: []v1.IPFamily{v1.IPv4Protocol, v1.IPv6Protocol},
},
},
pips: &[]network.PublicIPAddress{},
isIPv6: false,
expectedPIPName: "azure-auid-prefix-id",
},
Expand All @@ -2371,19 +2375,42 @@ func TestGetPublicIPName(t *testing.T) {
IPFamilies: []v1.IPFamily{v1.IPv4Protocol, v1.IPv6Protocol},
},
},
pips: &[]network.PublicIPAddress{},
isIPv6: true,
expectedPIPName: "azure-auid-prefix-id-ipv6-IPv6",
},
{
desc: "Service PIP IPv6 only with existing PIP",
svc: &v1.Service{
ObjectMeta: meta.ObjectMeta{
UID: types.UID("uid"),
},
Spec: v1.ServiceSpec{
IPFamilies: []v1.IPFamily{v1.IPv6Protocol},
},
},
pips: &[]network.PublicIPAddress{
{
Name: pointer.String("auid"),
PublicIPAddressPropertiesFormat: &network.PublicIPAddressPropertiesFormat{
PublicIPAddressVersion: network.IPv6,
},
},
},
isIPv6: true,
expectedPIPName: "auid",
},
}

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

}

0 comments on commit 2ff52b2

Please sign in to comment.