2015-05-23 20:04:16 +01:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/myodc/go-micro/registry"
|
|
|
|
|
|
|
|
k8s "github.com/GoogleCloudPlatform/kubernetes/pkg/client"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
|
|
|
)
|
|
|
|
|
|
|
|
type kregistry struct {
|
|
|
|
client *k8s.Client
|
|
|
|
namespace string
|
|
|
|
|
|
|
|
mtx sync.RWMutex
|
2015-05-25 22:14:28 +01:00
|
|
|
services map[string]*registry.Service
|
2015-05-23 20:04:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *kregistry) Watch() {
|
|
|
|
newWatcher(c)
|
|
|
|
}
|
|
|
|
|
2015-05-25 22:14:28 +01:00
|
|
|
func (c *kregistry) Deregister(s *registry.Service) error {
|
2015-05-23 20:04:16 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-25 22:14:28 +01:00
|
|
|
func (c *kregistry) Register(s *registry.Service) error {
|
2015-05-23 20:04:16 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-25 22:14:28 +01:00
|
|
|
func (c *kregistry) GetService(name string) (*registry.Service, error) {
|
2015-05-23 20:04:16 +01:00
|
|
|
c.mtx.RLock()
|
|
|
|
svc, ok := c.services[name]
|
|
|
|
c.mtx.RUnlock()
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
return svc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
selector := labels.SelectorFromSet(labels.Set{"name": name})
|
|
|
|
|
|
|
|
services, err := c.client.Services(c.namespace).List(selector)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(services.Items) == 0 {
|
|
|
|
return nil, fmt.Errorf("Service not found")
|
|
|
|
}
|
|
|
|
|
2015-05-25 22:14:28 +01:00
|
|
|
ks := ®istry.Service{
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
|
2015-05-23 20:04:16 +01:00
|
|
|
for _, item := range services.Items {
|
2015-05-25 22:14:28 +01:00
|
|
|
ks.Nodes = append(ks.Nodes, ®istry.Node{
|
|
|
|
Address: item.Spec.PortalIP,
|
|
|
|
Port: item.Spec.Ports[0].Port,
|
2015-05-23 20:04:16 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return ks, nil
|
|
|
|
}
|
|
|
|
|
2015-05-25 22:14:28 +01:00
|
|
|
func (c *kregistry) ListServices() ([]*registry.Service, error) {
|
2015-05-23 20:04:16 +01:00
|
|
|
c.mtx.RLock()
|
|
|
|
serviceMap := c.services
|
|
|
|
c.mtx.RUnlock()
|
|
|
|
|
2015-05-25 22:14:28 +01:00
|
|
|
var services []*registry.Service
|
2015-05-23 20:04:16 +01:00
|
|
|
|
|
|
|
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 _, svc := range rsp.Items {
|
|
|
|
if len(svc.ObjectMeta.Labels["name"]) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-05-25 22:14:28 +01:00
|
|
|
services = append(services, ®istry.Service{
|
|
|
|
Name: svc.ObjectMeta.Labels["name"],
|
2015-05-23 20:04:16 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRegistry(addrs []string, opts ...registry.Option) registry.Registry {
|
|
|
|
host := "http://" + os.Getenv("KUBERNETES_RO_SERVICE_HOST") + ":" + os.Getenv("KUBERNETES_RO_SERVICE_PORT")
|
|
|
|
if len(addrs) > 0 {
|
|
|
|
host = addrs[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
client, _ := k8s.New(&k8s.Config{
|
|
|
|
Host: host,
|
|
|
|
})
|
|
|
|
|
|
|
|
kr := &kregistry{
|
|
|
|
client: client,
|
|
|
|
namespace: "default",
|
2015-05-25 22:14:28 +01:00
|
|
|
services: make(map[string]*registry.Service),
|
2015-05-23 20:04:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
kr.Watch()
|
|
|
|
|
|
|
|
return kr
|
|
|
|
}
|