-
Notifications
You must be signed in to change notification settings - Fork 888
/
membercluster_client.go
221 lines (185 loc) · 7.21 KB
/
membercluster_client.go
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package util
import (
"context"
"fmt"
"net/http"
"net/url"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/dynamic"
kubeclientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/scale"
"k8s.io/klog/v2"
controllerruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1"
)
// ClusterClient stands for a cluster Clientset for the given member cluster
type ClusterClient struct {
KubeClient *kubeclientset.Clientset
ClusterName string
}
// DynamicClusterClient stands for a dynamic client for the given member cluster
type DynamicClusterClient struct {
DynamicClientSet dynamic.Interface
ClusterName string
}
// ClusterScaleClient stands for a cluster ClientSet with scale client for the given member cluster
type ClusterScaleClient struct {
KubeClient *kubeclientset.Clientset
ScaleClient scale.ScalesGetter
ClusterName string
}
// Config holds the common attributes that can be passed to a Kubernetes client on
// initialization.
// ClientOption holds the attributes that should be injected to a Kubernetes client.
type ClientOption struct {
// QPS indicates the maximum QPS to the master from this client.
// If it's zero, the created RESTClient will use DefaultQPS: 5
QPS float32
// Burst indicates the maximum burst for throttle.
// If it's zero, the created RESTClient will use DefaultBurst: 10.
Burst int
}
// NewClusterScaleClientSet returns a ClusterScaleClient for the given member cluster.
func NewClusterScaleClientSet(clusterName string, client client.Client) (*ClusterScaleClient, error) {
clusterConfig, err := BuildClusterConfig(clusterName, clusterGetter(client), secretGetter(client))
if err != nil {
return nil, err
}
var clusterScaleClientSet = ClusterScaleClient{ClusterName: clusterName}
if clusterConfig != nil {
hpaClient := kubeclientset.NewForConfigOrDie(clusterConfig)
scaleKindResolver := scale.NewDiscoveryScaleKindResolver(hpaClient.Discovery())
mapper, err := apiutil.NewDiscoveryRESTMapper(clusterConfig)
if err != nil {
return nil, err
}
scaleClient, err := scale.NewForConfig(clusterConfig, mapper, dynamic.LegacyAPIPathResolverFunc, scaleKindResolver)
if err != nil {
return nil, err
}
clusterScaleClientSet.KubeClient = hpaClient
clusterScaleClientSet.ScaleClient = scaleClient
}
return &clusterScaleClientSet, nil
}
// NewClusterClientSet returns a ClusterClient for the given member cluster.
func NewClusterClientSet(clusterName string, client client.Client, clientOption *ClientOption) (*ClusterClient, error) {
clusterConfig, err := BuildClusterConfig(clusterName, clusterGetter(client), secretGetter(client))
if err != nil {
return nil, err
}
var clusterClientSet = ClusterClient{ClusterName: clusterName}
if clusterConfig != nil {
if clientOption != nil {
clusterConfig.QPS = clientOption.QPS
clusterConfig.Burst = clientOption.Burst
}
clusterClientSet.KubeClient = kubeclientset.NewForConfigOrDie(clusterConfig)
}
return &clusterClientSet, nil
}
// NewClusterClientSetForAgent returns a ClusterClient for the given member cluster which will be used in karmada agent.
func NewClusterClientSetForAgent(clusterName string, _ client.Client, clientOption *ClientOption) (*ClusterClient, error) {
clusterConfig, err := controllerruntime.GetConfig()
if err != nil {
return nil, fmt.Errorf("error building kubeconfig of member cluster: %s", err.Error())
}
var clusterClientSet = ClusterClient{ClusterName: clusterName}
if clusterConfig != nil {
if clientOption != nil {
clusterConfig.QPS = clientOption.QPS
clusterConfig.Burst = clientOption.Burst
}
clusterClientSet.KubeClient = kubeclientset.NewForConfigOrDie(clusterConfig)
}
return &clusterClientSet, nil
}
// NewClusterDynamicClientSet returns a dynamic client for the given member cluster.
func NewClusterDynamicClientSet(clusterName string, client client.Client) (*DynamicClusterClient, error) {
clusterConfig, err := BuildClusterConfig(clusterName, clusterGetter(client), secretGetter(client))
if err != nil {
return nil, err
}
var clusterClientSet = DynamicClusterClient{ClusterName: clusterName}
if clusterConfig != nil {
clusterClientSet.DynamicClientSet = dynamic.NewForConfigOrDie(clusterConfig)
}
return &clusterClientSet, nil
}
// NewClusterDynamicClientSetForAgent returns a dynamic client for the given member cluster which will be used in karmada agent.
func NewClusterDynamicClientSetForAgent(clusterName string, _ client.Client) (*DynamicClusterClient, error) {
clusterConfig, err := controllerruntime.GetConfig()
if err != nil {
return nil, fmt.Errorf("error building kubeconfig of member cluster: %s", err.Error())
}
var clusterClientSet = DynamicClusterClient{ClusterName: clusterName}
if clusterConfig != nil {
clusterClientSet.DynamicClientSet = dynamic.NewForConfigOrDie(clusterConfig)
}
return &clusterClientSet, nil
}
// BuildClusterConfig return rest config for member cluster.
func BuildClusterConfig(clusterName string,
clusterGetter func(string) (*clusterv1alpha1.Cluster, error),
secretGetter func(string, string) (*corev1.Secret, error)) (*rest.Config, error) {
cluster, err := clusterGetter(clusterName)
if err != nil {
return nil, err
}
apiEndpoint := cluster.Spec.APIEndpoint
if apiEndpoint == "" {
return nil, fmt.Errorf("the api endpoint of cluster %s is empty", clusterName)
}
if cluster.Spec.SecretRef == nil {
return nil, fmt.Errorf("cluster %s does not have a secret", clusterName)
}
secret, err := secretGetter(cluster.Spec.SecretRef.Namespace, cluster.Spec.SecretRef.Name)
if err != nil {
return nil, err
}
token, ok := secret.Data[clusterv1alpha1.SecretTokenKey]
if !ok || len(token) == 0 {
return nil, fmt.Errorf("the secret for cluster %s is missing a non-empty value for %q", clusterName, clusterv1alpha1.SecretTokenKey)
}
// Initialize cluster configuration.
clusterConfig := &rest.Config{
BearerToken: string(token),
Host: apiEndpoint,
}
// Handle TLS configuration.
if cluster.Spec.InsecureSkipTLSVerification {
clusterConfig.TLSClientConfig.Insecure = true
} else {
ca, ok := secret.Data[clusterv1alpha1.SecretCADataKey]
if !ok {
return nil, fmt.Errorf("the secret for cluster %s is missing the CA data key %q", clusterName, clusterv1alpha1.SecretCADataKey)
}
clusterConfig.TLSClientConfig = rest.TLSClientConfig{CAData: ca}
}
// Handle proxy configuration.
if cluster.Spec.ProxyURL != "" {
proxy, err := url.Parse(cluster.Spec.ProxyURL)
if err != nil {
klog.Errorf("parse proxy error. %v", err)
return nil, err
}
clusterConfig.Proxy = http.ProxyURL(proxy)
}
return clusterConfig, nil
}
func clusterGetter(client client.Client) func(string) (*clusterv1alpha1.Cluster, error) {
return func(cluster string) (*clusterv1alpha1.Cluster, error) {
return GetCluster(client, cluster)
}
}
func secretGetter(client client.Client) func(string, string) (*corev1.Secret, error) {
return func(namespace string, name string) (*corev1.Secret, error) {
secret := &corev1.Secret{}
err := client.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: name}, secret)
return secret, err
}
}