github.phpd.cn/cilium/cilium@v1.6.12/pkg/k8s/factory_functions_test.go (about) 1 // Copyright 2018-2019 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // +build !privileged_tests 16 17 package k8s 18 19 import ( 20 "time" 21 22 "github.com/cilium/cilium/pkg/annotation" 23 "github.com/cilium/cilium/pkg/checker" 24 v2 "github.com/cilium/cilium/pkg/k8s/apis/cilium.io/v2" 25 "github.com/cilium/cilium/pkg/k8s/types" 26 "github.com/cilium/cilium/pkg/labels" 27 "github.com/cilium/cilium/pkg/policy/api" 28 29 . "gopkg.in/check.v1" 30 "k8s.io/api/core/v1" 31 core_v1 "k8s.io/api/core/v1" 32 "k8s.io/api/extensions/v1beta1" 33 networkingv1 "k8s.io/api/networking/v1" 34 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 35 "k8s.io/apimachinery/pkg/util/intstr" 36 "k8s.io/client-go/tools/cache" 37 ) 38 39 func (s *K8sSuite) Test_EqualV2CNP(c *C) { 40 type args struct { 41 o1 *types.SlimCNP 42 o2 *types.SlimCNP 43 } 44 tests := []struct { 45 name string 46 args args 47 want bool 48 }{ 49 { 50 name: "CNP with the same name", 51 args: args{ 52 o1: &types.SlimCNP{ 53 CiliumNetworkPolicy: &v2.CiliumNetworkPolicy{ 54 ObjectMeta: metav1.ObjectMeta{ 55 Name: "rule1", 56 }, 57 }, 58 }, 59 o2: &types.SlimCNP{ 60 CiliumNetworkPolicy: &v2.CiliumNetworkPolicy{ 61 ObjectMeta: metav1.ObjectMeta{ 62 Name: "rule1", 63 }, 64 }, 65 }, 66 }, 67 want: true, 68 }, 69 { 70 name: "CNP with the different spec", 71 args: args{ 72 o1: &types.SlimCNP{ 73 CiliumNetworkPolicy: &v2.CiliumNetworkPolicy{ 74 ObjectMeta: metav1.ObjectMeta{ 75 Name: "rule1", 76 }, 77 Spec: &api.Rule{ 78 EndpointSelector: api.NewESFromLabels(labels.NewLabel("foo", "bar", "k8s")), 79 }, 80 }, 81 }, 82 o2: &types.SlimCNP{ 83 CiliumNetworkPolicy: &v2.CiliumNetworkPolicy{ 84 ObjectMeta: metav1.ObjectMeta{ 85 Name: "rule1", 86 }, 87 Spec: nil, 88 }, 89 }, 90 }, 91 want: false, 92 }, 93 { 94 name: "CNP with the same spec", 95 args: args{ 96 o1: &types.SlimCNP{ 97 CiliumNetworkPolicy: &v2.CiliumNetworkPolicy{ 98 ObjectMeta: metav1.ObjectMeta{ 99 Name: "rule1", 100 }, 101 Spec: &api.Rule{}, 102 }, 103 }, 104 o2: &types.SlimCNP{ 105 CiliumNetworkPolicy: &v2.CiliumNetworkPolicy{ 106 ObjectMeta: metav1.ObjectMeta{ 107 Name: "rule1", 108 }, 109 Spec: &api.Rule{}, 110 }, 111 }, 112 }, 113 want: true, 114 }, 115 { 116 name: "CNP with different last applied annotations. The are ignored so they should be equal", 117 args: args{ 118 o1: &types.SlimCNP{ 119 CiliumNetworkPolicy: &v2.CiliumNetworkPolicy{ 120 ObjectMeta: metav1.ObjectMeta{ 121 Name: "rule1", 122 Annotations: map[string]string{ 123 core_v1.LastAppliedConfigAnnotation: "foo", 124 }, 125 }, 126 Spec: &api.Rule{}, 127 }, 128 }, 129 o2: &types.SlimCNP{ 130 CiliumNetworkPolicy: &v2.CiliumNetworkPolicy{ 131 ObjectMeta: metav1.ObjectMeta{ 132 Name: "rule1", 133 Annotations: map[string]string{ 134 core_v1.LastAppliedConfigAnnotation: "bar", 135 }, 136 }, 137 Spec: &api.Rule{}, 138 }, 139 }, 140 }, 141 want: true, 142 }, 143 } 144 for _, tt := range tests { 145 got := EqualV2CNP(tt.args.o1, tt.args.o2) 146 c.Assert(got, Equals, tt.want, Commentf("Test Name: %s", tt.name)) 147 } 148 } 149 150 func (s *K8sSuite) Test_EqualV1Endpoints(c *C) { 151 type args struct { 152 o1 *types.Endpoints 153 o2 *types.Endpoints 154 } 155 tests := []struct { 156 name string 157 args args 158 want bool 159 }{ 160 { 161 name: "EPs with the same name", 162 args: args{ 163 o1: &types.Endpoints{ 164 Endpoints: &core_v1.Endpoints{ 165 ObjectMeta: metav1.ObjectMeta{ 166 Name: "rule1", 167 }, 168 }, 169 }, 170 o2: &types.Endpoints{ 171 Endpoints: &core_v1.Endpoints{ 172 ObjectMeta: metav1.ObjectMeta{ 173 Name: "rule1", 174 }, 175 }, 176 }, 177 }, 178 want: true, 179 }, 180 { 181 name: "EPs with the different spec", 182 args: args{ 183 o1: &types.Endpoints{ 184 Endpoints: &core_v1.Endpoints{ 185 ObjectMeta: metav1.ObjectMeta{ 186 Name: "rule1", 187 }, 188 Subsets: []core_v1.EndpointSubset{ 189 { 190 Addresses: []core_v1.EndpointAddress{ 191 { 192 IP: "172.0.0.1", 193 }, 194 }, 195 }, 196 }, 197 }, 198 }, 199 o2: &types.Endpoints{ 200 Endpoints: &core_v1.Endpoints{ 201 ObjectMeta: metav1.ObjectMeta{ 202 Name: "rule1", 203 }, 204 }, 205 }, 206 }, 207 want: false, 208 }, 209 { 210 name: "EPs with the same spec", 211 args: args{ 212 o1: &types.Endpoints{ 213 Endpoints: &core_v1.Endpoints{ 214 ObjectMeta: metav1.ObjectMeta{ 215 Name: "rule1", 216 }, 217 Subsets: []core_v1.EndpointSubset{ 218 { 219 Addresses: []core_v1.EndpointAddress{ 220 { 221 IP: "172.0.0.1", 222 }, 223 }, 224 }, 225 }, 226 }, 227 }, 228 o2: &types.Endpoints{ 229 Endpoints: &core_v1.Endpoints{ 230 ObjectMeta: metav1.ObjectMeta{ 231 Name: "rule1", 232 }, 233 Subsets: []core_v1.EndpointSubset{ 234 { 235 Addresses: []core_v1.EndpointAddress{ 236 { 237 IP: "172.0.0.1", 238 }, 239 }, 240 }, 241 }, 242 }, 243 }, 244 }, 245 want: true, 246 }, 247 { 248 name: "EPs with the same spec (multiple IPs)", 249 args: args{ 250 o1: &types.Endpoints{ 251 Endpoints: &core_v1.Endpoints{ 252 ObjectMeta: metav1.ObjectMeta{ 253 Name: "rule1", 254 }, 255 Subsets: []core_v1.EndpointSubset{ 256 { 257 Addresses: []core_v1.EndpointAddress{ 258 { 259 IP: "172.0.0.1", 260 }, 261 { 262 IP: "172.0.0.2", 263 }, 264 }, 265 }, 266 }, 267 }, 268 }, 269 o2: &types.Endpoints{ 270 Endpoints: &core_v1.Endpoints{ 271 ObjectMeta: metav1.ObjectMeta{ 272 Name: "rule1", 273 }, 274 Subsets: []core_v1.EndpointSubset{ 275 { 276 Addresses: []core_v1.EndpointAddress{ 277 { 278 IP: "172.0.0.1", 279 }, 280 { 281 IP: "172.0.0.2", 282 }, 283 }, 284 }, 285 }, 286 }, 287 }, 288 }, 289 want: true, 290 }, 291 } 292 for _, tt := range tests { 293 got := EqualV1Endpoints(tt.args.o1, tt.args.o2) 294 c.Assert(got, Equals, tt.want, Commentf("Test Name: %s", tt.name)) 295 } 296 } 297 298 func (s *K8sSuite) Test_EqualV1Pod(c *C) { 299 type args struct { 300 o1 *types.Pod 301 o2 *types.Pod 302 } 303 tests := []struct { 304 name string 305 args args 306 want bool 307 }{ 308 309 { 310 name: "Pods with the same name", 311 args: args{ 312 o1: &types.Pod{ 313 ObjectMeta: metav1.ObjectMeta{ 314 Name: "pod1", 315 }, 316 }, 317 o2: &types.Pod{ 318 ObjectMeta: metav1.ObjectMeta{ 319 Name: "pod1", 320 }, 321 }, 322 }, 323 want: true, 324 }, 325 { 326 name: "Pods with the different spec", 327 args: args{ 328 o1: &types.Pod{ 329 ObjectMeta: metav1.ObjectMeta{ 330 Name: "pod1", 331 }, 332 StatusHostIP: "127.0.0.1", 333 StatusPodIP: "127.0.0.2", 334 }, 335 o2: &types.Pod{ 336 ObjectMeta: metav1.ObjectMeta{ 337 Name: "pod1", 338 }, 339 StatusHostIP: "127.0.0.1", 340 StatusPodIP: "127.0.0.1", 341 }, 342 }, 343 want: false, 344 }, 345 { 346 name: "Pods with the same spec", 347 args: args{ 348 o1: &types.Pod{ 349 ObjectMeta: metav1.ObjectMeta{ 350 Name: "pod1", 351 }, 352 StatusHostIP: "127.0.0.1", 353 StatusPodIP: "127.0.0.2", 354 }, 355 o2: &types.Pod{ 356 ObjectMeta: metav1.ObjectMeta{ 357 Name: "pod1", 358 }, 359 StatusHostIP: "127.0.0.1", 360 StatusPodIP: "127.0.0.2", 361 }, 362 }, 363 want: true, 364 }, 365 { 366 name: "Pods with the same spec but different labels", 367 args: args{ 368 o1: &types.Pod{ 369 ObjectMeta: metav1.ObjectMeta{ 370 Name: "pod1", 371 Labels: map[string]string{ 372 "foo": "bar", 373 }, 374 }, 375 StatusHostIP: "127.0.0.1", 376 StatusPodIP: "127.0.0.2", 377 }, 378 o2: &types.Pod{ 379 ObjectMeta: metav1.ObjectMeta{ 380 Name: "pod1", 381 }, 382 StatusHostIP: "127.0.0.1", 383 StatusPodIP: "127.0.0.2", 384 }, 385 }, 386 want: false, 387 }, 388 { 389 name: "Pods with the same spec and same labels", 390 args: args{ 391 o1: &types.Pod{ 392 ObjectMeta: metav1.ObjectMeta{ 393 Name: "pod1", 394 Labels: map[string]string{ 395 "foo": "bar", 396 }, 397 }, 398 StatusHostIP: "127.0.0.1", 399 StatusPodIP: "127.0.0.2", 400 }, 401 o2: &types.Pod{ 402 ObjectMeta: metav1.ObjectMeta{ 403 Name: "pod1", 404 Labels: map[string]string{ 405 "foo": "bar", 406 }, 407 }, 408 StatusHostIP: "127.0.0.1", 409 StatusPodIP: "127.0.0.2", 410 }, 411 }, 412 want: true, 413 }, 414 } 415 for _, tt := range tests { 416 got := EqualV1Pod(tt.args.o1, tt.args.o2) 417 c.Assert(got, Equals, tt.want, Commentf("Test Name: %s", tt.name)) 418 } 419 } 420 421 func (s *K8sSuite) Test_EqualV1Node(c *C) { 422 type args struct { 423 o1 *types.Node 424 o2 *types.Node 425 } 426 tests := []struct { 427 name string 428 args args 429 want bool 430 }{ 431 { 432 name: "Nodes with the same name", 433 args: args{ 434 o1: &types.Node{ 435 ObjectMeta: metav1.ObjectMeta{ 436 Name: "Node1", 437 }, 438 }, 439 o2: &types.Node{ 440 ObjectMeta: metav1.ObjectMeta{ 441 Name: "Node1", 442 }, 443 }, 444 }, 445 want: true, 446 }, 447 { 448 name: "Nodes with the different names", 449 args: args{ 450 o1: &types.Node{ 451 ObjectMeta: metav1.ObjectMeta{ 452 Name: "Node1", 453 }, 454 }, 455 o2: &types.Node{ 456 ObjectMeta: metav1.ObjectMeta{ 457 Name: "Node2", 458 }, 459 }, 460 }, 461 want: false, 462 }, 463 { 464 name: "Nodes with the different spec should return true as we don't care about the spec", 465 args: args{ 466 o1: &types.Node{ 467 ObjectMeta: metav1.ObjectMeta{ 468 Name: "Node1", 469 }, 470 SpecPodCIDR: "192.168.0.0/10", 471 }, 472 o2: &types.Node{ 473 ObjectMeta: metav1.ObjectMeta{ 474 Name: "Node1", 475 }, 476 SpecPodCIDR: "127.0.0.1/10", 477 }, 478 }, 479 want: true, 480 }, 481 { 482 name: "Nodes with the same annotations", 483 args: args{ 484 o1: &types.Node{ 485 ObjectMeta: metav1.ObjectMeta{ 486 Name: "Node1", 487 Annotations: map[string]string{ 488 annotation.CiliumHostIP: "127.0.0.1", 489 }, 490 }, 491 }, 492 o2: &types.Node{ 493 ObjectMeta: metav1.ObjectMeta{ 494 Name: "Node1", 495 Annotations: map[string]string{ 496 annotation.CiliumHostIP: "127.0.0.1", 497 }, 498 }, 499 }, 500 }, 501 want: true, 502 }, 503 { 504 name: "Nodes with the different annotations", 505 args: args{ 506 o1: &types.Node{ 507 ObjectMeta: metav1.ObjectMeta{ 508 Name: "Node1", 509 Annotations: map[string]string{ 510 annotation.CiliumHostIP: "127.0.0.1", 511 }, 512 }, 513 }, 514 o2: &types.Node{ 515 ObjectMeta: metav1.ObjectMeta{ 516 Name: "Node1", 517 Annotations: map[string]string{ 518 annotation.CiliumHostIP: "127.0.0.2", 519 }, 520 }, 521 }, 522 }, 523 want: false, 524 }, 525 { 526 name: "Nodes with the same annotations and different specs should return true because he don't care about the spec", 527 args: args{ 528 o1: &types.Node{ 529 ObjectMeta: metav1.ObjectMeta{ 530 Name: "Node1", 531 Annotations: map[string]string{ 532 annotation.CiliumHostIP: "127.0.0.1", 533 }, 534 }, 535 SpecPodCIDR: "192.168.0.0/10", 536 }, 537 o2: &types.Node{ 538 ObjectMeta: metav1.ObjectMeta{ 539 Name: "Node1", 540 Annotations: map[string]string{ 541 annotation.CiliumHostIP: "127.0.0.1", 542 }, 543 }, 544 SpecPodCIDR: "127.0.0.1/10", 545 }, 546 }, 547 want: true, 548 }, 549 { 550 name: "Nodes with the same taints and different specs should return true because he don't care about the spec", 551 args: args{ 552 o1: &types.Node{ 553 ObjectMeta: metav1.ObjectMeta{ 554 Name: "Node1", 555 }, 556 SpecTaints: []core_v1.Taint{ 557 { 558 Key: "key", 559 Value: "value", 560 Effect: "no-effect", 561 }, 562 }, 563 SpecPodCIDR: "192.168.0.0/10", 564 }, 565 o2: &types.Node{ 566 ObjectMeta: metav1.ObjectMeta{ 567 Name: "Node1", 568 }, 569 SpecTaints: []core_v1.Taint{ 570 { 571 Key: "key", 572 Value: "value", 573 Effect: "no-effect", 574 }, 575 }, 576 SpecPodCIDR: "127.0.0.1/10", 577 }, 578 }, 579 want: true, 580 }, 581 { 582 name: "Nodes with the same taints and different specs should return true because he don't care about the spec", 583 args: args{ 584 o1: &types.Node{ 585 ObjectMeta: metav1.ObjectMeta{ 586 Name: "Node1", 587 }, 588 SpecTaints: []core_v1.Taint{ 589 { 590 Key: "key", 591 Value: "value", 592 Effect: "no-effect", 593 TimeAdded: func() *metav1.Time { return &metav1.Time{Time: time.Unix(1, 1)} }(), 594 }, 595 }, 596 SpecPodCIDR: "192.168.0.0/10", 597 }, 598 o2: &types.Node{ 599 ObjectMeta: metav1.ObjectMeta{ 600 Name: "Node1", 601 }, 602 SpecTaints: []core_v1.Taint{ 603 { 604 Key: "key", 605 Value: "value", 606 Effect: "no-effect", 607 TimeAdded: func() *metav1.Time { return &metav1.Time{Time: time.Unix(1, 1)} }(), 608 }, 609 }, 610 SpecPodCIDR: "127.0.0.1/10", 611 }, 612 }, 613 want: true, 614 }, 615 { 616 name: "Nodes with the different taints and different specs should return true because he don't care about the spec", 617 args: args{ 618 o1: &types.Node{ 619 ObjectMeta: metav1.ObjectMeta{ 620 Name: "Node1", 621 }, 622 SpecTaints: []core_v1.Taint{ 623 { 624 Key: "key", 625 Value: "value", 626 Effect: "no-effect", 627 }, 628 }, 629 SpecPodCIDR: "192.168.0.0/10", 630 }, 631 o2: &types.Node{ 632 ObjectMeta: metav1.ObjectMeta{ 633 Name: "Node1", 634 }, 635 SpecTaints: []core_v1.Taint{ 636 { 637 Key: "key", 638 Value: "value", 639 Effect: "no-effect", 640 TimeAdded: func() *metav1.Time { return &metav1.Time{Time: time.Unix(1, 1)} }(), 641 }, 642 }, 643 SpecPodCIDR: "127.0.0.1/10", 644 }, 645 }, 646 want: false, 647 }, 648 } 649 for _, tt := range tests { 650 got := EqualV1Node(tt.args.o1, tt.args.o2) 651 c.Assert(got, Equals, tt.want, Commentf("Test Name: %s", tt.name)) 652 } 653 } 654 655 func (s *K8sSuite) Test_EqualV1Namespace(c *C) { 656 type args struct { 657 o1 *types.Namespace 658 o2 *types.Namespace 659 } 660 tests := []struct { 661 name string 662 args args 663 want bool 664 }{ 665 { 666 name: "Namespaces with the same name", 667 args: args{ 668 o1: &types.Namespace{ 669 ObjectMeta: metav1.ObjectMeta{ 670 Name: "Namespace1", 671 }, 672 }, 673 o2: &types.Namespace{ 674 ObjectMeta: metav1.ObjectMeta{ 675 Name: "Namespace1", 676 }, 677 }, 678 }, 679 want: true, 680 }, 681 { 682 name: "Namespaces with the different names", 683 args: args{ 684 o1: &types.Namespace{ 685 ObjectMeta: metav1.ObjectMeta{ 686 Name: "Namespace1", 687 }, 688 }, 689 o2: &types.Namespace{ 690 ObjectMeta: metav1.ObjectMeta{ 691 Name: "Namespace2", 692 }, 693 }, 694 }, 695 want: false, 696 }, 697 { 698 name: "Namespaces with the same labels", 699 args: args{ 700 o1: &types.Namespace{ 701 ObjectMeta: metav1.ObjectMeta{ 702 Name: "Namespace1", 703 Labels: map[string]string{ 704 "prod": "true", 705 }, 706 }, 707 }, 708 o2: &types.Namespace{ 709 ObjectMeta: metav1.ObjectMeta{ 710 Name: "Namespace1", 711 Labels: map[string]string{ 712 "prod": "true", 713 }, 714 }, 715 }, 716 }, 717 want: true, 718 }, 719 { 720 name: "Namespaces with the different labels", 721 args: args{ 722 o1: &types.Namespace{ 723 ObjectMeta: metav1.ObjectMeta{ 724 Name: "Namespace1", 725 Labels: map[string]string{ 726 "prod": "true", 727 }, 728 }, 729 }, 730 o2: &types.Namespace{ 731 ObjectMeta: metav1.ObjectMeta{ 732 Name: "Namespace1", 733 Labels: map[string]string{ 734 "prod": "false", 735 }, 736 }, 737 }, 738 }, 739 want: false, 740 }, 741 } 742 for _, tt := range tests { 743 got := EqualV1Namespace(tt.args.o1, tt.args.o2) 744 c.Assert(got, Equals, tt.want, Commentf("Test Name: %s", tt.name)) 745 } 746 } 747 748 func (s *K8sSuite) Test_EqualV1beta1Ingress(c *C) { 749 type args struct { 750 o1 *types.Ingress 751 o2 *types.Ingress 752 } 753 tests := []struct { 754 name string 755 args args 756 want bool 757 }{ 758 { 759 name: "Ingresses with the same name", 760 args: args{ 761 o1: &types.Ingress{ 762 Ingress: &v1beta1.Ingress{ 763 ObjectMeta: metav1.ObjectMeta{ 764 Name: "Ingress1", 765 }, 766 }, 767 }, 768 o2: &types.Ingress{ 769 Ingress: &v1beta1.Ingress{ 770 ObjectMeta: metav1.ObjectMeta{ 771 Name: "Ingress1", 772 }, 773 }, 774 }, 775 }, 776 want: true, 777 }, 778 { 779 name: "Ingresses with the different names", 780 args: args{ 781 o1: &types.Ingress{ 782 Ingress: &v1beta1.Ingress{ 783 ObjectMeta: metav1.ObjectMeta{ 784 Name: "Ingress1", 785 }, 786 }, 787 }, 788 o2: &types.Ingress{ 789 Ingress: &v1beta1.Ingress{ 790 ObjectMeta: metav1.ObjectMeta{ 791 Name: "Ingress2", 792 }, 793 }, 794 }, 795 }, 796 want: false, 797 }, 798 { 799 name: "Ingresses with the different spec should return true as we don't care about the spec", 800 args: args{ 801 o1: &types.Ingress{ 802 Ingress: &v1beta1.Ingress{ 803 ObjectMeta: metav1.ObjectMeta{ 804 Name: "Ingress1", 805 }, 806 Spec: v1beta1.IngressSpec{ 807 Rules: []v1beta1.IngressRule{ 808 { 809 Host: "not relevant", 810 }, 811 }, 812 }, 813 }, 814 }, 815 o2: &types.Ingress{ 816 Ingress: &v1beta1.Ingress{ 817 ObjectMeta: metav1.ObjectMeta{ 818 Name: "Ingress1", 819 }, 820 }, 821 }, 822 }, 823 want: true, 824 }, 825 { 826 name: "Ingresses with the different backend name should be considered the same because we only care about the ports", 827 args: args{ 828 o1: &types.Ingress{ 829 Ingress: &v1beta1.Ingress{ 830 ObjectMeta: metav1.ObjectMeta{ 831 Name: "Ingress1", 832 }, 833 Spec: v1beta1.IngressSpec{ 834 Backend: &v1beta1.IngressBackend{ 835 ServiceName: "svc ingress 1", 836 }, 837 }, 838 }, 839 }, 840 o2: &types.Ingress{ 841 Ingress: &v1beta1.Ingress{ 842 ObjectMeta: metav1.ObjectMeta{ 843 Name: "Ingress1", 844 }, 845 Spec: v1beta1.IngressSpec{ 846 Backend: &v1beta1.IngressBackend{ 847 ServiceName: "svc ingress", 848 }, 849 }, 850 }, 851 }, 852 }, 853 want: true, 854 }, 855 { 856 name: "Ingresses with the different backend name should be considered the same because we only care about the ports", 857 args: args{ 858 o1: &types.Ingress{ 859 Ingress: &v1beta1.Ingress{ 860 ObjectMeta: metav1.ObjectMeta{ 861 Name: "Ingress1", 862 }, 863 Spec: v1beta1.IngressSpec{ 864 Backend: &v1beta1.IngressBackend{ 865 ServiceName: "svc ingress 1", 866 ServicePort: intstr.FromString("10"), 867 }, 868 }, 869 }, 870 }, 871 o2: &types.Ingress{ 872 Ingress: &v1beta1.Ingress{ 873 ObjectMeta: metav1.ObjectMeta{ 874 Name: "Ingress1", 875 }, 876 Spec: v1beta1.IngressSpec{ 877 Backend: &v1beta1.IngressBackend{ 878 ServiceName: "svc ingress", 879 ServicePort: intstr.FromString("10"), 880 }, 881 }, 882 }, 883 }, 884 }, 885 want: true, 886 }, 887 { 888 name: "Ingresses with the different ports should be considered different", 889 args: args{ 890 o1: &types.Ingress{ 891 Ingress: &v1beta1.Ingress{ 892 ObjectMeta: metav1.ObjectMeta{ 893 Name: "Ingress1", 894 }, 895 Spec: v1beta1.IngressSpec{ 896 Backend: &v1beta1.IngressBackend{ 897 ServiceName: "svc ingress 1", 898 ServicePort: intstr.FromString("1"), 899 }, 900 }, 901 }, 902 }, 903 o2: &types.Ingress{ 904 Ingress: &v1beta1.Ingress{ 905 ObjectMeta: metav1.ObjectMeta{ 906 Name: "Ingress1", 907 }, 908 Spec: v1beta1.IngressSpec{ 909 Backend: &v1beta1.IngressBackend{ 910 ServiceName: "svc ingress", 911 ServicePort: intstr.FromString("10"), 912 }, 913 }, 914 }, 915 }, 916 }, 917 want: false, 918 }, 919 } 920 for _, tt := range tests { 921 got := EqualV1beta1Ingress(tt.args.o1, tt.args.o2) 922 c.Assert(got, Equals, tt.want, Commentf("Test Name: %s", tt.name)) 923 } 924 } 925 926 func (s *K8sSuite) Test_EqualV1Service(c *C) { 927 type args struct { 928 o1 *types.Service 929 o2 *types.Service 930 } 931 tests := []struct { 932 name string 933 args args 934 want bool 935 }{ 936 { 937 name: "Service with different annotations", 938 args: args{ 939 o1: &types.Service{ 940 Service: &core_v1.Service{ 941 ObjectMeta: metav1.ObjectMeta{ 942 Annotations: map[string]string{}, 943 }, 944 }, 945 }, 946 o2: &types.Service{ 947 Service: &core_v1.Service{ 948 ObjectMeta: metav1.ObjectMeta{ 949 Annotations: map[string]string{ 950 "io.cilium/shared-service": "true", 951 }, 952 }, 953 }, 954 }, 955 }, 956 want: false, 957 }, 958 } 959 for _, tt := range tests { 960 got := EqualV1Services(tt.args.o1, tt.args.o2) 961 c.Assert(got, Equals, tt.want, Commentf("Test Name: %s", tt.name)) 962 } 963 } 964 965 func (s *K8sSuite) Test_ConvertToNetworkPolicy(c *C) { 966 type args struct { 967 obj interface{} 968 } 969 tests := []struct { 970 name string 971 args args 972 want interface{} 973 }{ 974 { 975 name: "normal conversion", 976 args: args{ 977 obj: &networkingv1.NetworkPolicy{}, 978 }, 979 want: &types.NetworkPolicy{ 980 NetworkPolicy: &networkingv1.NetworkPolicy{}, 981 }, 982 }, 983 { 984 name: "delete final state unknown conversion", 985 args: args{ 986 obj: cache.DeletedFinalStateUnknown{ 987 Key: "foo", 988 Obj: &networkingv1.NetworkPolicy{}, 989 }, 990 }, 991 want: cache.DeletedFinalStateUnknown{ 992 Key: "foo", 993 Obj: &types.NetworkPolicy{ 994 NetworkPolicy: &networkingv1.NetworkPolicy{}, 995 }, 996 }, 997 }, 998 { 999 name: "unknown object type in delete final state unknown conversion", 1000 args: args{ 1001 obj: cache.DeletedFinalStateUnknown{ 1002 Key: "foo", 1003 Obj: 100, 1004 }, 1005 }, 1006 want: cache.DeletedFinalStateUnknown{ 1007 Key: "foo", 1008 Obj: 100, 1009 }, 1010 }, 1011 { 1012 name: "unknown object type in conversion", 1013 args: args{ 1014 obj: 100, 1015 }, 1016 want: 100, 1017 }, 1018 } 1019 for _, tt := range tests { 1020 got := ConvertToNetworkPolicy(tt.args.obj) 1021 c.Assert(got, checker.DeepEquals, tt.want, Commentf("Test Name: %s", tt.name)) 1022 } 1023 } 1024 1025 func (s *K8sSuite) Test_ConvertToK8sService(c *C) { 1026 type args struct { 1027 obj interface{} 1028 } 1029 tests := []struct { 1030 name string 1031 args args 1032 want interface{} 1033 }{ 1034 { 1035 name: "normal conversion", 1036 args: args{ 1037 obj: &v1.Service{}, 1038 }, 1039 want: &types.Service{ 1040 Service: &v1.Service{}, 1041 }, 1042 }, 1043 { 1044 name: "delete final state unknown conversion", 1045 args: args{ 1046 obj: cache.DeletedFinalStateUnknown{ 1047 Key: "foo", 1048 Obj: &v1.Service{}, 1049 }, 1050 }, 1051 want: cache.DeletedFinalStateUnknown{ 1052 Key: "foo", 1053 Obj: &types.Service{ 1054 Service: &v1.Service{}, 1055 }, 1056 }, 1057 }, 1058 { 1059 name: "unknown object type in delete final state unknown conversion", 1060 args: args{ 1061 obj: cache.DeletedFinalStateUnknown{ 1062 Key: "foo", 1063 Obj: 100, 1064 }, 1065 }, 1066 want: cache.DeletedFinalStateUnknown{ 1067 Key: "foo", 1068 Obj: 100, 1069 }, 1070 }, 1071 { 1072 name: "unknown object type in conversion", 1073 args: args{ 1074 obj: 100, 1075 }, 1076 want: 100, 1077 }, 1078 } 1079 for _, tt := range tests { 1080 got := ConvertToK8sService(tt.args.obj) 1081 c.Assert(got, checker.DeepEquals, tt.want, Commentf("Test Name: %s", tt.name)) 1082 } 1083 } 1084 1085 func (s *K8sSuite) Test_ConvertToK8sEndpoints(c *C) { 1086 type args struct { 1087 obj interface{} 1088 } 1089 tests := []struct { 1090 name string 1091 args args 1092 want interface{} 1093 }{ 1094 { 1095 name: "normal conversion", 1096 args: args{ 1097 obj: &v1.Endpoints{}, 1098 }, 1099 want: &types.Endpoints{ 1100 Endpoints: &v1.Endpoints{}, 1101 }, 1102 }, 1103 { 1104 name: "delete final state unknown conversion", 1105 args: args{ 1106 obj: cache.DeletedFinalStateUnknown{ 1107 Key: "foo", 1108 Obj: &v1.Endpoints{}, 1109 }, 1110 }, 1111 want: cache.DeletedFinalStateUnknown{ 1112 Key: "foo", 1113 Obj: &types.Endpoints{ 1114 Endpoints: &v1.Endpoints{}, 1115 }, 1116 }, 1117 }, 1118 { 1119 name: "unknown object type in delete final state unknown conversion", 1120 args: args{ 1121 obj: cache.DeletedFinalStateUnknown{ 1122 Key: "foo", 1123 Obj: 100, 1124 }, 1125 }, 1126 want: cache.DeletedFinalStateUnknown{ 1127 Key: "foo", 1128 Obj: 100, 1129 }, 1130 }, 1131 { 1132 name: "unknown object type in conversion", 1133 args: args{ 1134 obj: 100, 1135 }, 1136 want: 100, 1137 }, 1138 } 1139 for _, tt := range tests { 1140 got := ConvertToK8sEndpoints(tt.args.obj) 1141 c.Assert(got, checker.DeepEquals, tt.want, Commentf("Test Name: %s", tt.name)) 1142 } 1143 } 1144 1145 func (s *K8sSuite) Test_ConvertToIngress(c *C) { 1146 type args struct { 1147 obj interface{} 1148 } 1149 tests := []struct { 1150 name string 1151 args args 1152 want interface{} 1153 }{ 1154 { 1155 name: "normal conversion", 1156 args: args{ 1157 obj: &v1beta1.Ingress{}, 1158 }, 1159 want: &types.Ingress{ 1160 Ingress: &v1beta1.Ingress{}, 1161 }, 1162 }, 1163 { 1164 name: "delete final state unknown conversion", 1165 args: args{ 1166 obj: cache.DeletedFinalStateUnknown{ 1167 Key: "foo", 1168 Obj: &v1beta1.Ingress{}, 1169 }, 1170 }, 1171 want: cache.DeletedFinalStateUnknown{ 1172 Key: "foo", 1173 Obj: &types.Ingress{ 1174 Ingress: &v1beta1.Ingress{}, 1175 }, 1176 }, 1177 }, 1178 { 1179 name: "unknown object type in delete final state unknown conversion", 1180 args: args{ 1181 obj: cache.DeletedFinalStateUnknown{ 1182 Key: "foo", 1183 Obj: 100, 1184 }, 1185 }, 1186 want: cache.DeletedFinalStateUnknown{ 1187 Key: "foo", 1188 Obj: 100, 1189 }, 1190 }, 1191 { 1192 name: "unknown object type in conversion", 1193 args: args{ 1194 obj: 100, 1195 }, 1196 want: 100, 1197 }, 1198 } 1199 for _, tt := range tests { 1200 got := ConvertToIngress(tt.args.obj) 1201 c.Assert(got, checker.DeepEquals, tt.want, Commentf("Test Name: %s", tt.name)) 1202 } 1203 } 1204 1205 func (s *K8sSuite) Test_ConvertToCNPWithStatus(c *C) { 1206 type args struct { 1207 obj interface{} 1208 } 1209 tests := []struct { 1210 name string 1211 args args 1212 want interface{} 1213 }{ 1214 { 1215 name: "normal conversion", 1216 args: args{ 1217 obj: &v2.CiliumNetworkPolicy{}, 1218 }, 1219 want: &types.SlimCNP{ 1220 CiliumNetworkPolicy: &v2.CiliumNetworkPolicy{}, 1221 }, 1222 }, 1223 { 1224 name: "delete final state unknown conversion", 1225 args: args{ 1226 obj: cache.DeletedFinalStateUnknown{ 1227 Key: "foo", 1228 Obj: &v2.CiliumNetworkPolicy{}, 1229 }, 1230 }, 1231 want: cache.DeletedFinalStateUnknown{ 1232 Key: "foo", 1233 Obj: &types.SlimCNP{ 1234 CiliumNetworkPolicy: &v2.CiliumNetworkPolicy{}, 1235 }, 1236 }, 1237 }, 1238 { 1239 name: "unknown object type in delete final state unknown conversion", 1240 args: args{ 1241 obj: cache.DeletedFinalStateUnknown{ 1242 Key: "foo", 1243 Obj: 100, 1244 }, 1245 }, 1246 want: cache.DeletedFinalStateUnknown{ 1247 Key: "foo", 1248 Obj: 100, 1249 }, 1250 }, 1251 { 1252 name: "unknown object type in conversion", 1253 args: args{ 1254 obj: 100, 1255 }, 1256 want: 100, 1257 }, 1258 } 1259 for _, tt := range tests { 1260 got := ConvertToCNPWithStatus(tt.args.obj) 1261 c.Assert(got, checker.DeepEquals, tt.want, Commentf("Test Name: %s", tt.name)) 1262 } 1263 } 1264 1265 func (s *K8sSuite) Test_ConvertToCNP(c *C) { 1266 type args struct { 1267 obj interface{} 1268 } 1269 tests := []struct { 1270 name string 1271 args args 1272 want interface{} 1273 }{ 1274 { 1275 name: "normal conversion", 1276 args: args{ 1277 obj: &v2.CiliumNetworkPolicy{}, 1278 }, 1279 want: &types.SlimCNP{ 1280 CiliumNetworkPolicy: &v2.CiliumNetworkPolicy{}, 1281 }, 1282 }, 1283 { 1284 name: "delete final state unknown conversion", 1285 args: args{ 1286 obj: cache.DeletedFinalStateUnknown{ 1287 Key: "foo", 1288 Obj: &v2.CiliumNetworkPolicy{}, 1289 }, 1290 }, 1291 want: cache.DeletedFinalStateUnknown{ 1292 Key: "foo", 1293 Obj: &types.SlimCNP{ 1294 CiliumNetworkPolicy: &v2.CiliumNetworkPolicy{}, 1295 }, 1296 }, 1297 }, 1298 { 1299 name: "unknown object type in delete final state unknown conversion", 1300 args: args{ 1301 obj: cache.DeletedFinalStateUnknown{ 1302 Key: "foo", 1303 Obj: 100, 1304 }, 1305 }, 1306 want: cache.DeletedFinalStateUnknown{ 1307 Key: "foo", 1308 Obj: 100, 1309 }, 1310 }, 1311 { 1312 name: "unknown object type in conversion", 1313 args: args{ 1314 obj: 100, 1315 }, 1316 want: 100, 1317 }, 1318 } 1319 for _, tt := range tests { 1320 got := ConvertToCNP(tt.args.obj) 1321 c.Assert(got, checker.DeepEquals, tt.want, Commentf("Test Name: %s", tt.name)) 1322 } 1323 } 1324 1325 func (s *K8sSuite) Test_ConvertToK8sPod(c *C) { 1326 type args struct { 1327 obj interface{} 1328 } 1329 tests := []struct { 1330 name string 1331 args args 1332 want interface{} 1333 }{ 1334 { 1335 name: "normal conversion", 1336 args: args{ 1337 obj: &v1.Pod{}, 1338 }, 1339 want: &types.Pod{}, 1340 }, 1341 { 1342 name: "delete final state unknown conversion", 1343 args: args{ 1344 obj: cache.DeletedFinalStateUnknown{ 1345 Key: "foo", 1346 Obj: &v1.Pod{}, 1347 }, 1348 }, 1349 want: cache.DeletedFinalStateUnknown{ 1350 Key: "foo", 1351 Obj: &types.Pod{}, 1352 }, 1353 }, 1354 { 1355 name: "unknown object type in delete final state unknown conversion", 1356 args: args{ 1357 obj: cache.DeletedFinalStateUnknown{ 1358 Key: "foo", 1359 Obj: 100, 1360 }, 1361 }, 1362 want: cache.DeletedFinalStateUnknown{ 1363 Key: "foo", 1364 Obj: 100, 1365 }, 1366 }, 1367 { 1368 name: "unknown object type in conversion", 1369 args: args{ 1370 obj: 100, 1371 }, 1372 want: 100, 1373 }, 1374 } 1375 for _, tt := range tests { 1376 got := ConvertToPod(tt.args.obj) 1377 c.Assert(got, checker.DeepEquals, tt.want, Commentf("Test Name: %s", tt.name)) 1378 } 1379 } 1380 1381 func (s *K8sSuite) Test_ConvertToNode(c *C) { 1382 type args struct { 1383 obj interface{} 1384 } 1385 tests := []struct { 1386 name string 1387 args args 1388 want interface{} 1389 }{ 1390 { 1391 name: "normal conversion", 1392 args: args{ 1393 obj: &v1.Node{}, 1394 }, 1395 want: &types.Node{}, 1396 }, 1397 { 1398 name: "delete final state unknown conversion", 1399 args: args{ 1400 obj: cache.DeletedFinalStateUnknown{ 1401 Key: "foo", 1402 Obj: &v1.Node{}, 1403 }, 1404 }, 1405 want: cache.DeletedFinalStateUnknown{ 1406 Key: "foo", 1407 Obj: &types.Node{}, 1408 }, 1409 }, 1410 { 1411 name: "unknown object type in delete final state unknown conversion", 1412 args: args{ 1413 obj: cache.DeletedFinalStateUnknown{ 1414 Key: "foo", 1415 Obj: 100, 1416 }, 1417 }, 1418 want: cache.DeletedFinalStateUnknown{ 1419 Key: "foo", 1420 Obj: 100, 1421 }, 1422 }, 1423 { 1424 name: "unknown object type in conversion", 1425 args: args{ 1426 obj: 100, 1427 }, 1428 want: 100, 1429 }, 1430 } 1431 for _, tt := range tests { 1432 got := ConvertToNode(tt.args.obj) 1433 c.Assert(got, checker.DeepEquals, tt.want, Commentf("Test Name: %s", tt.name)) 1434 } 1435 } 1436 1437 func (s *K8sSuite) Test_ConvertToNamespace(c *C) { 1438 type args struct { 1439 obj interface{} 1440 } 1441 tests := []struct { 1442 name string 1443 args args 1444 want interface{} 1445 }{ 1446 { 1447 name: "normal conversion", 1448 args: args{ 1449 obj: &v1.Namespace{}, 1450 }, 1451 want: &types.Namespace{}, 1452 }, 1453 { 1454 name: "delete final state unknown conversion", 1455 args: args{ 1456 obj: cache.DeletedFinalStateUnknown{ 1457 Key: "foo", 1458 Obj: &v1.Namespace{}, 1459 }, 1460 }, 1461 want: cache.DeletedFinalStateUnknown{ 1462 Key: "foo", 1463 Obj: &types.Namespace{}, 1464 }, 1465 }, 1466 { 1467 name: "unknown object type in delete final state unknown conversion", 1468 args: args{ 1469 obj: cache.DeletedFinalStateUnknown{ 1470 Key: "foo", 1471 Obj: 100, 1472 }, 1473 }, 1474 want: cache.DeletedFinalStateUnknown{ 1475 Key: "foo", 1476 Obj: 100, 1477 }, 1478 }, 1479 { 1480 name: "unknown object type in conversion", 1481 args: args{ 1482 obj: 100, 1483 }, 1484 want: 100, 1485 }, 1486 } 1487 for _, tt := range tests { 1488 got := ConvertToNamespace(tt.args.obj) 1489 c.Assert(got, checker.DeepEquals, tt.want, Commentf("Test Name: %s", tt.name)) 1490 } 1491 } 1492 1493 func (s *K8sSuite) Test_ConvertToCiliumNode(c *C) { 1494 type args struct { 1495 obj interface{} 1496 } 1497 tests := []struct { 1498 name string 1499 args args 1500 want interface{} 1501 }{ 1502 { 1503 name: "normal conversion", 1504 args: args{ 1505 obj: &v2.CiliumNode{}, 1506 }, 1507 want: &v2.CiliumNode{}, 1508 }, 1509 { 1510 name: "delete final state unknown conversion", 1511 args: args{ 1512 obj: cache.DeletedFinalStateUnknown{ 1513 Key: "foo", 1514 Obj: &v2.CiliumNode{}, 1515 }, 1516 }, 1517 want: cache.DeletedFinalStateUnknown{ 1518 Key: "foo", 1519 Obj: &v2.CiliumNode{}, 1520 }, 1521 }, 1522 { 1523 name: "unknown object type in delete final state unknown conversion", 1524 args: args{ 1525 obj: cache.DeletedFinalStateUnknown{ 1526 Key: "foo", 1527 Obj: 100, 1528 }, 1529 }, 1530 want: cache.DeletedFinalStateUnknown{ 1531 Key: "foo", 1532 Obj: 100, 1533 }, 1534 }, 1535 { 1536 name: "unknown object type in conversion", 1537 args: args{ 1538 obj: 100, 1539 }, 1540 want: 100, 1541 }, 1542 } 1543 for _, tt := range tests { 1544 got := ConvertToCiliumNode(tt.args.obj) 1545 c.Assert(got, checker.DeepEquals, tt.want, Commentf("Test Name: %s", tt.name)) 1546 } 1547 } 1548 1549 func (s *K8sSuite) Test_ConvertToCiliumEndpoint(c *C) { 1550 type args struct { 1551 obj interface{} 1552 } 1553 tests := []struct { 1554 name string 1555 args args 1556 want interface{} 1557 }{ 1558 { 1559 name: "normal conversion", 1560 args: args{ 1561 obj: &v2.CiliumEndpoint{}, 1562 }, 1563 want: &types.CiliumEndpoint{ 1564 Encryption: &v2.EncryptionSpec{}, 1565 }, 1566 }, 1567 { 1568 name: "delete final state unknown conversion", 1569 args: args{ 1570 obj: cache.DeletedFinalStateUnknown{ 1571 Key: "foo", 1572 Obj: &v2.CiliumEndpoint{}, 1573 }, 1574 }, 1575 want: cache.DeletedFinalStateUnknown{ 1576 Key: "foo", 1577 Obj: &types.CiliumEndpoint{ 1578 Encryption: &v2.EncryptionSpec{}, 1579 }, 1580 }, 1581 }, 1582 { 1583 name: "unknown object type in delete final state unknown conversion", 1584 args: args{ 1585 obj: cache.DeletedFinalStateUnknown{ 1586 Key: "foo", 1587 Obj: 100, 1588 }, 1589 }, 1590 want: cache.DeletedFinalStateUnknown{ 1591 Key: "foo", 1592 Obj: 100, 1593 }, 1594 }, 1595 { 1596 name: "unknown object type in conversion", 1597 args: args{ 1598 obj: 100, 1599 }, 1600 want: 100, 1601 }, 1602 } 1603 for _, tt := range tests { 1604 got := ConvertToCiliumEndpoint(tt.args.obj) 1605 c.Assert(got, checker.DeepEquals, tt.want, Commentf("Test Name: %s", tt.name)) 1606 } 1607 }