Skip to content

Commit

Permalink
Merge pull request #52330 from m1093782566/ipvs-service-equal
Browse files Browse the repository at this point in the history
Automatic merge from submit-queue (batch tested with PRs 52469, 52574, 52330, 52689, 52829). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>..

Fix IPVS service Flags

**What this PR does / why we need it**:

**Which issue this PR fixes**: 

fixes #52393

**Special notes for your reviewer**:

**Release note**:

```release-note
NONE
```
  • Loading branch information
Kubernetes Submit Queue committed Sep 24, 2017
2 parents c2a7814 + 21b02ad commit 5364090
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
2 changes: 2 additions & 0 deletions pkg/util/ipvs/ipvs.go
Expand Up @@ -63,6 +63,8 @@ type ServiceFlags uint32
const (
// FlagPersistent specify IPVS service session affinity
FlagPersistent = 0x1
// FlagHashed specify IPVS service hash flag
FlagHashed = 0x2
)

// Equal check the equality of virtual server.
Expand Down
9 changes: 8 additions & 1 deletion pkg/util/ipvs/ipvs_linux.go
Expand Up @@ -203,10 +203,17 @@ func toVirtualServer(svc *ipvs.Service) (*VirtualServer, error) {
Port: svc.Port,
Scheduler: svc.SchedName,
Protocol: protocolNumbeToString(ProtoType(svc.Protocol)),
Flags: ServiceFlags(svc.Flags),
Timeout: svc.Timeout,
}

// Test Flags >= 0x2, valid Flags ranges [0x2, 0x3]
if svc.Flags&FlagHashed == 0 {
return nil, fmt.Errorf("Flags of successfully created IPVS service should be >= %d since every service is hashed into the service table", FlagHashed)
}
// Sub Flags to 0x2
// 011 -> 001, 010 -> 000
vs.Flags = ServiceFlags(svc.Flags &^ uint32(FlagHashed))

if vs.Address == nil {
if svc.AddressFamily == syscall.AF_INET {
vs.Address = net.IPv4zero
Expand Down
56 changes: 44 additions & 12 deletions pkg/util/ipvs/ipvs_linux_test.go
Expand Up @@ -19,6 +19,7 @@ limitations under the License.
package ipvs

import (
"fmt"
"net"
"reflect"
"syscall"
Expand Down Expand Up @@ -117,18 +118,36 @@ func TestUnbindVirtualServerAddress(t *testing.T) {
}
}

func Test_toFrontendService(t *testing.T) {
func Test_toVirtualServer(t *testing.T) {
Tests := []struct {
ipvsService ipvs.Service
virtualServer VirtualServer
expectError bool
reason string
}{
{
ipvs.Service{
Flags: 0x0,
},
VirtualServer{},
true,
fmt.Sprintf("IPVS Service Flags should be >= %d, got 0x0", FlagHashed),
},
{
ipvs.Service{
Flags: 0x1,
},
VirtualServer{},
true,
fmt.Sprintf("IPVS Service Flags should be >= %d, got 0x1", FlagHashed),
},
{
ipvs.Service{
Protocol: syscall.IPPROTO_TCP,
Port: 80,
FWMark: 0,
SchedName: "",
Flags: 0,
Flags: uint32(FlagPersistent + FlagHashed),
Timeout: 0,
Netmask: 0xffffffff,
AddressFamily: syscall.AF_INET,
Expand All @@ -140,17 +159,19 @@ func Test_toFrontendService(t *testing.T) {
Protocol: "TCP",
Port: 80,
Scheduler: "",
Flags: 0,
Flags: ServiceFlags(FlagPersistent),
Timeout: 0,
},
false,
"",
},
{
ipvs.Service{
Protocol: syscall.IPPROTO_UDP,
Port: 33434,
FWMark: 0,
SchedName: "wlc",
Flags: 1234,
Flags: uint32(0 + FlagHashed),
Timeout: 100,
Netmask: 128,
AddressFamily: syscall.AF_INET6,
Expand All @@ -162,17 +183,19 @@ func Test_toFrontendService(t *testing.T) {
Protocol: "UDP",
Port: 33434,
Scheduler: "wlc",
Flags: 1234,
Flags: ServiceFlags(0),
Timeout: 100,
},
false,
"",
},
{
ipvs.Service{
Protocol: 0,
Port: 0,
FWMark: 0,
SchedName: "lc",
Flags: 0,
Flags: uint32(0 + FlagHashed),
Timeout: 0,
Netmask: 0xffffffff,
AddressFamily: syscall.AF_INET,
Expand All @@ -184,17 +207,19 @@ func Test_toFrontendService(t *testing.T) {
Protocol: "",
Port: 0,
Scheduler: "lc",
Flags: 0,
Flags: ServiceFlags(0),
Timeout: 0,
},
false,
"",
},
{
ipvs.Service{
Protocol: 0,
Port: 0,
FWMark: 0,
SchedName: "wrr",
Flags: 0,
Flags: uint32(FlagPersistent + FlagHashed),
Timeout: 0,
Netmask: 128,
AddressFamily: syscall.AF_INET6,
Expand All @@ -206,19 +231,26 @@ func Test_toFrontendService(t *testing.T) {
Protocol: "",
Port: 0,
Scheduler: "wrr",
Flags: 0,
Flags: ServiceFlags(FlagPersistent),
Timeout: 0,
},
false,
"",
},
}

for i := range Tests {
got, err := toVirtualServer(&Tests[i].ipvsService)
if err != nil {
if Tests[i].expectError && err == nil {
t.Errorf("case: %d, expected error: %s, got nil", i, Tests[i].reason)
}
if !Tests[i].expectError && err != nil {
t.Errorf("case: %d, unexpected error: %v", i, err)
}
if !reflect.DeepEqual(*got, Tests[i].virtualServer) {
t.Errorf("case: %d, got %#v, want %#v", i, *got, Tests[i].virtualServer)
if got != nil && &Tests[i].virtualServer != nil {
if !reflect.DeepEqual(*got, Tests[i].virtualServer) {
t.Errorf("case: %d, got %#v, want %#v", i, *got, Tests[i].virtualServer)
}
}
}
}
Expand Down

0 comments on commit 5364090

Please sign in to comment.