-
Notifications
You must be signed in to change notification settings - Fork 10
/
client.go
602 lines (525 loc) · 22.1 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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
package k8s
import (
"context"
"fmt"
"strconv"
"strings"
mapset "github.com/deckarep/golang-set"
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/apimachinery/pkg/types"
"k8s.io/utils/pointer"
machinev1alpha1 "github.com/flomesh-io/fsm/pkg/apis/machine/v1alpha1"
pluginv1alpha1 "github.com/flomesh-io/fsm/pkg/apis/plugin/v1alpha1"
policyv1alpha1 "github.com/flomesh-io/fsm/pkg/apis/policy/v1alpha1"
pluginv1alpha1Client "github.com/flomesh-io/fsm/pkg/gen/client/plugin/clientset/versioned"
policyv1alpha1Client "github.com/flomesh-io/fsm/pkg/gen/client/policy/clientset/versioned"
"github.com/flomesh-io/fsm/pkg/announcements"
"github.com/flomesh-io/fsm/pkg/connector"
"github.com/flomesh-io/fsm/pkg/constants"
"github.com/flomesh-io/fsm/pkg/errcode"
"github.com/flomesh-io/fsm/pkg/identity"
fsminformers "github.com/flomesh-io/fsm/pkg/k8s/informers"
"github.com/flomesh-io/fsm/pkg/messaging"
"github.com/flomesh-io/fsm/pkg/models"
"github.com/flomesh-io/fsm/pkg/service"
)
// NewKubernetesController returns a new kubernetes.Controller which means to provide access to locally-cached k8s resources
func NewKubernetesController(informerCollection *fsminformers.InformerCollection, policyClient policyv1alpha1Client.Interface, pluginClient pluginv1alpha1Client.Interface, msgBroker *messaging.Broker, selectInformers ...InformerKey) Controller {
return newClient(informerCollection, policyClient, pluginClient, msgBroker, selectInformers...)
}
func newClient(informerCollection *fsminformers.InformerCollection, policyClient policyv1alpha1Client.Interface, pluginClient pluginv1alpha1Client.Interface, msgBroker *messaging.Broker, selectInformers ...InformerKey) *client {
// Initialize client object
c := &client{
informers: informerCollection,
msgBroker: msgBroker,
policyClient: policyClient,
pluginClient: pluginClient,
}
// 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(fsminformers.InformerKeyNamespace, GetEventHandlerFuncs(nil, nsEventTypes, c.msgBroker))
}
// Function to filter K8s meta Objects by FSM'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(fsminformers.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(fsminformers.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(fsminformers.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(fsminformers.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(fsminformers.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
}
func (c *client) isExclusionService(svc *corev1.Service) bool {
exclude, ok := svc.Annotations[constants.ServiceExclusionAnnotation]
if ok {
switch strings.ToLower(exclude) {
case "enabled", "yes", "true":
return true
default:
return false
}
}
if ns := c.GetNamespace(svc.Namespace); ns != nil {
if svcsToExcludeStr, exist := ns.Annotations[constants.ServiceExclusionListAnnotation]; exist {
svcsToExclude := strings.Split(svcsToExcludeStr, ",")
for _, svcStr := range svcsToExclude {
svcName := strings.TrimSpace(svcStr)
svcName = strings.ToLower(svcName)
if strings.EqualFold(svcName, svc.Name) {
return true
}
}
}
}
return false
}
// 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(fsminformers.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(fsminformers.InformerKeyService) {
svc := serviceInterface.(*corev1.Service)
if !c.IsMonitoredNamespace(svc.Namespace) {
continue
}
if c.isExclusionService(svc) {
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(fsminformers.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(fsminformers.InformerKeyNamespace, ns)
if exists && err == nil {
return nsIf.(*corev1.Namespace)
}
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(fsminformers.InformerKeyPod) {
pod := podInterface.(*corev1.Pod)
if !c.IsMonitoredNamespace(pod.Namespace) {
continue
}
pods = append(pods, pod)
}
return pods
}
// ListVms returns a list of vms part of the mesh
// Kubecontroller does not currently segment vm notifications, hence it receives notifications
// for all k8s vms.
func (c *client) ListVms() []*machinev1alpha1.VirtualMachine {
var vms []*machinev1alpha1.VirtualMachine
for _, vmInterface := range c.informers.List(fsminformers.InformerKeyVirtualMachine) {
vm := vmInterface.(*machinev1alpha1.VirtualMachine)
vms = append(vms, vm)
}
return vms
}
// 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(fsminformers.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, fmt.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 {
prometheusScrapeAnnotation, ok := pod.Annotations[constants.PrometheusScrapeAnnotation]
if !ok {
return false
}
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.AccessCert:
obj := resource.(*policyv1alpha1.AccessCert)
return c.policyClient.PolicyV1alpha1().AccessCerts(obj.Namespace).UpdateStatus(context.Background(), obj, metav1.UpdateOptions{})
case *policyv1alpha1.AccessControl:
obj := resource.(*policyv1alpha1.AccessControl)
return c.policyClient.PolicyV1alpha1().AccessControls(obj.Namespace).UpdateStatus(context.Background(), obj, metav1.UpdateOptions{})
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{})
case *pluginv1alpha1.Plugin:
obj := resource.(*pluginv1alpha1.Plugin)
return c.pluginClient.PluginV1alpha1().Plugins().UpdateStatus(context.Background(), obj, metav1.UpdateOptions{})
case *pluginv1alpha1.PluginChain:
obj := resource.(*pluginv1alpha1.PluginChain)
return c.pluginClient.PluginV1alpha1().PluginChains(obj.Namespace).UpdateStatus(context.Background(), obj, metav1.UpdateOptions{})
default:
return nil, fmt.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),
}
if len(svc.Annotations) > 0 {
if inheritedFrom, ok := svc.Annotations[connector.AnnotationCloudServiceInheritedFrom]; ok {
meshSvc.CloudInheritedFrom = inheritedFrom
}
if clusterId, ok := svc.Annotations[connector.AnnotationCloudServiceInheritedClusterID]; ok {
meshSvc.ClusterID = clusterId
}
}
// 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
}
// If there's not at least 1 subdomain-ed MeshService added,
// add the entire headless service
var added bool
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,
})
added = true
}
}
if !added {
meshServices = append(meshServices, meshSvc)
}
}
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 models.Proxy) (*v1.Pod, error) {
proxyUUID, svcAccount := proxy.GetUUID().String(), proxy.GetIdentity().ToK8sServiceAccount()
log.Trace().Msgf("Looking for pod with label %q=%q", constants.SidecarUniqueIDLabelName, proxyUUID)
podList := c.ListPods()
var pods []v1.Pod
for _, pod := range podList {
if uuid, labelFound := pod.Labels[constants.SidecarUniqueIDLabelName]; labelFound && uuid == proxyUUID {
pods = append(pods, *pod)
}
}
if len(pods) == 0 {
log.Info().Str(errcode.Kind, errcode.GetErrCodeWithMetric(errcode.ErrFetchingPodFromCert)).
Msgf("Did not find Pod with label %s = %s in namespace %s",
constants.SidecarUniqueIDLabelName, 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.SidecarUniqueIDLabelName, 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/flomesh-io/fsm/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
}
func (c *client) GetVmForProxy(proxy models.Proxy) (*machinev1alpha1.VirtualMachine, error) {
proxyUUID, svcAccount := proxy.GetUUID().String(), proxy.GetIdentity().ToK8sServiceAccount()
log.Trace().Msgf("Looking for VM with label %q=%q", constants.SidecarUniqueIDLabelName, proxyUUID)
vmList := c.ListVms()
var vms []machinev1alpha1.VirtualMachine
for _, vm := range vmList {
if uuid, labelFound := vm.Labels[constants.SidecarUniqueIDLabelName]; labelFound && uuid == proxyUUID {
vms = append(vms, *vm)
}
}
if len(vms) == 0 {
log.Info().Str(errcode.Kind, errcode.GetErrCodeWithMetric(errcode.ErrFetchingPodFromCert)).
Msgf("Did not find VM with label %s = %s in namespace %s",
constants.SidecarUniqueIDLabelName, proxyUUID, svcAccount.Namespace)
return nil, errDidNotFindPodForUUID
}
// Each VM is assigned a unique UUID at the time of sidecar injection.
// The certificate's CommonName encodes this UUID, and we lookup the vm
// whose label matches this UUID.
// Only 1 vm must match the UUID encoded in the given certificate. If multiple
// vms match, it is an error.
if len(vms) > 1 {
log.Error().Str(errcode.Kind, errcode.GetErrCodeWithMetric(errcode.ErrPodBelongsToMultipleServices)).
Msgf("Found more than one vm with label %s = %s in namespace %s. There can be only one!",
constants.SidecarUniqueIDLabelName, proxyUUID, svcAccount.Namespace)
return nil, errMoreThanOnePodForUUID
}
vm := vms[0]
log.Trace().Msgf("Found VM with UID=%s for proxyID %s", vm.ObjectMeta.UID, proxyUUID)
if vm.Namespace != svcAccount.Namespace {
log.Warn().Str(errcode.Kind, errcode.GetErrCodeWithMetric(errcode.ErrFetchingPodFromCert)).
Msgf("VM with UID=%s belongs to Namespace %s. The vm's xDS certificate was issued for Namespace %s",
vm.ObjectMeta.UID, vm.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/flomesh-io/fsm/issues/3173]
if vm.Spec.ServiceAccountName != svcAccount.Name {
// Since we search for the vm in the namespace we obtain from the certificate -- these namespaces will always match.
log.Warn().Str(errcode.Kind, errcode.GetErrCodeWithMetric(errcode.ErrFetchingPodFromCert)).
Msgf("VM with UID=%s belongs to ServiceAccount=%s. The vm's xDS certificate was issued for ServiceAccount=%s",
vm.ObjectMeta.UID, vm.Spec.ServiceAccountName, svcAccount)
return nil, errServiceAccountDoesNotMatchProxy
}
return &vm, nil
}
// GetTargetPortForServicePort returns the TargetPort corresponding to the Port used by clients
// to communicate with it.
func (c *client) GetTargetPortForServicePort(namespacedSvc types.NamespacedName, port uint16) (uint16, error) {
// Lookup the k8s service corresponding to the given service name.
// The k8s service is necessary to lookup the TargetPort from the Endpoint whose name
// matches the name of the port on the k8s Service object.
svcIf, exists, err := c.informers.GetByKey(fsminformers.InformerKeyService, namespacedSvc.String())
if err != nil {
return 0, err
}
if !exists {
return 0, fmt.Errorf("service %s not found in cache", namespacedSvc)
}
svc := svcIf.(*corev1.Service)
var portName string
for _, portSpec := range svc.Spec.Ports {
if uint16(portSpec.Port) == port {
portName = portSpec.Name
break
}
}
// Lookup the endpoint port (TargetPort) that matches the given service and 'portName'
ep, exists, err := c.informers.GetByKey(fsminformers.InformerKeyEndpoints, namespacedSvc.String())
if err != nil {
return 0, err
}
if !exists {
return 0, fmt.Errorf("endpoint for service %s not found in cache", namespacedSvc)
}
endpoint := ep.(*corev1.Endpoints)
for _, subset := range endpoint.Subsets {
for _, portSpec := range subset.Ports {
if portSpec.Name == portName {
return uint16(portSpec.Port), nil
}
}
}
return 0, fmt.Errorf("error finding port name %s for endpoint %s", portName, namespacedSvc)
}