Beispiel #1
0
// IPVSGetServices gets the currently configured services from the IPVS table.
func (ncc *SeesawNCC) IPVSGetServices(in int, s *ncctypes.IPVSServices) error {
	ipvsMutex.Lock()
	defer ipvsMutex.Unlock()
	svcs, err := ipvs.GetServices()
	if err != nil {
		return err
	}
	s.Services = nil
	for _, svc := range svcs {
		s.Services = append(s.Services, svc)
	}
	return nil
}
Beispiel #2
0
func (ipvs *Ipvs) getCurrentServicesSet() (mapset.Set, error) {
	svcs, err := gipvs.GetServices()
	if err != nil {
		return nil, err
	}

	currentSet := mapset.NewSet()
	for _, s := range svcs {
		currentSet.Add(FromService(s))
	}

	return currentSet, nil
}
Beispiel #3
0
// testIPVS adds all of the services and destinations provided via the testing
// table, then gets all of the services and destinations from IPVS before
// comparing and reporting on the result. If everything worked correctly we
// should end up with an exact match between what we added and what we got back
// from IPVS.
func testIPVS() {
	log.Println("Testing IPVS...")

	// Make sure we have a clean slate.
	if err := ipvs.Flush(); err != nil {
		log.Fatalf("Failed to flush IPVS table: %v\n", err)
	}

	// Add the test services and destinations to IPVS.
	log.Println("Adding services and destinations...")
	for _, test := range ipvsTests {
		svc := test.service
		log.Println(test.desc)
		log.Printf("=> Adding service %s\n", svc)
		svc.Destinations = nil
		for i := range test.destinations {
			dst := &test.destinations[i]
			log.Printf("--> Destination %s\n", dst)
			svc.Destinations = append(svc.Destinations, dst)
		}
		if err := ipvs.AddService(svc); err != nil {
			log.Fatalf("ipvs.AddService() failed: %v\n", err)
		}
	}

	// Get the services from IPVS and compare to what we added. Note that
	// the order of the returned services and destinations are not
	// guaranteed to match the order in which they were added.
	log.Println("Getting services from IPVS...")
	svcs, err := ipvs.GetServices()
	if err != nil {
		log.Fatalf("ipvs.GetServices() failed: %v\n", err)
	}
	if len(ipvsTests) != len(svcs) {
		log.Printf("ERROR: Added %d services but got %d!\n",
			len(ipvsTests), len(svcs))
	}
	for _, test := range ipvsTests {
		svc := compareSvc(&test.service, svcs)
		if svc == nil {
			continue
		}
		if len(test.destinations) != len(svc.Destinations) {
			log.Printf("ERROR: Added %d destinations by got %d!\n",
				len(test.destinations), len(svc.Destinations))
		}
		for _, testDst := range test.destinations {
			compareDst(&testDst, svc.Destinations)
		}
	}
}
Beispiel #4
0
func (m Metrics) collect() error {
	services, err := ipvs.GetServices()
	if err != nil {
		return errors.Wrap(err, "ipvs.GetServices() failed when collecting metrics")
	}

	for _, s := range services {
		if err := m.publisher.PublishServiceStats(s); err != nil {
			return err
		}
	}

	return nil
}
Beispiel #5
0
func GetIPVSStats() (vips []*VirtualIPPoint, err error) {
	svcs, err := ipvs.GetServices()
	if err != nil {
		return nil, err
	}

	var vip *VirtualIPPoint
	var ActiveConns uint32
	var InactiveConns uint32
	var RsNum int
	//var PersistConns uint32
	for _, svc := range svcs {
		ActiveConns = 0
		InactiveConns = 0
		RsNum = len(svc.Destinations)

		for _, dest := range svc.Destinations {
			ActiveConns += dest.Statistics.ActiveConns
			InactiveConns += dest.Statistics.InactiveConns
			//PersistConns += dest.Statistics.PersistConns
		}

		ipstr := fmt.Sprintf("%v", svc.Address)
		vip = &VirtualIPPoint{
			IP:            ipstr,
			Port:          int(svc.Port),
			ActiveConns:   ActiveConns,
			InactiveConns: InactiveConns,
			RealServerNum: RsNum,

			Connections: svc.Statistics.Connections,
			PacketsIn:   svc.Statistics.PacketsIn,
			PacketsOut:  svc.Statistics.PacketsOut,
			BytesIn:     svc.Statistics.BytesIn,
			BytesOut:    svc.Statistics.BytesOut,
		}

		vips = append(vips, vip)
	}

	return vips, nil
}
Beispiel #6
0
func TestIpvsSync(t *testing.T) {
	svc := types.Service{
		Name:      "",
		Address:   "10.0.1.1",
		Port:      80,
		Scheduler: "lc",
		Protocol:  "tcp",
	}

	dst := types.Destination{
		Name:      "host1",
		Address:   "192.168.1.1",
		Port:      80,
		Mode:      "nat",
		Weight:    1,
		ServiceId: "service1",
	}

	dst2 := types.Destination{
		Name:      "host2",
		Address:   "192.168.1.2",
		Port:      80,
		Mode:      "nat",
		Weight:    1,
		ServiceId: "service1",
	}

	state := &mocks.State{}
	state.On("GetServices").Return([]types.Service{svc})
	state.On("GetDestinations", &svc).Return([]types.Destination{dst})

	ipvsMngr, err := ipvs.New()
	assert.Nil(t, err)

	// Testing Services and destinations addition
	ipvsMngr.Sync(state)

	svcs, err := gipvs.GetServices()
	assert.Nil(t, err)

	// Asserting Services
	assert.Equal(t, svc.Address, svcs[0].Address.String())
	assert.Equal(t, svc.Port, svcs[0].Port)

	// Asserting Destinations
	assert.Equal(t, dst.Address, svcs[0].Destinations[0].Address.String())
	assert.Equal(t, dst.Port, svcs[0].Destinations[0].Port)

	// Updating destinations
	state = &mocks.State{}
	state.On("GetServices").Return([]types.Service{svc})
	state.On("GetDestinations", &svc).Return([]types.Destination{dst2})
	ipvsMngr.Sync(state)

	svcs, err = gipvs.GetServices()
	assert.Nil(t, err)

	assert.Equal(t, dst2.Address, svcs[0].Destinations[0].Address.String())
	assert.Equal(t, dst2.Port, svcs[0].Destinations[0].Port)

	// Testing Services and Destinations deletion
	state = &mocks.State{}
	state.On("GetServices").Return([]types.Service{})

	ipvsMngr.Sync(state)

	svcs, err = gipvs.GetServices()
	assert.Nil(t, err)

	assert.Len(t, svcs, 0)

	ipvsMngr.Flush()
}
Beispiel #7
0
// testIPVSModification adds, updates and deletes services and destinations
// from the test configurations. At each step the service or destination is
// retrieved from IPVS and compared against what should be configured.
func testIPVSModification() {
	log.Println("Testing IPVS modifications...")

	// Make sure we have a clean slate.
	if err := ipvs.Flush(); err != nil {
		log.Fatalf("Failed to flush IPVS table: %v\n", err)
	}

	for _, test := range ipvsTests {
		var svc *ipvs.Service
		var err error
		testSvc := test.service
		testDst := test.destinations[0]

		// Add service.
		log.Printf("=> Adding service %s\n", testSvc)
		if err = ipvs.AddService(testSvc); err != nil {
			log.Fatalf("ipvs.AddService() failed: %v\n", err)
		}
		if svc, err = ipvs.GetService(&testSvc); err != nil {
			log.Fatalf("ipvs.GetService() failed: %v\n", err)
		}
		compareSvc(&testSvc, []*ipvs.Service{svc})

		// Add destination.
		log.Printf("--> Adding destination %s\n", testDst)
		if err = ipvs.AddDestination(testSvc, testDst); err != nil {
			log.Fatalf("ipvs.AddDestination() failed: %v\n", err)
		}
		if svc, err = ipvs.GetService(&testSvc); err != nil {
			log.Fatalf("ipvs.GetService() failed: %v\n", err)
		}
		compareSvc(&testSvc, []*ipvs.Service{svc})
		compareDst(&testDst, svc.Destinations)

		// Update service.
		testSvc.Scheduler = "lc"
		if err = ipvs.UpdateService(testSvc); err != nil {
			log.Fatalf("ipvs.UpdateService() failed: %v\n", err)
		}
		if svc, err = ipvs.GetService(&testSvc); err != nil {
			log.Fatalf("ipvs.GetService() failed: %v\n", err)
		}
		compareSvc(&testSvc, []*ipvs.Service{svc})
		compareDst(&testDst, svc.Destinations)

		// Update destination.
		testDst.Weight = 1000
		if err = ipvs.UpdateDestination(testSvc, testDst); err != nil {
			log.Fatalf("ipvs.UpdateDestination() failed: %v\n", err)
		}
		if svc, err = ipvs.GetService(&testSvc); err != nil {
			log.Fatalf("ipvs.GetService() failed: %v\n", err)
		}
		compareSvc(&testSvc, []*ipvs.Service{svc})
		compareDst(&testDst, svc.Destinations)

		// Delete destination.
		if err = ipvs.DeleteDestination(testSvc, testDst); err != nil {
			log.Fatalf("ipvs.DeleteDestination() failed: %v\n", err)
		}
		if svc, err = ipvs.GetService(&testSvc); err != nil {
			log.Fatalf("ipvs.GetService() failed: %v\n", err)
		}
		if len(svc.Destinations) != 0 {
			log.Printf("ERROR: Service still has destinations\n")
		}

		// Delete service.
		if err = ipvs.DeleteService(testSvc); err != nil {
			log.Fatalf("ipvs.DeleteService() failed: %v\n", err)
		}

		// Make sure there is nothing left behind.
		svcs, err := ipvs.GetServices()
		if err != nil {
			log.Fatalf("ipvs.GetServices() failed: %v\n", err)
		}
		if len(svcs) != 0 {
			log.Printf("ERROR: IPVS services still exist!\n")
			for _, svc = range svcs {
				log.Printf("=> Got service %s\n", svc)
			}
		}
	}
}