Add support for listing services, very rough

This commit is contained in:
Asim 2015-04-03 00:52:49 +02:00
parent f2daa02256
commit 52f140da5f
3 changed files with 63 additions and 0 deletions

View File

@ -84,6 +84,32 @@ func (c *ConsulRegistry) GetService(name string) (Service, error) {
return cs, nil
}
func (c *ConsulRegistry) ListServices() ([]Service, error) {
c.mtx.RLock()
serviceMap := c.services
c.mtx.RUnlock()
var services []Service
if len(serviceMap) > 0 {
for _, service := range services {
services = append(services, service)
}
return services, nil
}
rsp, _, err := c.Client.Catalog().Services(&consul.QueryOptions{})
if err != nil {
return nil, err
}
for service, _ := range rsp {
services = append(services, &ConsulService{ServiceName: service})
}
return services, nil
}
func (c *ConsulRegistry) NewService(name string, nodes ...Node) Service {
var snodes []*ConsulNode

View File

@ -60,6 +60,38 @@ func (c *KubernetesRegistry) GetService(name string) (Service, error) {
return ks, nil
}
func (c *KubernetesRegistry) ListServices() ([]Service, error) {
c.mtx.RLock()
serviceMap := c.services
c.mtx.RUnlock()
var services []Service
if len(serviceMap) > 0 {
for _, service := range serviceMap {
services = append(services, service)
}
return services, nil
}
rsp, err := c.Client.Services(c.Namespace).List(labels.Everything())
if err != nil {
return nil, err
}
for _, service := range rsp.Items {
if len(service.ObjectMeta.Labels["name"]) == 0 {
continue
}
services = append(services, &KubernetesService{
ServiceName: service.ObjectMeta.Labels["name"],
})
}
return services, nil
}
func (c *KubernetesRegistry) NewService(name string, nodes ...Node) Service {
var snodes []*KubernetesNode

View File

@ -4,6 +4,7 @@ type Registry interface {
Register(Service) error
Deregister(Service) error
GetService(string) (Service, error)
ListServices() ([]Service, error)
NewService(string, ...Node) Service
NewNode(string, string, int) Node
}
@ -23,3 +24,7 @@ func Deregister(s Service) error {
func GetService(name string) (Service, error) {
return DefaultRegistry.GetService(name)
}
func ListServices() ([]Service, error) {
return DefaultRegistry.ListServices()
}