open-consul/consul/catalog_endpoint.go

181 lines
5.2 KiB
Go
Raw Normal View History

package consul
import (
2013-12-24 20:43:34 +00:00
"fmt"
2014-02-20 23:16:26 +00:00
"github.com/armon/go-metrics"
2013-12-19 20:03:57 +00:00
"github.com/hashicorp/consul/consul/structs"
2014-02-20 23:16:26 +00:00
"time"
)
// Catalog endpoint is used to manipulate the service catalog
type Catalog struct {
2013-12-11 22:57:40 +00:00
srv *Server
}
// Register is used register that a node is providing a given service.
2013-12-19 20:03:57 +00:00
func (c *Catalog) Register(args *structs.RegisterRequest, reply *struct{}) error {
2013-12-11 22:57:40 +00:00
if done, err := c.srv.forward("Catalog.Register", args.Datacenter, args, reply); done {
return err
}
2014-02-20 23:16:26 +00:00
defer metrics.MeasureSince([]string{"consul", "catalog", "register"}, time.Now())
2013-12-24 20:43:34 +00:00
// Verify the args
if args.Node == "" || args.Address == "" {
return fmt.Errorf("Must provide node and address")
}
if args.Service != nil {
// If no service id, but service name, use default
if args.Service.ID == "" && args.Service.Service != "" {
args.Service.ID = args.Service.Service
}
// Verify ServiceName provided if ID
if args.Service.ID != "" && args.Service.Service == "" {
return fmt.Errorf("Must provide service name with ID")
}
}
if args.Check != nil {
if args.Check.CheckID == "" && args.Check.Name != "" {
args.Check.CheckID = args.Check.Name
}
if args.Check.Node == "" {
args.Check.Node = args.Node
}
}
2013-12-19 20:03:57 +00:00
_, err := c.srv.raftApply(structs.RegisterRequestType, args)
if err != nil {
2014-01-10 19:07:04 +00:00
c.srv.logger.Printf("[ERR] consul.catalog: Register failed: %v", err)
return err
}
return nil
}
// Deregister is used to remove a service registration for a given node.
2013-12-19 20:03:57 +00:00
func (c *Catalog) Deregister(args *structs.DeregisterRequest, reply *struct{}) error {
2013-12-11 23:34:10 +00:00
if done, err := c.srv.forward("Catalog.Deregister", args.Datacenter, args, reply); done {
return err
}
2014-02-20 23:16:26 +00:00
defer metrics.MeasureSince([]string{"consul", "catalog", "deregister"}, time.Now())
2013-12-11 23:34:10 +00:00
2013-12-24 20:43:34 +00:00
// Verify the args
if args.Node == "" {
return fmt.Errorf("Must provide node")
}
2013-12-19 20:03:57 +00:00
_, err := c.srv.raftApply(structs.DeregisterRequestType, args)
2013-12-11 23:34:10 +00:00
if err != nil {
2014-01-10 19:07:04 +00:00
c.srv.logger.Printf("[ERR] consul.catalog: Deregister failed: %v", err)
2013-12-11 23:34:10 +00:00
return err
}
return nil
}
2013-12-12 18:35:50 +00:00
// ListDatacenters is used to query for the list of known datacenters
func (c *Catalog) ListDatacenters(args *struct{}, reply *[]string) error {
c.srv.remoteLock.RLock()
defer c.srv.remoteLock.RUnlock()
// Read the known DCs
var dcs []string
for dc := range c.srv.remoteConsuls {
dcs = append(dcs, dc)
}
// Return
*reply = dcs
return nil
}
2013-12-12 18:48:36 +00:00
// ListNodes is used to query the nodes in a DC
func (c *Catalog) ListNodes(args *structs.DCSpecificRequest, reply *structs.IndexedNodes) error {
if done, err := c.srv.forward("Catalog.ListNodes", args.Datacenter, args, reply); done {
2013-12-12 18:48:36 +00:00
return err
}
// Get the local state
2013-12-12 18:48:36 +00:00
state := c.srv.fsm.State()
return c.srv.blockingRPC(&args.BlockingQuery,
state.QueryTables("Nodes"),
func() (uint64, error) {
reply.Index, reply.Nodes = state.Nodes()
return reply.Index, nil
})
2013-12-12 18:48:36 +00:00
}
2013-12-12 19:07:14 +00:00
// ListServices is used to query the services in a DC
func (c *Catalog) ListServices(args *structs.DCSpecificRequest, reply *structs.IndexedServices) error {
if done, err := c.srv.forward("Catalog.ListServices", args.Datacenter, args, reply); done {
2013-12-12 19:07:14 +00:00
return err
}
// Get the current nodes
state := c.srv.fsm.State()
return c.srv.blockingRPC(&args.BlockingQuery,
state.QueryTables("Services"),
func() (uint64, error) {
reply.Index, reply.Services = state.Services()
return reply.Index, nil
})
2013-12-12 19:07:14 +00:00
}
2013-12-12 19:37:19 +00:00
// ServiceNodes returns all the nodes registered as part of a service
func (c *Catalog) ServiceNodes(args *structs.ServiceSpecificRequest, reply *structs.IndexedServiceNodes) error {
2013-12-12 19:37:19 +00:00
if done, err := c.srv.forward("Catalog.ServiceNodes", args.Datacenter, args, reply); done {
return err
}
2013-12-24 20:43:34 +00:00
// Verify the arguments
if args.ServiceName == "" {
return fmt.Errorf("Must provide service name")
}
2013-12-12 19:37:19 +00:00
// Get the nodes
state := c.srv.fsm.State()
err := c.srv.blockingRPC(&args.BlockingQuery,
state.QueryTables("ServiceNodes"),
func() (uint64, error) {
if args.TagFilter {
reply.Index, reply.ServiceNodes = state.ServiceTagNodes(args.ServiceName, args.ServiceTag)
} else {
reply.Index, reply.ServiceNodes = state.ServiceNodes(args.ServiceName)
}
return reply.Index, nil
})
// Provide some metrics
if err == nil {
metrics.IncrCounter([]string{"consul", "catalog", "service", "query", args.ServiceName}, 1)
if args.ServiceTag != "" {
metrics.IncrCounter([]string{"consul", "catalog", "service", "query-tag", args.ServiceName, args.ServiceTag}, 1)
}
if len(reply.ServiceNodes) == 0 {
metrics.IncrCounter([]string{"consul", "catalog", "service", "not-found", args.ServiceName}, 1)
}
}
return err
2013-12-12 19:37:19 +00:00
}
2013-12-12 19:46:25 +00:00
// NodeServices returns all the services registered as part of a node
func (c *Catalog) NodeServices(args *structs.NodeSpecificRequest, reply *structs.IndexedNodeServices) error {
2013-12-12 19:46:25 +00:00
if done, err := c.srv.forward("Catalog.NodeServices", args.Datacenter, args, reply); done {
return err
}
2013-12-24 20:43:34 +00:00
// Verify the arguments
if args.Node == "" {
return fmt.Errorf("Must provide node")
}
2013-12-12 19:46:25 +00:00
// Get the node services
state := c.srv.fsm.State()
return c.srv.blockingRPC(&args.BlockingQuery,
state.QueryTables("NodeServices"),
func() (uint64, error) {
reply.Index, reply.NodeServices = state.NodeServices(args.Node)
return reply.Index, nil
})
2013-12-12 19:46:25 +00:00
}