|
| 1 | +/* |
| 2 | +Copyright 2026 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package agentgateway |
| 18 | + |
| 19 | +import ( |
| 20 | + emitterir "github.com/kgateway-dev/ingress2gateway/pkg/i2gw/emitter_intermediate" |
| 21 | + |
| 22 | + agentgatewayv1alpha1 "github.com/kgateway-dev/kgateway/v2/api/v1alpha1/agentgateway" |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + "k8s.io/apimachinery/pkg/types" |
| 25 | + gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 26 | +) |
| 27 | + |
| 28 | +const sourceIngressAnnotation = "ingress2gateway.kubernetes.io/source-ingress" |
| 29 | + |
| 30 | +// applyServiceUpstream projects provider service-upstream backend metadata into |
| 31 | +// AgentgatewayBackend CRs and rewrites HTTPRoute backendRefs to target them. |
| 32 | +func applyServiceUpstream( |
| 33 | + pol emitterir.Policy, |
| 34 | + ingressName string, |
| 35 | + httpRouteKey types.NamespacedName, |
| 36 | + httpRouteCtx *emitterir.HTTPRouteContext, |
| 37 | + backends map[types.NamespacedName]*agentgatewayv1alpha1.AgentgatewayBackend, |
| 38 | +) { |
| 39 | + if len(pol.Backends) == 0 || len(pol.RuleBackendSources) == 0 { |
| 40 | + return |
| 41 | + } |
| 42 | + |
| 43 | + for _, idx := range pol.RuleBackendSources { |
| 44 | + if idx.Rule >= len(httpRouteCtx.Spec.Rules) { |
| 45 | + continue |
| 46 | + } |
| 47 | + rule := &httpRouteCtx.Spec.Rules[idx.Rule] |
| 48 | + if idx.Backend >= len(rule.BackendRefs) { |
| 49 | + continue |
| 50 | + } |
| 51 | + |
| 52 | + br := &rule.BackendRefs[idx.Backend] |
| 53 | + |
| 54 | + // Only core Services are eligible for service-upstream rewriting. |
| 55 | + if br.BackendRef.Group != nil && *br.BackendRef.Group != "" { |
| 56 | + continue |
| 57 | + } |
| 58 | + if br.BackendRef.Kind != nil && *br.BackendRef.Kind != "Service" { |
| 59 | + continue |
| 60 | + } |
| 61 | + if br.BackendRef.Name == "" { |
| 62 | + continue |
| 63 | + } |
| 64 | + |
| 65 | + svcName := string(br.BackendRef.Name) |
| 66 | + backendKey := serviceUpstreamBackendKey(httpRouteKey.Namespace, svcName) |
| 67 | + |
| 68 | + be, ok := pol.Backends[backendKey] |
| 69 | + if !ok { |
| 70 | + continue |
| 71 | + } |
| 72 | + |
| 73 | + agb := ensureServiceUpstreamBackend( |
| 74 | + ingressName, |
| 75 | + backendKey, |
| 76 | + be.Host, |
| 77 | + be.Port, |
| 78 | + backends, |
| 79 | + ) |
| 80 | + |
| 81 | + // Rewrite HTTPRoute backendRef to point at the AgentgatewayBackend. |
| 82 | + group := gatewayv1.Group(AgentgatewayBackendGVK.Group) |
| 83 | + kind := gatewayv1.Kind(AgentgatewayBackendGVK.Kind) |
| 84 | + |
| 85 | + br.BackendRef.Group = &group |
| 86 | + br.BackendRef.Kind = &kind |
| 87 | + br.BackendRef.Name = gatewayv1.ObjectName(agb.Name) |
| 88 | + // Port is defined by AgentgatewayBackend.spec.static. |
| 89 | + br.BackendRef.Port = nil |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func serviceUpstreamBackendKey(ns, svcName string) types.NamespacedName { |
| 94 | + return types.NamespacedName{ |
| 95 | + Namespace: ns, |
| 96 | + Name: svcName + "-service-upstream", |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func ensureServiceUpstreamBackend( |
| 101 | + ingressName string, |
| 102 | + backendKey types.NamespacedName, |
| 103 | + host string, |
| 104 | + port int32, |
| 105 | + backends map[types.NamespacedName]*agentgatewayv1alpha1.AgentgatewayBackend, |
| 106 | +) *agentgatewayv1alpha1.AgentgatewayBackend { |
| 107 | + if be, ok := backends[backendKey]; ok { |
| 108 | + return be |
| 109 | + } |
| 110 | + |
| 111 | + be := &agentgatewayv1alpha1.AgentgatewayBackend{ |
| 112 | + TypeMeta: metav1.TypeMeta{ |
| 113 | + Kind: AgentgatewayBackendGVK.Kind, |
| 114 | + APIVersion: AgentgatewayBackendGVK.GroupVersion().String(), |
| 115 | + }, |
| 116 | + ObjectMeta: metav1.ObjectMeta{ |
| 117 | + Name: backendKey.Name, |
| 118 | + Namespace: backendKey.Namespace, |
| 119 | + Labels: map[string]string{ |
| 120 | + sourceIngressAnnotation: ingressName, |
| 121 | + }, |
| 122 | + }, |
| 123 | + Spec: agentgatewayv1alpha1.AgentgatewayBackendSpec{ |
| 124 | + Static: &agentgatewayv1alpha1.StaticBackend{ |
| 125 | + Host: agentgatewayv1alpha1.ShortString(host), |
| 126 | + Port: port, |
| 127 | + }, |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + backends[backendKey] = be |
| 132 | + return be |
| 133 | +} |
0 commit comments