@@ -3,26 +3,26 @@ package k8s
3
3
import (
4
4
"context"
5
5
6
+ smiAccess "github.com/servicemeshinterface/smi-sdk-go/pkg/apis/access/v1alpha3"
7
+ smiSpecs "github.com/servicemeshinterface/smi-sdk-go/pkg/apis/specs/v1alpha4"
8
+ smiSplit "github.com/servicemeshinterface/smi-sdk-go/pkg/apis/split/v1alpha2"
6
9
corev1 "k8s.io/api/core/v1"
7
10
v1 "k8s.io/api/core/v1"
8
11
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12
+ "k8s.io/apimachinery/pkg/labels"
9
13
"k8s.io/apimachinery/pkg/types"
10
14
"k8s.io/client-go/kubernetes"
11
15
12
- smiAccess "github.com/servicemeshinterface/smi-sdk-go/pkg/apis/access/v1alpha3"
13
- smiSpecs "github.com/servicemeshinterface/smi-sdk-go/pkg/apis/specs/v1alpha4"
14
- smiSplit "github.com/servicemeshinterface/smi-sdk-go/pkg/apis/split/v1alpha2"
15
-
16
16
configv1alpha2 "github.com/openservicemesh/osm/pkg/apis/config/v1alpha2"
17
17
policyv1alpha1 "github.com/openservicemesh/osm/pkg/apis/policy/v1alpha1"
18
18
configv1alpha2Client "github.com/openservicemesh/osm/pkg/gen/client/config/clientset/versioned"
19
19
policyv1alpha1Client "github.com/openservicemesh/osm/pkg/gen/client/policy/clientset/versioned"
20
- "github.com/openservicemesh/osm/pkg/models"
21
20
22
21
"github.com/openservicemesh/osm/pkg/constants"
23
22
"github.com/openservicemesh/osm/pkg/errcode"
24
23
"github.com/openservicemesh/osm/pkg/k8s/informers"
25
24
"github.com/openservicemesh/osm/pkg/messaging"
25
+ "github.com/openservicemesh/osm/pkg/models"
26
26
)
27
27
28
28
// NewClient returns a new kubernetes.Controller which means to provide access to locally-cached k8s resources
@@ -47,10 +47,12 @@ func NewClient(osmNamespace, meshConfigName string, informerCollection *informer
47
47
Endpoints : c .initEndpointMonitor ,
48
48
MeshConfig : c .initMeshConfigMonitor ,
49
49
MeshRootCertificate : c .initMRCMonitor ,
50
+ ExtensionService : c .initExtensionServiceMonitor ,
50
51
Egress : c .initEgressMonitor ,
51
52
IngressBackend : c .initIngressBackendMonitor ,
52
53
Retry : c .initRetryMonitor ,
53
54
UpstreamTrafficSetting : c .initUpstreamTrafficSettingMonitor ,
55
+ Telemetry : c .initTelemetryMonitor ,
54
56
TrafficSplit : c .initTrafficSplitMonitor ,
55
57
HTTPRouteGroup : c .initHTTPRouteGroupMonitor ,
56
58
TCPRoute : c .initTCPRouteMonitor ,
@@ -60,8 +62,8 @@ func NewClient(osmNamespace, meshConfigName string, informerCollection *informer
60
62
// If specific informers are not selected to be initialized, initialize all informers
61
63
if len (selectInformers ) == 0 {
62
64
selectInformers = []InformerKey {
63
- Namespaces , Services , ServiceAccounts , Pods , Endpoints , MeshConfig , MeshRootCertificate ,
64
- Egress , IngressBackend , Retry , UpstreamTrafficSetting , TrafficSplit , HTTPRouteGroup , TCPRoute ,
65
+ Namespaces , Services , ServiceAccounts , Pods , Endpoints , MeshConfig , MeshRootCertificate , ExtensionService ,
66
+ Egress , IngressBackend , Retry , UpstreamTrafficSetting , Telemetry , TrafficSplit , HTTPRouteGroup , TCPRoute ,
65
67
TrafficTarget }
66
68
}
67
69
@@ -87,6 +89,10 @@ func (c *Client) initMRCMonitor() {
87
89
c .informers .AddEventHandler (informers .InformerKeyMeshRootCertificate , GetEventHandlerFuncs (nil , c .msgBroker ))
88
90
}
89
91
92
+ func (c * Client ) initExtensionServiceMonitor () {
93
+ c .informers .AddEventHandler (informers .InformerKeyExtensionService , GetEventHandlerFuncs (nil , c .msgBroker ))
94
+ }
95
+
90
96
func (c * Client ) initEgressMonitor () {
91
97
c .informers .AddEventHandler (informers .InformerKeyEgress , GetEventHandlerFuncs (c .shouldObserve , c .msgBroker ))
92
98
}
@@ -103,6 +109,10 @@ func (c *Client) initUpstreamTrafficSettingMonitor() {
103
109
c .informers .AddEventHandler (informers .InformerKeyUpstreamTrafficSetting , GetEventHandlerFuncs (c .shouldObserve , c .msgBroker ))
104
110
}
105
111
112
+ func (c * Client ) initTelemetryMonitor () {
113
+ c .informers .AddEventHandler (informers .InformerKeyTelemetry , GetEventHandlerFuncs (c .shouldObserve , c .msgBroker ))
114
+ }
115
+
106
116
// Function to filter K8s meta Objects by OSM's isMonitoredNamespace
107
117
func (c * Client ) shouldObserve (obj interface {}) bool {
108
118
object , ok := obj .(metav1.Object )
@@ -593,3 +603,52 @@ func (c *Client) ListTrafficTargets() []*smiAccess.TrafficTarget {
593
603
}
594
604
return trafficTargets
595
605
}
606
+
607
+ // GetTelemetryPolicy returns the Telemetry policy for the given proxy instance.
608
+ // It returns the most specific match if multiple matching policies exist, in the following
609
+ // order of preference: 1. selector match, 2. namespace match, 3. global match
610
+ func (c * Client ) GetTelemetryPolicy (proxy * models.Proxy ) * policyv1alpha1.Telemetry {
611
+ pod , _ := c .GetPodForProxy (proxy )
612
+ if pod == nil {
613
+ return nil
614
+ }
615
+
616
+ var policy * policyv1alpha1.Telemetry
617
+
618
+ for _ , resource := range c .informers .List (informers .InformerKeyTelemetry ) {
619
+ t := resource .(* policyv1alpha1.Telemetry )
620
+
621
+ // If there is a global policy and a more specific policy hasn't been
622
+ // found yet, consider the global policy as a candidate
623
+ if policy == nil && t .Namespace == c .osmNamespace {
624
+ policy = t
625
+ continue
626
+ }
627
+
628
+ if ! c .IsMonitoredNamespace (t .Namespace ) {
629
+ continue
630
+ }
631
+
632
+ // If the policy matches the namespace of the proxy's pod,
633
+ // consider this policy to be a candidate, but continue
634
+ // to look for a more specific policy that matches the pod
635
+ // based on a selector
636
+ if t .Namespace == pod .Namespace {
637
+ policy = t
638
+ }
639
+
640
+ // Look for a more specific match based on pod selector on the Telemetry resource.
641
+ // If we find a Telemetry resource that matches the pod's selector, this is
642
+ // the best match for this proxy.
643
+ selector := t .Spec .Selector
644
+ if len (selector ) == 0 {
645
+ continue
646
+ }
647
+ sel := labels .Set (selector ).AsSelector ()
648
+ if sel .Matches (labels .Set (pod .Labels )) {
649
+ return t
650
+ }
651
+ }
652
+
653
+ return policy
654
+ }
0 commit comments