| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package knftables |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"fmt" |
| 22 |
"reflect" |
| 23 |
"sort" |
| 24 |
"strings" |
| 25 |
"testing" |
| 26 |
|
| 27 |
"github.com/google/go-cmp/cmp" |
| 28 |
"github.com/lithammer/dedent" |
| 29 |
) |
| 30 |
|
| 31 |
func TestFakeRun(t *testing.T) { |
| 32 |
fake := NewFake(IPv4Family, "kube-proxy") |
| 33 |
|
| 34 |
_, err := fake.List(context.Background(), "chains") |
| 35 |
if err == nil || !IsNotFound(err) { |
| 36 |
t.Errorf("expected table not found error but got: %v", err) |
| 37 |
} |
| 38 |
|
| 39 |
tx := fake.NewTransaction() |
| 40 |
|
| 41 |
tx.Add(&Table{}) |
| 42 |
tx.Add(&Chain{ |
| 43 |
Name: "chain", |
| 44 |
Comment: PtrTo("foo"), |
| 45 |
}) |
| 46 |
tx.Add(&Rule{ |
| 47 |
Chain: "chain", |
| 48 |
Rule: "ip daddr 10.0.0.0/8 drop", |
| 49 |
}) |
| 50 |
tx.Add(&Rule{ |
| 51 |
Chain: "chain", |
| 52 |
Rule: "masquerade", |
| 53 |
Comment: PtrTo("comment"), |
| 54 |
}) |
| 55 |
|
| 56 |
tx.Add(&Chain{ |
| 57 |
Name: "anotherchain", |
| 58 |
}) |
| 59 |
tx.Add(&Rule{ |
| 60 |
Chain: "anotherchain", |
| 61 |
Rule: "ip saddr 1.2.3.4 drop", |
| 62 |
Comment: PtrTo("drop rule"), |
| 63 |
}) |
| 64 |
tx.Add(&Rule{ |
| 65 |
Chain: "anotherchain", |
| 66 |
Rule: "ip daddr 5.6.7.8 reject", |
| 67 |
Comment: PtrTo("reject rule"), |
| 68 |
}) |
| 69 |
|
| 70 |
tx.Add(&Map{ |
| 71 |
Name: "map1", |
| 72 |
Type: "ipv4_addr . inet_proto . inet_service : verdict", |
| 73 |
}) |
| 74 |
tx.Add(&Element{ |
| 75 |
Map: "map1", |
| 76 |
Key: []string{"192.168.0.1", "tcp", "80"}, |
| 77 |
Value: []string{"goto chain"}, |
| 78 |
}) |
| 79 |
tx.Add(&Element{ |
| 80 |
Map: "map1", |
| 81 |
Key: []string{"192.168.0.2", "tcp", "443"}, |
| 82 |
Value: []string{"goto anotherchain"}, |
| 83 |
Comment: PtrTo("with a comment"), |
| 84 |
}) |
| 85 |
|
| 86 |
tx.Add(&Element{ |
| 87 |
Map: "map1", |
| 88 |
Key: []string{"192.168.0.1", "tcp", "80"}, |
| 89 |
Value: []string{"drop"}, |
| 90 |
}) |
| 91 |
tx.Add(&Flowtable{ |
| 92 |
Name: "myflowtable", |
| 93 |
Devices: []string{"eth0", "eth1"}, |
| 94 |
}) |
| 95 |
|
| 96 |
|
| 97 |
expected := strings.TrimPrefix(dedent.Dedent(` |
| 98 |
add table ip kube-proxy |
| 99 |
add chain ip kube-proxy chain { comment "foo" ; } |
| 100 |
add rule ip kube-proxy chain ip daddr 10.0.0.0/8 drop |
| 101 |
add rule ip kube-proxy chain masquerade comment "comment" |
| 102 |
add chain ip kube-proxy anotherchain |
| 103 |
add rule ip kube-proxy anotherchain ip saddr 1.2.3.4 drop comment "drop rule" |
| 104 |
add rule ip kube-proxy anotherchain ip daddr 5.6.7.8 reject comment "reject rule" |
| 105 |
add map ip kube-proxy map1 { type ipv4_addr . inet_proto . inet_service : verdict ; } |
| 106 |
add element ip kube-proxy map1 { 192.168.0.1 . tcp . 80 : goto chain } |
| 107 |
add element ip kube-proxy map1 { 192.168.0.2 . tcp . 443 comment "with a comment" : goto anotherchain } |
| 108 |
add element ip kube-proxy map1 { 192.168.0.1 . tcp . 80 : drop } |
| 109 |
add flowtable ip kube-proxy myflowtable { devices = { eth0, eth1 } ; } |
| 110 |
`), "\n") |
| 111 |
diff := cmp.Diff(expected, tx.String()) |
| 112 |
if diff != "" { |
| 113 |
t.Errorf("unexpected transaction content:\n%s", diff) |
| 114 |
} |
| 115 |
|
| 116 |
err = fake.Run(context.Background(), tx) |
| 117 |
if err != nil { |
| 118 |
t.Fatalf("unexpected error from Run: %v", err) |
| 119 |
} |
| 120 |
|
| 121 |
if fake.Table == nil { |
| 122 |
t.Fatalf("fake.Table is nil") |
| 123 |
} |
| 124 |
|
| 125 |
chain := fake.Table.Chains["chain"] |
| 126 |
if chain == nil || len(fake.Table.Chains) != 2 { |
| 127 |
t.Fatalf("unexpected contents of table.Chains: %+v", fake.Table.Chains) |
| 128 |
} |
| 129 |
|
| 130 |
if len(chain.Rules) != 2 { |
| 131 |
t.Fatalf("unexpected chain.Rules length: expected 2, got %d", len(chain.Rules)) |
| 132 |
} |
| 133 |
expectedRule := "ip daddr 10.0.0.0/8 drop" |
| 134 |
if chain.Rules[0].Rule != expectedRule { |
| 135 |
t.Fatalf("unexpected chain.Rules content: expected %q, got %q", expectedRule, chain.Rules[0].Rule) |
| 136 |
} |
| 137 |
expectedRule = "masquerade" |
| 138 |
if chain.Rules[1].Rule != expectedRule { |
| 139 |
t.Fatalf("unexpected chain.Rules content: expected %q, got %q", expectedRule, chain.Rules[1].Rule) |
| 140 |
} |
| 141 |
expectedComment := "comment" |
| 142 |
if chain.Rules[1].Comment == nil { |
| 143 |
t.Fatalf("unexpected chain.Rules content: expected comment %q, got nil", expectedComment) |
| 144 |
} else if *chain.Rules[1].Comment != expectedComment { |
| 145 |
t.Fatalf("unexpected chain.Rules content: expected comment %q, got %q", expectedComment, *chain.Rules[1].Comment) |
| 146 |
} |
| 147 |
|
| 148 |
ruleToDelete := chain.Rules[1] |
| 149 |
|
| 150 |
m := fake.Table.Maps["map1"] |
| 151 |
if m == nil || len(fake.Table.Maps) != 1 { |
| 152 |
t.Fatalf("unexpected contents of table.Maps: %+v", fake.Table.Maps) |
| 153 |
} |
| 154 |
|
| 155 |
elem := m.FindElement("192.168.0.2", "tcp", "443") |
| 156 |
if elem == nil { |
| 157 |
t.Fatalf("missing map element for key \"192.168.0.2 . tcp . 443\"") |
| 158 |
} else if len(elem.Value) != 1 || elem.Value[0] != "goto anotherchain" { |
| 159 |
t.Fatalf("unexpected map element for key \"192.168.0.2 . tcp . 443\": %+v", elem) |
| 160 |
} |
| 161 |
|
| 162 |
elem = m.FindElement("192.168.0.1", "tcp", "80") |
| 163 |
if elem == nil { |
| 164 |
t.Fatalf("missing map element for key \"192.168.0.1 . tcp . 80\"") |
| 165 |
} else if len(elem.Value) == 1 && elem.Value[0] == "goto chain" { |
| 166 |
t.Fatalf("map element for key \"192.168.0.1 . tcp . 80\" did not get overwritten") |
| 167 |
} else if len(elem.Value) != 1 || elem.Value[0] != "drop" { |
| 168 |
t.Fatalf("unexpected map element for key \"192.168.0.1 . tcp . 80\": %+v", elem) |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
expected = strings.TrimPrefix(dedent.Dedent(` |
| 175 |
add table ip kube-proxy |
| 176 |
add flowtable ip kube-proxy myflowtable { devices = { eth0, eth1 } ; } |
| 177 |
add chain ip kube-proxy anotherchain |
| 178 |
add chain ip kube-proxy chain { comment "foo" ; } |
| 179 |
add map ip kube-proxy map1 { type ipv4_addr . inet_proto . inet_service : verdict ; } |
| 180 |
add rule ip kube-proxy anotherchain ip saddr 1.2.3.4 drop comment "drop rule" |
| 181 |
add rule ip kube-proxy anotherchain ip daddr 5.6.7.8 reject comment "reject rule" |
| 182 |
add rule ip kube-proxy chain ip daddr 10.0.0.0/8 drop |
| 183 |
add rule ip kube-proxy chain masquerade comment "comment" |
| 184 |
add element ip kube-proxy map1 { 192.168.0.1 . tcp . 80 : drop } |
| 185 |
add element ip kube-proxy map1 { 192.168.0.2 . tcp . 443 comment "with a comment" : goto anotherchain } |
| 186 |
`), "\n") |
| 187 |
diff = cmp.Diff(expected, fake.Dump()) |
| 188 |
if diff != "" { |
| 189 |
t.Errorf("unexpected Dump content:\n%s", diff) |
| 190 |
} |
| 191 |
|
| 192 |
chains, err := fake.List(context.Background(), "chains") |
| 193 |
if err != nil { |
| 194 |
t.Errorf("unexpected error listing chains: %v", err) |
| 195 |
} |
| 196 |
|
| 197 |
sort.Strings(chains) |
| 198 |
expectedChains := []string{"chain", "anotherchain"} |
| 199 |
sort.Strings(expectedChains) |
| 200 |
if !reflect.DeepEqual(chains, expectedChains) { |
| 201 |
t.Errorf("unexpected result from List(chains): %v", chains) |
| 202 |
} |
| 203 |
|
| 204 |
tx = fake.NewTransaction() |
| 205 |
tx.Delete(ruleToDelete) |
| 206 |
expected = fmt.Sprintf("delete rule ip kube-proxy chain handle %d\n", *ruleToDelete.Handle) |
| 207 |
diff = cmp.Diff(expected, tx.String()) |
| 208 |
if diff != "" { |
| 209 |
t.Errorf("unexpected transaction content:\n%s", diff) |
| 210 |
} |
| 211 |
err = fake.Run(context.Background(), tx) |
| 212 |
if err != nil { |
| 213 |
t.Fatalf("unexpected error from Run: %v", err) |
| 214 |
} |
| 215 |
expected = strings.TrimPrefix(dedent.Dedent(` |
| 216 |
add table ip kube-proxy |
| 217 |
add flowtable ip kube-proxy myflowtable { devices = { eth0, eth1 } ; } |
| 218 |
add chain ip kube-proxy anotherchain |
| 219 |
add chain ip kube-proxy chain { comment "foo" ; } |
| 220 |
add map ip kube-proxy map1 { type ipv4_addr . inet_proto . inet_service : verdict ; } |
| 221 |
add rule ip kube-proxy anotherchain ip saddr 1.2.3.4 drop comment "drop rule" |
| 222 |
add rule ip kube-proxy anotherchain ip daddr 5.6.7.8 reject comment "reject rule" |
| 223 |
add rule ip kube-proxy chain ip daddr 10.0.0.0/8 drop |
| 224 |
add element ip kube-proxy map1 { 192.168.0.1 . tcp . 80 : drop } |
| 225 |
add element ip kube-proxy map1 { 192.168.0.2 . tcp . 443 comment "with a comment" : goto anotherchain } |
| 226 |
`), "\n") |
| 227 |
diff = cmp.Diff(expected, fake.Dump()) |
| 228 |
if diff != "" { |
| 229 |
t.Errorf("unexpected Dump content:\n%s", diff) |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
tx = fake.NewTransaction() |
| 235 |
tx.Add(&Element{ |
| 236 |
Map: "map1", |
| 237 |
Key: []string{"192.168.0.3", "tcp", "80"}, |
| 238 |
Value: []string{"accept"}, |
| 239 |
}) |
| 240 |
tx.Delete(ruleToDelete) |
| 241 |
tx.Add(&Element{ |
| 242 |
Map: "map1", |
| 243 |
Key: []string{"192.168.0.4", "tcp", "80"}, |
| 244 |
Value: []string{"accept"}, |
| 245 |
}) |
| 246 |
err = fake.Run(context.Background(), tx) |
| 247 |
if err == nil || !IsNotFound(err) { |
| 248 |
t.Fatalf("unexpected error from Run: %v", err) |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
m = fake.Table.Maps["map1"] |
| 253 |
if m == nil || len(fake.Table.Maps) != 1 { |
| 254 |
t.Fatalf("unexpected contents of table.Maps: %+v", fake.Table.Maps) |
| 255 |
} |
| 256 |
|
| 257 |
elem = m.FindElement("192.168.0.3", "tcp", "80") |
| 258 |
if elem != nil { |
| 259 |
t.Errorf("element should not have been added: %+v", elem) |
| 260 |
} |
| 261 |
elem = m.FindElement("192.168.0.4", "tcp", "80") |
| 262 |
if elem != nil { |
| 263 |
t.Errorf("element should not have been added: %+v", elem) |
| 264 |
} |
| 265 |
if len(m.Elements) != 2 { |
| 266 |
t.Errorf("unexpected contents of map1: %+v", m) |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
tx = fake.NewTransaction() |
| 271 |
tx.Delete(&Element{ |
| 272 |
Map: "map1", |
| 273 |
Key: []string{"192.168.0.1", "tcp", "80"}, |
| 274 |
}) |
| 275 |
err = fake.Run(context.Background(), tx) |
| 276 |
if err != nil { |
| 277 |
t.Fatalf("unexpected error from Run: %v", err) |
| 278 |
} |
| 279 |
m = fake.Table.Maps["map1"] |
| 280 |
if len(m.Elements) != 1 { |
| 281 |
t.Errorf("unexpected contents of map1: %+v", m) |
| 282 |
} |
| 283 |
elem = m.FindElement("192.168.0.1", "tcp", "80") |
| 284 |
if elem != nil { |
| 285 |
t.Errorf("element should have been deleted: %+v", elem) |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
tx = fake.NewTransaction() |
| 290 |
tx.Add(&Rule{ |
| 291 |
Chain: "chain", |
| 292 |
Rule: "tcp dport 80 goto missingchain", |
| 293 |
}) |
| 294 |
err = fake.Run(context.Background(), tx) |
| 295 |
if err == nil || !IsNotFound(err) { |
| 296 |
t.Fatalf("unexpected error from Run: %v", err) |
| 297 |
} |
| 298 |
|
| 299 |
tx = fake.NewTransaction() |
| 300 |
tx.Add(&Rule{ |
| 301 |
Chain: "chain", |
| 302 |
Rule: "tcp dport 80 vmap @missingmap", |
| 303 |
}) |
| 304 |
err = fake.Run(context.Background(), tx) |
| 305 |
if err == nil || !IsNotFound(err) { |
| 306 |
t.Fatalf("unexpected error from Run: %v", err) |
| 307 |
} |
| 308 |
|
| 309 |
tx = fake.NewTransaction() |
| 310 |
tx.Add(&Rule{ |
| 311 |
Chain: "chain", |
| 312 |
Rule: "tcp dport @missingset drop", |
| 313 |
}) |
| 314 |
err = fake.Run(context.Background(), tx) |
| 315 |
if err == nil || !IsNotFound(err) { |
| 316 |
t.Fatalf("unexpected error from Run: %v", err) |
| 317 |
} |
| 318 |
|
| 319 |
tx = fake.NewTransaction() |
| 320 |
tx.Add(&Element{ |
| 321 |
Map: "map1", |
| 322 |
Key: []string{"192.168.0.5", "tcp", "80"}, |
| 323 |
Value: []string{"jump missingchain"}, |
| 324 |
}) |
| 325 |
err = fake.Run(context.Background(), tx) |
| 326 |
if err == nil || !IsNotFound(err) { |
| 327 |
t.Fatalf("unexpected error from Run: %v", err) |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
func TestFakeCheck(t *testing.T) { |
| 332 |
fake := NewFake(IPv4Family, "kube-proxy") |
| 333 |
|
| 334 |
tx := fake.NewTransaction() |
| 335 |
|
| 336 |
if tx.NumOperations() != 0 { |
| 337 |
t.Errorf("empty transaction should have 0 operations, got %d", tx.NumOperations()) |
| 338 |
} |
| 339 |
|
| 340 |
tx.Add(&Table{}) |
| 341 |
tx.Add(&Chain{ |
| 342 |
Name: "chain", |
| 343 |
Comment: PtrTo("foo"), |
| 344 |
}) |
| 345 |
tx.Add(&Rule{ |
| 346 |
Chain: "chain", |
| 347 |
Rule: "ip daddr 10.0.0.0/8 drop", |
| 348 |
}) |
| 349 |
tx.Add(&Rule{ |
| 350 |
Chain: "chain", |
| 351 |
Rule: "masquerade", |
| 352 |
Comment: PtrTo("comment"), |
| 353 |
}) |
| 354 |
|
| 355 |
|
| 356 |
expected := strings.TrimPrefix(dedent.Dedent(` |
| 357 |
add table ip kube-proxy |
| 358 |
add chain ip kube-proxy chain { comment "foo" ; } |
| 359 |
add rule ip kube-proxy chain ip daddr 10.0.0.0/8 drop |
| 360 |
add rule ip kube-proxy chain masquerade comment "comment" |
| 361 |
`), "\n") |
| 362 |
diff := cmp.Diff(expected, tx.String()) |
| 363 |
if diff != "" { |
| 364 |
t.Errorf("unexpected transaction content:\n%s", diff) |
| 365 |
} |
| 366 |
|
| 367 |
if tx.NumOperations() != 4 { |
| 368 |
t.Errorf("unexpected number of operations: expected 4, got %d", tx.NumOperations()) |
| 369 |
} |
| 370 |
|
| 371 |
err := fake.Run(context.Background(), tx) |
| 372 |
if err != nil { |
| 373 |
t.Fatalf("unexpected error from Run: %v", err) |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
tx = fake.NewTransaction() |
| 379 |
tx.Delete(&Chain{ |
| 380 |
Name: "chain", |
| 381 |
}) |
| 382 |
err = fake.Check(context.Background(), tx) |
| 383 |
if err != nil { |
| 384 |
t.Fatalf("unexpected error from Check: %v", err) |
| 385 |
} |
| 386 |
chain := fake.Table.Chains["chain"] |
| 387 |
if chain == nil || len(fake.Table.Chains) != 1 { |
| 388 |
t.Fatalf("unexpected contents of table.Chains: %+v", fake.Table.Chains) |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
tx = fake.NewTransaction() |
| 393 |
tx.Delete(&Chain{ |
| 394 |
Name: "another-chain", |
| 395 |
}) |
| 396 |
err = fake.Check(context.Background(), tx) |
| 397 |
if err == nil || !IsNotFound(err) { |
| 398 |
t.Fatalf("unexpected error from Check: %v", err) |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
func assertRules(t *testing.T, fake *Fake, expected ...string) { |
| 403 |
t.Helper() |
| 404 |
|
| 405 |
actual, err := fake.ListRules(context.Background(), "test") |
| 406 |
if err != nil { |
| 407 |
t.Fatalf("could not ListRules: %v", err) |
| 408 |
} |
| 409 |
|
| 410 |
if len(actual) != len(expected) { |
| 411 |
t.Errorf("expected %d rules, got %d", len(expected), len(actual)) |
| 412 |
} |
| 413 |
|
| 414 |
for i := range expected { |
| 415 |
if i == len(actual) { |
| 416 |
break |
| 417 |
} |
| 418 |
if actual[i].Rule != expected[i] { |
| 419 |
t.Errorf("expected rule %d to be %q but got %q", i+1, expected[i], actual[i].Rule) |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
rulesByHandle := make(map[int][]string) |
| 424 |
for _, r := range actual { |
| 425 |
rulesByHandle[*r.Handle] = append(rulesByHandle[*r.Handle], r.Rule) |
| 426 |
} |
| 427 |
for handle, rules := range rulesByHandle { |
| 428 |
if len(rules) > 1 { |
| 429 |
t.Errorf("multiple rules for handle %d: %v", handle, rules) |
| 430 |
} |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
func TestFakeAddInsertReplace(t *testing.T) { |
| 435 |
fake := NewFake(IPv4Family, "kube-proxy") |
| 436 |
|
| 437 |
tx := fake.NewTransaction() |
| 438 |
tx.Add(&Table{}) |
| 439 |
err := fake.Run(context.Background(), tx) |
| 440 |
if err != nil { |
| 441 |
t.Fatalf("unexpected error from Run: %v", err) |
| 442 |
} |
| 443 |
|
| 444 |
_, err = fake.ListRules(context.Background(), "test") |
| 445 |
if err == nil || !IsNotFound(err) { |
| 446 |
t.Errorf("expected chain not found but got: %v", err) |
| 447 |
} |
| 448 |
|
| 449 |
tx = fake.NewTransaction() |
| 450 |
tx.Add(&Chain{ |
| 451 |
Name: "test", |
| 452 |
}) |
| 453 |
err = fake.Run(context.Background(), tx) |
| 454 |
if err != nil { |
| 455 |
t.Fatalf("unexpected error from Run: %v", err) |
| 456 |
} |
| 457 |
|
| 458 |
assertRules(t, fake ) |
| 459 |
|
| 460 |
|
| 461 |
tx = fake.NewTransaction() |
| 462 |
tx.Add(&Rule{ |
| 463 |
Chain: "test", |
| 464 |
Rule: "first", |
| 465 |
}) |
| 466 |
tx.Add(&Rule{ |
| 467 |
Chain: "test", |
| 468 |
Rule: "second", |
| 469 |
}) |
| 470 |
tx.Add(&Rule{ |
| 471 |
Chain: "test", |
| 472 |
Rule: "third", |
| 473 |
}) |
| 474 |
err = fake.Run(context.Background(), tx) |
| 475 |
if err != nil { |
| 476 |
t.Fatalf("unexpected error from Run: %v", err) |
| 477 |
} |
| 478 |
|
| 479 |
assertRules(t, fake, "first", "second", "third") |
| 480 |
|
| 481 |
|
| 482 |
rules, _ := fake.ListRules(context.Background(), "test") |
| 483 |
firstHandle := *rules[0].Handle |
| 484 |
secondHandle := *rules[1].Handle |
| 485 |
thirdHandle := *rules[2].Handle |
| 486 |
|
| 487 |
|
| 488 |
tx = fake.NewTransaction() |
| 489 |
|
| 490 |
tx.Add(&Rule{ |
| 491 |
Chain: "test", |
| 492 |
Rule: "fourth", |
| 493 |
Index: PtrTo(1), |
| 494 |
}) |
| 495 |
|
| 496 |
tx.Add(&Rule{ |
| 497 |
Chain: "test", |
| 498 |
Rule: "fifth", |
| 499 |
Handle: PtrTo(firstHandle), |
| 500 |
}) |
| 501 |
err = fake.Run(context.Background(), tx) |
| 502 |
if err != nil { |
| 503 |
t.Fatalf("unexpected error from Run: %v", err) |
| 504 |
} |
| 505 |
|
| 506 |
assertRules(t, fake, "first", "fifth", "second", "fourth", "third") |
| 507 |
|
| 508 |
|
| 509 |
tx = fake.NewTransaction() |
| 510 |
|
| 511 |
tx.Insert(&Rule{ |
| 512 |
Chain: "test", |
| 513 |
Rule: "sixth", |
| 514 |
}) |
| 515 |
|
| 516 |
tx.Insert(&Rule{ |
| 517 |
Chain: "test", |
| 518 |
Rule: "seventh", |
| 519 |
Handle: PtrTo(secondHandle), |
| 520 |
}) |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
tx.Insert(&Rule{ |
| 525 |
Chain: "test", |
| 526 |
Rule: "eighth", |
| 527 |
Index: PtrTo(5), |
| 528 |
}) |
| 529 |
err = fake.Run(context.Background(), tx) |
| 530 |
if err != nil { |
| 531 |
t.Fatalf("unexpected error from Run: %v", err) |
| 532 |
} |
| 533 |
|
| 534 |
assertRules(t, fake, "sixth", "first", "fifth", "seventh", "second", "eighth", "fourth", "third") |
| 535 |
|
| 536 |
|
| 537 |
tx = fake.NewTransaction() |
| 538 |
|
| 539 |
tx.Replace(&Rule{ |
| 540 |
Chain: "test", |
| 541 |
Rule: "ninth", |
| 542 |
Handle: PtrTo(secondHandle), |
| 543 |
}) |
| 544 |
tx.Delete(&Rule{ |
| 545 |
Chain: "test", |
| 546 |
Handle: PtrTo(firstHandle), |
| 547 |
}) |
| 548 |
err = fake.Run(context.Background(), tx) |
| 549 |
if err != nil { |
| 550 |
t.Fatalf("unexpected error from Run: %v", err) |
| 551 |
} |
| 552 |
|
| 553 |
assertRules(t, fake, "sixth", "fifth", "seventh", "ninth", "eighth", "fourth", "third") |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
rules, _ = fake.ListRules(context.Background(), "test") |
| 558 |
if *rules[3].Handle != secondHandle { |
| 559 |
t.Errorf("Replace changed the rule handle: expected %d got %d", secondHandle, *rules[3].Handle) |
| 560 |
} |
| 561 |
if *rules[6].Handle != thirdHandle { |
| 562 |
t.Errorf("Handle for original third rule changed? expected %d got %d", thirdHandle, *rules[6].Handle) |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
tx = fake.NewTransaction() |
| 567 |
tx.Add(&Rule{ |
| 568 |
Chain: "test", |
| 569 |
Rule: "tenth", |
| 570 |
Index: PtrTo(len(rules) - 1), |
| 571 |
}) |
| 572 |
tx.Insert(&Rule{ |
| 573 |
Chain: "test", |
| 574 |
Rule: "eleventh", |
| 575 |
Index: PtrTo(len(rules)), |
| 576 |
}) |
| 577 |
tx.Add(&Rule{ |
| 578 |
Chain: "test", |
| 579 |
Rule: "twelfth", |
| 580 |
Index: PtrTo(0), |
| 581 |
}) |
| 582 |
tx.Insert(&Rule{ |
| 583 |
Chain: "test", |
| 584 |
Rule: "thirteenth", |
| 585 |
Index: PtrTo(0), |
| 586 |
}) |
| 587 |
err = fake.Run(context.Background(), tx) |
| 588 |
if err != nil { |
| 589 |
t.Fatalf("unexpected error from Run: %v", err) |
| 590 |
} |
| 591 |
|
| 592 |
assertRules(t, fake, "thirteenth", "sixth", "twelfth", "fifth", "seventh", "ninth", "eighth", "fourth", "third", "eleventh", "tenth") |
| 593 |
} |
| 594 |
|
| 595 |
func TestFakeParseDump(t *testing.T) { |
| 596 |
for _, tc := range []struct { |
| 597 |
ipFamily Family |
| 598 |
dump string |
| 599 |
}{ |
| 600 |
{ |
| 601 |
ipFamily: IPv4Family, |
| 602 |
dump: ` |
| 603 |
add table ip kube-proxy |
| 604 |
add flowtable ip kube-proxy myflowtable { hook ingress priority filter ; devices = { eth0, eth1 } ; } |
| 605 |
add chain ip kube-proxy anotherchain |
| 606 |
add chain ip kube-proxy chain { comment "foo" ; } |
| 607 |
add map ip kube-proxy map1 { type ipv4_addr . inet_proto . inet_service ; } |
| 608 |
add set ip kube-proxy set1 { type ipv4_addr . inet_proto . inet_service ; flags dynamic ; gc-interval 15s ; policy memory ; auto-merge ; } |
| 609 |
add rule ip kube-proxy anotherchain ip saddr 1.2.3.4 drop comment "drop rule" |
| 610 |
add rule ip kube-proxy anotherchain ip daddr 5.6.7.8 reject comment "reject rule" |
| 611 |
add rule ip kube-proxy chain ip daddr 10.0.0.0/8 drop |
| 612 |
add rule ip kube-proxy chain masquerade comment "comment" |
| 613 |
add element ip kube-proxy map1 { 192.168.0.1 . tcp . 80 : drop } |
| 614 |
add element ip kube-proxy map1 { 192.168.0.2 . tcp . 443 comment "with a comment" : goto anotherchain } |
| 615 |
`, |
| 616 |
}, |
| 617 |
{ |
| 618 |
ipFamily: IPv4Family, |
| 619 |
dump: ` |
| 620 |
add table ip kube-proxy { comment "rules for kube-proxy" ; } |
| 621 |
add chain ip kube-proxy mark-for-masquerade |
| 622 |
add chain ip kube-proxy masquerading |
| 623 |
add chain ip kube-proxy services |
| 624 |
add chain ip kube-proxy firewall-check |
| 625 |
add chain ip kube-proxy endpoints-check |
| 626 |
add chain ip kube-proxy filter-prerouting { type filter hook prerouting priority -110 ; } |
| 627 |
add chain ip kube-proxy filter-forward { type filter hook forward priority -110 ; } |
| 628 |
add chain ip kube-proxy filter-input { type filter hook input priority -110 ; } |
| 629 |
add chain ip kube-proxy filter-output { type filter hook output priority -110 ; } |
| 630 |
add chain ip kube-proxy nat-output { type nat hook output priority -100 ; } |
| 631 |
add chain ip kube-proxy nat-postrouting { type nat hook postrouting priority 100 ; } |
| 632 |
add chain ip kube-proxy nat-prerouting { type nat hook prerouting priority -100 ; } |
| 633 |
add chain ip kube-proxy reject-chain { comment "helper for @no-endpoint-services / @no-endpoint-nodeports" ; } |
| 634 |
add chain ip kube-proxy service-ULMVA6XW-ns1/svc1/tcp/p80 |
| 635 |
add chain ip kube-proxy endpoint-5OJB2KTY-ns1/svc1/tcp/p80__10.180.0.1/80 |
| 636 |
add chain ip kube-proxy service-42NFTM6N-ns2/svc2/tcp/p80 |
| 637 |
add chain ip kube-proxy endpoint-SGOXE6O3-ns2/svc2/tcp/p80__10.180.0.2/80 |
| 638 |
add chain ip kube-proxy external-42NFTM6N-ns2/svc2/tcp/p80 |
| 639 |
add chain ip kube-proxy service-4AT6LBPK-ns3/svc3/tcp/p80 |
| 640 |
add chain ip kube-proxy endpoint-UEIP74TE-ns3/svc3/tcp/p80__10.180.0.3/80 |
| 641 |
add chain ip kube-proxy external-4AT6LBPK-ns3/svc3/tcp/p80 |
| 642 |
add chain ip kube-proxy service-LAUZTJTB-ns4/svc4/tcp/p80 |
| 643 |
add chain ip kube-proxy endpoint-UNZV3OEC-ns4/svc4/tcp/p80__10.180.0.4/80 |
| 644 |
add chain ip kube-proxy endpoint-5RFCDDV7-ns4/svc4/tcp/p80__10.180.0.5/80 |
| 645 |
add chain ip kube-proxy external-LAUZTJTB-ns4/svc4/tcp/p80 |
| 646 |
add chain ip kube-proxy service-HVFWP5L3-ns5/svc5/tcp/p80 |
| 647 |
add chain ip kube-proxy external-HVFWP5L3-ns5/svc5/tcp/p80 |
| 648 |
add chain ip kube-proxy endpoint-GTK6MW7G-ns5/svc5/tcp/p80__10.180.0.3/80 |
| 649 |
add chain ip kube-proxy firewall-HVFWP5L3-ns5/svc5/tcp/p80 |
| 650 |
|
| 651 |
add rule ip kube-proxy mark-for-masquerade mark set mark or 0x4000 |
| 652 |
add rule ip kube-proxy masquerading mark and 0x4000 == 0 return |
| 653 |
add rule ip kube-proxy masquerading mark set mark xor 0x4000 |
| 654 |
add rule ip kube-proxy masquerading masquerade fully-random |
| 655 |
add rule ip kube-proxy filter-prerouting ct state new jump firewall-check |
| 656 |
add rule ip kube-proxy filter-forward ct state new jump endpoints-check |
| 657 |
add rule ip kube-proxy filter-input ct state new jump endpoints-check |
| 658 |
add rule ip kube-proxy filter-output ct state new jump endpoints-check |
| 659 |
add rule ip kube-proxy filter-output ct state new jump firewall-check |
| 660 |
add rule ip kube-proxy nat-output jump services |
| 661 |
add rule ip kube-proxy nat-postrouting jump masquerading |
| 662 |
add rule ip kube-proxy nat-prerouting jump services |
| 663 |
|
| 664 |
add map ip kube-proxy firewall-ips { type ipv4_addr . inet_proto . inet_service : verdict ; comment "destinations that are subject to LoadBalancerSourceRanges" ; } |
| 665 |
add rule ip kube-proxy firewall-check ip daddr . meta l4proto . th dport vmap @firewall-ips |
| 666 |
|
| 667 |
add rule ip kube-proxy reject-chain reject |
| 668 |
|
| 669 |
add map ip kube-proxy no-endpoint-services { type ipv4_addr . inet_proto . inet_service : verdict ; comment "vmap to drop or reject packets to services with no endpoints" ; } |
| 670 |
add map ip kube-proxy no-endpoint-nodeports { type inet_proto . inet_service : verdict ; comment "vmap to drop or reject packets to service nodeports with no endpoints" ; } |
| 671 |
|
| 672 |
add rule ip kube-proxy endpoints-check ip daddr . meta l4proto . th dport vmap @no-endpoint-services |
| 673 |
add rule ip kube-proxy endpoints-check fib daddr type local ip daddr != 127.0.0.0/8 meta l4proto . th dport vmap @no-endpoint-nodeports |
| 674 |
|
| 675 |
add map ip kube-proxy service-ips { type ipv4_addr . inet_proto . inet_service : verdict ; comment "ClusterIP, ExternalIP and LoadBalancer IP traffic" ; } |
| 676 |
add map ip kube-proxy service-nodeports { type inet_proto . inet_service : verdict ; comment "NodePort traffic" ; } |
| 677 |
add rule ip kube-proxy services ip daddr . meta l4proto . th dport vmap @service-ips |
| 678 |
add rule ip kube-proxy services fib daddr type local ip daddr != 127.0.0.0/8 meta l4proto . th dport vmap @service-nodeports |
| 679 |
|
| 680 |
# svc1 |
| 681 |
add rule ip kube-proxy service-ULMVA6XW-ns1/svc1/tcp/p80 ip daddr 172.30.0.41 tcp dport 80 ip saddr != 10.0.0.0/8 jump mark-for-masquerade |
| 682 |
add rule ip kube-proxy service-ULMVA6XW-ns1/svc1/tcp/p80 numgen random mod 1 vmap { 0 : goto endpoint-5OJB2KTY-ns1/svc1/tcp/p80__10.180.0.1/80 } |
| 683 |
|
| 684 |
add rule ip kube-proxy endpoint-5OJB2KTY-ns1/svc1/tcp/p80__10.180.0.1/80 ip saddr 10.180.0.1 jump mark-for-masquerade |
| 685 |
add rule ip kube-proxy endpoint-5OJB2KTY-ns1/svc1/tcp/p80__10.180.0.1/80 meta l4proto tcp dnat to 10.180.0.1:80 |
| 686 |
|
| 687 |
add element ip kube-proxy service-ips { 172.30.0.41 . tcp . 80 : goto service-ULMVA6XW-ns1/svc1/tcp/p80 } |
| 688 |
|
| 689 |
# svc2 |
| 690 |
add rule ip kube-proxy service-42NFTM6N-ns2/svc2/tcp/p80 ip daddr 172.30.0.42 tcp dport 80 ip saddr != 10.0.0.0/8 jump mark-for-masquerade |
| 691 |
add rule ip kube-proxy service-42NFTM6N-ns2/svc2/tcp/p80 numgen random mod 1 vmap { 0 : goto endpoint-SGOXE6O3-ns2/svc2/tcp/p80__10.180.0.2/80 } |
| 692 |
add rule ip kube-proxy external-42NFTM6N-ns2/svc2/tcp/p80 ip saddr 10.0.0.0/8 goto service-42NFTM6N-ns2/svc2/tcp/p80 comment "short-circuit pod traffic" |
| 693 |
add rule ip kube-proxy external-42NFTM6N-ns2/svc2/tcp/p80 fib saddr type local jump mark-for-masquerade comment "masquerade local traffic" |
| 694 |
add rule ip kube-proxy external-42NFTM6N-ns2/svc2/tcp/p80 fib saddr type local goto service-42NFTM6N-ns2/svc2/tcp/p80 comment "short-circuit local traffic" |
| 695 |
add rule ip kube-proxy endpoint-SGOXE6O3-ns2/svc2/tcp/p80__10.180.0.2/80 ip saddr 10.180.0.2 jump mark-for-masquerade |
| 696 |
add rule ip kube-proxy endpoint-SGOXE6O3-ns2/svc2/tcp/p80__10.180.0.2/80 meta l4proto tcp dnat to 10.180.0.2:80 |
| 697 |
|
| 698 |
add element ip kube-proxy service-ips { 172.30.0.42 . tcp . 80 : goto service-42NFTM6N-ns2/svc2/tcp/p80 } |
| 699 |
add element ip kube-proxy service-ips { 192.168.99.22 . tcp . 80 : goto external-42NFTM6N-ns2/svc2/tcp/p80 } |
| 700 |
add element ip kube-proxy service-ips { 1.2.3.4 . tcp . 80 : goto external-42NFTM6N-ns2/svc2/tcp/p80 } |
| 701 |
add element ip kube-proxy service-nodeports { tcp . 3001 : goto external-42NFTM6N-ns2/svc2/tcp/p80 } |
| 702 |
|
| 703 |
add element ip kube-proxy no-endpoint-nodeports { tcp . 3001 comment "ns2/svc2:p80" : drop } |
| 704 |
add element ip kube-proxy no-endpoint-services { 1.2.3.4 . tcp . 80 comment "ns2/svc2:p80" : drop } |
| 705 |
add element ip kube-proxy no-endpoint-services { 192.168.99.22 . tcp . 80 comment "ns2/svc2:p80" : drop } |
| 706 |
|
| 707 |
# svc3 |
| 708 |
add rule ip kube-proxy service-4AT6LBPK-ns3/svc3/tcp/p80 ip daddr 172.30.0.43 tcp dport 80 ip saddr != 10.0.0.0/8 jump mark-for-masquerade |
| 709 |
add rule ip kube-proxy service-4AT6LBPK-ns3/svc3/tcp/p80 numgen random mod 1 vmap { 0 : goto endpoint-UEIP74TE-ns3/svc3/tcp/p80__10.180.0.3/80 } |
| 710 |
add rule ip kube-proxy external-4AT6LBPK-ns3/svc3/tcp/p80 jump mark-for-masquerade |
| 711 |
add rule ip kube-proxy external-4AT6LBPK-ns3/svc3/tcp/p80 goto service-4AT6LBPK-ns3/svc3/tcp/p80 |
| 712 |
add rule ip kube-proxy endpoint-UEIP74TE-ns3/svc3/tcp/p80__10.180.0.3/80 ip saddr 10.180.0.3 jump mark-for-masquerade |
| 713 |
add rule ip kube-proxy endpoint-UEIP74TE-ns3/svc3/tcp/p80__10.180.0.3/80 meta l4proto tcp dnat to 10.180.0.3:80 |
| 714 |
|
| 715 |
add element ip kube-proxy service-ips { 172.30.0.43 . tcp . 80 : goto service-4AT6LBPK-ns3/svc3/tcp/p80 } |
| 716 |
add element ip kube-proxy service-nodeports { tcp . 3003 : goto external-4AT6LBPK-ns3/svc3/tcp/p80 } |
| 717 |
|
| 718 |
# svc4 |
| 719 |
add rule ip kube-proxy service-LAUZTJTB-ns4/svc4/tcp/p80 ip daddr 172.30.0.44 tcp dport 80 ip saddr != 10.0.0.0/8 jump mark-for-masquerade |
| 720 |
add rule ip kube-proxy service-LAUZTJTB-ns4/svc4/tcp/p80 numgen random mod 2 vmap { 0 : goto endpoint-UNZV3OEC-ns4/svc4/tcp/p80__10.180.0.4/80 , 1 : goto endpoint-5RFCDDV7-ns4/svc4/tcp/p80__10.180.0.5/80 } |
| 721 |
add rule ip kube-proxy external-LAUZTJTB-ns4/svc4/tcp/p80 jump mark-for-masquerade |
| 722 |
add rule ip kube-proxy external-LAUZTJTB-ns4/svc4/tcp/p80 goto service-LAUZTJTB-ns4/svc4/tcp/p80 |
| 723 |
add rule ip kube-proxy endpoint-5RFCDDV7-ns4/svc4/tcp/p80__10.180.0.5/80 ip saddr 10.180.0.5 jump mark-for-masquerade |
| 724 |
add rule ip kube-proxy endpoint-5RFCDDV7-ns4/svc4/tcp/p80__10.180.0.5/80 meta l4proto tcp dnat to 10.180.0.5:80 |
| 725 |
add rule ip kube-proxy endpoint-UNZV3OEC-ns4/svc4/tcp/p80__10.180.0.4/80 ip saddr 10.180.0.4 jump mark-for-masquerade |
| 726 |
add rule ip kube-proxy endpoint-UNZV3OEC-ns4/svc4/tcp/p80__10.180.0.4/80 meta l4proto tcp dnat to 10.180.0.4:80 |
| 727 |
|
| 728 |
add element ip kube-proxy service-ips { 172.30.0.44 . tcp . 80 : goto service-LAUZTJTB-ns4/svc4/tcp/p80 } |
| 729 |
add element ip kube-proxy service-ips { 192.168.99.33 . tcp . 80 : goto external-LAUZTJTB-ns4/svc4/tcp/p80 } |
| 730 |
|
| 731 |
# svc5 |
| 732 |
add set ip kube-proxy affinity-GTK6MW7G-ns5/svc5/tcp/p80__10.180.0.3/80 { type ipv4_addr ; flags dynamic,timeout ; timeout 10800s ; } |
| 733 |
add rule ip kube-proxy service-HVFWP5L3-ns5/svc5/tcp/p80 ip daddr 172.30.0.45 tcp dport 80 ip saddr != 10.0.0.0/8 jump mark-for-masquerade |
| 734 |
add rule ip kube-proxy service-HVFWP5L3-ns5/svc5/tcp/p80 ip saddr @affinity-GTK6MW7G-ns5/svc5/tcp/p80__10.180.0.3/80 goto endpoint-GTK6MW7G-ns5/svc5/tcp/p80__10.180.0.3/80 |
| 735 |
add rule ip kube-proxy service-HVFWP5L3-ns5/svc5/tcp/p80 numgen random mod 1 vmap { 0 : goto endpoint-GTK6MW7G-ns5/svc5/tcp/p80__10.180.0.3/80 } |
| 736 |
add rule ip kube-proxy external-HVFWP5L3-ns5/svc5/tcp/p80 jump mark-for-masquerade |
| 737 |
add rule ip kube-proxy external-HVFWP5L3-ns5/svc5/tcp/p80 goto service-HVFWP5L3-ns5/svc5/tcp/p80 |
| 738 |
|
| 739 |
add rule ip kube-proxy endpoint-GTK6MW7G-ns5/svc5/tcp/p80__10.180.0.3/80 ip saddr 10.180.0.3 jump mark-for-masquerade |
| 740 |
add rule ip kube-proxy endpoint-GTK6MW7G-ns5/svc5/tcp/p80__10.180.0.3/80 update @affinity-GTK6MW7G-ns5/svc5/tcp/p80__10.180.0.3/80 { ip saddr } |
| 741 |
add rule ip kube-proxy endpoint-GTK6MW7G-ns5/svc5/tcp/p80__10.180.0.3/80 meta l4proto tcp dnat to 10.180.0.3:80 |
| 742 |
|
| 743 |
add rule ip kube-proxy firewall-HVFWP5L3-ns5/svc5/tcp/p80 ip saddr != { 203.0.113.0/25 } drop |
| 744 |
|
| 745 |
add element ip kube-proxy service-ips { 172.30.0.45 . tcp . 80 : goto service-HVFWP5L3-ns5/svc5/tcp/p80 } |
| 746 |
add element ip kube-proxy service-ips { 5.6.7.8 . tcp . 80 : goto external-HVFWP5L3-ns5/svc5/tcp/p80 } |
| 747 |
add element ip kube-proxy service-nodeports { tcp . 3002 : goto external-HVFWP5L3-ns5/svc5/tcp/p80 } |
| 748 |
add element ip kube-proxy firewall-ips { 5.6.7.8 . tcp . 80 comment "ns5/svc5:p80" : goto firewall-HVFWP5L3-ns5/svc5/tcp/p80 } |
| 749 |
|
| 750 |
# svc6 |
| 751 |
add element ip kube-proxy no-endpoint-services { 172.30.0.46 . tcp . 80 comment "ns6/svc6:p80" : goto reject-chain } |
| 752 |
`, |
| 753 |
}, |
| 754 |
{ |
| 755 |
ipFamily: IPv6Family, |
| 756 |
dump: ` |
| 757 |
add table ip6 kube-proxy { comment "rules for kube-proxy" ; } |
| 758 |
add chain ip6 kube-proxy cluster-ips-check |
| 759 |
add chain ip6 kube-proxy endpoint-2CRNCTTE-ns1/svc1/tcp/p80__fd00.10.180..2.1/80 |
| 760 |
add chain ip6 kube-proxy endpoint-ZVRFLKHO-ns1/svc1/tcp/p80__fd00.10.180..1/80 |
| 761 |
add chain ip6 kube-proxy external-ULMVA6XW-ns1/svc1/tcp/p80 |
| 762 |
add chain ip6 kube-proxy filter-forward { type filter hook forward priority -110 ; } |
| 763 |
add chain ip6 kube-proxy filter-input { type filter hook input priority -110 ; } |
| 764 |
add chain ip6 kube-proxy filter-output { type filter hook output priority -110 ; } |
| 765 |
add chain ip6 kube-proxy filter-output-post-dnat { type filter hook output priority -90 ; } |
| 766 |
add chain ip6 kube-proxy filter-prerouting { type filter hook prerouting priority -110 ; } |
| 767 |
add chain ip6 kube-proxy firewall-check |
| 768 |
add chain ip6 kube-proxy mark-for-masquerade |
| 769 |
add chain ip6 kube-proxy masquerading |
| 770 |
add chain ip6 kube-proxy nat-output { type nat hook output priority -100 ; } |
| 771 |
add chain ip6 kube-proxy nat-postrouting { type nat hook postrouting priority 100 ; } |
| 772 |
add chain ip6 kube-proxy nat-prerouting { type nat hook prerouting priority -100 ; } |
| 773 |
add chain ip6 kube-proxy nodeport-endpoints-check |
| 774 |
add chain ip6 kube-proxy reject-chain { comment "helper for @no-endpoint-services / @no-endpoint-nodeports" ; } |
| 775 |
add chain ip6 kube-proxy service-ULMVA6XW-ns1/svc1/tcp/p80 |
| 776 |
add chain ip6 kube-proxy service-endpoints-check |
| 777 |
add chain ip6 kube-proxy services |
| 778 |
add set ip6 kube-proxy cluster-ips { type ipv6_addr ; comment "Active ClusterIPs" ; } |
| 779 |
add set ip6 kube-proxy nodeport-ips { type ipv6_addr ; comment "IPs that accept NodePort traffic" ; } |
| 780 |
add map ip6 kube-proxy firewall-ips { type ipv6_addr . inet_proto . inet_service : verdict ; comment "destinations that are subject to LoadBalancerSourceRanges" ; } |
| 781 |
add map ip6 kube-proxy no-endpoint-nodeports { type inet_proto . inet_service : verdict ; comment "vmap to drop or reject packets to service nodeports with no endpoints" ; } |
| 782 |
add map ip6 kube-proxy no-endpoint-services { type ipv6_addr . inet_proto . inet_service : verdict ; comment "vmap to drop or reject packets to services with no endpoints" ; } |
| 783 |
add map ip6 kube-proxy service-ips { type ipv6_addr . inet_proto . inet_service : verdict ; comment "ClusterIP, ExternalIP and LoadBalancer IP traffic" ; } |
| 784 |
add map ip6 kube-proxy service-nodeports { type inet_proto . inet_service : verdict ; comment "NodePort traffic" ; } |
| 785 |
add rule ip6 kube-proxy cluster-ips-check ip6 daddr @cluster-ips reject comment "Reject traffic to invalid ports of ClusterIPs" |
| 786 |
add rule ip6 kube-proxy cluster-ips-check ip6 daddr { fd00:10:96::/112 } drop comment "Drop traffic to unallocated ClusterIPs" |
| 787 |
add rule ip6 kube-proxy endpoint-2CRNCTTE-ns1/svc1/tcp/p80__fd00.10.180..2.1/80 ip6 saddr fd00:10:180::2:1 jump mark-for-masquerade |
| 788 |
add rule ip6 kube-proxy endpoint-2CRNCTTE-ns1/svc1/tcp/p80__fd00.10.180..2.1/80 meta l4proto tcp dnat to [fd00:10:180::2:1]:80 |
| 789 |
add rule ip6 kube-proxy endpoint-ZVRFLKHO-ns1/svc1/tcp/p80__fd00.10.180..1/80 ip6 saddr fd00:10:180::1 jump mark-for-masquerade |
| 790 |
add rule ip6 kube-proxy endpoint-ZVRFLKHO-ns1/svc1/tcp/p80__fd00.10.180..1/80 meta l4proto tcp dnat to [fd00:10:180::1]:80 |
| 791 |
add rule ip6 kube-proxy external-ULMVA6XW-ns1/svc1/tcp/p80 jump mark-for-masquerade |
| 792 |
add rule ip6 kube-proxy external-ULMVA6XW-ns1/svc1/tcp/p80 goto service-ULMVA6XW-ns1/svc1/tcp/p80 |
| 793 |
add rule ip6 kube-proxy filter-forward ct state new jump service-endpoints-check |
| 794 |
add rule ip6 kube-proxy filter-forward ct state new jump cluster-ips-check |
| 795 |
add rule ip6 kube-proxy filter-input ct state new jump nodeport-endpoints-check |
| 796 |
add rule ip6 kube-proxy filter-input ct state new jump service-endpoints-check |
| 797 |
add rule ip6 kube-proxy filter-output ct state new jump service-endpoints-check |
| 798 |
add rule ip6 kube-proxy filter-output ct state new jump firewall-check |
| 799 |
add rule ip6 kube-proxy filter-output-post-dnat ct state new jump cluster-ips-check |
| 800 |
add rule ip6 kube-proxy filter-prerouting ct state new jump firewall-check |
| 801 |
add rule ip6 kube-proxy firewall-check ip6 daddr . meta l4proto . th dport vmap @firewall-ips |
| 802 |
add rule ip6 kube-proxy mark-for-masquerade mark set mark or 0x4000 |
| 803 |
add rule ip6 kube-proxy masquerading mark and 0x4000 == 0 return |
| 804 |
add rule ip6 kube-proxy masquerading mark set mark xor 0x4000 |
| 805 |
add rule ip6 kube-proxy masquerading masquerade fully-random |
| 806 |
add rule ip6 kube-proxy nat-output jump services |
| 807 |
add rule ip6 kube-proxy nat-postrouting jump masquerading |
| 808 |
add rule ip6 kube-proxy nat-prerouting jump services |
| 809 |
add rule ip6 kube-proxy nodeport-endpoints-check ip6 daddr @nodeport-ips meta l4proto . th dport vmap @no-endpoint-nodeports |
| 810 |
add rule ip6 kube-proxy reject-chain reject |
| 811 |
add rule ip6 kube-proxy service-ULMVA6XW-ns1/svc1/tcp/p80 ip6 daddr fd00:172:30::41 tcp dport 80 ip6 saddr != fd00:10::/64 jump mark-for-masquerade |
| 812 |
add rule ip6 kube-proxy service-ULMVA6XW-ns1/svc1/tcp/p80 numgen random mod 2 vmap { 0 : goto endpoint-ZVRFLKHO-ns1/svc1/tcp/p80__fd00.10.180..1/80 , 1 : goto endpoint-2CRNCTTE-ns1/svc1/tcp/p80__fd00.10.180..2.1/80 } |
| 813 |
add rule ip6 kube-proxy service-endpoints-check ip6 daddr . meta l4proto . th dport vmap @no-endpoint-services |
| 814 |
add rule ip6 kube-proxy services ip6 daddr . meta l4proto . th dport vmap @service-ips |
| 815 |
add rule ip6 kube-proxy services ip6 daddr @nodeport-ips meta l4proto . th dport vmap @service-nodeports |
| 816 |
add element ip6 kube-proxy cluster-ips { fd00:172:30::41 } |
| 817 |
add element ip6 kube-proxy nodeport-ips { 2001:db8::1 comment "test comment" } |
| 818 |
add element ip6 kube-proxy nodeport-ips { 2001:db8:1::2 } |
| 819 |
add element ip6 kube-proxy service-ips { fd00:172:30::41 . tcp . 80 : goto service-ULMVA6XW-ns1/svc1/tcp/p80 } |
| 820 |
add element ip6 kube-proxy service-nodeports { tcp . 3001 comment "test comment" : goto external-ULMVA6XW-ns1/svc1/tcp/p80 } |
| 821 |
`, |
| 822 |
}, |
| 823 |
} { |
| 824 |
rules := dedent.Dedent(tc.dump) |
| 825 |
fake := NewFake(tc.ipFamily, "kube-proxy") |
| 826 |
err := fake.ParseDump(rules) |
| 827 |
if err != nil { |
| 828 |
t.Fatalf("unexpected error from ParseDump: %v", err) |
| 829 |
} |
| 830 |
|
| 831 |
|
| 832 |
rulesSlice := []string{""} |
| 833 |
for _, rule := range strings.Split(rules, "\n") { |
| 834 |
if rule == "" || strings.HasPrefix(rule, "#") { |
| 835 |
continue |
| 836 |
} |
| 837 |
rulesSlice = append(rulesSlice, rule) |
| 838 |
} |
| 839 |
sort.Strings(rulesSlice) |
| 840 |
dumpSlice := strings.Split(fake.Dump(), "\n") |
| 841 |
sort.Strings(dumpSlice) |
| 842 |
|
| 843 |
diff := cmp.Diff(rulesSlice, dumpSlice) |
| 844 |
if diff != "" { |
| 845 |
t.Errorf("Dump doesn't match given rules:\n%s", diff) |
| 846 |
} |
| 847 |
} |
| 848 |
} |