Example #1
0
func (d *daemon) ListenAndServe() {
	router := mux.NewRouter()

	// Create a new api controller
	d.apiController = objApi.NewAPIController(router)

	// initialize policy manager
	mastercfg.InitPolicyMgr(d.stateDriver)

	// Register netmaster service
	d.registerService()

	// register web ui handlers
	registerWebuiHandler(router)

	// Add REST routes
	s := router.Headers("Content-Type", "application/json").Methods("Post").Subrouter()
	s.HandleFunc(fmt.Sprintf("/%s", master.DesiredConfigRESTEndpoint),
		post(d.desiredConfig))
	s.HandleFunc(fmt.Sprintf("/%s", master.AddConfigRESTEndpoint),
		post(d.addConfig))
	s.HandleFunc(fmt.Sprintf("/%s", master.DelConfigRESTEndpoint),
		post(d.delConfig))
	s.HandleFunc(fmt.Sprintf("/%s", master.HostBindingConfigRESTEndpoint),
		post(d.hostBindingsConfig))

	s.HandleFunc("/plugin/allocAddress", makeHTTPHandler(master.AllocAddressHandler))
	s.HandleFunc("/plugin/releaseAddress", makeHTTPHandler(master.ReleaseAddressHandler))
	s.HandleFunc("/plugin/createEndpoint", makeHTTPHandler(master.CreateEndpointHandler))
	s.HandleFunc("/plugin/deleteEndpoint", makeHTTPHandler(master.DeleteEndpointHandler))

	s = router.Methods("Get").Subrouter()
	s.HandleFunc(fmt.Sprintf("/%s/%s", master.GetEndpointRESTEndpoint, "{id}"),
		get(false, d.endpoints))
	s.HandleFunc(fmt.Sprintf("/%s", master.GetEndpointsRESTEndpoint),
		get(true, d.endpoints))
	s.HandleFunc(fmt.Sprintf("/%s/%s", master.GetNetworkRESTEndpoint, "{id}"),
		get(false, d.networks))
	s.HandleFunc(fmt.Sprintf("/%s", master.GetNetworksRESTEndpoint),
		get(true, d.networks))

	log.Infof("Netmaster listening on %s", d.opts.listenURL)

	go objApi.CreateDefaultTenant()

	if err := http.ListenAndServe(d.opts.listenURL, router); err != nil {
		log.Fatalf("Error listening for http requests. Error: %s", err)
	}

}
Example #2
0
// setup the test netmaster REST server and client
func TestMain(m *testing.M) {
	var err error

	// Setup state store
	stateStore, err = initStateDriver()
	if err != nil {
		log.Fatalf("Error initializing state store. Err: %v", err)
	}
	// little hack to clear all state from etcd
	stateStore.(*state.EtcdStateDriver).Client.Delete("/contiv.io", true)

	// Setup resource manager
	if _, err = resources.NewStateResourceManager(stateStore); err != nil {
		log.Fatalf("Failed to init resource manager. Error: %s", err)
	}

	router := mux.NewRouter()

	// Create a new api controller
	apiController = NewAPIController(router)

	ofnetMaster := ofnet.NewOfnetMaster(ofnet.OFNET_MASTER_PORT)
	if ofnetMaster == nil {
		log.Fatalf("Error creating ofnet master")
	}

	// initialize policy manager
	mastercfg.InitPolicyMgr(stateStore, ofnetMaster)

	// Create HTTP server
	go http.ListenAndServe(netmasterTestListenURL, router)
	time.Sleep(time.Second)

	// create a new contiv client
	contivClient, err = client.NewContivClient(netmasterTestURL)
	if err != nil {
		log.Fatalf("Error creating contiv client. Err: %v", err)
	}

	// Create default tenant
	createDefaultTenant()

	exitCode := m.Run()
	if exitCode == 0 {
		cleanupState()
	}
	os.Exit(exitCode)
}
Example #3
0
// runLeader runs leader loop
func (d *daemon) runLeader() {
	router := mux.NewRouter()

	// acquire listener mutex
	d.listenerMutex.Lock()
	defer d.listenerMutex.Unlock()

	// Create a new api controller
	d.apiController = objApi.NewAPIController(router, d.clusterStore)

	//Restore state from clusterStore

	d.restoreCache()

	// Register netmaster service
	d.registerService()

	// initialize policy manager
	mastercfg.InitPolicyMgr(d.stateDriver, d.ofnetMaster)

	// setup HTTP routes
	d.registerRoutes(router)

	// Create HTTP server and listener
	server := &http.Server{Handler: router}
	server.SetKeepAlivesEnabled(false)
	listener, err := net.Listen("tcp", d.listenURL)
	if nil != err {
		log.Fatalln(err)
	}

	log.Infof("Netmaster listening on %s", d.listenURL)

	listener = utils.ListenWrapper(listener)

	// start server
	go server.Serve(listener)

	// Wait till we are asked to stop
	<-d.stopLeaderChan

	// Close the listener and exit
	listener.Close()
	log.Infof("Exiting Leader mode")
}