Skip to content

Commit a8fe89e

Browse files
authored
apis: add CRD + informer for Telemetry & ExtensionService (openservicemesh#5184)
- Adds CRD for Telemetry and ExtensionService APIs - Adds informer clients - Adds an API to lookup the Telemetry config for a proxy - Minor changes to the API definition Part of openservicemesh#5136 Signed-off-by: Shashank Ram <[email protected]>
1 parent ae55d73 commit a8fe89e

File tree

12 files changed

+422
-19
lines changed

12 files changed

+422
-19
lines changed

charts/osm/templates/osm-rbac.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ rules:
4141
resources: ["customresourcedefinitions"]
4242
verbs: ["get", "list", "watch", "create", "update", "patch"]
4343
- apiGroups: ["config.openservicemesh.io"]
44-
resources: ["meshconfigs", "meshrootcertificates"]
44+
resources: ["meshconfigs", "meshrootcertificates", "extensionservices"]
4545
verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
4646
- apiGroups: ["config.openservicemesh.io"]
4747
resources: ["meshrootcertificates/status"]
@@ -58,10 +58,10 @@ rules:
5858

5959
# OSM's custom policy API
6060
- apiGroups: ["policy.openservicemesh.io"]
61-
resources: ["egresses", "ingressbackends", "retries", "upstreamtrafficsettings"]
61+
resources: ["egresses", "ingressbackends", "retries", "upstreamtrafficsettings", "telemetries"]
6262
verbs: ["list", "get", "watch"]
6363
- apiGroups: ["policy.openservicemesh.io"]
64-
resources: ["ingressbackends/status", "upstreamtrafficsettings/status"]
64+
resources: ["ingressbackends/status", "upstreamtrafficsettings/status", "telemetry/status"]
6565
verbs: ["update"]
6666

6767
# Used for interacting with cert-manager CertificateRequest resources.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Custom Resource Definition (CRD) for OSM's ExtensionService specification.
2+
#
3+
# Copyright Open Service Mesh authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
---
17+
apiVersion: apiextensions.k8s.io/v1
18+
kind: CustomResourceDefinition
19+
metadata:
20+
name: extensionservices.config.openservicemesh.io
21+
labels:
22+
app.kubernetes.io/name : "openservicemesh.io"
23+
spec:
24+
group: config.openservicemesh.io
25+
scope: Namespaced
26+
names:
27+
kind: ExtensionService
28+
listKind: ExtensionServiceList
29+
shortNames:
30+
- extsvc
31+
singular: extensionservice
32+
plural: extensionservices
33+
conversion:
34+
strategy: None
35+
versions:
36+
- name: v1alpha2
37+
served: true
38+
storage: true
39+
schema:
40+
openAPIV3Schema:
41+
type: object
42+
properties:
43+
spec:
44+
type: object
45+
required:
46+
- host
47+
- port
48+
properties:
49+
host:
50+
description: Hostname of the service.
51+
type: string
52+
minLength: 1
53+
port:
54+
description: Port of the service.
55+
type: integer
56+
minimum: 1
57+
maximum: 65535
58+
protocol:
59+
description: Protocol of the service.
60+
type: string
61+
connectTimeout:
62+
description: Timeout for connecting to the service.
63+
type: string
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Custom Resource Definition (CRD) for OSM's Telemetry API.
2+
#
3+
# Copyright Open Service Mesh authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
---
17+
apiVersion: apiextensions.k8s.io/v1
18+
kind: CustomResourceDefinition
19+
metadata:
20+
name: telemetries.policy.openservicemesh.io
21+
labels:
22+
app.kubernetes.io/name : "openservicemesh.io"
23+
spec:
24+
group: policy.openservicemesh.io
25+
scope: Namespaced
26+
names:
27+
kind: Telemetry
28+
listKind: TelemetryList
29+
shortNames:
30+
- telemetry
31+
singular: telemetry
32+
plural: telemetries
33+
conversion:
34+
strategy: None
35+
versions:
36+
- name: v1alpha1
37+
served: true
38+
storage: true
39+
additionalPrinterColumns:
40+
- description: Current status of the Telemetry policy.
41+
jsonPath: .status.currentStatus
42+
name: Status
43+
type: string
44+
schema:
45+
openAPIV3Schema:
46+
type: object
47+
properties:
48+
spec:
49+
type: object
50+
properties:
51+
selector:
52+
description: selector (optional) defines the pod label selector for pods the Telemetry
53+
configuration is applicable to. It selects pods with matching label keys
54+
and values. If not specified, the configuration applies to all pods
55+
in the Telemetry resource's namespace.
56+
type: object
57+
additionalProperties: true
58+
accessLog:
59+
description: accessLog (optional) defines the Envoy access log configuration.
60+
type: object
61+
properties:
62+
format:
63+
description: format (optional) defines the Envoy access log format.
64+
The format can either be unstructured or structured (e.g. JSON).
65+
Refer to https://www.envoyproxy.io/docs/envoy/latest/configuration/observability/access_log/usage#format-strings
66+
regarding how a format string can be specified.
67+
type: string
68+
minLength: 1
69+
openTelemetry:
70+
description: openTelemetry (optional) defines the OpenTelemetry configuration used to export the
71+
Envoy access logs to an OpenTelemetry collector.
72+
type: object
73+
required:
74+
- extensionService
75+
properties:
76+
extensionService:
77+
description: extensionService defines the reference to ExtensionService resource
78+
corresponding to the OpenTelemetry collector the access log should be exported to.
79+
type: object
80+
required:
81+
- namespace
82+
- name
83+
properties:
84+
namespace:
85+
description: Namespace of the ExtensionService resource.
86+
type: string
87+
minLength: 1
88+
name:
89+
description: Name of the ExtensionService resource.
90+
type: string
91+
minLength: 1
92+
attributes:
93+
description: attributes (optional) defines key-value pairs as additional metadata corresponding access log record.
94+
type: object
95+
additionalProperties: true
96+
status:
97+
type: object
98+
x-kubernetes-preserve-unknown-fields: true
99+
subresources:
100+
# status enables the status subresource
101+
status: {}

pkg/apis/policy/v1alpha1/telemetry.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ type EnvoyAccessLogConfig struct {
4545
// The format can either be unstructured or structured (e.g. JSON).
4646
// Refer to https://www.envoyproxy.io/docs/envoy/latest/configuration/observability/access_log/usage#format-strings
4747
// regarding how a format string can be specified.
48-
Format string `json:"format"`
48+
// +optional
49+
Format string `json:"format,omitempty"`
4950

5051
// OpenTelemetry defines the OpenTelemetry configuration used to export the
5152
// Envoy access logs to an OpenTelemetry collector.
@@ -56,8 +57,8 @@ type EnvoyAccessLogConfig struct {
5657
// EnvoyAccessLogOpenTelemetryConfig defines the Envoy access log OpenTelemetry
5758
// configuration.
5859
type EnvoyAccessLogOpenTelemetryConfig struct {
59-
// ExtensionService defines the references to ExtensionService resource
60-
// corresponding to the OpenTelemetry collector.
60+
// ExtensionService defines the referenence to ExtensionService resource
61+
// corresponding to the OpenTelemetry collector the access log should be exported to.
6162
ExtensionService ExtensionServiceRef `json:"extensionService"`
6263

6364
// Attributes defines key-value pairs as additional metadata corresponding access log record.

pkg/catalog/mock_catalog_generated.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/compute/mock_compute_client_generated.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/k8s/client.go

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@ package k8s
33
import (
44
"context"
55

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"
69
corev1 "k8s.io/api/core/v1"
710
v1 "k8s.io/api/core/v1"
811
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
"k8s.io/apimachinery/pkg/labels"
913
"k8s.io/apimachinery/pkg/types"
1014
"k8s.io/client-go/kubernetes"
1115

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-
1616
configv1alpha2 "github.com/openservicemesh/osm/pkg/apis/config/v1alpha2"
1717
policyv1alpha1 "github.com/openservicemesh/osm/pkg/apis/policy/v1alpha1"
1818
configv1alpha2Client "github.com/openservicemesh/osm/pkg/gen/client/config/clientset/versioned"
1919
policyv1alpha1Client "github.com/openservicemesh/osm/pkg/gen/client/policy/clientset/versioned"
20-
"github.com/openservicemesh/osm/pkg/models"
2120

2221
"github.com/openservicemesh/osm/pkg/constants"
2322
"github.com/openservicemesh/osm/pkg/errcode"
2423
"github.com/openservicemesh/osm/pkg/k8s/informers"
2524
"github.com/openservicemesh/osm/pkg/messaging"
25+
"github.com/openservicemesh/osm/pkg/models"
2626
)
2727

2828
// 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
4747
Endpoints: c.initEndpointMonitor,
4848
MeshConfig: c.initMeshConfigMonitor,
4949
MeshRootCertificate: c.initMRCMonitor,
50+
ExtensionService: c.initExtensionServiceMonitor,
5051
Egress: c.initEgressMonitor,
5152
IngressBackend: c.initIngressBackendMonitor,
5253
Retry: c.initRetryMonitor,
5354
UpstreamTrafficSetting: c.initUpstreamTrafficSettingMonitor,
55+
Telemetry: c.initTelemetryMonitor,
5456
TrafficSplit: c.initTrafficSplitMonitor,
5557
HTTPRouteGroup: c.initHTTPRouteGroupMonitor,
5658
TCPRoute: c.initTCPRouteMonitor,
@@ -60,8 +62,8 @@ func NewClient(osmNamespace, meshConfigName string, informerCollection *informer
6062
// If specific informers are not selected to be initialized, initialize all informers
6163
if len(selectInformers) == 0 {
6264
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,
6567
TrafficTarget}
6668
}
6769

@@ -87,6 +89,10 @@ func (c *Client) initMRCMonitor() {
8789
c.informers.AddEventHandler(informers.InformerKeyMeshRootCertificate, GetEventHandlerFuncs(nil, c.msgBroker))
8890
}
8991

92+
func (c *Client) initExtensionServiceMonitor() {
93+
c.informers.AddEventHandler(informers.InformerKeyExtensionService, GetEventHandlerFuncs(nil, c.msgBroker))
94+
}
95+
9096
func (c *Client) initEgressMonitor() {
9197
c.informers.AddEventHandler(informers.InformerKeyEgress, GetEventHandlerFuncs(c.shouldObserve, c.msgBroker))
9298
}
@@ -103,6 +109,10 @@ func (c *Client) initUpstreamTrafficSettingMonitor() {
103109
c.informers.AddEventHandler(informers.InformerKeyUpstreamTrafficSetting, GetEventHandlerFuncs(c.shouldObserve, c.msgBroker))
104110
}
105111

112+
func (c *Client) initTelemetryMonitor() {
113+
c.informers.AddEventHandler(informers.InformerKeyTelemetry, GetEventHandlerFuncs(c.shouldObserve, c.msgBroker))
114+
}
115+
106116
// Function to filter K8s meta Objects by OSM's isMonitoredNamespace
107117
func (c *Client) shouldObserve(obj interface{}) bool {
108118
object, ok := obj.(metav1.Object)
@@ -593,3 +603,52 @@ func (c *Client) ListTrafficTargets() []*smiAccess.TrafficTarget {
593603
}
594604
return trafficTargets
595605
}
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

Comments
 (0)