|
| 1 | +/* |
| 2 | +Copyright 2023 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 kgateway |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/kgateway-dev/ingress2gateway/pkg/i2gw/intermediate" |
| 21 | + kgw "github.com/kgateway-dev/kgateway/v2/api/v1alpha1/kgateway" |
| 22 | + |
| 23 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 24 | + "k8s.io/apimachinery/pkg/types" |
| 25 | + gwv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 26 | +) |
| 27 | + |
| 28 | +// applyServiceUpstreamBackend projects provider-specific static backend mappings |
| 29 | +// into typed Kgateway Backend CRs and rewrites HTTPRoute backendRefs to reference |
| 30 | +// those Backends. |
| 31 | +// |
| 32 | +// Semantics: |
| 33 | +// - One Backend CR per (namespace, svcName-service-upstream) |
| 34 | +// - Backend.Spec.Static.Hosts contains a single host+port |
| 35 | +// - HTTPRoute backendRefs for those services are rewritten to: |
| 36 | +// group: gateway.kgateway.dev |
| 37 | +// kind: Backend |
| 38 | +// name: <svc>-service-upstream |
| 39 | +// |
| 40 | +// Returns true if it mutated the HTTPRouteContext or produced a Backend CR. |
| 41 | +func applyServiceUpstreamBackend( |
| 42 | + pol intermediate.Policy, |
| 43 | + ingressName string, |
| 44 | + httpRouteKey types.NamespacedName, |
| 45 | + httpRouteCtx *intermediate.HTTPRouteContext, |
| 46 | + backends map[types.NamespacedName]*kgw.Backend, |
| 47 | +) { |
| 48 | + if len(pol.Backends) == 0 || len(pol.RuleBackendSources) == 0 { |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + for _, idx := range pol.RuleBackendSources { |
| 53 | + // Validate indices |
| 54 | + if idx.Rule >= len(httpRouteCtx.Spec.Rules) { |
| 55 | + continue |
| 56 | + } |
| 57 | + rule := &httpRouteCtx.Spec.Rules[idx.Rule] |
| 58 | + if idx.Backend >= len(rule.BackendRefs) { |
| 59 | + continue |
| 60 | + } |
| 61 | + |
| 62 | + br := &rule.BackendRefs[idx.Backend] |
| 63 | + |
| 64 | + // Only core Services |
| 65 | + if br.BackendRef.Group != nil && *br.BackendRef.Group != "" { |
| 66 | + continue |
| 67 | + } |
| 68 | + if br.BackendRef.Kind != nil && *br.BackendRef.Kind != "Service" { |
| 69 | + continue |
| 70 | + } |
| 71 | + if br.BackendRef.Name == "" { |
| 72 | + continue |
| 73 | + } |
| 74 | + |
| 75 | + svcName := string(br.BackendRef.Name) |
| 76 | + |
| 77 | + backendName := svcName + "-service-upstream" |
| 78 | + backendKey := types.NamespacedName{ |
| 79 | + Namespace: httpRouteKey.Namespace, |
| 80 | + Name: backendName, |
| 81 | + } |
| 82 | + |
| 83 | + // Find provider-produced backend metadata |
| 84 | + be, ok := pol.Backends[backendKey] |
| 85 | + if !ok { |
| 86 | + continue |
| 87 | + } |
| 88 | + |
| 89 | + // Create or reuse typed Backend CR |
| 90 | + kb, exists := backends[backendKey] |
| 91 | + if !exists { |
| 92 | + kb = &kgw.Backend{ |
| 93 | + TypeMeta: metav1.TypeMeta{ |
| 94 | + Kind: BackendGVK.Kind, |
| 95 | + APIVersion: BackendGVK.GroupVersion().String(), |
| 96 | + }, |
| 97 | + ObjectMeta: metav1.ObjectMeta{ |
| 98 | + Name: backendKey.Name, |
| 99 | + Namespace: backendKey.Namespace, |
| 100 | + Labels: map[string]string{ |
| 101 | + "ingress2gateway.kubernetes.io/source-ingress": ingressName, |
| 102 | + }, |
| 103 | + }, |
| 104 | + Spec: kgw.BackendSpec{ |
| 105 | + Type: kgw.BackendTypeStatic, |
| 106 | + Static: &kgw.StaticBackend{ |
| 107 | + Hosts: []kgw.Host{ |
| 108 | + { |
| 109 | + Host: be.Host, |
| 110 | + Port: gwv1.PortNumber(be.Port), |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + } |
| 116 | + backends[backendKey] = kb |
| 117 | + } |
| 118 | + |
| 119 | + // Rewrite BackendRef to point to this Backend |
| 120 | + group := gwv1.Group(BackendGVK.Group) |
| 121 | + kind := gwv1.Kind(BackendGVK.Kind) |
| 122 | + |
| 123 | + br.BackendRef.Group = &group |
| 124 | + br.BackendRef.Kind = &kind |
| 125 | + br.BackendRef.Name = gwv1.ObjectName(backendKey.Name) |
| 126 | + br.BackendRef.Port = nil // backend controls port |
| 127 | + } |
| 128 | +} |
0 commit comments