|
| 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 | + "fmt" |
| 21 | + "sort" |
| 22 | + |
| 23 | + "github.com/kgateway-dev/ingress2gateway/pkg/i2gw/intermediate" |
| 24 | + "github.com/kgateway-dev/kgateway/v2/api/v1alpha1/kgateway" |
| 25 | + gwv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 26 | +) |
| 27 | + |
| 28 | +// applyRewriteTargetPolicies projects ingress-nginx rewrite-target into *per-rule* Kgateway TrafficPolicies |
| 29 | +// and attaches them via ExtensionRef filters to the covered backendRefs. |
| 30 | +// |
| 31 | +// Why per-rule? |
| 32 | +// - The regex rewrite pattern must align with the rule's path regex so capture groups ($1, $2, ...) |
| 33 | +// behave like ingress-nginx. |
| 34 | +// |
| 35 | +// Assumptions: |
| 36 | +// - applyRegexPathMatchingForHost(...) has already run (if host-wide regex location mode is enabled), |
| 37 | +// so rule path matches will already be RegularExpression where needed. |
| 38 | +func applyRewriteTargetPolicies( |
| 39 | + pol intermediate.Policy, |
| 40 | + sourceIngressName, namespace string, |
| 41 | + httpRouteCtx *intermediate.HTTPRouteContext, |
| 42 | + tp map[string]*kgateway.TrafficPolicy, |
| 43 | +) { |
| 44 | + if pol.RewriteTarget == nil || *pol.RewriteTarget == "" { |
| 45 | + return |
| 46 | + } |
| 47 | + if httpRouteCtx == nil { |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + // Group covered backendRefs by rule index. |
| 52 | + // ruleIdx -> set(backendIdx) |
| 53 | + byRule := map[int]map[int]struct{}{} |
| 54 | + for _, idx := range pol.RuleBackendSources { |
| 55 | + if idx.Rule < 0 || idx.Backend < 0 { |
| 56 | + continue |
| 57 | + } |
| 58 | + if _, ok := byRule[idx.Rule]; !ok { |
| 59 | + byRule[idx.Rule] = map[int]struct{}{} |
| 60 | + } |
| 61 | + byRule[idx.Rule][idx.Backend] = struct{}{} |
| 62 | + } |
| 63 | + if len(byRule) == 0 { |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + // Deterministic iteration for stable goldens. |
| 68 | + ruleIdxs := make([]int, 0, len(byRule)) |
| 69 | + for r := range byRule { |
| 70 | + ruleIdxs = append(ruleIdxs, r) |
| 71 | + } |
| 72 | + sort.Ints(ruleIdxs) |
| 73 | + |
| 74 | + for _, ruleIdx := range ruleIdxs { |
| 75 | + if ruleIdx >= len(httpRouteCtx.Spec.Rules) { |
| 76 | + continue |
| 77 | + } |
| 78 | + |
| 79 | + pattern := deriveRulePathRegexPattern(httpRouteCtx.Spec.Rules[ruleIdx]) |
| 80 | + |
| 81 | + // Name is unique per ingress + rule, so we can safely create multiple TPs per ingress. |
| 82 | + tpName := fmt.Sprintf("%s-rewrite-%d", sourceIngressName, ruleIdx) |
| 83 | + t := ensureTrafficPolicy(tp, tpName, namespace) |
| 84 | + |
| 85 | + t.Spec.UrlRewrite = &kgateway.URLRewrite{ |
| 86 | + PathRegex: &kgateway.PathRegexRewrite{ |
| 87 | + Pattern: pattern, |
| 88 | + Substitution: *pol.RewriteTarget, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + // Attach this rewrite TP to every covered backendRef in the rule via ExtensionRef filter. |
| 93 | + backendSet := byRule[ruleIdx] |
| 94 | + backendIdxs := make([]int, 0, len(backendSet)) |
| 95 | + for b := range backendSet { |
| 96 | + backendIdxs = append(backendIdxs, b) |
| 97 | + } |
| 98 | + sort.Ints(backendIdxs) |
| 99 | + |
| 100 | + for _, backendIdx := range backendIdxs { |
| 101 | + if backendIdx >= len(httpRouteCtx.Spec.Rules[ruleIdx].BackendRefs) { |
| 102 | + continue |
| 103 | + } |
| 104 | + |
| 105 | + httpRouteCtx.Spec.Rules[ruleIdx].BackendRefs[backendIdx].Filters = |
| 106 | + append( |
| 107 | + httpRouteCtx.Spec.Rules[ruleIdx].BackendRefs[backendIdx].Filters, |
| 108 | + gwv1.HTTPRouteFilter{ |
| 109 | + Type: gwv1.HTTPRouteFilterExtensionRef, |
| 110 | + ExtensionRef: &gwv1.LocalObjectReference{ |
| 111 | + Group: gwv1.Group(TrafficPolicyGVK.Group), |
| 112 | + Kind: gwv1.Kind(TrafficPolicyGVK.Kind), |
| 113 | + Name: gwv1.ObjectName(t.Name), |
| 114 | + }, |
| 115 | + }, |
| 116 | + ) |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +// deriveRulePathRegexPattern returns a single regex pattern for the rule if possible. |
| 122 | +// If the rule has: |
| 123 | +// - exactly one distinct RegularExpression path value -> return it |
| 124 | +// - zero or multiple distinct regex values -> fall back to "^(.*)" |
| 125 | +// |
| 126 | +// Note: If a rule has multiple *different* path regex matches, we can't represent |
| 127 | +// match-specific rewrites without splitting the rule, so we choose a safe fallback. |
| 128 | +func deriveRulePathRegexPattern(rule gwv1.HTTPRouteRule) string { |
| 129 | + patterns := map[string]struct{}{} |
| 130 | + |
| 131 | + for i := range rule.Matches { |
| 132 | + m := rule.Matches[i] |
| 133 | + if m.Path == nil || m.Path.Type == nil || m.Path.Value == nil { |
| 134 | + continue |
| 135 | + } |
| 136 | + if *m.Path.Type != gwv1.PathMatchRegularExpression { |
| 137 | + continue |
| 138 | + } |
| 139 | + if v := *m.Path.Value; v != "" { |
| 140 | + patterns[v] = struct{}{} |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + if len(patterns) == 1 { |
| 145 | + for p := range patterns { |
| 146 | + return p |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return "^(.*)" |
| 151 | +} |
0 commit comments