Update subnet openflow rules in case of host ip change
@@ -214,32 +214,44 @@ func (oc *OvsController) ServeExistingMinions() error {
return nil
}
+func (oc *OvsController) getNodeIP(node string) (string, error) {
+ ip := net.ParseIP(node)
+ if ip == nil {
+ addrs, err := net.LookupIP(minion)
+ if err != nil {
+ log.Errorf("Failed to lookup IP address for minion %s: %v", minion, err)
+ return "", err
+ }
+ for _, addr := range addrs {
+ if addr.String() != "127.0.0.1" {
+ ip = addr
+ break
+ }
+ }
+ }
+ if ip == nil || len(ip.String()) == 0 {
+ return "", fmt.Errorf("Failed to obtain IP address from node label: %s", node)
+ }
+ return ip.String(), nil
+}
+
func (oc *OvsController) AddNode(minion string) error {
sn, err := oc.subnetAllocator.GetNetwork()
if err != nil {
log.Errorf("Error creating network for minion %s.", minion)
return err
}
- var minionIP string
- ip := net.ParseIP(minion)
- if ip == nil {
- addrs, err := net.LookupIP(minion)
+
+ minionIP, err := oc.getMinionIP(minion)
if err != nil {
- log.Errorf("Failed to lookup IP address for minion %s: %v", minion, err)
return err
}
- minionIP = addrs[0].String()
- if minionIP == "" {
- return fmt.Errorf("Failed to obtain IP address from minion label: %s", minion)
- }
- } else {
- minionIP = ip.String()
- }
+
sub := &api.Subnet{
Minion: minionIP,
Sub: sn.String(),
}
- oc.subnetRegistry.CreateSubnet(minion, sub)
+ err = oc.subnetRegistry.CreateSubnet(minion, sub)
if err != nil {
log.Errorf("Error writing subnet to etcd for minion %s: %v", minion, sn)
return err
@@ -378,10 +390,30 @@ func (oc *OvsController) watchMinions() {
case ev := <-minevent:
switch ev.Type {
case api.Added:
- _, err := oc.subnetRegistry.GetSubnet(ev.Minion)
+ sub, err := oc.subnetRegistry.GetSubnet(ev.Minion)
if err != nil {
// subnet does not exist already
oc.AddNode(ev.Minion)
+ } else {
+ // get IP of the minion
+ ip, err := oc.getMinionIP(ev.Minion)
+ if err != nil {
+ log.Errorf("Error calculating IP address of node %s", ev.Minion)
+ continue
+ }
+ if sub.Minion != ip {
+ err = oc.subnetRegistry.DeleteSubnet(ev.Minion)
+ if err != nil {
+ log.Errorf("Error deleting subnet for node %s, old ip %s", ev.Minion, sub.Minion)
+ continue
+ }
+ sub.Minion = ip
+ err = oc.subnetRegistry.CreateSubnet(ev.Minion, sub)
+ if err != nil {
+ log.Errorf("Error creating subnet for node %s, ip %s", ev.Minion, sub.Minion)
+ continue
+ }
+ }
}
case api.Deleted:
oc.DeleteNode(ev.Minion)
