func (ipvs *Ipvs) syncDestinations(state state.State, svc types.Service) error { stateSet := ipvs.getStateDestinationsSet(state, svc) currentSet, err := ipvs.getCurrentDestinationsSet(svc) if err != nil { return err } rulesToAdd := stateSet.Difference(currentSet) rulesToRemove := currentSet.Difference(stateSet) for r := range rulesToAdd.Iter() { destination := r.(types.Destination) if err := gipvs.AddDestination(*ToIpvsService(&svc), *ToIpvsDestination(&destination)); err != nil { return err } } for r := range rulesToRemove.Iter() { destination := r.(types.Destination) err := gipvs.DeleteDestination(*ToIpvsService(&svc), *ToIpvsDestination(&destination)) if err != nil { return err } } return nil }
// IPVSDeleteDestination deletes the specified destination from the IPVS table. func (ncc *SeesawNCC) IPVSDeleteDestination(dst *ncctypes.IPVSDestination, out *int) error { ipvsMutex.Lock() defer ipvsMutex.Unlock() return ipvs.DeleteDestination(*dst.Service, *dst.Destination) }
// 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) } } } }