-
Notifications
You must be signed in to change notification settings - Fork 631
Expand file tree
/
Copy pathmulticlient.go
More file actions
100 lines (88 loc) · 2.8 KB
/
multiclient.go
File metadata and controls
100 lines (88 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package framework
import (
"fmt"
"sync"
"k8s.io/client-go/dynamic"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/perf-tests/clusterloader2/pkg/framework/config"
)
// MultiClientSet is a set of kubernetes clients.
type MultiClientSet struct {
lock sync.Mutex
clients []clientset.Interface
current int
}
// NewMultiClientSet creates new MultiClientSet for given kubeconfig and number.
func NewMultiClientSet(kubeconfigPath string, number int) (*MultiClientSet, error) {
m := MultiClientSet{
clients: make([]clientset.Interface, number),
}
for i := 0; i < number; i++ {
conf, err := config.PrepareConfig(kubeconfigPath)
if err != nil {
return nil, fmt.Errorf("config prepare failed: %v", err)
}
if number < 1 {
return nil, fmt.Errorf("incorrect clients number")
}
m.clients[i], err = clientset.NewForConfig(conf)
if err != nil {
return nil, fmt.Errorf("creating clientset failed: %v", err)
}
}
return &m, nil
}
// GetClient return one client instance from the set using round robin.
func (m *MultiClientSet) GetClient() clientset.Interface {
m.lock.Lock()
defer m.lock.Unlock()
m.current = (m.current + 1) % len(m.clients)
return m.clients[m.current]
}
// MultiDynamicClient is a set of dynamic client.
type MultiDynamicClient struct {
lock sync.Mutex
clients []dynamic.Interface
current int
}
// NewMultiDynamicClient creates new MultiDynamicClient for given kubeconfig and number.
func NewMultiDynamicClient(kubeconfigPath string, number int) (*MultiDynamicClient, error) {
m := MultiDynamicClient{
clients: make([]dynamic.Interface, number),
}
for i := 0; i < number; i++ {
conf, err := config.PrepareConfig(kubeconfigPath)
if err != nil {
return nil, fmt.Errorf("config prepare failed: %v", err)
}
conf.QPS = 500
conf.Burst = 500
if number < 1 {
return nil, fmt.Errorf("incorrect clients number")
}
m.clients[i], err = dynamic.NewForConfig(conf)
if err != nil {
return nil, fmt.Errorf("creating dynamic config failed: %v", err)
}
}
return &m, nil
}
// GetClient return one client instance from the set using round robin.
func (m *MultiDynamicClient) GetClient() dynamic.Interface {
m.lock.Lock()
defer m.lock.Unlock()
m.current = (m.current + 1) % len(m.clients)
return m.clients[m.current]
}