generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathconverter.go
420 lines (366 loc) · 13.1 KB
/
converter.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
/*
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"
"github.com/kubernetes-sigs/ingress2gateway/pkg/i2gw/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"
gatewayv1 "sigs.k8s.io/gateway-api/apis/v1"
)
// ToIR converts the received ingresses to intermediate.IR without taking into
// consideration any provider specific logic.
func ToIR(ingresses []networkingv1.Ingress, options i2gw.ProviderImplementationSpecificOptions) (intermediate.IR, field.ErrorList) {
aggregator := ingressAggregator{ruleGroups: map[ruleGroupKey]*ingressRuleGroup{}}
var errs field.ErrorList
for _, ingress := range ingresses {
aggregator.addIngress(ingress)
}
if len(errs) > 0 {
return intermediate.IR{}, errs
}
routes, gateways, errs := aggregator.toHTTPRoutesAndGateways(options)
if len(errs) > 0 {
return intermediate.IR{}, errs
}
routeByKey := make(map[types.NamespacedName]intermediate.HTTPRouteContext)
for _, route := range routes {
key := types.NamespacedName{Namespace: route.Namespace, Name: route.Name}
routeByKey[key] = intermediate.HTTPRouteContext{HTTPRoute: route}
}
gatewayByKey := make(map[types.NamespacedName]intermediate.GatewayContext)
for _, gateway := range gateways {
key := types.NamespacedName{Namespace: gateway.Namespace, Name: gateway.Name}
gatewayByKey[key] = intermediate.GatewayContext{Gateway: gateway}
}
return intermediate.IR{
Gateways: gatewayByKey,
HTTPRoutes: routeByKey,
}, 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: "v1alpha2",
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",
}
)
type ruleGroupKey string
type ingressAggregator struct {
ruleGroups map[ruleGroupKey]*ingressRuleGroup
defaultBackends []ingressDefaultBackend
}
type pathMatchKey string
type ingressRuleGroup struct {
namespace string
name string
ingressClass string
host string
tls []networkingv1.IngressTLS
rules []ingressRule
}
type ingressRule struct {
rule networkingv1.IngressRule
}
type ingressDefaultBackend struct {
name string
namespace string
ingressClass string
backend networkingv1.IngressBackend
}
type ingressPath struct {
ruleIdx int
pathIdx int
ruleType string
path networkingv1.HTTPIngressPath
}
func (a *ingressAggregator) addIngress(ingress networkingv1.Ingress) {
ingressClass := GetIngressClass(ingress)
for _, rule := range ingress.Spec.Rules {
a.addIngressRule(ingress.Namespace, ingress.Name, ingressClass, rule, ingress.Spec)
}
if ingress.Spec.DefaultBackend != nil {
a.defaultBackends = append(a.defaultBackends, ingressDefaultBackend{
name: ingress.Name,
namespace: ingress.Namespace,
ingressClass: ingressClass,
backend: *ingress.Spec.DefaultBackend,
})
}
}
func (a *ingressAggregator) addIngressRule(namespace, name, ingressClass string, rule networkingv1.IngressRule, iSpec networkingv1.IngressSpec) {
rgKey := ruleGroupKey(fmt.Sprintf("%s/%s/%s", namespace, ingressClass, rule.Host))
rg, ok := a.ruleGroups[rgKey]
if !ok {
rg = &ingressRuleGroup{
namespace: namespace,
name: name,
ingressClass: ingressClass,
host: rule.Host,
}
a.ruleGroups[rgKey] = rg
}
if len(iSpec.TLS) > 0 {
rg.tls = append(rg.tls, iSpec.TLS...)
}
rg.rules = append(rg.rules, ingressRule{rule: rule})
}
func (a *ingressAggregator) toHTTPRoutesAndGateways(options i2gw.ProviderImplementationSpecificOptions) ([]gatewayv1.HTTPRoute, []gatewayv1.Gateway, field.ErrorList) {
var httpRoutes []gatewayv1.HTTPRoute
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.GatewayTLSConfig{}
}
for _, tls := range rg.tls {
listener.TLS.CertificateRefs = append(listener.TLS.CertificateRefs,
gatewayv1.SecretObjectReference{Name: gatewayv1.ObjectName(tls.SecretName)})
}
gwKey := fmt.Sprintf("%s/%s", rg.namespace, rg.ingressClass)
listenersByNamespacedGateway[gwKey] = append(listenersByNamespacedGateway[gwKey], listener)
httpRoute, errs := rg.toHTTPRoute(options)
httpRoutes = append(httpRoutes, httpRoute)
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)
backendRef, err := toBackendRef(db.backend, 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}},
})
}
httpRoutes = append(httpRoutes, httpRoute)
}
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
}
for _, listener := range listeners {
var listenerNamePrefix string
if listener.Hostname != nil && *listener.Hostname != "" {
listenerNamePrefix = fmt.Sprintf("%s-", NameFromHost(string(*listener.Hostname)))
}
gateway.Spec.Listeners = append(gateway.Spec.Listeners, gatewayv1.Listener{
Name: gatewayv1.SectionName(fmt.Sprintf("%shttp", listenerNamePrefix)),
Hostname: listener.Hostname,
Port: 80,
Protocol: gatewayv1.HTTPProtocolType,
})
if listener.TLS != nil {
gateway.Spec.Listeners = append(gateway.Spec.Listeners, gatewayv1.Listener{
Name: gatewayv1.SectionName(fmt.Sprintf("%shttps", listenerNamePrefix)),
Hostname: listener.Hostname,
Port: 443,
Protocol: gatewayv1.HTTPSProtocolType,
TLS: listener.TLS,
})
}
}
}
var gateways []gatewayv1.Gateway
for _, gw := range gatewaysByKey {
gateways = append(gateways, *gw)
}
return httpRoutes, gateways, errors
}
func (rg *ingressRuleGroup) toHTTPRoute(options i2gw.ProviderImplementationSpecificOptions) (gatewayv1.HTTPRoute, 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
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, errs := rg.configureBackendRef(paths)
errors = append(errors, errs...)
hrRule.BackendRefs = backendRefs
httpRoute.Spec.Rules = append(httpRoute.Spec.Rules, hrRule)
}
return httpRoute, errors
}
func (rg *ingressRuleGroup) configureBackendRef(paths []ingressPath) ([]gatewayv1.HTTPBackendRef, field.ErrorList) {
var errors field.ErrorList
var backendRefs []gatewayv1.HTTPBackendRef
for i, path := range paths {
backendRef, err := toBackendRef(path.path.Backend, field.NewPath("paths", "backends").Index(i))
if err != nil {
errors = append(errors, err)
continue
}
backendRefs = append(backendRefs, gatewayv1.HTTPBackendRef{BackendRef: *backendRef})
}
return removeBackendRefsDuplicates(backendRefs), 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 toBackendRef(ib networkingv1.IngressBackend, path *field.Path) (*gatewayv1.BackendRef, *field.Error) {
if ib.Service != nil {
if ib.Service.Port.Name != "" {
fieldPath := path.Child("service", "port")
return nil, field.Invalid(fieldPath, "name", fmt.Sprintf("named ports not supported: %s", ib.Service.Port.Name))
}
return &gatewayv1.BackendRef{
BackendObjectReference: gatewayv1.BackendObjectReference{
Name: gatewayv1.ObjectName(ib.Service.Name),
Port: (*gatewayv1.PortNumber)(&ib.Service.Port.Number),
},
}, nil
}
return &gatewayv1.BackendRef{
BackendObjectReference: gatewayv1.BackendObjectReference{
Group: (*gatewayv1.Group)(ib.Resource.APIGroup),
Kind: (*gatewayv1.Kind)(&ib.Resource.Kind),
Name: gatewayv1.ObjectName(ib.Resource.Name),
},
}, nil
}