func (oi *OsdnRegistryInterface) WatchNodes(receiver chan *osdnapi.NodeEvent, stop chan bool) error { nodeEventQueue := oscache.NewEventQueue(cache.MetaNamespaceKeyFunc) listWatch := &cache.ListWatch{ ListFunc: func() (runtime.Object, error) { return oi.kClient.Nodes().List(labels.Everything(), fields.Everything()) }, WatchFunc: func(resourceVersion string) (watch.Interface, error) { return oi.kClient.Nodes().Watch(labels.Everything(), fields.Everything(), resourceVersion) }, } cache.NewReflector(listWatch, &kapi.Node{}, nodeEventQueue, 4*time.Minute).Run() nodeAddressMap, err := oi.getNodeAddressMap() if err != nil { return err } for { eventType, obj, err := nodeEventQueue.Pop() if err != nil { return err } node := obj.(*kapi.Node) nodeIP := "" if len(node.Status.Addresses) > 0 { nodeIP = node.Status.Addresses[0].Address } else { nodeIP, err = osdn.GetNodeIP(node.ObjectMeta.Name) if err != nil { return err } } switch eventType { case watch.Added: receiver <- &osdnapi.NodeEvent{Type: osdnapi.Added, Node: osdnapi.Node{Name: node.ObjectMeta.Name, IP: nodeIP}} nodeAddressMap[node.ObjectMeta.UID] = nodeIP case watch.Modified: oldNodeIP, ok := nodeAddressMap[node.ObjectMeta.UID] if ok && oldNodeIP != nodeIP { // Node Added event will handle update subnet if there is ip mismatch receiver <- &osdnapi.NodeEvent{Type: osdnapi.Added, Node: osdnapi.Node{Name: node.ObjectMeta.Name, IP: nodeIP}} nodeAddressMap[node.ObjectMeta.UID] = nodeIP } 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. receiver <- &osdnapi.NodeEvent{Type: osdnapi.Deleted, Node: osdnapi.Node{Name: node.ObjectMeta.Name}} delete(nodeAddressMap, node.ObjectMeta.UID) } } }
func (oi *OsdnRegistryInterface) WatchNodes(receiver chan<- *osdnapi.NodeEvent, ready chan<- bool, start <-chan string, stop <-chan bool) error { eventQueue, startVersion := oi.createAndRunEventQueue("Node", nil, ready, start) nodeAddressMap, err := oi.getNodeAddressMap() if err != nil { return err } checkCondition := true for { eventType, obj, err := getEvent(eventQueue, startVersion, &checkCondition) if err != nil { return err } node := obj.(*kapi.Node) nodeIP := "" if len(node.Status.Addresses) > 0 { nodeIP = node.Status.Addresses[0].Address } else { nodeIP, err = osdn.GetNodeIP(node.ObjectMeta.Name) if err != nil { return err } } switch eventType { case watch.Added: receiver <- &osdnapi.NodeEvent{Type: osdnapi.Added, Node: osdnapi.Node{Name: node.ObjectMeta.Name, IP: nodeIP}} nodeAddressMap[node.ObjectMeta.UID] = nodeIP case watch.Modified: oldNodeIP, ok := nodeAddressMap[node.ObjectMeta.UID] if ok && oldNodeIP != nodeIP { // Node Added event will handle update subnet if there is ip mismatch receiver <- &osdnapi.NodeEvent{Type: osdnapi.Added, Node: osdnapi.Node{Name: node.ObjectMeta.Name, IP: nodeIP}} nodeAddressMap[node.ObjectMeta.UID] = nodeIP } case watch.Deleted: receiver <- &osdnapi.NodeEvent{Type: osdnapi.Deleted, Node: osdnapi.Node{Name: node.ObjectMeta.Name}} delete(nodeAddressMap, node.ObjectMeta.UID) } } }
func (oi *OsdnRegistryInterface) GetNodes() ([]osdnapi.Node, error) { knodes, err := oi.kClient.Nodes().List(labels.Everything(), fields.Everything()) if err != nil { return nil, err } nodes := make([]osdnapi.Node, 0, len(knodes.Items)) for _, node := range knodes.Items { var nodeIP string if len(node.Status.Addresses) > 0 { nodeIP = node.Status.Addresses[0].Address } else { var err error nodeIP, err = osdn.GetNodeIP(node.ObjectMeta.Name) if err != nil { return nil, err } } nodes = append(nodes, osdnapi.Node{Name: node.ObjectMeta.Name, IP: nodeIP}) } return nodes, nil }