forked from openservicemesh/osm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
429 lines (372 loc) · 15.3 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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package k8s
import (
"context"
"fmt"
"strconv"
"strings"
mapset "github.com/deckarep/golang-set"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/utils/pointer"
"github.com/openservicemesh/osm/pkg/announcements"
policyv1alpha1 "github.com/openservicemesh/osm/pkg/apis/policy/v1alpha1"
"github.com/openservicemesh/osm/pkg/envoy"
"github.com/openservicemesh/osm/pkg/errcode"
policyv1alpha1Client "github.com/openservicemesh/osm/pkg/gen/client/policy/clientset/versioned"
"github.com/openservicemesh/osm/pkg/messaging"
"github.com/openservicemesh/osm/pkg/constants"
"github.com/openservicemesh/osm/pkg/identity"
osminformers "github.com/openservicemesh/osm/pkg/k8s/informers"
"github.com/openservicemesh/osm/pkg/service"
)
// NewKubernetesController returns a new kubernetes.Controller which means to provide access to locally-cached k8s resources
func NewKubernetesController(informerCollection *osminformers.InformerCollection, policyClient policyv1alpha1Client.Interface, msgBroker *messaging.Broker, selectInformers ...InformerKey) Controller {
return newClient(informerCollection, policyClient, msgBroker, selectInformers...)
}
func newClient(informerCollection *osminformers.InformerCollection, policyClient policyv1alpha1Client.Interface, msgBroker *messaging.Broker, selectInformers ...InformerKey) *client {
// Initialize client object
c := &client{
informers: informerCollection,
msgBroker: msgBroker,
policyClient: policyClient,
}
// Initialize informers
informerInitHandlerMap := map[InformerKey]func(){
Namespaces: c.initNamespaceMonitor,
Services: c.initServicesMonitor,
ServiceAccounts: c.initServiceAccountsMonitor,
Pods: c.initPodMonitor,
Endpoints: c.initEndpointMonitor,
}
// If specific informers are not selected to be initialized, initialize all informers
if len(selectInformers) == 0 {
selectInformers = []InformerKey{Namespaces, Services, ServiceAccounts, Pods, Endpoints}
}
for _, informer := range selectInformers {
informerInitHandlerMap[informer]()
}
return c
}
// Initializes Namespace monitoring
func (c *client) initNamespaceMonitor() {
// Add event handler to informer
nsEventTypes := EventTypes{
Add: announcements.NamespaceAdded,
Update: announcements.NamespaceUpdated,
Delete: announcements.NamespaceDeleted,
}
c.informers.AddEventHandler(osminformers.InformerKeyNamespace, GetEventHandlerFuncs(nil, nsEventTypes, c.msgBroker))
}
// Function to filter K8s meta Objects by OSM's isMonitoredNamespace
func (c *client) shouldObserve(obj interface{}) bool {
object, ok := obj.(metav1.Object)
if !ok {
return false
}
return c.IsMonitoredNamespace(object.GetNamespace())
}
// Initializes Service monitoring
func (c *client) initServicesMonitor() {
svcEventTypes := EventTypes{
Add: announcements.ServiceAdded,
Update: announcements.ServiceUpdated,
Delete: announcements.ServiceDeleted,
}
c.informers.AddEventHandler(osminformers.InformerKeyService, GetEventHandlerFuncs(c.shouldObserve, svcEventTypes, c.msgBroker))
}
// Initializes Service Account monitoring
func (c *client) initServiceAccountsMonitor() {
svcEventTypes := EventTypes{
Add: announcements.ServiceAccountAdded,
Update: announcements.ServiceAccountUpdated,
Delete: announcements.ServiceAccountDeleted,
}
c.informers.AddEventHandler(osminformers.InformerKeyServiceAccount, GetEventHandlerFuncs(c.shouldObserve, svcEventTypes, c.msgBroker))
}
func (c *client) initPodMonitor() {
podEventTypes := EventTypes{
Add: announcements.PodAdded,
Update: announcements.PodUpdated,
Delete: announcements.PodDeleted,
}
c.informers.AddEventHandler(osminformers.InformerKeyPod, GetEventHandlerFuncs(c.shouldObserve, podEventTypes, c.msgBroker))
}
func (c *client) initEndpointMonitor() {
eptEventTypes := EventTypes{
Add: announcements.EndpointAdded,
Update: announcements.EndpointUpdated,
Delete: announcements.EndpointDeleted,
}
c.informers.AddEventHandler(osminformers.InformerKeyEndpoints, GetEventHandlerFuncs(c.shouldObserve, eptEventTypes, c.msgBroker))
}
// IsMonitoredNamespace returns a boolean indicating if the namespace is among the list of monitored namespaces
func (c client) IsMonitoredNamespace(namespace string) bool {
return c.informers.IsMonitoredNamespace(namespace)
}
// ListMonitoredNamespaces returns all namespaces that the mesh is monitoring.
func (c client) ListMonitoredNamespaces() ([]string, error) {
var namespaces []string
for _, ns := range c.informers.List(osminformers.InformerKeyNamespace) {
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.GetByKey(osminformers.InformerKeyService, svc.NamespacedKey())
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.List(osminformers.InformerKeyService) {
svc := serviceInterface.(*corev1.Service)
if !c.IsMonitoredNamespace(svc.Namespace) {
continue
}
services = append(services, svc)
}
return services
}
// ListServiceAccounts returns a list of service accounts that are part of monitored namespaces
func (c client) ListServiceAccounts() []*corev1.ServiceAccount {
var serviceAccounts []*corev1.ServiceAccount
for _, serviceInterface := range c.informers.List(osminformers.InformerKeyServiceAccount) {
sa := serviceInterface.(*corev1.ServiceAccount)
if !c.IsMonitoredNamespace(sa.Namespace) {
continue
}
serviceAccounts = append(serviceAccounts, sa)
}
return serviceAccounts
}
// GetNamespace returns a Namespace resource if found, nil otherwise.
func (c client) GetNamespace(ns string) *corev1.Namespace {
nsIf, exists, err := c.informers.GetByKey(osminformers.InformerKeyNamespace, 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.List(osminformers.InformerKeyPod) {
pod := podInterface.(*corev1.Pod)
if !c.IsMonitoredNamespace(pod.Namespace) {
continue
}
pods = append(pods, pod)
}
return pods
}
// GetEndpoints returns the endpoint for a given service, otherwise returns nil if not found
// or error if the API errored out.
func (c client) GetEndpoints(svc service.MeshService) (*corev1.Endpoints, error) {
ep, exists, err := c.informers.GetByKey(osminformers.InformerKeyEndpoints, svc.NamespacedKey())
if err != nil {
return nil, err
}
if exists {
return ep.(*corev1.Endpoints), nil
}
return nil, nil
}
// ListServiceIdentitiesForService lists ServiceAccounts associated with the given service
func (c client) ListServiceIdentitiesForService(svc service.MeshService) ([]identity.K8sServiceAccount, error) {
var svcAccounts []identity.K8sServiceAccount
k8sSvc := c.GetService(svc)
if k8sSvc == nil {
return nil, errors.Errorf("Error fetching service %q: %s", svc, errServiceNotFound)
}
svcAccountsSet := mapset.NewSet()
pods := c.ListPods()
for _, pod := range pods {
svcRawSelector := k8sSvc.Spec.Selector
selector := labels.Set(svcRawSelector).AsSelector()
// service has no selectors, we do not need to match against the pod label
if len(svcRawSelector) == 0 {
continue
}
if selector.Matches(labels.Set(pod.Labels)) {
podSvcAccount := identity.K8sServiceAccount{
Name: pod.Spec.ServiceAccountName,
Namespace: pod.Namespace, // ServiceAccount must belong to the same namespace as the pod
}
svcAccountsSet.Add(podSvcAccount)
}
}
for svcAcc := range svcAccountsSet.Iter() {
svcAccounts = append(svcAccounts, svcAcc.(identity.K8sServiceAccount))
}
return svcAccounts, nil
}
// IsMetricsEnabled returns true if the pod in the mesh is correctly annotated for prometheus scrapping
func IsMetricsEnabled(pod *corev1.Pod) bool {
isScrapingEnabled := false
prometheusScrapeAnnotation, ok := pod.Annotations[constants.PrometheusScrapeAnnotation]
if !ok {
return isScrapingEnabled
}
isScrapingEnabled, _ = strconv.ParseBool(prometheusScrapeAnnotation)
return isScrapingEnabled
}
// UpdateStatus updates the status subresource for the given resource and GroupVersionKind
// The resource within the 'interface{}' must be a pointer to the underlying resource
func (c client) UpdateStatus(resource interface{}) (metav1.Object, error) {
switch t := resource.(type) {
case *policyv1alpha1.IngressBackend:
obj := resource.(*policyv1alpha1.IngressBackend)
return c.policyClient.PolicyV1alpha1().IngressBackends(obj.Namespace).UpdateStatus(context.Background(), obj, metav1.UpdateOptions{})
case *policyv1alpha1.UpstreamTrafficSetting:
obj := resource.(*policyv1alpha1.UpstreamTrafficSetting)
return c.policyClient.PolicyV1alpha1().UpstreamTrafficSettings(obj.Namespace).UpdateStatus(context.Background(), obj, metav1.UpdateOptions{})
default:
return nil, errors.Errorf("Unsupported type: %T", t)
}
}
// ServiceToMeshServices translates a k8s service with one or more ports to one or more
// MeshService objects per port.
func ServiceToMeshServices(c Controller, svc corev1.Service) []service.MeshService {
var meshServices []service.MeshService
for _, portSpec := range svc.Spec.Ports {
meshSvc := service.MeshService{
Namespace: svc.Namespace,
Name: svc.Name,
Port: uint16(portSpec.Port),
}
// attempt to parse protocol from port name
// Order of Preference is:
// 1. port.appProtocol field
// 2. protocol prefixed to port name (e.g. tcp-my-port)
// 3. default to http
protocol := constants.ProtocolHTTP
for _, p := range constants.SupportedProtocolsInMesh {
if strings.HasPrefix(portSpec.Name, p+"-") {
protocol = p
break
}
}
// use port.appProtocol if specified, else use port protocol
meshSvc.Protocol = pointer.StringDeref(portSpec.AppProtocol, protocol)
// The endpoints for the kubernetes service carry information that allows
// us to retrieve the TargetPort for the MeshService.
endpoints, _ := c.GetEndpoints(meshSvc)
if endpoints != nil {
meshSvc.TargetPort = GetTargetPortFromEndpoints(portSpec.Name, *endpoints)
} else {
log.Warn().Msgf("k8s service %s/%s does not have endpoints but is being represented as a MeshService", svc.Namespace, svc.Name)
}
if !IsHeadlessService(svc) || endpoints == nil {
meshServices = append(meshServices, meshSvc)
continue
}
for _, subset := range endpoints.Subsets {
for _, address := range subset.Addresses {
if address.Hostname == "" {
continue
}
meshServices = append(meshServices, service.MeshService{
Namespace: svc.Namespace,
Name: fmt.Sprintf("%s.%s", address.Hostname, svc.Name),
Port: meshSvc.Port,
TargetPort: meshSvc.TargetPort,
Protocol: meshSvc.Protocol,
})
}
}
}
return meshServices
}
// GetTargetPortFromEndpoints returns the endpoint port corresponding to the given endpoint name and endpoints
func GetTargetPortFromEndpoints(endpointName string, endpoints corev1.Endpoints) (endpointPort uint16) {
// Per https://pkg.go.dev/k8s.io/api/core/v1#ServicePort and
// https://pkg.go.dev/k8s.io/api/core/v1#EndpointPort, if a service has multiple
// ports, then ServicePort.Name must match EndpointPort.Name when considering
// matching endpoints for the service's port. ServicePort.Name and EndpointPort.Name
// can be unset when the service has a single port exposed, in which case we are
// guaranteed to have the same port specified in the list of EndpointPort.Subsets.
//
// The logic below works as follows:
// If the service has multiple ports, retrieve the matching endpoint port using
// the given ServicePort.Name specified by `endpointName`.
// Otherwise, simply return the only port referenced in EndpointPort.Subsets.
for _, subset := range endpoints.Subsets {
for _, port := range subset.Ports {
if endpointName == "" || len(subset.Ports) == 1 {
// ServicePort.Name is not passed or a single port exists on the service.
// Both imply that this service has a single ServicePort and EndpointPort.
endpointPort = uint16(port.Port)
return
}
// If more than 1 port is specified
if port.Name == endpointName {
endpointPort = uint16(port.Port)
return
}
}
}
return
}
func (c client) GetPodForProxy(proxy *envoy.Proxy) (*v1.Pod, error) {
proxyUUID, svcAccount := proxy.UUID.String(), proxy.Identity.ToK8sServiceAccount()
log.Trace().Msgf("Looking for pod with label %q=%q", constants.EnvoyUniqueIDLabelName, proxyUUID)
podList := c.ListPods()
var pods []v1.Pod
for _, pod := range podList {
if uuid, labelFound := pod.Labels[constants.EnvoyUniqueIDLabelName]; labelFound && uuid == proxyUUID {
pods = append(pods, *pod)
}
}
if len(pods) == 0 {
log.Error().Str(errcode.Kind, errcode.GetErrCodeWithMetric(errcode.ErrFetchingPodFromCert)).
Msgf("Did not find Pod with label %s = %s in namespace %s",
constants.EnvoyUniqueIDLabelName, proxyUUID, svcAccount.Namespace)
return nil, errDidNotFindPodForUUID
}
// Each pod is assigned a unique UUID at the time of sidecar injection.
// The certificate's CommonName encodes this UUID, and we lookup the pod
// whose label matches this UUID.
// Only 1 pod must match the UUID encoded in the given certificate. If multiple
// pods match, it is an error.
if len(pods) > 1 {
log.Error().Str(errcode.Kind, errcode.GetErrCodeWithMetric(errcode.ErrPodBelongsToMultipleServices)).
Msgf("Found more than one pod with label %s = %s in namespace %s. There can be only one!",
constants.EnvoyUniqueIDLabelName, proxyUUID, svcAccount.Namespace)
return nil, errMoreThanOnePodForUUID
}
pod := pods[0]
log.Trace().Msgf("Found Pod with UID=%s for proxyID %s", pod.ObjectMeta.UID, proxyUUID)
if pod.Namespace != svcAccount.Namespace {
log.Warn().Str(errcode.Kind, errcode.GetErrCodeWithMetric(errcode.ErrFetchingPodFromCert)).
Msgf("Pod with UID=%s belongs to Namespace %s. The pod's xDS certificate was issued for Namespace %s",
pod.ObjectMeta.UID, pod.Namespace, svcAccount.Namespace)
return nil, errNamespaceDoesNotMatchProxy
}
// Ensure the Name encoded in the certificate matches that of the Pod
// TODO(draychev): check that the Kind matches too! [https://github.com/openservicemesh/osm/issues/3173]
if pod.Spec.ServiceAccountName != svcAccount.Name {
// Since we search for the pod in the namespace we obtain from the certificate -- these namespaces will always match.
log.Warn().Str(errcode.Kind, errcode.GetErrCodeWithMetric(errcode.ErrFetchingPodFromCert)).
Msgf("Pod with UID=%s belongs to ServiceAccount=%s. The pod's xDS certificate was issued for ServiceAccount=%s",
pod.ObjectMeta.UID, pod.Spec.ServiceAccountName, svcAccount)
return nil, errServiceAccountDoesNotMatchProxy
}
return &pod, nil
}