2015-05-23 21:04:16 +02:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
2015-06-01 19:55:27 +02:00
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/fields"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
|
|
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/watch"
|
2015-05-25 23:14:28 +02:00
|
|
|
"github.com/myodc/go-micro/registry"
|
2015-05-23 21:04:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type watcher struct {
|
|
|
|
registry *kregistry
|
2015-06-01 19:55:27 +02:00
|
|
|
watcher watch.Interface
|
2015-05-23 21:04:16 +02:00
|
|
|
}
|
|
|
|
|
2015-06-01 19:55:27 +02:00
|
|
|
func (k *watcher) update(event watch.Event) {
|
|
|
|
if event.Object == nil {
|
|
|
|
return
|
|
|
|
}
|
2015-05-23 21:04:16 +02:00
|
|
|
|
2015-06-01 19:55:27 +02:00
|
|
|
var service *api.Service
|
|
|
|
switch obj := event.Object.(type) {
|
|
|
|
case *api.Service:
|
|
|
|
service = obj
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
2015-05-23 21:04:16 +02:00
|
|
|
|
2015-06-01 19:55:27 +02:00
|
|
|
name, exists := service.ObjectMeta.Labels["name"]
|
|
|
|
if !exists {
|
|
|
|
return
|
|
|
|
}
|
2015-05-23 21:04:16 +02:00
|
|
|
|
2015-06-01 19:55:27 +02:00
|
|
|
switch event.Type {
|
|
|
|
case watch.Added, watch.Modified:
|
|
|
|
case watch.Deleted:
|
2015-05-23 21:04:16 +02:00
|
|
|
k.registry.mtx.Lock()
|
2015-06-01 19:55:27 +02:00
|
|
|
delete(k.registry.services, name)
|
2015-05-23 21:04:16 +02:00
|
|
|
k.registry.mtx.Unlock()
|
2015-06-01 19:55:27 +02:00
|
|
|
return
|
|
|
|
default:
|
|
|
|
return
|
2015-05-23 21:04:16 +02:00
|
|
|
}
|
|
|
|
|
2015-06-01 19:55:27 +02:00
|
|
|
serviceIP := net.ParseIP(service.Spec.ClusterIP)
|
|
|
|
|
2015-05-23 21:04:16 +02:00
|
|
|
k.registry.mtx.Lock()
|
2015-06-01 19:55:27 +02:00
|
|
|
k.registry.services[name] = ®istry.Service{
|
|
|
|
Name: name,
|
|
|
|
Nodes: []*registry.Node{
|
|
|
|
®istry.Node{
|
|
|
|
Address: serviceIP.String(),
|
|
|
|
Port: service.Spec.Ports[0].Port,
|
|
|
|
},
|
|
|
|
},
|
2015-05-23 21:04:16 +02:00
|
|
|
}
|
2015-06-01 19:55:27 +02:00
|
|
|
k.registry.mtx.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *watcher) Stop() {
|
|
|
|
k.watcher.Stop()
|
2015-05-23 21:04:16 +02:00
|
|
|
}
|
|
|
|
|
2015-06-01 19:55:27 +02:00
|
|
|
func newWatcher(kr *kregistry) (registry.Watcher, error) {
|
|
|
|
svi := kr.client.Services(api.NamespaceAll)
|
|
|
|
|
|
|
|
services, err := svi.List(labels.Everything())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-23 21:04:16 +02:00
|
|
|
|
2015-06-01 19:55:27 +02:00
|
|
|
watch, err := svi.Watch(labels.Everything(), fields.Everything(), services.ResourceVersion)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-05-23 21:04:16 +02:00
|
|
|
|
2015-06-01 19:55:27 +02:00
|
|
|
w := &watcher{
|
2015-05-23 21:04:16 +02:00
|
|
|
registry: kr,
|
2015-06-01 19:55:27 +02:00
|
|
|
watcher: watch,
|
2015-05-23 21:04:16 +02:00
|
|
|
}
|
|
|
|
|
2015-06-01 19:55:27 +02:00
|
|
|
go func() {
|
|
|
|
for event := range watch.ResultChan() {
|
|
|
|
w.update(event)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return w, nil
|
2015-05-23 21:04:16 +02:00
|
|
|
}
|