-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathconverter.go
More file actions
617 lines (538 loc) · 20.3 KB
/
converter.go
File metadata and controls
617 lines (538 loc) · 20.3 KB
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*
Copyright 2023 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package common
import (
"cmp"
"fmt"
"slices"
"strings"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw"
providerir "github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/provider_intermediate"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/utils/ptr"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
)
type Protocol string
const (
HTTP Protocol = "http"
GRPC Protocol = "grpc"
)
// ToIR converts the received ingresses to providerir.ProviderIR without taking into
// consideration any provider specific logic.
func ToIR(httpIngresses []networkingv1.Ingress, grpcIngresses []networkingv1.Ingress, servicePorts map[types.NamespacedName]map[string]int32, options i2gw.ProviderImplementationSpecificOptions) (providerir.ProviderIR, field.ErrorList) {
aggregator := ingressAggregator{
ruleGroups: map[ruleGroupKey]*ingressRuleGroup{},
servicePorts: servicePorts,
}
var errs field.ErrorList
for _, ingress := range httpIngresses {
aggregator.addIngress(ingress, HTTP)
}
for _, ingress := range grpcIngresses {
aggregator.addIngress(ingress, GRPC)
}
httproutes, grpcroutes, gateways, errs := aggregator.toRoutesAndGateways(options)
if len(errs) > 0 {
return providerir.ProviderIR{}, errs
}
httpRouteByKey := make(map[types.NamespacedName]providerir.HTTPRouteContext)
for _, routeWithSources := range httproutes {
key := types.NamespacedName{Namespace: routeWithSources.route.Namespace, Name: routeWithSources.route.Name}
httpRouteByKey[key] = providerir.HTTPRouteContext{
HTTPRoute: routeWithSources.route,
RuleBackendSources: routeWithSources.sources,
}
}
grpcRouteByKey := make(map[types.NamespacedName]providerir.GRPCRouteContext)
for _, routeWithSources := range grpcroutes {
key := types.NamespacedName{Namespace: routeWithSources.route.Namespace, Name: routeWithSources.route.Name}
grpcRouteByKey[key] = providerir.GRPCRouteContext{
GRPCRoute: routeWithSources.route,
RuleBackendSources: routeWithSources.sources,
}
}
gatewayByKey := make(map[types.NamespacedName]providerir.GatewayContext)
for _, gateway := range gateways {
key := types.NamespacedName{Namespace: gateway.Namespace, Name: gateway.Name}
gatewayByKey[key] = providerir.GatewayContext{Gateway: gateway}
}
return providerir.ProviderIR{
Gateways: gatewayByKey,
HTTPRoutes: httpRouteByKey,
Services: make(map[types.NamespacedName]providerir.ProviderSpecificServiceIR),
GatewayClasses: make(map[types.NamespacedName]gatewayv1.GatewayClass),
TLSRoutes: make(map[types.NamespacedName]gatewayv1.TLSRoute),
TCPRoutes: make(map[types.NamespacedName]gatewayv1alpha2.TCPRoute),
UDPRoutes: make(map[types.NamespacedName]gatewayv1alpha2.UDPRoute),
GRPCRoutes: grpcRouteByKey,
BackendTLSPolicies: make(map[types.NamespacedName]gatewayv1.BackendTLSPolicy),
ReferenceGrants: make(map[types.NamespacedName]gatewayv1beta1.ReferenceGrant),
}, nil
}
var (
GatewayGVK = schema.GroupVersionKind{
Group: "gateway.networking.k8s.io",
Version: "v1",
Kind: "Gateway",
}
HTTPRouteGVK = schema.GroupVersionKind{
Group: "gateway.networking.k8s.io",
Version: "v1",
Kind: "HTTPRoute",
}
TLSRouteGVK = schema.GroupVersionKind{
Group: "gateway.networking.k8s.io",
Version: "v1",
Kind: "TLSRoute",
}
TCPRouteGVK = schema.GroupVersionKind{
Group: "gateway.networking.k8s.io",
Version: "v1alpha2",
Kind: "TCPRoute",
}
ReferenceGrantGVK = schema.GroupVersionKind{
Group: "gateway.networking.k8s.io",
Version: "v1beta1",
Kind: "ReferenceGrant",
}
GRPCRouteGVK = schema.GroupVersionKind{
Group: "gateway.networking.k8s.io",
Version: "v1",
Kind: "GRPCRoute",
}
)
type ruleGroupKey string
type ingressAggregator struct {
ruleGroups map[ruleGroupKey]*ingressRuleGroup
defaultBackends []ingressDefaultBackend
servicePorts map[types.NamespacedName]map[string]int32
}
type pathMatchKey string
type ingressRuleGroup struct {
namespace string
name string
ingressClass string
host string
tls []networkingv1.IngressTLS
rules []ingressRule
protocol Protocol
}
type ingressRule struct {
// Source tracking
ingress *networkingv1.Ingress
rule networkingv1.IngressRule
}
type ingressDefaultBackend struct {
name string
namespace string
ingressClass string
backend networkingv1.IngressBackend
sourceIngress *networkingv1.Ingress
}
type ingressPath struct {
// These are for source error propagation
ruleIdx int
pathIdx int
ruleType string
path networkingv1.HTTPIngressPath
sourceIngress *networkingv1.Ingress
}
func (a *ingressAggregator) addIngress(ingress networkingv1.Ingress, protocol Protocol) {
ingressClass := GetIngressClass(ingress)
for _, rule := range ingress.Spec.Rules {
a.addIngressRule(ingress, ingressClass, rule, protocol)
}
if ingress.Spec.DefaultBackend != nil {
a.defaultBackends = append(a.defaultBackends, ingressDefaultBackend{
name: ingress.Name,
namespace: ingress.Namespace,
ingressClass: ingressClass,
backend: *ingress.Spec.DefaultBackend,
sourceIngress: &ingress,
})
}
}
func (a *ingressAggregator) addIngressRule(ingress networkingv1.Ingress, ingressClass string, rule networkingv1.IngressRule, protocol Protocol) {
rgKey := ruleGroupKey(fmt.Sprintf("%s/%s/%s/%s", ingress.Namespace, ingressClass, rule.Host, protocol))
rg, ok := a.ruleGroups[rgKey]
if !ok {
rg = &ingressRuleGroup{
namespace: ingress.Namespace,
name: ingress.Name,
ingressClass: ingressClass,
host: rule.Host,
protocol: protocol,
}
a.ruleGroups[rgKey] = rg
}
if len(ingress.Spec.TLS) > 0 {
rg.tls = append(rg.tls, ingress.Spec.TLS...)
}
rg.rules = append(rg.rules, ingressRule{
ingress: &ingress,
rule: rule,
})
}
type httpRouteWithSources struct {
route gatewayv1.HTTPRoute
sources [][]providerir.BackendSource
}
type grpcRouteWithSources struct {
route gatewayv1.GRPCRoute
sources [][]providerir.BackendSource
}
func (a *ingressAggregator) toRoutesAndGateways(options i2gw.ProviderImplementationSpecificOptions) ([]httpRouteWithSources, []grpcRouteWithSources, []gatewayv1.Gateway, field.ErrorList) {
var httpRoutes []httpRouteWithSources
var grpcRoutes []grpcRouteWithSources
var errors field.ErrorList
listenersByNamespacedGateway := map[string][]gatewayv1.Listener{}
// Sort the rulegroups to iterate the map in a sorted order.
ruleGroupsKeys := make([]ruleGroupKey, 0, len(a.ruleGroups))
for k := range a.ruleGroups {
ruleGroupsKeys = append(ruleGroupsKeys, k)
}
slices.SortFunc(ruleGroupsKeys, func(a, b ruleGroupKey) int {
return cmp.Compare(a, b)
})
for _, rgk := range ruleGroupsKeys {
rg := a.ruleGroups[rgk]
listener := gatewayv1.Listener{}
if rg.host != "" {
listener.Hostname = (*gatewayv1.Hostname)(&rg.host)
} else if len(rg.tls) == 1 && len(rg.tls[0].Hosts) == 1 {
listener.Hostname = (*gatewayv1.Hostname)(&rg.tls[0].Hosts[0])
}
if len(rg.tls) > 0 {
listener.TLS = &gatewayv1.ListenerTLSConfig{}
}
certNames := map[string]struct{}{}
for _, tls := range rg.tls {
certNames[tls.SecretName] = struct{}{}
}
for certName := range certNames {
listener.TLS.CertificateRefs = append(listener.TLS.CertificateRefs,
gatewayv1.SecretObjectReference{
Group: ptr.To(gatewayv1.Group("")),
Kind: ptr.To(gatewayv1.Kind("Secret")),
Name: gatewayv1.ObjectName(certName),
})
}
gwKey := fmt.Sprintf("%s/%s", rg.namespace, rg.ingressClass)
listenersByNamespacedGateway[gwKey] = append(listenersByNamespacedGateway[gwKey], listener)
if rg.protocol == HTTP {
httpRoute, sources, errs := rg.toHTTPRoute(a.servicePorts, options)
httpRoutes = append(httpRoutes, httpRouteWithSources{route: httpRoute, sources: sources})
errors = append(errors, errs...)
} else {
grpcRoute, sources, errs := rg.toGRPCRoute(a.servicePorts, options)
grpcRoutes = append(grpcRoutes, grpcRouteWithSources{route: grpcRoute, sources: sources})
errors = append(errors, errs...)
}
}
for i, db := range a.defaultBackends {
httpRoute := gatewayv1.HTTPRoute{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-default-backend", db.name),
Namespace: db.namespace,
},
Spec: gatewayv1.HTTPRouteSpec{
CommonRouteSpec: gatewayv1.CommonRouteSpec{
ParentRefs: []gatewayv1.ParentReference{{
Name: gatewayv1.ObjectName(db.ingressClass),
}},
},
},
Status: gatewayv1.HTTPRouteStatus{
RouteStatus: gatewayv1.RouteStatus{
Parents: []gatewayv1.RouteParentStatus{},
},
},
}
httpRoute.SetGroupVersionKind(HTTPRouteGVK)
// We create an HTTPRoute with a single rule and a single backend.
backendRef, err := ToBackendRef(db.namespace, db.backend, a.servicePorts, field.NewPath(db.name, "paths", "backends").Index(i))
if err != nil {
errors = append(errors, err)
} else {
httpRoute.Spec.Rules = append(httpRoute.Spec.Rules, gatewayv1.HTTPRouteRule{
BackendRefs: []gatewayv1.HTTPBackendRef{{BackendRef: *backendRef}},
})
}
// Set the single source for this default backend.
sources := [][]providerir.BackendSource{
{
{
Ingress: db.sourceIngress,
DefaultBackend: &db.backend,
},
},
}
httpRoutes = append(httpRoutes, httpRouteWithSources{route: httpRoute, sources: sources})
}
gatewaysByKey := map[string]*gatewayv1.Gateway{}
for gwKey, listeners := range listenersByNamespacedGateway {
parts := strings.Split(gwKey, "/")
if len(parts) != 2 {
errors = append(errors, field.Invalid(field.NewPath(""), "", fmt.Sprintf("error generating Gateway listeners for key: %s", gwKey)))
continue
}
gateway := gatewaysByKey[gwKey]
if gateway == nil {
gateway = &gatewayv1.Gateway{
ObjectMeta: metav1.ObjectMeta{
Namespace: parts[0],
Name: parts[1],
},
Spec: gatewayv1.GatewaySpec{
GatewayClassName: gatewayv1.ObjectName(parts[1]),
},
}
gateway.SetGroupVersionKind(GatewayGVK)
gatewaysByKey[gwKey] = gateway
}
uniqueListeners := make(map[gatewayv1.SectionName]*gatewayv1.Listener)
var orderedNames []gatewayv1.SectionName
for _, l := range listeners {
var listenerNamePrefix string
if l.Hostname != nil && *l.Hostname != "" {
listenerNamePrefix = fmt.Sprintf("%s-", NameFromHost(string(*l.Hostname)))
}
// Add/Update HTTP listener
httpName := gatewayv1.SectionName(fmt.Sprintf("%shttp", listenerNamePrefix))
if _, exists := uniqueListeners[httpName]; !exists {
uniqueListeners[httpName] = &gatewayv1.Listener{
Name: httpName,
Hostname: l.Hostname,
Port: 80,
Protocol: gatewayv1.HTTPProtocolType,
}
orderedNames = append(orderedNames, httpName)
}
// Add/Update HTTPS listener
if l.TLS != nil {
httpsName := gatewayv1.SectionName(fmt.Sprintf("%shttps", listenerNamePrefix))
if _, exists := uniqueListeners[httpsName]; !exists {
uniqueListeners[httpsName] = &gatewayv1.Listener{
Name: httpsName,
Hostname: l.Hostname,
Port: 443,
Protocol: gatewayv1.HTTPSProtocolType,
TLS: &gatewayv1.ListenerTLSConfig{},
}
orderedNames = append(orderedNames, httpsName)
}
// Merge CertificateRefs
uniqueListeners[httpsName].TLS.CertificateRefs = append(uniqueListeners[httpsName].TLS.CertificateRefs, l.TLS.CertificateRefs...)
}
}
for _, name := range orderedNames {
l := uniqueListeners[name]
// Final deduplication of certificates for this listener
if l.TLS != nil && len(l.TLS.CertificateRefs) > 0 {
uniqueRefs := make(map[gatewayv1.ObjectName]bool)
var certificates []gatewayv1.SecretObjectReference
for _, ref := range l.TLS.CertificateRefs {
if !uniqueRefs[ref.Name] {
certificates = append(certificates, ref)
uniqueRefs[ref.Name] = true
}
}
l.TLS.CertificateRefs = certificates
}
gateway.Spec.Listeners = append(gateway.Spec.Listeners, *l)
}
}
var gateways []gatewayv1.Gateway
for _, gw := range gatewaysByKey {
gateways = append(gateways, *gw)
}
return httpRoutes, grpcRoutes, gateways, errors
}
func (rg *ingressRuleGroup) toHTTPRoute(servicePorts map[types.NamespacedName]map[string]int32, options i2gw.ProviderImplementationSpecificOptions) (gatewayv1.HTTPRoute, [][]providerir.BackendSource, field.ErrorList) {
ingressPathsByMatchKey := groupIngressPathsByMatchKey(rg.rules)
httpRoute := gatewayv1.HTTPRoute{
ObjectMeta: metav1.ObjectMeta{
Name: RouteName(rg.name, rg.host),
Namespace: rg.namespace,
},
Spec: gatewayv1.HTTPRouteSpec{},
Status: gatewayv1.HTTPRouteStatus{
RouteStatus: gatewayv1.RouteStatus{
Parents: []gatewayv1.RouteParentStatus{},
},
},
}
httpRoute.SetGroupVersionKind(HTTPRouteGVK)
if rg.ingressClass != "" {
httpRoute.Spec.ParentRefs = []gatewayv1.ParentReference{{Name: gatewayv1.ObjectName(rg.ingressClass)}}
}
if rg.host != "" {
httpRoute.Spec.Hostnames = []gatewayv1.Hostname{gatewayv1.Hostname(rg.host)}
}
var errors field.ErrorList
var allRuleBackendSources [][]providerir.BackendSource
for _, key := range ingressPathsByMatchKey.keys {
paths := ingressPathsByMatchKey.data[key]
path := paths[0]
fieldPath := field.NewPath("spec", "rules").Index(path.ruleIdx).Child(path.ruleType).Child("paths").Index(path.pathIdx)
match, err := toHTTPRouteMatch(path.path, fieldPath, options.ToImplementationSpecificHTTPPathTypeMatch)
if err != nil {
errors = append(errors, err)
continue
}
hrRule := gatewayv1.HTTPRouteRule{
Matches: []gatewayv1.HTTPRouteMatch{*match},
}
backendRefs, sources, errs := rg.configureBackendRef(servicePorts, paths)
errors = append(errors, errs...)
hrRule.BackendRefs = backendRefs
httpRoute.Spec.Rules = append(httpRoute.Spec.Rules, hrRule)
allRuleBackendSources = append(allRuleBackendSources, sources)
}
return httpRoute, allRuleBackendSources, errors
}
func (rg *ingressRuleGroup) configureBackendRef(servicePorts map[types.NamespacedName]map[string]int32, paths []ingressPath) ([]gatewayv1.HTTPBackendRef, []providerir.BackendSource, field.ErrorList) {
var errors field.ErrorList
var backendRefs []gatewayv1.HTTPBackendRef
var sources []providerir.BackendSource
for i, path := range paths {
backendRef, err := ToBackendRef(rg.namespace, path.path.Backend, servicePorts, field.NewPath("paths", "backends").Index(i))
if err != nil {
errors = append(errors, err)
continue
}
backendRefs = append(backendRefs, gatewayv1.HTTPBackendRef{BackendRef: *backendRef})
// Track source for this backend
sources = append(sources, providerir.BackendSource{
Ingress: path.sourceIngress,
Path: &path.path,
})
}
// keep duplicates as they might have different sources.
return backendRefs, sources, errors
}
func getPathMatchKey(ip ingressPath) pathMatchKey {
var pathType string
if ip.path.PathType != nil {
pathType = string(*ip.path.PathType)
}
return pathMatchKey(fmt.Sprintf("%s/%s", pathType, ip.path.Path))
}
func toHTTPRouteMatch(routePath networkingv1.HTTPIngressPath, path *field.Path, toImplementationSpecificPathMatch i2gw.ImplementationSpecificHTTPPathTypeMatchConverter) (*gatewayv1.HTTPRouteMatch, *field.Error) {
pmPrefix := gatewayv1.PathMatchPathPrefix
pmExact := gatewayv1.PathMatchExact
match := &gatewayv1.HTTPRouteMatch{Path: &gatewayv1.HTTPPathMatch{Value: &routePath.Path}}
if routePath.PathType == nil {
return nil, field.Invalid(path.Child("pathType"), routePath.PathType, "pathType is required")
}
switch *routePath.PathType {
case networkingv1.PathTypePrefix:
match.Path.Type = &pmPrefix
case networkingv1.PathTypeExact:
match.Path.Type = &pmExact
// In case the path type is ImplementationSpecific, the path value and type
// will be set by the provider-specific customization function. If such function
// is not given by the provider, an error is returned.
case networkingv1.PathTypeImplementationSpecific:
if toImplementationSpecificPathMatch != nil {
toImplementationSpecificPathMatch(match.Path)
} else {
return nil, field.Invalid(path.Child("pathType"), routePath.PathType, "implementationSpecific path type is not supported in generic translation, and your provider does not provide custom support to translate it")
}
default:
// default should never hit, as all the possible cases are already checked
// via proper switch cases.
return nil, field.Invalid(path.Child("pathType"), match.Path.Type, fmt.Sprintf("unsupported path match type: %s", *match.Path.Type))
}
return match, nil
}
func (rg *ingressRuleGroup) toGRPCRoute(servicePorts map[types.NamespacedName]map[string]int32, _ i2gw.ProviderImplementationSpecificOptions) (gatewayv1.GRPCRoute, [][]providerir.BackendSource, field.ErrorList) {
// Parse paths to create proper GRPCRouteMatches
ingressPathsByMatchKey := groupIngressPathsByMatchKey(rg.rules)
grpcRoute := gatewayv1.GRPCRoute{
ObjectMeta: metav1.ObjectMeta{
Name: RouteName(rg.name, rg.host),
Namespace: rg.namespace,
},
Spec: gatewayv1.GRPCRouteSpec{},
}
grpcRoute.SetGroupVersionKind(GRPCRouteGVK)
if rg.ingressClass != "" {
grpcRoute.Spec.ParentRefs = []gatewayv1.ParentReference{{Name: gatewayv1.ObjectName(rg.ingressClass)}}
}
if rg.host != "" {
grpcRoute.Spec.Hostnames = []gatewayv1.Hostname{gatewayv1.Hostname(rg.host)}
}
var errors field.ErrorList
var allRuleBackendSources [][]providerir.BackendSource
for _, key := range ingressPathsByMatchKey.keys {
paths := ingressPathsByMatchKey.data[key]
path := paths[0]
fieldPath := field.NewPath("spec", "rules").Index(path.ruleIdx).Child(path.ruleType).Child("paths").Index(path.pathIdx)
// Parse the path to create a GRPCRouteMatch
match := toGRPCRouteMatch(path.path, fieldPath)
grpcRule := gatewayv1.GRPCRouteRule{}
// Only add matches if there's actually something to match (service or method)
if match.Method != nil {
grpcRule.Matches = []gatewayv1.GRPCRouteMatch{*match}
}
backendRefs, sources, errs := rg.configureGRPCBackendRef(servicePorts, paths)
errors = append(errors, errs...)
grpcRule.BackendRefs = backendRefs
grpcRoute.Spec.Rules = append(grpcRoute.Spec.Rules, grpcRule)
allRuleBackendSources = append(allRuleBackendSources, sources)
}
return grpcRoute, allRuleBackendSources, errors
}
func (rg *ingressRuleGroup) configureGRPCBackendRef(servicePorts map[types.NamespacedName]map[string]int32, paths []ingressPath) ([]gatewayv1.GRPCBackendRef, []providerir.BackendSource, field.ErrorList) {
var errors field.ErrorList
var backendRefs []gatewayv1.GRPCBackendRef
var sources []providerir.BackendSource
for i, path := range paths {
backendRef, err := ToBackendRef(rg.namespace, path.path.Backend, servicePorts, field.NewPath("paths", "backends").Index(i))
if err != nil {
errors = append(errors, err)
continue
}
backendRefs = append(backendRefs, gatewayv1.GRPCBackendRef{BackendRef: *backendRef})
// Track source for this backend
sources = append(sources, providerir.BackendSource{
Ingress: path.sourceIngress,
Path: &path.path,
})
}
// keep duplicates as they might have different sources.
return backendRefs, sources, errors
}
func toGRPCRouteMatch(routePath networkingv1.HTTPIngressPath, _ *field.Path) *gatewayv1.GRPCRouteMatch {
// Parse the path to extract service and method
// Example: /hello.HelloService/SayHello -> service="hello.HelloService", method="SayHello"
service, method := ParseGRPCServiceMethod(routePath.Path)
match := &gatewayv1.GRPCRouteMatch{}
if service != "" || method != "" {
match.Method = &gatewayv1.GRPCMethodMatch{}
if service != "" {
match.Method.Service = &service
}
if method != "" {
match.Method.Method = &method
}
}
return match
}