k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/framework/multiclient.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 framework 18 19 import ( 20 "fmt" 21 "sync" 22 23 "k8s.io/client-go/dynamic" 24 clientset "k8s.io/client-go/kubernetes" 25 "k8s.io/perf-tests/clusterloader2/pkg/framework/config" 26 ) 27 28 // MultiClientSet is a set of kubernetes clients. 29 type MultiClientSet struct { 30 lock sync.Mutex 31 clients []clientset.Interface 32 current int 33 } 34 35 // NewMultiClientSet creates new MultiClientSet for given kubeconfig and number. 36 func NewMultiClientSet(kubeconfigPath string, number int) (*MultiClientSet, error) { 37 m := MultiClientSet{ 38 clients: make([]clientset.Interface, number), 39 } 40 for i := 0; i < number; i++ { 41 conf, err := config.PrepareConfig(kubeconfigPath) 42 if err != nil { 43 return nil, fmt.Errorf("config prepare failed: %v", err) 44 } 45 if number < 1 { 46 return nil, fmt.Errorf("incorrect clients number") 47 } 48 m.clients[i], err = clientset.NewForConfig(conf) 49 if err != nil { 50 return nil, fmt.Errorf("creating clientset failed: %v", err) 51 } 52 } 53 return &m, nil 54 } 55 56 // GetClient return one client instance from the set using round robin. 57 func (m *MultiClientSet) GetClient() clientset.Interface { 58 m.lock.Lock() 59 defer m.lock.Unlock() 60 m.current = (m.current + 1) % len(m.clients) 61 return m.clients[m.current] 62 } 63 64 // MultiDynamicClient is a set of dynamic client. 65 type MultiDynamicClient struct { 66 lock sync.Mutex 67 clients []dynamic.Interface 68 current int 69 } 70 71 // NewMultiDynamicClient creates new MultiDynamicClient for given kubeconfig and number. 72 func NewMultiDynamicClient(kubeconfigPath string, number int) (*MultiDynamicClient, error) { 73 m := MultiDynamicClient{ 74 clients: make([]dynamic.Interface, number), 75 } 76 for i := 0; i < number; i++ { 77 conf, err := config.PrepareConfig(kubeconfigPath) 78 if err != nil { 79 return nil, fmt.Errorf("config prepare failed: %v", err) 80 } 81 if number < 1 { 82 return nil, fmt.Errorf("incorrect clients number") 83 } 84 m.clients[i], err = dynamic.NewForConfig(conf) 85 if err != nil { 86 return nil, fmt.Errorf("creating dynamic config failed: %v", err) 87 } 88 } 89 return &m, nil 90 } 91 92 // GetClient return one client instance from the set using round robin. 93 func (m *MultiDynamicClient) GetClient() dynamic.Interface { 94 m.lock.Lock() 95 defer m.lock.Unlock() 96 m.current = (m.current + 1) % len(m.clients) 97 return m.clients[m.current] 98 }