|
| 1 | +/* |
| 2 | +Copyright 2025 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 | + "fmt" |
| 21 | + "math" |
| 22 | + |
| 23 | + emitterir "github.com/kgateway-dev/ingress2gateway/pkg/i2gw/emitter_intermediate" |
| 24 | + agentgatewayv1alpha1 "github.com/kgateway-dev/kgateway/v2/api/v1alpha1/agentgateway" |
| 25 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 26 | +) |
| 27 | + |
| 28 | +// applyBufferPolicy projects ingress-nginx body-size annotations into |
| 29 | +// AgentgatewayPolicy.spec.frontend.http.maxBufferSize. |
| 30 | +// |
| 31 | +// Semantics: |
| 32 | +// - Prefer proxy-body-size when set. |
| 33 | +// - Otherwise fall back to client-body-buffer-size. |
| 34 | +func applyBufferPolicy( |
| 35 | + pol emitterir.Policy, |
| 36 | + ingressName, namespace string, |
| 37 | + ap map[string]*agentgatewayv1alpha1.AgentgatewayPolicy, |
| 38 | +) (bool, *field.Error) { |
| 39 | + if pol.ProxyBodySize == nil && pol.ClientBodyBufferSize == nil { |
| 40 | + return false, nil |
| 41 | + } |
| 42 | + |
| 43 | + size := pol.ProxyBodySize |
| 44 | + if size == nil { |
| 45 | + size = pol.ClientBodyBufferSize |
| 46 | + } |
| 47 | + if size == nil { |
| 48 | + return false, nil |
| 49 | + } |
| 50 | + |
| 51 | + sizeBytes := size.Value() |
| 52 | + if sizeBytes <= 0 { |
| 53 | + return false, field.Invalid( |
| 54 | + field.NewPath("emitter", "agentgateway", "AgentgatewayPolicy", "frontend", "http", "maxBufferSize"), |
| 55 | + size.String(), |
| 56 | + "resolved maxBufferSize must be greater than 0", |
| 57 | + ) |
| 58 | + } |
| 59 | + if sizeBytes > math.MaxInt32 { |
| 60 | + return false, field.Invalid( |
| 61 | + field.NewPath("emitter", "agentgateway", "AgentgatewayPolicy", "frontend", "http", "maxBufferSize"), |
| 62 | + size.String(), |
| 63 | + fmt.Sprintf("resolved maxBufferSize exceeds int32 limit (%d)", int64(math.MaxInt32)), |
| 64 | + ) |
| 65 | + } |
| 66 | + |
| 67 | + maxBufferSize := int32(sizeBytes) |
| 68 | + agp := ensureAgentgatewayPolicy(ap, ingressName, namespace) |
| 69 | + if agp.Spec.Frontend == nil { |
| 70 | + agp.Spec.Frontend = &agentgatewayv1alpha1.Frontend{} |
| 71 | + } |
| 72 | + if agp.Spec.Frontend.HTTP == nil { |
| 73 | + agp.Spec.Frontend.HTTP = &agentgatewayv1alpha1.FrontendHTTP{} |
| 74 | + } |
| 75 | + agp.Spec.Frontend.HTTP.MaxBufferSize = &maxBufferSize |
| 76 | + return true, nil |
| 77 | +} |
0 commit comments