|
| 1 | +package node |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + ktypes "k8s.io/apimachinery/pkg/types" |
| 10 | + |
| 11 | + networkapi "github.com/openshift/origin/pkg/network/apis/network" |
| 12 | + "github.com/openshift/origin/pkg/network/common" |
| 13 | +) |
| 14 | + |
| 15 | +func assertHostSubnetFlowChanges(hsw *hostSubnetWatcher, flows *[]string, changes ...flowChange) error { |
| 16 | + oldFlows := *flows |
| 17 | + newFlows, err := hsw.oc.ovs.DumpFlows("") |
| 18 | + if err != nil { |
| 19 | + return fmt.Errorf("unexpected error dumping OVS flows: %v", err) |
| 20 | + } |
| 21 | + |
| 22 | + err = assertFlowChanges(oldFlows, newFlows, changes...) |
| 23 | + if err != nil { |
| 24 | + return fmt.Errorf("unexpected flow changes: %v\nOrig:\n%s\nNew:\n%s", err, |
| 25 | + strings.Join(oldFlows, "\n"), strings.Join(newFlows, "\n")) |
| 26 | + } |
| 27 | + |
| 28 | + *flows = newFlows |
| 29 | + return nil |
| 30 | +} |
| 31 | + |
| 32 | +func setupHostSubnetWatcher(t *testing.T) (*hostSubnetWatcher, []string) { |
| 33 | + _, oc, _ := setupOVSController(t) |
| 34 | + |
| 35 | + networkInfo, err := common.ParseNetworkInfo( |
| 36 | + []networkapi.ClusterNetworkEntry{ |
| 37 | + { |
| 38 | + CIDR: "10.128.0.0/14", |
| 39 | + HostSubnetLength: 9, |
| 40 | + }, |
| 41 | + }, |
| 42 | + "172.30.0.0/16", |
| 43 | + ) |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("unexpected error parsing network info: %v", err) |
| 46 | + } |
| 47 | + |
| 48 | + hsw := newHostSubnetWatcher(oc, oc.localIP, networkInfo) |
| 49 | + |
| 50 | + flows, err := hsw.oc.ovs.DumpFlows("") |
| 51 | + if err != nil { |
| 52 | + t.Fatalf("unexpected error dumping OVS flows: %v", err) |
| 53 | + } |
| 54 | + |
| 55 | + return hsw, flows |
| 56 | +} |
| 57 | + |
| 58 | +func makeHostSubnet(name, hostIP, subnet string) *networkapi.HostSubnet { |
| 59 | + return &networkapi.HostSubnet{ |
| 60 | + TypeMeta: metav1.TypeMeta{ |
| 61 | + Kind: "HostSubnet", |
| 62 | + }, |
| 63 | + ObjectMeta: metav1.ObjectMeta{ |
| 64 | + Name: name, |
| 65 | + UID: ktypes.UID(name + "-uid"), |
| 66 | + }, |
| 67 | + Host: name, |
| 68 | + HostIP: hostIP, |
| 69 | + Subnet: subnet, |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestHostSubnetWatcher(t *testing.T) { |
| 74 | + hsw, flows := setupHostSubnetWatcher(t) |
| 75 | + |
| 76 | + hs1 := makeHostSubnet("node1", "192.168.0.2", "10.128.0.0/23") |
| 77 | + hs2 := makeHostSubnet("node2", "192.168.1.2", "10.129.0.0/23") |
| 78 | + |
| 79 | + err := hsw.updateHostSubnet(hs1) |
| 80 | + if err != nil { |
| 81 | + t.Fatalf("Unexpected error adding HostSubnet: %v", err) |
| 82 | + } |
| 83 | + err = assertHostSubnetFlowChanges(hsw, &flows, |
| 84 | + flowChange{ |
| 85 | + kind: flowAdded, |
| 86 | + match: []string{"table=10", "tun_src=192.168.0.2"}, |
| 87 | + }, |
| 88 | + flowChange{ |
| 89 | + kind: flowAdded, |
| 90 | + match: []string{"table=50", "arp", "arp_tpa=10.128.0.0/23", "192.168.0.2->tun_dst"}, |
| 91 | + }, |
| 92 | + flowChange{ |
| 93 | + kind: flowAdded, |
| 94 | + match: []string{"table=90", "ip", "nw_dst=10.128.0.0/23", "192.168.0.2->tun_dst"}, |
| 95 | + }, |
| 96 | + flowChange{ |
| 97 | + kind: flowRemoved, |
| 98 | + match: []string{"table=111", "goto_table:120"}, |
| 99 | + noMatch: []string{"->tun_dst"}, |
| 100 | + }, |
| 101 | + flowChange{ |
| 102 | + kind: flowAdded, |
| 103 | + match: []string{"table=111", "192.168.0.2->tun_dst"}, |
| 104 | + }, |
| 105 | + ) |
| 106 | + if err != nil { |
| 107 | + t.Fatalf("%v", err) |
| 108 | + } |
| 109 | + |
| 110 | + err = hsw.updateHostSubnet(hs2) |
| 111 | + if err != nil { |
| 112 | + t.Fatalf("Unexpected error adding HostSubnet: %v", err) |
| 113 | + } |
| 114 | + err = assertHostSubnetFlowChanges(hsw, &flows, |
| 115 | + flowChange{ |
| 116 | + kind: flowAdded, |
| 117 | + match: []string{"table=10", "tun_src=192.168.1.2"}, |
| 118 | + }, |
| 119 | + flowChange{ |
| 120 | + kind: flowAdded, |
| 121 | + match: []string{"table=50", "arp", "arp_tpa=10.129.0.0/23", "192.168.1.2->tun_dst"}, |
| 122 | + }, |
| 123 | + flowChange{ |
| 124 | + kind: flowAdded, |
| 125 | + match: []string{"table=90", "ip", "nw_dst=10.129.0.0/23", "192.168.1.2->tun_dst"}, |
| 126 | + }, |
| 127 | + flowChange{ |
| 128 | + kind: flowRemoved, |
| 129 | + match: []string{"table=111", "192.168.0.2->tun_dst"}, |
| 130 | + }, |
| 131 | + flowChange{ |
| 132 | + kind: flowAdded, |
| 133 | + match: []string{"table=111", "192.168.0.2->tun_dst", "192.168.1.2->tun_dst"}, |
| 134 | + }, |
| 135 | + ) |
| 136 | + if err != nil { |
| 137 | + t.Fatalf("%v", err) |
| 138 | + } |
| 139 | + |
| 140 | + err = hsw.deleteHostSubnet(hs1) |
| 141 | + if err != nil { |
| 142 | + t.Fatalf("Unexpected error deleting HostSubnet: %v", err) |
| 143 | + } |
| 144 | + err = assertHostSubnetFlowChanges(hsw, &flows, |
| 145 | + flowChange{ |
| 146 | + kind: flowRemoved, |
| 147 | + match: []string{"table=10", "tun_src=192.168.0.2"}, |
| 148 | + }, |
| 149 | + flowChange{ |
| 150 | + kind: flowRemoved, |
| 151 | + match: []string{"table=50", "arp", "arp_tpa=10.128.0.0/23", "192.168.0.2->tun_dst"}, |
| 152 | + }, |
| 153 | + flowChange{ |
| 154 | + kind: flowRemoved, |
| 155 | + match: []string{"table=90", "ip", "nw_dst=10.128.0.0/23", "192.168.0.2->tun_dst"}, |
| 156 | + }, |
| 157 | + flowChange{ |
| 158 | + kind: flowRemoved, |
| 159 | + match: []string{"table=111", "192.168.0.2->tun_dst", "192.168.1.2->tun_dst"}, |
| 160 | + }, |
| 161 | + flowChange{ |
| 162 | + kind: flowAdded, |
| 163 | + match: []string{"table=111", "192.168.1.2->tun_dst"}, |
| 164 | + noMatch: []string{"192.168.0.2"}, |
| 165 | + }, |
| 166 | + ) |
| 167 | + if err != nil { |
| 168 | + t.Fatalf("%v", err) |
| 169 | + } |
| 170 | + |
| 171 | + err = hsw.deleteHostSubnet(hs2) |
| 172 | + if err != nil { |
| 173 | + t.Fatalf("Unexpected error deleting HostSubnet: %v", err) |
| 174 | + } |
| 175 | + err = assertHostSubnetFlowChanges(hsw, &flows, |
| 176 | + flowChange{ |
| 177 | + kind: flowRemoved, |
| 178 | + match: []string{"table=10", "tun_src=192.168.1.2"}, |
| 179 | + }, |
| 180 | + flowChange{ |
| 181 | + kind: flowRemoved, |
| 182 | + match: []string{"table=50", "arp", "arp_tpa=10.129.0.0/23", "192.168.1.2->tun_dst"}, |
| 183 | + }, |
| 184 | + flowChange{ |
| 185 | + kind: flowRemoved, |
| 186 | + match: []string{"table=90", "ip", "nw_dst=10.129.0.0/23", "192.168.1.2->tun_dst"}, |
| 187 | + }, |
| 188 | + flowChange{ |
| 189 | + kind: flowRemoved, |
| 190 | + match: []string{"table=111", "192.168.1.2->tun_dst"}, |
| 191 | + }, |
| 192 | + flowChange{ |
| 193 | + kind: flowAdded, |
| 194 | + match: []string{"table=111", "goto_table:120"}, |
| 195 | + noMatch: []string{"tun_dst"}, |
| 196 | + }, |
| 197 | + ) |
| 198 | + if err != nil { |
| 199 | + t.Fatalf("%v", err) |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +func TestHostSubnetReassignment(t *testing.T) { |
| 204 | + hsw, flows := setupHostSubnetWatcher(t) |
| 205 | + |
| 206 | + hs1orig := makeHostSubnet("node1", "192.168.0.2", "10.128.0.0/23") |
| 207 | + hs2orig := makeHostSubnet("node2", "192.168.1.2", "10.129.0.0/23") |
| 208 | + |
| 209 | + // Create original HostSubnets |
| 210 | + |
| 211 | + err := hsw.updateHostSubnet(hs1orig) |
| 212 | + if err != nil { |
| 213 | + t.Fatalf("Unexpected error adding HostSubnet: %v", err) |
| 214 | + } |
| 215 | + err = hsw.updateHostSubnet(hs2orig) |
| 216 | + if err != nil { |
| 217 | + t.Fatalf("Unexpected error adding HostSubnet: %v", err) |
| 218 | + } |
| 219 | + |
| 220 | + err = assertHostSubnetFlowChanges(hsw, &flows, |
| 221 | + flowChange{ |
| 222 | + kind: flowAdded, |
| 223 | + match: []string{"table=10", "tun_src=192.168.0.2"}, |
| 224 | + }, |
| 225 | + flowChange{ |
| 226 | + kind: flowAdded, |
| 227 | + match: []string{"table=10", "tun_src=192.168.1.2"}, |
| 228 | + }, |
| 229 | + flowChange{ |
| 230 | + kind: flowAdded, |
| 231 | + match: []string{"table=50", "arp", "arp_tpa=10.128.0.0/23", "192.168.0.2->tun_dst"}, |
| 232 | + }, |
| 233 | + flowChange{ |
| 234 | + kind: flowAdded, |
| 235 | + match: []string{"table=50", "arp", "arp_tpa=10.129.0.0/23", "192.168.1.2->tun_dst"}, |
| 236 | + }, |
| 237 | + flowChange{ |
| 238 | + kind: flowAdded, |
| 239 | + match: []string{"table=90", "ip", "nw_dst=10.128.0.0/23", "192.168.0.2->tun_dst"}, |
| 240 | + }, |
| 241 | + flowChange{ |
| 242 | + kind: flowAdded, |
| 243 | + match: []string{"table=90", "ip", "nw_dst=10.129.0.0/23", "192.168.1.2->tun_dst"}, |
| 244 | + }, |
| 245 | + flowChange{ |
| 246 | + kind: flowRemoved, |
| 247 | + match: []string{"table=111", "goto_table:120"}, |
| 248 | + noMatch: []string{"->tun_dst"}, |
| 249 | + }, |
| 250 | + flowChange{ |
| 251 | + kind: flowAdded, |
| 252 | + match: []string{"table=111", "192.168.0.2->tun_dst", "192.168.1.2->tun_dst"}, |
| 253 | + }, |
| 254 | + ) |
| 255 | + if err != nil { |
| 256 | + t.Fatalf("%v", err) |
| 257 | + } |
| 258 | + |
| 259 | + // Now both nodes go offline (without their Node objects being deleted), reboot and |
| 260 | + // get assigned the opposite IPs after reboot. They reregister with the master, which |
| 261 | + // updates their Node IPs, which causes the SDN master to update their HostSubnets. |
| 262 | + // After the first update, we'll have two HostSubnets with the same HostIP, which |
| 263 | + // used to cause us to break things when we got the second update. |
| 264 | + |
| 265 | + hs1new := hs1orig.DeepCopy() |
| 266 | + hs1new.HostIP = hs2orig.HostIP |
| 267 | + hs2new := hs2orig.DeepCopy() |
| 268 | + hs2new.HostIP = hs1orig.HostIP |
| 269 | + |
| 270 | + err = hsw.updateHostSubnet(hs1new) |
| 271 | + if err != nil { |
| 272 | + t.Fatalf("Unexpected error adding HostSubnet: %v", err) |
| 273 | + } |
| 274 | + err = hsw.updateHostSubnet(hs2new) |
| 275 | + if err != nil { |
| 276 | + t.Fatalf("Unexpected error adding HostSubnet: %v", err) |
| 277 | + } |
| 278 | + |
| 279 | + err = assertHostSubnetFlowChanges(hsw, &flows, |
| 280 | + // (We have to check for these table=10 removes+adds because they're not |
| 281 | + // actually identical; the cookies will have changed.) |
| 282 | + flowChange{ |
| 283 | + kind: flowRemoved, |
| 284 | + match: []string{"table=10", "tun_src=192.168.0.2"}, |
| 285 | + }, |
| 286 | + flowChange{ |
| 287 | + kind: flowAdded, |
| 288 | + match: []string{"table=10", "tun_src=192.168.0.2"}, |
| 289 | + }, |
| 290 | + flowChange{ |
| 291 | + kind: flowRemoved, |
| 292 | + match: []string{"table=10", "tun_src=192.168.1.2"}, |
| 293 | + }, |
| 294 | + flowChange{ |
| 295 | + kind: flowAdded, |
| 296 | + match: []string{"table=10", "tun_src=192.168.1.2"}, |
| 297 | + }, |
| 298 | + |
| 299 | + flowChange{ |
| 300 | + kind: flowRemoved, |
| 301 | + match: []string{"table=50", "arp", "arp_tpa=10.128.0.0/23", "192.168.0.2->tun_dst"}, |
| 302 | + }, |
| 303 | + flowChange{ |
| 304 | + kind: flowAdded, |
| 305 | + match: []string{"table=50", "arp", "arp_tpa=10.128.0.0/23", "192.168.1.2->tun_dst"}, |
| 306 | + }, |
| 307 | + |
| 308 | + flowChange{ |
| 309 | + kind: flowRemoved, |
| 310 | + match: []string{"table=50", "arp", "arp_tpa=10.129.0.0/23", "192.168.1.2->tun_dst"}, |
| 311 | + }, |
| 312 | + flowChange{ |
| 313 | + kind: flowAdded, |
| 314 | + match: []string{"table=50", "arp", "arp_tpa=10.129.0.0/23", "192.168.0.2->tun_dst"}, |
| 315 | + }, |
| 316 | + |
| 317 | + flowChange{ |
| 318 | + kind: flowRemoved, |
| 319 | + match: []string{"table=90", "ip", "nw_dst=10.128.0.0/23", "192.168.0.2->tun_dst"}, |
| 320 | + }, |
| 321 | + flowChange{ |
| 322 | + kind: flowAdded, |
| 323 | + match: []string{"table=90", "ip", "nw_dst=10.128.0.0/23", "192.168.1.2->tun_dst"}, |
| 324 | + }, |
| 325 | + |
| 326 | + flowChange{ |
| 327 | + kind: flowRemoved, |
| 328 | + match: []string{"table=90", "ip", "nw_dst=10.129.0.0/23", "192.168.1.2->tun_dst"}, |
| 329 | + }, |
| 330 | + flowChange{ |
| 331 | + kind: flowAdded, |
| 332 | + match: []string{"table=90", "ip", "nw_dst=10.129.0.0/23", "192.168.0.2->tun_dst"}, |
| 333 | + }, |
| 334 | + ) |
| 335 | + if err != nil { |
| 336 | + t.Fatalf("%v", err) |
| 337 | + } |
| 338 | +} |
0 commit comments