-
Notifications
You must be signed in to change notification settings - Fork 330
/
client.go
125 lines (110 loc) · 3.77 KB
/
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
/*
* Tencent is pleased to support the open source community by making TKEStack
* available.
*
* Copyright (C) 2012-2021 Tencent. All Rights Reserved.
*
* 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
*
* https://opensource.org/licenses/Apache-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 OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
*/
package kubernetes
import (
"context"
"fmt"
"sync"
istioconfig "istio.io/client-go/pkg/apis/config/v1alpha2"
istioscheme "istio.io/client-go/pkg/clientset/versioned/scheme"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
kubescheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
"tkestack.io/tke/pkg/util/log"
)
// Client facade interface
type Client interface {
// RestConfig given cluster id to get Kubernetes cluster rest config
RestConfig(cluster string) (*rest.Config, error)
// Cluster given cluster id to get Kubernetes cluster client
Cluster(cluster string) (ctrlclient.Client, error)
// Istio given cluster id to get Istio client
Istio(cluster string) (ctrlclient.Client, error)
}
// ClusterProvider retrieve cluster kubeconfig provider interface
type ClusterProvider interface {
// RestConfig Kubernetes cluster rest config
RestConfig(clusterName string) (*rest.Config, error)
// Client Kubernetes cluster client with scheme
Client(clusterName string, scheme *runtime.Scheme) (ctrlclient.Client, error)
}
var (
ClusterNameToClient sync.Map
ClusterNameToIstioClient sync.Map
)
type client struct {
clusterProvider ClusterProvider
}
// New new client
func New(provider ClusterProvider) Client {
return &client{
clusterProvider: provider,
}
}
// RestConfig
func (c *client) RestConfig(clusterName string) (*rest.Config, error) {
restConfig, err := c.clusterProvider.RestConfig(clusterName)
return restConfig, err
}
// Cluster
func (c *client) Cluster(clusterName string) (ctrlclient.Client, error) {
// First check from cache
if item, ok := ClusterNameToClient.Load(clusterName); ok {
// Check if is available
clusterClient := item.(ctrlclient.Client)
ns := &corev1.Namespace{}
err := clusterClient.Get(context.TODO(), types.NamespacedName{Name: metav1.NamespaceSystem}, ns)
if err == nil {
return clusterClient, nil
}
ClusterNameToClient.Delete(clusterName)
}
clusterClient, err := c.clusterProvider.Client(clusterName, kubescheme.Scheme)
if err != nil {
log.Errorf("create kubernetes client failed: %v", err)
return nil, fmt.Errorf("create kubernetes client failed")
}
ClusterNameToClient.Store(clusterName, clusterClient)
return clusterClient, nil
}
// Istio
func (c *client) Istio(clusterName string) (ctrlclient.Client, error) {
// First check from cache
if item, ok := ClusterNameToIstioClient.Load(clusterName); ok {
// Check if is available
istioClient := item.(ctrlclient.Client)
err := istioClient.List(context.TODO(), &istioconfig.RuleList{})
if err == nil {
return istioClient, nil
}
log.Warnf("load istio client from cache: %v", err)
ClusterNameToIstioClient.Delete(clusterName)
}
clusterClient, err := c.clusterProvider.Client(clusterName, istioscheme.Scheme)
if err != nil {
log.Errorf("create istio client failed: %v", err)
return nil, fmt.Errorf("create istio client failed")
}
ClusterNameToIstioClient.Store(clusterName, clusterClient)
return clusterClient, nil
}