node_ipam_controller_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package nodeipam
  14. import (
  15. "net"
  16. "os"
  17. "os/exec"
  18. "strings"
  19. "testing"
  20. "k8s.io/api/core/v1"
  21. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  22. "k8s.io/client-go/informers"
  23. "k8s.io/client-go/kubernetes/fake"
  24. "k8s.io/kubernetes/pkg/controller"
  25. "k8s.io/kubernetes/pkg/controller/nodeipam/ipam"
  26. "k8s.io/kubernetes/pkg/controller/testutil"
  27. "k8s.io/legacy-cloud-providers/gce"
  28. netutils "k8s.io/utils/net"
  29. )
  30. func newTestNodeIpamController(clusterCIDR []*net.IPNet, serviceCIDR *net.IPNet, secondaryServiceCIDR *net.IPNet, nodeCIDRMaskSizes []int, allocatorType ipam.CIDRAllocatorType) (*Controller, error) {
  31. clientSet := fake.NewSimpleClientset()
  32. fakeNodeHandler := &testutil.FakeNodeHandler{
  33. Existing: []*v1.Node{
  34. {ObjectMeta: metav1.ObjectMeta{Name: "node0"}},
  35. },
  36. Clientset: fake.NewSimpleClientset(),
  37. }
  38. fakeClient := &fake.Clientset{}
  39. fakeInformerFactory := informers.NewSharedInformerFactory(fakeClient, controller.NoResyncPeriodFunc())
  40. fakeNodeInformer := fakeInformerFactory.Core().V1().Nodes()
  41. for _, node := range fakeNodeHandler.Existing {
  42. fakeNodeInformer.Informer().GetStore().Add(node)
  43. }
  44. fakeGCE := gce.NewFakeGCECloud(gce.DefaultTestClusterValues())
  45. return NewNodeIpamController(
  46. fakeNodeInformer, fakeGCE, clientSet,
  47. clusterCIDR, serviceCIDR, secondaryServiceCIDR, nodeCIDRMaskSizes, allocatorType,
  48. )
  49. }
  50. // TestNewNodeIpamControllerWithCIDRMasks tests if the controller can be
  51. // created with combinations of network CIDRs and masks.
  52. func TestNewNodeIpamControllerWithCIDRMasks(t *testing.T) {
  53. emptyServiceCIDR := ""
  54. for _, tc := range []struct {
  55. desc string
  56. clusterCIDR string
  57. serviceCIDR string
  58. secondaryServiceCIDR string
  59. maskSize []int
  60. allocatorType ipam.CIDRAllocatorType
  61. wantFatal bool
  62. }{
  63. {"valid_range_allocator", "10.0.0.0/21", "10.1.0.0/21", emptyServiceCIDR, []int{24}, ipam.RangeAllocatorType, false},
  64. {"valid_range_allocator_dualstack", "10.0.0.0/21,2000::/10", "10.1.0.0/21", emptyServiceCIDR, []int{24, 98}, ipam.RangeAllocatorType, false},
  65. {"valid_range_allocator_dualstack_dualstackservice", "10.0.0.0/21,2000::/10", "10.1.0.0/21", "3000::/10", []int{24, 98}, ipam.RangeAllocatorType, false},
  66. {"valid_cloud_allocator", "10.0.0.0/21", "10.1.0.0/21", emptyServiceCIDR, []int{24}, ipam.CloudAllocatorType, false},
  67. {"valid_ipam_from_cluster", "10.0.0.0/21", "10.1.0.0/21", emptyServiceCIDR, []int{24}, ipam.IPAMFromClusterAllocatorType, false},
  68. {"valid_ipam_from_cloud", "10.0.0.0/21", "10.1.0.0/21", emptyServiceCIDR, []int{24}, ipam.IPAMFromCloudAllocatorType, false},
  69. {"valid_skip_cluster_CIDR_validation_for_cloud_allocator", "invalid", "10.1.0.0/21", emptyServiceCIDR, []int{24}, ipam.CloudAllocatorType, false},
  70. {"invalid_cluster_CIDR", "invalid", "10.1.0.0/21", emptyServiceCIDR, []int{24}, ipam.IPAMFromClusterAllocatorType, true},
  71. {"valid_CIDR_smaller_than_mask_cloud_allocator", "10.0.0.0/26", "10.1.0.0/21", emptyServiceCIDR, []int{24}, ipam.CloudAllocatorType, false},
  72. {"invalid_CIDR_smaller_than_mask_other_allocators", "10.0.0.0/26", "10.1.0.0/21", emptyServiceCIDR, []int{24}, ipam.IPAMFromCloudAllocatorType, true},
  73. {"invalid_serviceCIDR_contains_clusterCIDR", "10.0.0.0/23", "10.0.0.0/21", emptyServiceCIDR, []int{24}, ipam.IPAMFromClusterAllocatorType, true},
  74. {"invalid_CIDR_mask_size", "10.0.0.0/24,2000::/64", "10.1.0.0/21", emptyServiceCIDR, []int{24, 48}, ipam.IPAMFromClusterAllocatorType, true},
  75. } {
  76. t.Run(tc.desc, func(t *testing.T) {
  77. clusterCidrs, _ := netutils.ParseCIDRs(strings.Split(tc.clusterCIDR, ","))
  78. _, serviceCIDRIpNet, _ := net.ParseCIDR(tc.serviceCIDR)
  79. _, secondaryServiceCIDRIpNet, _ := net.ParseCIDR(tc.secondaryServiceCIDR)
  80. if os.Getenv("EXIT_ON_FATAL") == "1" {
  81. // This is the subprocess which runs the actual code.
  82. newTestNodeIpamController(clusterCidrs, serviceCIDRIpNet, secondaryServiceCIDRIpNet, tc.maskSize, tc.allocatorType)
  83. return
  84. }
  85. // This is the host process that monitors the exit code of the subprocess.
  86. cmd := exec.Command(os.Args[0], "-test.run=TestNewNodeIpamControllerWithCIDRMasks/"+tc.desc)
  87. cmd.Env = append(os.Environ(), "EXIT_ON_FATAL=1")
  88. err := cmd.Run()
  89. var gotFatal bool
  90. if err != nil {
  91. exitErr, ok := err.(*exec.ExitError)
  92. if !ok {
  93. t.Fatalf("Failed to run subprocess: %v", err)
  94. }
  95. gotFatal = !exitErr.Success()
  96. }
  97. if gotFatal != tc.wantFatal {
  98. t.Errorf("newTestNodeIpamController(%v, %v, %v, %v) : gotFatal = %t ; wantFatal = %t", clusterCidrs, serviceCIDRIpNet, tc.maskSize, tc.allocatorType, gotFatal, tc.wantFatal)
  99. }
  100. })
  101. }
  102. }