Skip to content

Commit 729ab6a

Browse files
committed
Merge branch 'master' into chore-logr
2 parents b1d8511 + fe5c135 commit 729ab6a

30 files changed

+1929
-119
lines changed

api/v2/apisixroute_types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,15 @@ type ApisixRouteSpec struct {
3636
IngressClassName string `json:"ingressClassName,omitempty" yaml:"ingressClassName,omitempty"`
3737
// HTTP defines a list of HTTP route rules.
3838
// Each rule specifies conditions to match HTTP requests and how to forward them.
39+
//
40+
// +listType=map
41+
// +listMapKey=name
3942
HTTP []ApisixRouteHTTP `json:"http,omitempty" yaml:"http,omitempty"`
4043
// Stream defines a list of stream route rules.
4144
// Each rule specifies conditions to match TCP/UDP traffic and how to forward them.
45+
//
46+
// +listType=map
47+
// +listMapKey=name
4248
Stream []ApisixRouteStream `json:"stream,omitempty" yaml:"stream,omitempty"`
4349
}
4450

config/crd/bases/apisix.apache.org_apisixroutes.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,9 @@ spec:
346346
- name
347347
type: object
348348
type: array
349+
x-kubernetes-list-map-keys:
350+
- name
351+
x-kubernetes-list-type: map
349352
ingressClassName:
350353
description: |-
351354
IngressClassName is the name of the IngressClass this route belongs to.
@@ -459,6 +462,9 @@ spec:
459462
- protocol
460463
type: object
461464
type: array
465+
x-kubernetes-list-map-keys:
466+
- name
467+
x-kubernetes-list-type: map
462468
type: object
463469
status:
464470
description: ApisixStatus is the status report for Apisix ingress Resources

config/webhook/manifests.yaml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,46 @@ webhooks:
124124
resources:
125125
- gatewayproxies
126126
sideEffects: None
127+
- admissionReviewVersions:
128+
- v1
129+
clientConfig:
130+
service:
131+
name: webhook-service
132+
namespace: system
133+
path: /validate-gateway-networking-k8s-io-v1-grpcroute
134+
failurePolicy: Fail
135+
name: vgrpcroute-v1.kb.io
136+
rules:
137+
- apiGroups:
138+
- gateway.networking.k8s.io
139+
apiVersions:
140+
- v1
141+
operations:
142+
- CREATE
143+
- UPDATE
144+
resources:
145+
- grpcroutes
146+
sideEffects: None
147+
- admissionReviewVersions:
148+
- v1
149+
clientConfig:
150+
service:
151+
name: webhook-service
152+
namespace: system
153+
path: /validate-gateway-networking-k8s-io-v1-httproute
154+
failurePolicy: Fail
155+
name: vhttproute-v1.kb.io
156+
rules:
157+
- apiGroups:
158+
- gateway.networking.k8s.io
159+
apiVersions:
160+
- v1
161+
operations:
162+
- CREATE
163+
- UPDATE
164+
resources:
165+
- httproutes
166+
sideEffects: None
127167
- admissionReviewVersions:
128168
- v1
129169
clientConfig:
@@ -164,3 +204,23 @@ webhooks:
164204
resources:
165205
- ingressclasses
166206
sideEffects: None
207+
- admissionReviewVersions:
208+
- v1
209+
clientConfig:
210+
service:
211+
name: webhook-service
212+
namespace: system
213+
path: /validate-gateway-networking-k8s-io-v1alpha2-tcproute
214+
failurePolicy: Fail
215+
name: vtcproute-v1alpha2.kb.io
216+
rules:
217+
- apiGroups:
218+
- gateway.networking.k8s.io
219+
apiVersions:
220+
- v1alpha2
221+
operations:
222+
- CREATE
223+
- UPDATE
224+
resources:
225+
- tcproutes
226+
sideEffects: None

internal/adc/translator/apisixroute.go

Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -209,30 +209,12 @@ func (t *Translator) buildUpstream(tctx *provider.TranslateContext, service *adc
209209
)
210210

