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
322 lines (271 loc) · 10.7 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
package smi
import (
"encoding/json"
"fmt"
"net/http"
smiAccess "github.com/servicemeshinterface/smi-sdk-go/pkg/apis/access/v1alpha3"
smiSpecs "github.com/servicemeshinterface/smi-sdk-go/pkg/apis/specs/v1alpha4"
smiSplit "github.com/servicemeshinterface/smi-sdk-go/pkg/apis/split/v1alpha2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
a "github.com/openservicemesh/osm/pkg/announcements"
"github.com/openservicemesh/osm/pkg/identity"
"github.com/openservicemesh/osm/pkg/k8s"
"github.com/openservicemesh/osm/pkg/k8s/informers"
"github.com/openservicemesh/osm/pkg/messaging"
)
const (
// ServiceAccountKind is the kind specified for the destination and sources in an SMI TrafficTarget policy
ServiceAccountKind = "ServiceAccount"
// TCPRouteKind is the kind specified for the TCP route rules in an SMI Traffictarget policy
TCPRouteKind = "TCPRoute"
// HTTPRouteGroupKind is the kind specified for the HTTP route rules in an SMI Traffictarget policy
HTTPRouteGroupKind = "HTTPRouteGroup"
// We have a few different k8s clients. This identifies these in logs.
kubernetesClientName = "MeshSpec"
)
// NewSMIClient implements mesh.MeshSpec and creates the Kubernetes client, which retrieves SMI specific CRDs.
func NewSMIClient(informerCollection *informers.InformerCollection, osmNamespace string, kubeController k8s.Controller, msgBroker *messaging.Broker) *Client {
client := Client{
providerIdent: kubernetesClientName,
informers: informerCollection,
osmNamespace: osmNamespace,
kubeController: kubeController,
}
shouldObserve := func(obj interface{}) bool {
object, ok := obj.(metav1.Object)
if !ok {
return false
}
return informerCollection.IsMonitoredNamespace(object.GetNamespace())
}
splitEventTypes := k8s.EventTypes{
Add: a.TrafficSplitAdded,
Update: a.TrafficSplitUpdated,
Delete: a.TrafficSplitDeleted,
}
informerCollection.AddEventHandler(informers.InformerKeyTrafficSplit, k8s.GetEventHandlerFuncs(shouldObserve, splitEventTypes, msgBroker))
routeGroupEventTypes := k8s.EventTypes{
Add: a.RouteGroupAdded,
Update: a.RouteGroupUpdated,
Delete: a.RouteGroupDeleted,
}
informerCollection.AddEventHandler(informers.InformerKeyHTTPRouteGroup, k8s.GetEventHandlerFuncs(shouldObserve, routeGroupEventTypes, msgBroker))
tcpRouteEventTypes := k8s.EventTypes{
Add: a.TCPRouteAdded,
Update: a.TCPRouteUpdated,
Delete: a.TCPRouteDeleted,
}
informerCollection.AddEventHandler(informers.InformerKeyTCPRoute, k8s.GetEventHandlerFuncs(shouldObserve, tcpRouteEventTypes, msgBroker))
trafficTargetEventTypes := k8s.EventTypes{
Add: a.TrafficTargetAdded,
Update: a.TrafficTargetUpdated,
Delete: a.TrafficTargetDeleted,
}
informerCollection.AddEventHandler(informers.InformerKeyTrafficTarget, k8s.GetEventHandlerFuncs(shouldObserve, trafficTargetEventTypes, msgBroker))
return &client
}
// ListTrafficSplits implements mesh.MeshSpec by returning the list of traffic splits.
func (c *Client) ListTrafficSplits(options ...TrafficSplitListOption) []*smiSplit.TrafficSplit {
var trafficSplits []*smiSplit.TrafficSplit
for _, splitIface := range c.informers.List(informers.InformerKeyTrafficSplit) {
trafficSplit := splitIface.(*smiSplit.TrafficSplit)
if !c.kubeController.IsMonitoredNamespace(trafficSplit.Namespace) {
continue
}
options = append(options, WithKubeController(c.kubeController))
if filteredSplit := FilterTrafficSplit(trafficSplit, options...); filteredSplit != nil {
trafficSplits = append(trafficSplits, filteredSplit)
}
}
return trafficSplits
}
// FilterTrafficSplit applies the given TrafficSplitListOption filter on the given TrafficSplit object
func FilterTrafficSplit(trafficSplit *smiSplit.TrafficSplit, options ...TrafficSplitListOption) *smiSplit.TrafficSplit {
if trafficSplit == nil {
return nil
}
o := &TrafficSplitListOpt{}
for _, opt := range options {
opt(o)
}
// If apex service filter option is set, ignore traffic splits whose apex service does not match
if o.ApexService.Name != "" && (o.ApexService.Namespace != trafficSplit.Namespace ||
o.ApexService.Name != k8s.GetServiceFromHostname(o.KubeController, trafficSplit.Spec.Service)) {
return nil
}
// If backend service filter option is set, ignore traffic splits whose backend service does not match
if o.BackendService.Name != "" {
if trafficSplit.Namespace != o.BackendService.Namespace {
return nil
}
backendFound := false
for _, backend := range trafficSplit.Spec.Backends {
if backend.Service == o.BackendService.Name {
backendFound = true
break
}
}
if !backendFound {
return nil
}
}
return trafficSplit
}
// ListHTTPTrafficSpecs lists SMI HTTPRouteGroup resources
func (c *Client) ListHTTPTrafficSpecs() []*smiSpecs.HTTPRouteGroup {
var httpTrafficSpec []*smiSpecs.HTTPRouteGroup
for _, specIface := range c.informers.List(informers.InformerKeyHTTPRouteGroup) {
routeGroup := specIface.(*smiSpecs.HTTPRouteGroup)
if !c.kubeController.IsMonitoredNamespace(routeGroup.Namespace) {
continue
}
httpTrafficSpec = append(httpTrafficSpec, routeGroup)
}
return httpTrafficSpec
}
// GetHTTPRouteGroup returns an SMI HTTPRouteGroup resource given its name of the form <namespace>/<name>
func (c *Client) GetHTTPRouteGroup(namespacedName string) *smiSpecs.HTTPRouteGroup {
// client-go cache uses <namespace>/<name> as key
routeIf, exists, err := c.informers.GetByKey(informers.InformerKeyHTTPRouteGroup, namespacedName)
if exists && err == nil {
route := routeIf.(*smiSpecs.HTTPRouteGroup)
if !c.kubeController.IsMonitoredNamespace(route.Namespace) {
log.Warn().Msgf("HTTPRouteGroup %s found, but belongs to a namespace that is not monitored, ignoring it", namespacedName)
return nil
}
return route
}
return nil
}
// ListTCPTrafficSpecs lists SMI TCPRoute resources
func (c *Client) ListTCPTrafficSpecs() []*smiSpecs.TCPRoute {
var tcpRouteSpec []*smiSpecs.TCPRoute
for _, specIface := range c.informers.List(informers.InformerKeyTCPRoute) {
tcpRoute := specIface.(*smiSpecs.TCPRoute)
if !c.kubeController.IsMonitoredNamespace(tcpRoute.Namespace) {
continue
}
tcpRouteSpec = append(tcpRouteSpec, tcpRoute)
}
return tcpRouteSpec
}
// GetTCPRoute returns an SMI TCPRoute resource given its name of the form <namespace>/<name>
func (c *Client) GetTCPRoute(namespacedName string) *smiSpecs.TCPRoute {
// client-go cache uses <namespace>/<name> as key
routeIf, exists, err := c.informers.GetByKey(informers.InformerKeyTCPRoute, namespacedName)
if exists && err == nil {
route := routeIf.(*smiSpecs.TCPRoute)
if !c.kubeController.IsMonitoredNamespace(route.Namespace) {
log.Warn().Msgf("TCPRoute %s found, but belongs to a namespace that is not monitored, ignoring it", namespacedName)
return nil
}
return route
}
return nil
}
// ListTrafficTargets implements mesh.Topology by returning the list of traffic targets.
func (c *Client) ListTrafficTargets(options ...TrafficTargetListOption) []*smiAccess.TrafficTarget {
var trafficTargets []*smiAccess.TrafficTarget
for _, targetIface := range c.informers.List(informers.InformerKeyTrafficTarget) {
trafficTarget := targetIface.(*smiAccess.TrafficTarget)
if !c.kubeController.IsMonitoredNamespace(trafficTarget.Namespace) {
continue
}
if !isValidTrafficTarget(trafficTarget) {
continue
}
// Filter TrafficTarget based on the given options
if filteredTrafficTarget := FilterTrafficTarget(trafficTarget, options...); filteredTrafficTarget != nil {
trafficTargets = append(trafficTargets, trafficTarget)
}
}
return trafficTargets
}
func isValidTrafficTarget(trafficTarget *smiAccess.TrafficTarget) bool {
// destination namespace must be same as traffic target namespace
if trafficTarget.Namespace != trafficTarget.Spec.Destination.Namespace {
return false
}
if !hasValidRules(trafficTarget.Spec.Rules) {
return false
}
return true
}
// hasValidRules checks if the given SMI TrafficTarget object has valid rules
func hasValidRules(rules []smiAccess.TrafficTargetRule) bool {
if len(rules) == 0 {
return false
}
for _, rule := range rules {
switch rule.Kind {
case HTTPRouteGroupKind, TCPRouteKind:
// valid Kind for rules
default:
log.Error().Msgf("Invalid Kind for rule %s in TrafficTarget policy %s", rule.Name, rule.Kind)
return false
}
}
return true
}
// FilterTrafficTarget applies the given TrafficTargetListOption filter on the given TrafficTarget object
func FilterTrafficTarget(trafficTarget *smiAccess.TrafficTarget, options ...TrafficTargetListOption) *smiAccess.TrafficTarget {
if trafficTarget == nil {
return nil
}
o := &TrafficTargetListOpt{}
for _, opt := range options {
opt(o)
}
if o.Destination.Name != "" && (o.Destination.Namespace != trafficTarget.Spec.Destination.Namespace ||
o.Destination.Name != trafficTarget.Spec.Destination.Name) {
return nil
}
return trafficTarget
}
// ListServiceAccounts lists ServiceAccounts specified in SMI TrafficTarget resources
func (c *Client) ListServiceAccounts() []identity.K8sServiceAccount {
var serviceAccounts []identity.K8sServiceAccount
for _, targetIface := range c.informers.List(informers.InformerKeyTrafficTarget) {
trafficTarget := targetIface.(*smiAccess.TrafficTarget)
if !c.kubeController.IsMonitoredNamespace(trafficTarget.Namespace) {
continue
}
if !isValidTrafficTarget(trafficTarget) {
continue
}
for _, sources := range trafficTarget.Spec.Sources {
// Only monitor sources in namespaces OSM is observing
if !c.kubeController.IsMonitoredNamespace(sources.Namespace) {
// Doesn't belong to namespaces we are observing
continue
}
namespacedServiceAccount := identity.K8sServiceAccount{
Namespace: sources.Namespace,
Name: sources.Name,
}
serviceAccounts = append(serviceAccounts, namespacedServiceAccount)
}
namespacedServiceAccount := identity.K8sServiceAccount{
Namespace: trafficTarget.Spec.Destination.Namespace,
Name: trafficTarget.Spec.Destination.Name,
}
serviceAccounts = append(serviceAccounts, namespacedServiceAccount)
}
return serviceAccounts
}
// GetSmiClientVersionHTTPHandler returns an http handler that returns supported smi version information
func GetSmiClientVersionHTTPHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
versionInfo := map[string]string{
"TrafficTarget": smiAccess.SchemeGroupVersion.String(),
"HTTPRouteGroup": smiSpecs.SchemeGroupVersion.String(),
"TCPRoute": smiSpecs.SchemeGroupVersion.String(),
"TrafficSplit": smiSplit.SchemeGroupVersion.String(),
}
if jsonVersionInfo, err := json.Marshal(versionInfo); err != nil {
log.Error().Err(err).Msgf("Error marshaling version info struct: %+v", versionInfo)
} else {
_, _ = fmt.Fprint(w, string(jsonVersionInfo))
}
})
}