-
Notifications
You must be signed in to change notification settings - Fork 18
/
k8sutils.go
317 lines (272 loc) · 9.98 KB
/
k8sutils.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* Copyright 2021 VMware, Inc.
* 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
* 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 k8sutils
import (
"context"
"fmt"
"os"
"sync"
akov1 "github.com/vmware/load-balancer-and-ingress-services-for-kubernetes/pkg/client/v1alpha1/clientset/versioned"
containerutils "github.com/vmware/load-balancer-and-ingress-services-for-kubernetes/pkg/utils"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
coreinformers "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/workqueue"
"github.com/vmware/global-load-balancing-services-for-kubernetes/gslb/gslbutils"
"github.com/vmware/global-load-balancing-services-for-kubernetes/service_discovery/utils"
)
const (
ServiceImportFullIndexer = "clusterNameNamespaceIndex"
ServiceImportClusterIndexer = "clusterIndex"
)
type K8sServiceDiscoveryConfig struct {
clientset *kubernetes.Clientset
amkov1Clientset *akov1.Clientset
clusters []*K8sClusterConfig
}
var k8sCfg *K8sServiceDiscoveryConfig
func InitK8sServiceDiscoveryConfig(cfg *rest.Config) (*K8sServiceDiscoveryConfig, error) {
kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
return nil, fmt.Errorf("error in initializing k8s service discovery config: %v", err)
}
amkoClient, err := akov1.NewForConfig(cfg)
if err != nil {
return nil, fmt.Errorf("error in initializing k8s service discovery config: %v", err)
}
k8sCfg = &K8sServiceDiscoveryConfig{
clientset: kubeClient,
amkov1Clientset: amkoClient,
}
return k8sCfg, nil
}
func (sdc *K8sServiceDiscoveryConfig) SetClusterConfigs(cc []*K8sClusterConfig) {
sdc.clusters = cc
}
func (sdc *K8sServiceDiscoveryConfig) GetClientset() *kubernetes.Clientset {
return sdc.clientset
}
func (sdc *K8sServiceDiscoveryConfig) GetAmkoV1Clientset() *akov1.Clientset {
return sdc.amkov1Clientset
}
const (
// SDKubePath is to be used for initializing member cluster clients, this constant
// must not be used directly, IsKubePathSet() must be checked before using the below
// constant
SDKubePath = "/tmp/sd-kubeconfig"
)
var kubePathInit bool
var kubePathOnce sync.Once
// IsKubePathSet returns true if the kubeconfig exists in the SDKubePath
func IsKubePathSet() bool {
kubePathOnce.Do(func() {
_, err := os.Stat(SDKubePath)
if err != nil {
gslbutils.Errf("error in fetching file %s: %v", SDKubePath, err)
return
}
kubePathInit = true
})
return kubePathInit
}
type K8sClusterConfig struct {
name string
informers *containerutils.Informers
workqueue []workqueue.RateLimitingInterface
}
// InitK8sClusterConfig initializes a kubernetes cluster client and informers.
// "name" must have the cluster context name.
func InitK8sClusterConfig(cname string) (*K8sClusterConfig, error) {
informersArg := make(map[string]interface{})
gslbutils.Logf("cluster: %s, msg: initializing clientset", cname)
if !IsKubePathSet() {
return nil, fmt.Errorf("can't initialize clientset for cluster %s, kubeconfig path is unset", cname)
}
cfg, err := BuildContextConfig(SDKubePath, cname)
if err != nil {
return nil, fmt.Errorf("error in building context config for %s: %v", cname, err)
}
kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
return nil, fmt.Errorf("error in creating a clientset for %s: %v", cname, err)
}
informersArg[containerutils.INFORMERS_INSTANTIATE_ONCE] = false
informersToStart, err := InformersToRegister(kubeClient, cname)
if err != nil {
return nil, fmt.Errorf("error in getting informers for %s: %v", cname, err)
}
informerInstance := containerutils.NewInformers(
containerutils.KubeClientIntf{
ClientSet: kubeClient,
},
informersToStart,
informersArg,
)
return &K8sClusterConfig{
name: cname,
informers: informerInstance,
workqueue: containerutils.SharedWorkQueue().GetQueueByName(containerutils.ObjectIngestionLayer).Workqueue,
}, nil
}
func (k8sCluster *K8sClusterConfig) Name() string {
return k8sCluster.name
}
func (k8sCluster *K8sClusterConfig) ClientSet() kubernetes.Interface {
return k8sCluster.informers.ClientSet
}
func (k8sCluster *K8sClusterConfig) ServiceInformer() coreinformers.ServiceInformer {
return k8sCluster.informers.ServiceInformer
}
func (k8sCluster *K8sClusterConfig) GetNodes() (*corev1.NodeList, error) {
nodes, err := k8sCluster.informers.ClientSet.CoreV1().Nodes().List(context.TODO(), v1.ListOptions{})
if err != nil {
return nil, err
}
return nodes, nil
}
// GetNodeFromInformer returns the IP of the node and the error (if any)
func (k8sCluster *K8sClusterConfig) GetNodeFromInformer(nodeName string) (string, error) {
node, err := k8sCluster.informers.NodeInformer.Lister().Get(nodeName)
if err != nil {
return "", err
}
return GetNodeIP(node.Status)
}
func (k8sCluster *K8sClusterConfig) GetSvcFromInformer(ns, svcName string) (*corev1.Service, error) {
svc, err := k8sCluster.informers.ServiceInformer.Lister().Services(ns).Get(svcName)
if err != nil {
gslbutils.Errf("cluster: %s, ns: %s, svc: %s, err: %v", k8sCluster.Name(),
ns, svcName, err)
return nil, err
}
return svc.DeepCopy(), nil
}
func (k8sCluster *K8sClusterConfig) GetWorkqueue() []workqueue.RateLimitingInterface {
return k8sCluster.workqueue
}
var sharedClusterList map[string]*K8sClusterConfig
func InitSharedClusterList(clusterConfigs []*K8sClusterConfig) {
sharedClusterList = make(map[string]*K8sClusterConfig)
for _, cc := range clusterConfigs {
sharedClusterList[cc.Name()] = cc
}
}
func RunSharedClusterInformers(stopCh <-chan struct{}) {
for cname, cc := range sharedClusterList {
if cc.informers.ServiceInformer != nil {
go cc.informers.ServiceInformer.Informer().Run(stopCh)
gslbutils.Logf("cluster: %s, msg: started service informer", cname)
}
if cc.informers.NodeInformer != nil {
go cc.informers.NodeInformer.Informer().Run(stopCh)
gslbutils.Logf("cluster: %s, msg: started namespace informer", cname)
}
}
}
func AddEventHandlersToClusterInformers(numWorkers uint32) {
for cname, cc := range sharedClusterList {
if cc.informers.ServiceInformer != nil {
cc.informers.ServiceInformer.Informer().AddEventHandler(SvcEventHandlers(numWorkers, cc))
gslbutils.Logf("cluster: %s, msg: added service event handler", cname)
}
if cc.informers.NodeInformer != nil {
cc.informers.NodeInformer.Informer().AddEventHandler(NodeEventHandlers(numWorkers, cc))
gslbutils.Logf("cluster: %s, msg: added node event handler", cname)
}
}
}
func GetNodeInfoFromSharedClusters(cname, nodeName string) (string, error) {
return sharedClusterList[cname].GetNodeFromInformer(nodeName)
}
func GetSvcInfoFromSharedClusters(cname, ns, svc string) (*corev1.Service, error) {
return sharedClusterList[cname].GetSvcFromInformer(ns, svc)
}
func GetWorkqueueForCluster(cname string) []workqueue.RateLimitingInterface {
return sharedClusterList[cname].GetWorkqueue()
}
func GetClusterListFromSharedClusters() []string {
result := []string{}
for cname := range sharedClusterList {
result = append(result, cname)
}
return result
}
func GetClusterListStr(clist []*K8sClusterConfig) []string {
result := []string{}
for _, c := range clist {
result = append(result, c.Name())
}
return result
}
// GetClustersetKubeConfig fetches the clusterset's secret, validates the secret
// and returns the kubeconfig for the clusters in the clusterset
func GetClustersetKubeConfig(kubeClient *kubernetes.Clientset, secretName string) (string, error) {
secretObj, err := kubeClient.CoreV1().Secrets(utils.AviSystemNS).Get(context.TODO(), secretName, v1.GetOptions{})
if err != nil {
return "", fmt.Errorf("error in fetching clusterset secret: %s", err)
}
clusterData, present := secretObj.Data["clusters"]
if !present {
return "", fmt.Errorf("key \"clusters\" is missing from secret data, required for fetching cluster kubeconfig")
}
if string(secretObj.Data["clusters"]) == "" {
return "", fmt.Errorf("data for key \"clusters\" is missing from secret data, required for fetching cluster kubeconfig")
}
return string(clusterData), nil
}
func GenerateKubeConfig(kubeConfigData string) error {
f, err := os.Create(SDKubePath)
if err != nil {
return fmt.Errorf("error in creating file: %v", err)
}
defer f.Close()
_, err = f.WriteString(kubeConfigData)
if err != nil {
return fmt.Errorf("error in writing to config file: %v", err)
}
return nil
}
func InformersToRegister(kclient *kubernetes.Clientset, cname string) ([]string, error) {
var informerTimeout int64 = 120
_, err := kclient.CoreV1().Services("").List(context.TODO(), v1.ListOptions{TimeoutSeconds: &informerTimeout})
if err != nil {
return nil, fmt.Errorf("can't access /api/services for cluster %s, error: %v", cname, err)
}
allInformers := []string{containerutils.ServiceInformer, containerutils.NodeInformer}
return allInformers, nil
}
// BuildContextConfig builds the kubernetes/openshift context config
func BuildContextConfig(kubeconfigPath, context string) (*restclient.Config, error) {
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
&clientcmd.ClientConfigLoadingRules{ExplicitPath: kubeconfigPath},
&clientcmd.ConfigOverrides{
CurrentContext: context,
}).ClientConfig()
}
func GetNodeIP(nodeStatus corev1.NodeStatus) (string, error) {
for _, addr := range nodeStatus.Addresses {
if addr.Type == "InternalIP" {
return addr.Address, nil
}
}
return "", fmt.Errorf("can't find node address")
}
func IsErrorTypeNotFound(err error) bool {
return k8serrors.IsNotFound(err)
}