k8s.io/kubernetes@v1.29.3/pkg/controller/nodeipam/node_ipam_controller_test.go (about) 1 //go:build !providerless 2 // +build !providerless 3 4 /* 5 Copyright 2018 The Kubernetes Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 */ 19 20 package nodeipam 21 22 import ( 23 "context" 24 "errors" 25 "net" 26 "strings" 27 "testing" 28 29 v1 "k8s.io/api/core/v1" 30 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 31 "k8s.io/client-go/informers" 32 "k8s.io/client-go/kubernetes/fake" 33 "k8s.io/klog/v2/ktesting" 34 "k8s.io/kubernetes/pkg/controller/nodeipam/ipam" 35 "k8s.io/kubernetes/pkg/controller/testutil" 36 "k8s.io/legacy-cloud-providers/gce" 37 netutils "k8s.io/utils/net" 38 ) 39 40 func newTestNodeIpamController(ctx context.Context, clusterCIDR []*net.IPNet, serviceCIDR *net.IPNet, secondaryServiceCIDR *net.IPNet, nodeCIDRMaskSizes []int, allocatorType ipam.CIDRAllocatorType) (*Controller, error) { 41 clientSet := fake.NewSimpleClientset() 42 fakeNodeHandler := &testutil.FakeNodeHandler{ 43 Existing: []*v1.Node{ 44 {ObjectMeta: metav1.ObjectMeta{Name: "node0"}}, 45 }, 46 Clientset: fake.NewSimpleClientset(), 47 } 48 fakeClient := &fake.Clientset{} 49 fakeInformerFactory := informers.NewSharedInformerFactory(fakeClient, 0) 50 fakeNodeInformer := fakeInformerFactory.Core().V1().Nodes() 51 52 for _, node := range fakeNodeHandler.Existing { 53 fakeNodeInformer.Informer().GetStore().Add(node) 54 } 55 56 fakeGCE := gce.NewFakeGCECloud(gce.DefaultTestClusterValues()) 57 return NewNodeIpamController( 58 ctx, 59 fakeNodeInformer, fakeGCE, clientSet, 60 clusterCIDR, serviceCIDR, secondaryServiceCIDR, nodeCIDRMaskSizes, allocatorType, 61 ) 62 } 63 64 // TestNewNodeIpamControllerWithCIDRMasks tests if the controller can be 65 // created with combinations of network CIDRs and masks. 66 func TestNewNodeIpamControllerWithCIDRMasks(t *testing.T) { 67 emptyServiceCIDR := "" 68 for _, tc := range []struct { 69 desc string 70 clusterCIDR string 71 serviceCIDR string 72 secondaryServiceCIDR string 73 maskSize []int 74 allocatorType ipam.CIDRAllocatorType 75 expectedError error 76 }{ 77 {"valid_range_allocator", "10.0.0.0/21", "10.1.0.0/21", emptyServiceCIDR, []int{24}, ipam.RangeAllocatorType, nil}, 78 {"valid_range_allocator_dualstack", "10.0.0.0/21,2000::/48", "10.1.0.0/21", emptyServiceCIDR, []int{24, 64}, ipam.RangeAllocatorType, nil}, 79 {"valid_range_allocator_dualstack_dualstackservice", "10.0.0.0/21,2000::/48", "10.1.0.0/21", "3000::/112", []int{24, 64}, ipam.RangeAllocatorType, nil}, 80 {"valid_cloud_allocator", "10.0.0.0/21", "10.1.0.0/21", emptyServiceCIDR, []int{24}, ipam.CloudAllocatorType, nil}, 81 {"valid_ipam_from_cluster", "10.0.0.0/21", "10.1.0.0/21", emptyServiceCIDR, []int{24}, ipam.IPAMFromClusterAllocatorType, nil}, 82 {"valid_ipam_from_cloud", "10.0.0.0/21", "10.1.0.0/21", emptyServiceCIDR, []int{24}, ipam.IPAMFromCloudAllocatorType, nil}, 83 {"valid_skip_cluster_CIDR_validation_for_cloud_allocator", "invalid", "10.1.0.0/21", emptyServiceCIDR, []int{24}, ipam.CloudAllocatorType, nil}, 84 {"valid_CIDR_larger_than_mask_cloud_allocator", "10.0.0.0/16", "10.1.0.0/21", emptyServiceCIDR, []int{24}, ipam.CloudAllocatorType, nil}, 85 {"invalid_cluster_CIDR", "", "10.1.0.0/21", emptyServiceCIDR, []int{24}, ipam.IPAMFromClusterAllocatorType, errors.New("Controller: Must specify --cluster-cidr if --allocate-node-cidrs is set")}, 86 {"invalid_CIDR_smaller_than_mask_other_allocators", "10.0.0.0/26", "10.1.0.0/21", emptyServiceCIDR, []int{24}, ipam.IPAMFromCloudAllocatorType, errors.New("Controller: Invalid --cluster-cidr, mask size of cluster CIDR must be less than or equal to --node-cidr-mask-size configured for CIDR family")}, 87 {"invalid_serviceCIDR_contains_clusterCIDR", "10.0.0.0/16", "10.0.0.0/8", emptyServiceCIDR, []int{24}, ipam.IPAMFromClusterAllocatorType, errors.New("error creating ipam controller: failed after occupy serviceCIDR: CIDR allocation failed; there are no remaining CIDRs left to allocate in the accepted range")}, 88 {"invalid_CIDR_mask_size", "10.0.0.0/24,2000::/64", "10.1.0.0/21", emptyServiceCIDR, []int{24, 48}, ipam.IPAMFromClusterAllocatorType, errors.New("Controller: Invalid --cluster-cidr, mask size of cluster CIDR must be less than or equal to --node-cidr-mask-size configured for CIDR family")}, 89 } { 90 test := tc 91 _, ctx := ktesting.NewTestContext(t) 92 t.Run(test.desc, func(t *testing.T) { 93 t.Parallel() 94 clusterCidrs, err := netutils.ParseCIDRs(strings.Split(test.clusterCIDR, ",")) 95 if err != nil { 96 clusterCidrs = nil 97 } 98 _, serviceCIDRIpNet, err := netutils.ParseCIDRSloppy(test.serviceCIDR) 99 if err != nil { 100 serviceCIDRIpNet = nil 101 } 102 _, secondaryServiceCIDRIpNet, err := netutils.ParseCIDRSloppy(test.secondaryServiceCIDR) 103 if err != nil { 104 secondaryServiceCIDRIpNet = nil 105 } 106 _, err = newTestNodeIpamController(ctx, clusterCidrs, serviceCIDRIpNet, secondaryServiceCIDRIpNet, test.maskSize, test.allocatorType) 107 if test.expectedError == nil { 108 if err != nil { 109 t.Errorf("Test %s, unexpected error: %v", test.desc, err) 110 } 111 } else { 112 if err.Error() != test.expectedError.Error() { 113 t.Errorf("Test %s, got error: %v, expected error: %v", test.desc, err, test.expectedError) 114 } 115 } 116 }) 117 } 118 }