From 89aeeb294cfe87c20379690bbad56ad93f50cd91 Mon Sep 17 00:00:00 2001 From: m1093782566 Date: Thu, 31 Aug 2017 19:06:55 +0800 Subject: [PATCH] support flush services API Signed-off-by: m1093782566 --- libnetwork/ipvs/ipvs.go | 7 +++++++ libnetwork/ipvs/ipvs_test.go | 35 +++++++++++++++++++++++++++++++++++ libnetwork/ipvs/netlink.go | 7 +++++++ 3 files changed, 49 insertions(+) diff --git a/libnetwork/ipvs/ipvs.go b/libnetwork/ipvs/ipvs.go index a285e102e3..ebcdd808c3 100644 --- a/libnetwork/ipvs/ipvs.go +++ b/libnetwork/ipvs/ipvs.go @@ -116,6 +116,13 @@ func (i *Handle) DelService(s *Service) error { return i.doCmd(s, nil, ipvsCmdDelService) } +// Flush deletes all existing services in the passed +// handle. +func (i *Handle) Flush() error { + _, err := i.doCmdWithoutAttr(ipvsCmdFlush) + return err +} + // NewDestination creates a new real server in the passed ipvs // service which should already be existing in the passed handle. func (i *Handle) NewDestination(s *Service, d *Destination) error { diff --git a/libnetwork/ipvs/ipvs_test.go b/libnetwork/ipvs/ipvs_test.go index 50b5c532e9..30ffe9717e 100644 --- a/libnetwork/ipvs/ipvs_test.go +++ b/libnetwork/ipvs/ipvs_test.go @@ -178,6 +178,41 @@ func TestService(t *testing.T) { } } + svcs := []Service{ + { + AddressFamily: nl.FAMILY_V4, + SchedName: RoundRobin, + Protocol: syscall.IPPROTO_TCP, + Port: 80, + Address: net.ParseIP("10.20.30.40"), + Netmask: 0xFFFFFFFF, + }, + { + AddressFamily: nl.FAMILY_V4, + SchedName: LeastConnection, + Protocol: syscall.IPPROTO_UDP, + Port: 8080, + Address: net.ParseIP("10.20.30.41"), + Netmask: 0xFFFFFFFF, + }, + } + // Create services for testing flush + for _, svc := range svcs { + if !i.IsServicePresent(&svc) { + err = i.NewService(&svc) + assert.NoError(t, err) + checkService(t, i, &svc, true) + } else { + t.Errorf("svc: %v exists", svc) + } + } + err = i.Flush() + assert.NoError(t, err) + got, err := i.GetServices() + assert.NoError(t, err) + if len(got) != 0 { + t.Errorf("Unexpected services after flush") + } } func createDummyInterface(t *testing.T) { diff --git a/libnetwork/ipvs/netlink.go b/libnetwork/ipvs/netlink.go index b8d33dcdc4..2089283d14 100644 --- a/libnetwork/ipvs/netlink.go +++ b/libnetwork/ipvs/netlink.go @@ -402,6 +402,13 @@ func (i *Handle) doGetServicesCmd(svc *Service) ([]*Service, error) { return res, nil } +// doCmdWithoutAttr a simple wrapper of netlink socket execute command +func (i *Handle) doCmdWithoutAttr(cmd uint8) ([][]byte, error) { + req := newIPVSRequest(cmd) + req.Seq = atomic.AddUint32(&i.seq, 1) + return execute(i.sock, req, 0) +} + func assembleDestination(attrs []syscall.NetlinkRouteAttr) (*Destination, error) { var d Destination