k8s.io/kubernetes@v1.29.3/pkg/controller/nodeipam/ipam/controller_test.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package ipam 18 19 import ( 20 "net" 21 "testing" 22 23 "k8s.io/kubernetes/pkg/controller/nodeipam/ipam/cidrset" 24 "k8s.io/kubernetes/pkg/controller/nodeipam/ipam/test" 25 ) 26 27 func TestOccupyServiceCIDR(t *testing.T) { 28 const clusterCIDR = "10.1.0.0/16" 29 30 TestCase: 31 for _, tc := range []struct { 32 serviceCIDR string 33 }{ 34 {"10.0.255.0/24"}, 35 {"10.1.0.0/24"}, 36 {"10.1.255.0/24"}, 37 {"10.2.0.0/24"}, 38 } { 39 serviceCIDR := test.MustParseCIDR(tc.serviceCIDR) 40 set, err := cidrset.NewCIDRSet(test.MustParseCIDR(clusterCIDR), 24) 41 if err != nil { 42 t.Errorf("test case %+v: NewCIDRSet() = %v, want nil", tc, err) 43 } 44 if err := occupyServiceCIDR(set, test.MustParseCIDR(clusterCIDR), serviceCIDR); err != nil { 45 t.Errorf("test case %+v: occupyServiceCIDR() = %v, want nil", tc, err) 46 } 47 // Allocate until full. 48 var cidrs []*net.IPNet 49 for { 50 cidr, err := set.AllocateNext() 51 if err != nil { 52 if err == cidrset.ErrCIDRRangeNoCIDRsRemaining { 53 break 54 } 55 t.Errorf("set.AllocateNext() = %v, want %v", err, cidrset.ErrCIDRRangeNoCIDRsRemaining) 56 continue TestCase 57 } 58 cidrs = append(cidrs, cidr) 59 } 60 // No allocated CIDR range should intersect with serviceCIDR. 61 for _, c := range cidrs { 62 if c.Contains(serviceCIDR.IP) || serviceCIDR.Contains(c.IP) { 63 t.Errorf("test case %+v: allocated CIDR %v from service range", tc, c) 64 } 65 } 66 } 67 }