211211
for _, backend := range rule.Backends {
212-
var backendErr error
213-
upstream := adc.NewDefaultUpstream()
214212
// try to get the apisixupstream with the same name as the backend service to be upstream config.
215213
// err is ignored because it does not care about the externalNodes of the apisixupstream.
216-
auNN := types.NamespacedName{Namespace: ar.GetNamespace(), Name: backend.ServiceName}
217-
if au, ok := tctx.Upstreams[auNN]; ok {
218-
upstream, _ = t.translateApisixUpstream(tctx, au)
219-
}
220-
221-
if backend.ResolveGranularity == apiv2.ResolveGranularityService {
222-
upstream.Nodes, backendErr = t.translateApisixRouteBackendResolveGranularityService(tctx, utils.NamespacedName(ar), backend)
223-
if backendErr != nil {
224-
t.Log.Error(backendErr, "failed to translate ApisixRoute backend with ResolveGranularity Service")
225-
continue
226-
}
227-
} else {
228-
upstream.Nodes, backendErr = t.translateApisixRouteBackendResolveGranularityEndpoint(tctx, utils.NamespacedName(ar), backend)
229-
if backendErr != nil {
230-
t.Log.Error(backendErr, "failed to translate ApisixRoute backend with ResolveGranularity Endpoint")
231-
continue
232-
}
233-
}
234-
if backend.Weight != nil {
235-
upstream.Labels["meta_weight"] = strconv.FormatInt(int64(*backend.Weight), 10)
214+
upstream, err := t.translateApisixRouteHTTPBackend(tctx, ar, backend)
215+
if err != nil {
216+
t.Log.Error(err, "failed to translate ApisixRoute backend", "backend", backend)
217+
continue
236218
}
237219

238220
upstreamName := adc.ComposeUpstreamName(ar.Namespace, backend.ServiceName, backend.Subset, backend.ServicePort, backend.ResolveGranularity)
@@ -348,6 +330,46 @@ func getPortFromService(svc *v1.Service, backendSvcPort intstr.IntOrString) (int
348330
return port, nil
349331
}
350332

333+
func (t *Translator) translateApisixRouteHTTPBackend(tctx *provider.TranslateContext, ar *apiv2.ApisixRoute, backend apiv2.ApisixRouteHTTPBackend) (*adc.Upstream, error) {
334+
auNN := types.NamespacedName{
335+
Namespace: ar.Namespace,
336+
Name: backend.ServiceName,
337+
}
338+
upstream := adc.NewDefaultUpstream()
339+
if au, ok := tctx.Upstreams[auNN]; ok {
340+
svc := tctx.Services[auNN]
341+
if svc == nil {
342+
return nil, errors.Errorf("service not found, ApisixRoute: %s, Service: %s", utils.NamespacedName(ar).String(), auNN)
343+
}
344+
port, err := getPortFromService(svc, backend.ServicePort)
345+
if err != nil {
346+
return nil, err
347+
}
348+
u, err := t.translateApisixUpstreamForPort(tctx, au, ptr.To(port))
349+
if err != nil {
350+
return nil, err
351+
}
352+
upstream = u
353+
}
354+
var (
355+
err error
356+
nodes adc.UpstreamNodes
357+
)
358+
if backend.ResolveGranularity == apiv2.ResolveGranularityService {
359+
nodes, err = t.translateApisixRouteBackendResolveGranularityService(tctx, auNN, backend)
360+
} else {
361+
nodes, err = t.translateApisixRouteBackendResolveGranularityEndpoint(tctx, auNN, backend)
362+
}
363+
if err != nil {
364+
return nil, err
365+
}
366+
upstream.Nodes = nodes
367+
if backend.Weight != nil {
368+
upstream.Labels["meta_weight"] = strconv.FormatInt(int64(*backend.Weight), 10)
369+
}
370+
return upstream, nil
371+
}
372+
351373
func (t *Translator) translateApisixRouteBackendResolveGranularityService(tctx *provider.TranslateContext, arNN types.NamespacedName, backend apiv2.ApisixRouteHTTPBackend) (adc.UpstreamNodes, error) {
352374
serviceNN := types.NamespacedName{
353375
Namespace: arNN.Namespace,
@@ -431,19 +453,39 @@ func (t *Translator) translateStreamRule(tctx *provider.TranslateContext, ar *ap
431453
svc.ID = id.GenID(svc.Name)
432454
svc.StreamRoutes = append(svc.StreamRoutes, sr)
433455

434-
auNN := types.NamespacedName{Namespace: ar.GetNamespace(), Name: part.Backend.ServiceName}
435-
upstream := adc.NewDefaultUpstream()
436-
if au, ok := tctx.Upstreams[auNN]; ok {
437-
upstream, _ = t.translateApisixUpstream(tctx, au)
438-
}
439-
nodes, err := t.translateApisixRouteStreamBackendResolveGranularity(tctx, utils.NamespacedName(ar), part.Backend)
456+
upstream, err := t.translateApisixRouteStreamBackend(tctx, ar, part.Backend)
440457
if err != nil {
441458
return nil, err
442459
}
443-
upstream.Nodes = nodes
444460
upstream.ID = ""
445461
upstream.Name = ""
446462

447463
svc.Upstream = upstream
448464
return svc, nil
449465
}
466+
467+
func (t *Translator) translateApisixRouteStreamBackend(tctx *provider.TranslateContext, ar *apiv2.ApisixRoute, backend apiv2.ApisixRouteStreamBackend) (*adc.Upstream, error) {
468+
auNN := types.NamespacedName{Namespace: ar.GetNamespace(), Name: backend.ServiceName}
469+
upstream := adc.NewDefaultUpstream()
470+
if au, ok := tctx.Upstreams[auNN]; ok {
471+
service := tctx.Services[auNN]
472+
if service == nil {
473+
return nil, errors.Errorf("service not found, ApisixRoute: %s, Service: %s", utils.NamespacedName(ar), auNN)
474+
}
475+
port, err := getPortFromService(service, backend.ServicePort)
476+
if err != nil {
477+
return nil, err
478+
}
479+
u, err := t.translateApisixUpstreamForPort(tctx, au, ptr.To(port))
480+
if err != nil {
481+
return nil, err
482+
}
483+
upstream = u
484+
}
485+
nodes, err := t.translateApisixRouteStreamBackendResolveGranularity(tctx, utils.NamespacedName(ar), backend)
486+
if err != nil {
487+
return nil, err
488+
}
489+
upstream.Nodes = nodes
490+
return upstream, nil
491+
}

0 commit comments

Comments
 (0)