@@ -48,33 +48,38 @@ func (master *OsdnMaster) SubnetStartMaster(clusterNetwork *net.IPNet, hostSubne
48
48
return nil
49
49
}
50
50
51
- func (master * OsdnMaster ) addNode (nodeName string , nodeIP string , hsAnnotations map [string ]string ) error {
51
+ // addNode takes the nodeName, a preferred nodeIP, the node's annotations and other valid ip addresses
52
+ // Creates or updates a HostSubnet if needed
53
+ // Returns the IP address used for hostsubnet (either the preferred or one from the otherValidAddresses) and any error
54
+ func (master * OsdnMaster ) addNode (nodeName string , nodeIP string , hsAnnotations map [string ]string , otherValidAddresses []kapi.NodeAddress ) (string , error ) {
52
55
// Validate node IP before proceeding
53
56
if err := master .networkInfo .validateNodeIP (nodeIP ); err != nil {
54
- return err
57
+ return "" , err
55
58
}
56
59
57
60
// Check if subnet needs to be created or updated
58
61
sub , err := master .osClient .HostSubnets ().Get (nodeName )
59
62
if err == nil {
60
63
if sub .HostIP == nodeIP {
61
- return nil
64
+ return nodeIP , nil
65
+ } else if isValidNodeIP (otherValidAddresses , sub .HostIP ) {
66
+ return sub .HostIP , nil
62
67
} else {
63
68
// Node IP changed, update old subnet
64
69
sub .HostIP = nodeIP
65
70
sub , err = master .osClient .HostSubnets ().Update (sub )
66
71
if err != nil {
67
- return fmt .Errorf ("error updating subnet %s for node %s: %v" , sub .Subnet , nodeName , err )
72
+ return "" , fmt .Errorf ("error updating subnet %s for node %s: %v" , sub .Subnet , nodeName , err )
68
73
}
69
74
log .Infof ("Updated HostSubnet %s" , hostSubnetToString (sub ))
70
- return nil
75
+ return nodeIP , nil
71
76
}
72
77
}
73
78
74
79
// Create new subnet
75
80
sn , err := master .subnetAllocator .GetNetwork ()
76
81
if err != nil {
77
- return fmt .Errorf ("error allocating network for node %s: %v" , nodeName , err )
82
+ return "" , fmt .Errorf ("error allocating network for node %s: %v" , nodeName , err )
78
83
}
79
84
80
85
sub = & osapi.HostSubnet {
@@ -87,10 +92,10 @@ func (master *OsdnMaster) addNode(nodeName string, nodeIP string, hsAnnotations
87
92
sub , err = master .osClient .HostSubnets ().Create (sub )
88
93
if err != nil {
89
94
master .subnetAllocator .ReleaseNetwork (sn )
90
- return fmt .Errorf ("error creating subnet %s for node %s: %v" , sn .String (), nodeName , err )
95
+ return "" , fmt .Errorf ("error creating subnet %s for node %s: %v" , sn .String (), nodeName , err )
91
96
}
92
97
log .Infof ("Created HostSubnet %s" , hostSubnetToString (sub ))
93
- return nil
98
+ return nodeIP , nil
94
99
}
95
100
96
101
func (master * OsdnMaster ) deleteNode (nodeName string ) error {
@@ -107,8 +112,8 @@ func (master *OsdnMaster) deleteNode(nodeName string) error {
107
112
return nil
108
113
}
109
114
110
- func isValidNodeIP (node * kapi.Node , nodeIP string ) bool {
111
- for _ , addr := range node . Status . Addresses {
115
+ func isValidNodeIP (validAddresses [] kapi.NodeAddress , nodeIP string ) bool {
116
+ for _ , addr := range validAddresses {
112
117
if addr .Address == nodeIP {
113
118
return true
114
119
}
@@ -180,17 +185,17 @@ func (master *OsdnMaster) watchNodes() {
180
185
case cache .Sync , cache .Added , cache .Updated :
181
186
master .clearInitialNodeNetworkUnavailableCondition (node )
182
187
183
- if oldNodeIP , ok := nodeAddressMap [uid ]; ok && ((nodeIP == oldNodeIP ) || isValidNodeIP (node , oldNodeIP )) {
188
+ if oldNodeIP , ok := nodeAddressMap [uid ]; ok && ((nodeIP == oldNodeIP ) || isValidNodeIP (node . Status . Addresses , oldNodeIP )) {
184
189
break
185
190
}
186
191
// Node status is frequently updated by kubelet, so log only if the above condition is not met
187
192
log .V (5 ).Infof ("Watch %s event for Node %q" , delta .Type , name )
188
193
189
- err = master .addNode (name , nodeIP , nil )
194
+ usedNodeIP , err : = master .addNode (name , nodeIP , nil , node . Status . Addresses )
190
195
if err != nil {
191
196
return fmt .Errorf ("error creating subnet for node %s, ip %s: %v" , name , nodeIP , err )
192
197
}
193
- nodeAddressMap [uid ] = nodeIP
198
+ nodeAddressMap [uid ] = usedNodeIP
194
199
case cache .Deleted :
195
200
log .V (5 ).Infof ("Watch %s event for Node %q" , delta .Type , name )
196
201
delete (nodeAddressMap , uid )
@@ -242,7 +247,7 @@ func (master *OsdnMaster) watchSubnets() {
242
247
log .Errorf ("Vnid %s is an invalid value for annotation %s. Annotation will be ignored." , vnid , osapi .FixedVNIDHostAnnotation )
243
248
}
244
249
}
245
- err = master .addNode (name , hostIP , hsAnnotations )
250
+ _ , err = master .addNode (name , hostIP , hsAnnotations , nil )
246
251
if err != nil {
247
252
log .Errorf ("Error creating subnet for node %s, ip %s: %v" , name , hostIP , err )
248
253
return nil
0 commit comments