Ejemplo n.º 1
0
func (oi *OsdnRegistryInterface) GetServices() ([]osdnapi.Service, error) {
	kNsList, err := oi.kClient.Namespaces().List(labels.Everything(), fields.Everything())
	if err != nil {
		return nil, err
	}
	oServList := make([]osdnapi.Service, 0)
	for _, ns := range kNsList.Items {
		kServList, err := oi.kClient.Services(ns.Name).List(labels.Everything())
		if err != nil {
			return nil, err
		}

		// convert kube ServiceList into []osdnapi.Service
		for _, kService := range kServList.Items {
			if kService.Spec.ClusterIP == "None" {
				continue
			}
			for i := range kService.Spec.Ports {
				oService := osdnapi.Service{
					Name:      kService.ObjectMeta.Name,
					Namespace: ns.Name,
					IP:        kService.Spec.ClusterIP,
					Protocol:  osdnapi.ServiceProtocol(kService.Spec.Ports[i].Protocol),
					Port:      uint(kService.Spec.Ports[i].Port),
				}
				oServList = append(oServList, oService)
			}
		}
	}
	return oServList, nil
}
Ejemplo n.º 2
0
func newSDNService(kServ *kapi.Service, namespace string, port kapi.ServicePort) osdnapi.Service {
	return osdnapi.Service{
		Name:      kServ.ObjectMeta.Name,
		Namespace: namespace,
		IP:        kServ.Spec.ClusterIP,
		Protocol:  osdnapi.ServiceProtocol(port.Protocol),
		Port:      uint(port.Port),
	}
}
Ejemplo n.º 3
0
func (oi *OsdnRegistryInterface) watchServicesForNamespace(namespace string, receiver chan *osdnapi.ServiceEvent, stop chan bool) error {
	serviceEventQueue := oscache.NewEventQueue(cache.MetaNamespaceKeyFunc)
	listWatch := &cache.ListWatch{
		ListFunc: func() (runtime.Object, error) {
			return oi.kClient.Services(namespace).List(labels.Everything())
		},
		WatchFunc: func(resourceVersion string) (watch.Interface, error) {
			return oi.kClient.Services(namespace).Watch(labels.Everything(), fields.Everything(), resourceVersion)
		},
	}
	cache.NewReflector(listWatch, &kapi.Service{}, serviceEventQueue, 4*time.Minute).Run()

	go func() {
		select {
		case <-stop:
			serviceEventQueue.Cancel()
		}
	}()

	for {
		eventType, obj, err := serviceEventQueue.Pop()
		if err != nil {
			if _, ok := err.(oscache.EventQueueStopped); ok {
				return nil
			}
			return err
		}
		switch eventType {
		case watch.Added:
			// we should ignore the modified event because status updates cause unnecessary noise
			// the only time we would care about modified would be if the service IP changes (does not happen)
			kServ := obj.(*kapi.Service)
			if kServ.Spec.ClusterIP == "None" {
				continue
			}
			for i := range kServ.Spec.Ports {
				oServ := osdnapi.Service{
					Name:      kServ.ObjectMeta.Name,
					Namespace: namespace,
					IP:        kServ.Spec.ClusterIP,
					Protocol:  osdnapi.ServiceProtocol(kServ.Spec.Ports[i].Protocol),
					Port:      uint(kServ.Spec.Ports[i].Port),
				}
				receiver <- &osdnapi.ServiceEvent{Type: osdnapi.Added, Service: oServ}
			}
		case watch.Deleted:
			// TODO: There is a chance that a Delete event will not get triggered.
			// Need to use a periodic sync loop that lists and compares.
			kServ := obj.(*kapi.Service)
			if kServ.Spec.ClusterIP == "None" {
				continue
			}
			for i := range kServ.Spec.Ports {
				oServ := osdnapi.Service{
					Name:      kServ.ObjectMeta.Name,
					Namespace: namespace,
					IP:        kServ.Spec.ClusterIP,
					Protocol:  osdnapi.ServiceProtocol(kServ.Spec.Ports[i].Protocol),
					Port:      uint(kServ.Spec.Ports[i].Port),
				}
				receiver <- &osdnapi.ServiceEvent{Type: osdnapi.Deleted, Service: oServ}
			}
		case watch.Error:
			// Check if the namespace is dead, if so quit
			_, err = oi.kClient.Namespaces().Get(namespace)
			if err != nil {
				break
			}
		}
	}
}