forked from joeholley/supergloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclientset.go
98 lines (80 loc) · 2.34 KB
/
clientset.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
package clients
import (
"context"
"sync"
"github.com/solo-io/supergloo/pkg/util"
"github.com/solo-io/solo-kit/pkg/api/v1/clients"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/solo-io/go-utils/kubeutils"
"github.com/solo-io/solo-kit/pkg/api/v1/clients/factory"
"github.com/solo-io/solo-kit/pkg/api/v1/clients/kube"
v1 "github.com/solo-io/supergloo/pkg/api/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
)
//go:generate mockgen -destination=./clientset_mock.go -source clientset.go -package clients
// Exposes only the methods that are actually used. Used to generate mocks.
type WebhookResourceClient interface {
ListMeshes(namespace string, opts clients.ListOpts) (v1.MeshList, error)
GetConfigMap(namespace, name string) (*corev1.ConfigMap, error)
GetSuperglooNamespace() (string, error)
}
// Contains the resource clients required by the webhook
type webhookClientSet struct {
meshClient v1.MeshClient
kubeClient kubernetes.Interface
}
func (c *webhookClientSet) ListMeshes(namespace string, opts clients.ListOpts) (v1.MeshList, error) {
return c.meshClient.List(namespace, opts)
}
func (c *webhookClientSet) GetConfigMap(namespace, name string) (*corev1.ConfigMap, error) {
return c.kubeClient.CoreV1().ConfigMaps(namespace).Get(name, metav1.GetOptions{})
}
func (c *webhookClientSet) GetSuperglooNamespace() (string, error) {
return util.GetSuperglooNamespace(c.kubeClient)
}
var (
mutex sync.Mutex
globalClientSet WebhookResourceClient
)
func SetClientSet(clientSet WebhookResourceClient) {
mutex.Lock()
defer mutex.Unlock()
globalClientSet = clientSet
}
func GetClientSet() WebhookResourceClient {
mutex.Lock()
defer mutex.Unlock()
return globalClientSet
}
func InitClientSet(ctx context.Context) error {
mutex.Lock()
defer mutex.Unlock()
if globalClientSet != nil {
return nil
}
restConfig, err := kubeutils.GetConfig("", "")
if err != nil {
return err
}
kubeClient, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return err
}
mesh, err := v1.NewMeshClient(&factory.KubeResourceClientFactory{
Crd: v1.MeshCrd,
Cfg: restConfig,
SharedCache: kube.NewKubeCache(ctx),
})
if err != nil {
return err
}
if err := mesh.Register(); err != nil {
return err
}
globalClientSet = &webhookClientSet{
meshClient: mesh,
kubeClient: kubeClient,
}
return nil
}