This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 277
/
client.go
202 lines (163 loc) · 6.38 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
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
package kubernetes
import (
"reflect"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"github.com/openservicemesh/osm/pkg/constants"
"github.com/openservicemesh/osm/pkg/service"
)
// NewKubernetesController returns a new kubernetes.Controller which means to provide access to locally-cached k8s resources
func NewKubernetesController(kubeClient kubernetes.Interface, meshName string, stop chan struct{}) (Controller, error) {
// Initialize client object
client := Client{
kubeClient: kubeClient,
meshName: meshName,
informers: InformerCollection{},
announcements: make(map[InformerKey]chan interface{}),
cacheSynced: make(chan interface{}),
}
// Initialize resources here
client.initNamespaceMonitor()
client.initServicesMonitor()
client.initPodMonitor()
if err := client.run(stop); err != nil {
log.Error().Err(err).Msg("Could not start Kubernetes Namespaces client")
return nil, err
}
return client, nil
}
// Initializes Namespace monitoring
func (c *Client) initNamespaceMonitor() {
monitorNamespaceLabel := map[string]string{constants.OSMKubeResourceMonitorAnnotation: c.meshName}
labelSelector := fields.SelectorFromSet(monitorNamespaceLabel).String()
option := informers.WithTweakListOptions(func(opt *metav1.ListOptions) {
opt.LabelSelector = labelSelector
})
informerFactory := informers.NewSharedInformerFactoryWithOptions(c.kubeClient, DefaultKubeEventResyncInterval, option)
// Add informer
c.informers[Namespaces] = informerFactory.Core().V1().Namespaces().Informer()
// Announcement channel for Namespaces
c.announcements[Namespaces] = make(chan interface{})
// Add event handler to informer
c.informers[Namespaces].AddEventHandler(GetKubernetesEventHandlers((string)(Namespaces), ProviderName, c.announcements[Namespaces], nil))
}
// Initializes Service monitoring
func (c *Client) initServicesMonitor() {
informerFactory := informers.NewSharedInformerFactory(c.kubeClient, DefaultKubeEventResyncInterval)
c.informers[Services] = informerFactory.Core().V1().Services().Informer()
// Function to filter Services by Namespace
shouldObserve := func(obj interface{}) bool {
ns := reflect.ValueOf(obj).Elem().FieldByName("ObjectMeta").FieldByName("Namespace").String()
return c.IsMonitoredNamespace(ns)
}
// Announcement channel for Services
c.announcements[Services] = make(chan interface{})
c.informers[Services].AddEventHandler(GetKubernetesEventHandlers((string)(Services), ProviderName, c.announcements[Services], shouldObserve))
}
func (c *Client) initPodMonitor() {
informerFactory := informers.NewSharedInformerFactory(c.kubeClient, DefaultKubeEventResyncInterval)
c.informers[Pods] = informerFactory.Core().V1().Pods().Informer()
// Function to filter Pods by Namespace
shouldObserve := func(obj interface{}) bool {
ns := reflect.ValueOf(obj).Elem().FieldByName("ObjectMeta").FieldByName("Namespace").String()
return c.IsMonitoredNamespace(ns)
}
// Announcement channel for Pods
c.announcements[Pods] = make(chan interface{})
c.informers[Pods].AddEventHandler(GetKubernetesEventHandlers((string)(Pods), ProviderName, c.announcements[Services], shouldObserve))
}
func (c *Client) run(stop <-chan struct{}) error {
log.Info().Msg("Namespace controller client started")
var hasSynced []cache.InformerSynced
var names []string
if c.informers == nil {
return errInitInformers
}
for name, informer := range c.informers {
if informer == nil {
continue
}
go informer.Run(stop)
names = append(names, (string)(name))
log.Info().Msgf("Waiting informer for %s cache sync...", name)
hasSynced = append(hasSynced, informer.HasSynced)
}
if !cache.WaitForCacheSync(stop, hasSynced...) {
return errSyncingCaches
}
// Closing the cacheSynced channel signals to the rest of the system that caches have synced.
close(c.cacheSynced)
log.Info().Msgf("Caches for %+s synced successfully", names)
return nil
}
// IsMonitoredNamespace returns a boolean indicating if the namespace is among the list of monitored namespaces
func (c Client) IsMonitoredNamespace(namespace string) bool {
_, exists, _ := c.informers[Namespaces].GetStore().GetByKey(namespace)
return exists
}
// ListMonitoredNamespaces returns all namespaces that the mesh is monitoring.
func (c Client) ListMonitoredNamespaces() ([]string, error) {
var namespaces []string
for _, ns := range c.informers[Namespaces].GetStore().List() {
namespace, ok := ns.(*corev1.Namespace)
if !ok {
log.Error().Err(errListingNamespaces).Msg("Failed to list monitored namespaces")
continue
}
namespaces = append(namespaces, namespace.Name)
}
return namespaces, nil
}
// GetService retrieves the Kubernetes Services resource for the given MeshService
func (c Client) GetService(svc service.MeshService) *corev1.Service {
// client-go cache uses <namespace>/<name> as key
svcIf, exists, err := c.informers[Services].GetStore().GetByKey(svc.String())
if exists && err == nil {
svc := svcIf.(*corev1.Service)
return svc
}
return nil
}
// ListServices returns a list of services that are part of monitored namespaces
func (c Client) ListServices() []*corev1.Service {
var services []*corev1.Service
for _, serviceInterface := range c.informers[Services].GetStore().List() {
svc := serviceInterface.(*corev1.Service)
if !c.IsMonitoredNamespace(svc.Namespace) {
continue
}
services = append(services, svc)
}
return services
}
// GetAnnouncementsChannel gets the Announcements channel back
func (c Client) GetAnnouncementsChannel(informerID InformerKey) <-chan interface{} {
return c.announcements[informerID]
}
// GetNamespace returns a Namespace resource if found, nil otherwise.
func (c Client) GetNamespace(ns string) *corev1.Namespace {
nsIf, exists, err := c.informers[Namespaces].GetStore().GetByKey(ns)
if exists && err == nil {
ns := nsIf.(*corev1.Namespace)
return ns
}
return nil
}
// ListPods returns a list of pods part of the mesh
// Kubecontroller does not currently segment pod notifications, hence it receives notifications
// for all k8s Pods.
func (c Client) ListPods() []*corev1.Pod {
var pods []*corev1.Pod
for _, podInterface := range c.informers[Pods].GetStore().List() {
pod := podInterface.(*corev1.Pod)
if !c.IsMonitoredNamespace(pod.Namespace) {
continue
}
pods = append(pods, pod)
}
return pods
}