k8s.io/kubernetes@v1.29.3/test/integration/ipamperf/util.go (about) 1 /* 2 Copyright 2018 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 ipamperf 18 19 import ( 20 "context" 21 "time" 22 23 "k8s.io/api/core/v1" 24 apierrors "k8s.io/apimachinery/pkg/api/errors" 25 "k8s.io/apimachinery/pkg/api/resource" 26 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 27 clientset "k8s.io/client-go/kubernetes" 28 "k8s.io/klog/v2" 29 ) 30 31 const ( 32 maxCreateRetries = 10 33 retryDelay = 10 * time.Second 34 ) 35 36 var ( 37 baseNodeTemplate = &v1.Node{ 38 ObjectMeta: metav1.ObjectMeta{ 39 GenerateName: "sample-node-", 40 }, 41 Status: v1.NodeStatus{ 42 Capacity: v1.ResourceList{ 43 v1.ResourcePods: *resource.NewQuantity(110, resource.DecimalSI), 44 v1.ResourceCPU: resource.MustParse("4"), 45 v1.ResourceMemory: resource.MustParse("32Gi"), 46 }, 47 Phase: v1.NodeRunning, 48 Conditions: []v1.NodeCondition{ 49 {Type: v1.NodeReady, Status: v1.ConditionTrue}, 50 }, 51 }, 52 } 53 ) 54 55 func deleteNodes(clientSet *clientset.Clientset) { 56 klog.Info("Deleting nodes") 57 noGrace := int64(0) 58 if err := clientSet.CoreV1().Nodes().DeleteCollection(context.TODO(), metav1.DeleteOptions{GracePeriodSeconds: &noGrace}, metav1.ListOptions{}); err != nil { 59 klog.Errorf("Error deleting node: %v", err) 60 } 61 } 62 63 func createNodes(clientSet *clientset.Clientset, config *Config) error { 64 klog.Infof("Creating %d nodes", config.NumNodes) 65 for i := 0; i < config.NumNodes; i++ { 66 var err error 67 for j := 0; j < maxCreateRetries; j++ { 68 if _, err = clientSet.CoreV1().Nodes().Create(context.TODO(), baseNodeTemplate, metav1.CreateOptions{}); err != nil && apierrors.IsServerTimeout(err) { 69 klog.Infof("Server timeout creating nodes, retrying after %v", retryDelay) 70 time.Sleep(retryDelay) 71 continue 72 } 73 break 74 } 75 if err != nil { 76 klog.Errorf("Error creating nodes: %v", err) 77 return err 78 } 79 } 80 klog.Infof("%d nodes created", config.NumNodes) 81 return nil 82 }