Просмотр исходного кода

Merge pull request #1929 from m1093782566/flush-ipvs

Support flush API for IPVS service
Madhu Venugopal 8 лет назад
Родитель
Сommit
56bb25d189
3 измененных файлов с 49 добавлено и 0 удалено
  1. 7 0
      libnetwork/ipvs/ipvs.go
  2. 35 0
      libnetwork/ipvs/ipvs_test.go
  3. 7 0
      libnetwork/ipvs/netlink.go

+ 7 - 0
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 {

+ 35 - 0
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) {

+ 7 - 0
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