-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefault-client.go
52 lines (47 loc) · 1.25 KB
/
default-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
package k8s
import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
"sync"
)
var lock = &sync.Mutex{}
var configLock = &sync.Mutex{}
var _clientset *kubernetes.Clientset
var _config *rest.Config
// GetConfig returns the Kubernetes REST configuration.
// If the configuration has not been initialized, it retrieves the in-cluster config.
// It returns the config or an error if there is any issue.
func GetConfig() (*rest.Config, error) {
if _config == nil {
configLock.Lock()
defer configLock.Unlock()
var err error
_config, err = rest.InClusterConfig()
if err != nil {
return nil, err
}
}
return _config, nil
}
// GetClientset returns the Kubernetes clientset.
// If the clientset has not been initialized, it creates a new one based on the config obtained from GetConfig.
// It returns the clientset or an error if there is any issue.
func GetClientset() (*kubernetes.Clientset, error) {
if _clientset == nil {
lock.Lock()
defer lock.Unlock()
if _clientset == nil {
klog.Info("Creating Clientset singleton.")
config, err := GetConfig()
if err != nil {
return nil, err
}
_clientset, err = kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
}
}
return _clientset, nil
}