k8s.io/kubernetes@v1.29.3/pkg/controller/nodeipam/ipam/test/utils.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 test 18 19 import ( 20 "net" 21 "time" 22 23 "k8s.io/apimachinery/pkg/util/wait" 24 "k8s.io/client-go/informers" 25 coreinformers "k8s.io/client-go/informers/core/v1" 26 "k8s.io/client-go/kubernetes/fake" 27 "k8s.io/kubernetes/pkg/controller" 28 "k8s.io/kubernetes/pkg/controller/testutil" 29 netutils "k8s.io/utils/net" 30 ) 31 32 const NodePollInterval = 10 * time.Millisecond 33 34 var AlwaysReady = func() bool { return true } 35 36 // MustParseCIDR returns the CIDR range parsed from s or panics if the string 37 // cannot be parsed. 38 func MustParseCIDR(s string) *net.IPNet { 39 _, ret, err := netutils.ParseCIDRSloppy(s) 40 if err != nil { 41 panic(err) 42 } 43 return ret 44 } 45 46 // FakeNodeInformer creates a fakeNodeInformer using the provided fakeNodeHandler. 47 func FakeNodeInformer(fakeNodeHandler *testutil.FakeNodeHandler) coreinformers.NodeInformer { 48 fakeClient := &fake.Clientset{} 49 fakeInformerFactory := informers.NewSharedInformerFactory(fakeClient, controller.NoResyncPeriodFunc()) 50 fakeNodeInformer := fakeInformerFactory.Core().V1().Nodes() 51 52 for _, node := range fakeNodeHandler.Existing { 53 fakeNodeInformer.Informer().GetStore().Add(node) 54 } 55 56 return fakeNodeInformer 57 } 58 59 func WaitForUpdatedNodeWithTimeout(nodeHandler *testutil.FakeNodeHandler, number int, timeout time.Duration) error { 60 return wait.Poll(NodePollInterval, timeout, func() (bool, error) { 61 if len(nodeHandler.GetUpdatedNodesCopy()) >= number { 62 return true, nil 63 } 64 return false, nil 65 }) 66 }