Merge pull request #53120 from m1093782566/fake-ipv6

Automatic merge from submit-queue (batch tested with PRs 53227, 53120). 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>.

remove ipv4 in pkg/util/ipvs

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

remove ipv4 in util/ipvs

**Which issue this PR fixes**:

xref: #51866


**Special notes for your reviewer**:

**Release note**:

```release-note
NONE
```
This commit is contained in:
Kubernetes Submit Queue 2017-10-05 13:07:38 -07:00 committed by GitHub
commit 3b1b19a1e2
3 changed files with 105 additions and 75 deletions

View File

@ -53,7 +53,7 @@ func New(exec utilexec.Interface) Interface {
// EnsureVirtualServerAddressBind is part of Interface. // EnsureVirtualServerAddressBind is part of Interface.
func (runner *runner) EnsureVirtualServerAddressBind(vs *VirtualServer, dummyDev string) (exist bool, err error) { func (runner *runner) EnsureVirtualServerAddressBind(vs *VirtualServer, dummyDev string) (exist bool, err error) {
addr := vs.Address.String() + "/32" addr := vs.Address.String()
args := []string{"addr", "add", addr, "dev", dummyDev} args := []string{"addr", "add", addr, "dev", dummyDev}
out, err := runner.exec.Command(cmdIP, args...).CombinedOutput() out, err := runner.exec.Command(cmdIP, args...).CombinedOutput()
if err != nil { if err != nil {
@ -70,7 +70,7 @@ func (runner *runner) EnsureVirtualServerAddressBind(vs *VirtualServer, dummyDev
// UnbindVirtualServerAddress is part of Interface. // UnbindVirtualServerAddress is part of Interface.
func (runner *runner) UnbindVirtualServerAddress(vs *VirtualServer, dummyDev string) error { func (runner *runner) UnbindVirtualServerAddress(vs *VirtualServer, dummyDev string) error {
addr := vs.Address.String() + "/32" addr := vs.Address.String()
args := []string{"addr", "del", addr, "dev", dummyDev} args := []string{"addr", "del", addr, "dev", dummyDev}
out, err := runner.exec.Command(cmdIP, args...).CombinedOutput() out, err := runner.exec.Command(cmdIP, args...).CombinedOutput()
if err != nil { if err != nil {

View File

@ -35,86 +35,116 @@ import (
const dummyDevice = "kube-ipvs0" const dummyDevice = "kube-ipvs0"
func TestEnsureVirtualServerAddressBind(t *testing.T) { func TestEnsureVirtualServerAddressBind(t *testing.T) {
vs := &VirtualServer{ tests := []VirtualServer{
Address: net.ParseIP("10.20.30.40"), {
Port: uint16(1234), Address: net.ParseIP("10.20.30.40"),
Protocol: string("TCP"), Port: uint16(1234),
} Protocol: string("TCP"),
fcmd := fakeexec.FakeCmd{ },
CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{ {
// Success. Address: net.ParseIP("2012::beef"),
func() ([]byte, error) { return []byte{}, nil }, Port: uint16(5678),
// Exists. Protocol: string("UDP"),
func() ([]byte, error) { return nil, &fakeexec.FakeExitError{Status: 2} },
}, },
} }
fexec := fakeexec.FakeExec{ for i := range tests {
CommandScript: []fakeexec.FakeCommandAction{ vs := &tests[i]
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) }, fcmd := fakeexec.FakeCmd{
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) }, CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
}, // Success.
} func() ([]byte, error) { return []byte{}, nil },
runner := New(&fexec) // Exists.
// Success. func() ([]byte, error) { return nil, &fakeexec.FakeExitError{Status: 2} },
exists, err := runner.EnsureVirtualServerAddressBind(vs, dummyDevice) },
if err != nil { }
t.Errorf("expected success, got %v", err) fexec := fakeexec.FakeExec{
} CommandScript: []fakeexec.FakeCommandAction{
if exists { func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
t.Errorf("expected exists = false") func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) },
} },
if fcmd.CombinedOutputCalls != 1 { }
t.Errorf("expected 1 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls) runner := New(&fexec)
} // Success.
if !sets.NewString(fcmd.CombinedOutputLog[0]...).HasAll("ip", "addr", "add", "10.20.30.40/32", "dev", "kube-ipvs0") { exists, err := runner.EnsureVirtualServerAddressBind(vs, dummyDevice)
t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[0]) if err != nil {
} t.Errorf("expected success, got %v", err)
// Exists. }
exists, err = runner.EnsureVirtualServerAddressBind(vs, dummyDevice) if exists {
if err != nil { t.Errorf("expected exists = false")
t.Errorf("expected success, got %v", err) }
} if fcmd.CombinedOutputCalls != 1 {
if !exists { t.Errorf("expected 1 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
t.Errorf("expected exists = true") }
IP := tests[i].Address.String()
if !sets.NewString(fcmd.CombinedOutputLog[0]...).HasAll("ip", "addr", "add", IP, "dev", dummyDevice) {
t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[0])
}
// Exists.
exists, err = runner.EnsureVirtualServerAddressBind(vs, dummyDevice)
if err != nil {
t.Errorf("expected success, got %v", err)
}
if !exists {
t.Errorf("expected exists = true")
}
} }
} }
func TestUnbindVirtualServerAddress(t *testing.T) { func TestUnbindVirtualServerAddress(t *testing.T) {
svc := &VirtualServer{ tests := []VirtualServer{
Address: net.ParseIP("10.20.30.41"), {
Port: uint16(80), Address: net.ParseIP("2012::beef"),
Protocol: string("TCP"), Port: uint16(5678),
} Protocol: string("UDP"),
fcmd := fakeexec.FakeCmd{ },
CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{ {
// Success. Address: net.ParseIP("10.20.30.40"),
func() ([]byte, error) { return []byte{}, nil }, Port: uint16(1234),
// Failure. Protocol: string("TCP"),
func() ([]byte, error) { return nil, &fakeexec.FakeExitError{Status: 2} },
}, },
} }
fexec := fakeexec.FakeExec{ for i := range tests {
CommandScript: []fakeexec.FakeCommandAction{ vs := &tests[i]
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) }, fcmd := fakeexec.FakeCmd{
func(cmd string, args ...string) exec.Cmd { return fakeexec.InitFakeCmd(&fcmd, cmd, args...) }, CombinedOutputScript: []fakeexec.FakeCombinedOutputAction{
}, // Success.
} func() ([]byte, error) {
runner := New(&fexec) return []byte{}, nil
// Success. },
err := runner.UnbindVirtualServerAddress(svc, dummyDevice) // Failure.
if err != nil { func() ([]byte, error) {
t.Errorf("expected success, got %v", err) return nil, &fakeexec.FakeExitError{Status: 2}
} },
if fcmd.CombinedOutputCalls != 1 { },
t.Errorf("expected 1 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls) }
} fexec := fakeexec.FakeExec{
if !sets.NewString(fcmd.CombinedOutputLog[0]...).HasAll("ip", "addr", "del", "10.20.30.41/32", "dev", "kube-ipvs0") { CommandScript: []fakeexec.FakeCommandAction{
t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[0]) func(cmd string, args ...string) exec.Cmd {
} return fakeexec.InitFakeCmd(&fcmd, cmd, args...)
// Failure. },
err = runner.UnbindVirtualServerAddress(svc, dummyDevice) func(cmd string, args ...string) exec.Cmd {
if err == nil { return fakeexec.InitFakeCmd(&fcmd, cmd, args...)
t.Errorf("expected failure") },
},
}
runner := New(&fexec)
// Success.
err := runner.UnbindVirtualServerAddress(vs, dummyDevice)
if err != nil {
t.Errorf("expected success, got %v", err)
}
if fcmd.CombinedOutputCalls != 1 {
t.Errorf("expected 1 CombinedOutput() calls, got %d", fcmd.CombinedOutputCalls)
}
IP := tests[i].Address.String()
if !sets.NewString(fcmd.CombinedOutputLog[0]...).HasAll("ip", "addr", "del", IP, "dev", dummyDevice) {
t.Errorf("wrong CombinedOutput() log, got %s", fcmd.CombinedOutputLog[0])
}
// Failure.
err = runner.UnbindVirtualServerAddress(vs, dummyDevice)
if err == nil {
t.Errorf("expected failure")
}
} }
} }

View File

@ -49,7 +49,7 @@ func NewFake() *FakeIPVS {
func toServiceKey(serv *utilipvs.VirtualServer) serviceKey { func toServiceKey(serv *utilipvs.VirtualServer) serviceKey {
return serviceKey{ return serviceKey{
IP: serv.Address.To4().String(), IP: serv.Address.String(),
Port: serv.Port, Port: serv.Port,
Protocol: serv.Protocol, Protocol: serv.Protocol,
} }