|
| 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 | + "fmt" |
| 21 | + |
| 22 | + emitterir "github.com/kgateway-dev/ingress2gateway/pkg/i2gw/emitter_intermediate" |
| 23 | + agentgatewayv1alpha1 "github.com/kgateway-dev/kgateway/v2/api/v1alpha1/agentgateway" |
| 24 | + "github.com/kgateway-dev/kgateway/v2/api/v1alpha1/shared" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/apimachinery/pkg/types" |
| 27 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 28 | + gatewayv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 29 | +) |
| 30 | + |
| 31 | +// applyFrontendTLSPolicy projects frontend TLS listener settings into |
| 32 | +// AgentgatewayPolicy.spec.frontend.tls. |
| 33 | +// |
| 34 | +// Agentgateway validates frontend policies only when they target the Gateway |
| 35 | +// resource directly, so these settings are tracked separately from the |
| 36 | +// HTTPRoute-scoped policy map used by traffic features. |
| 37 | +func applyFrontendTLSPolicy( |
| 38 | + pol emitterir.Policy, |
| 39 | + ingressName string, |
| 40 | + httpRoute gatewayv1.HTTPRoute, |
| 41 | + defaultNamespace string, |
| 42 | + ap map[types.NamespacedName]*agentgatewayv1alpha1.AgentgatewayPolicy, |
| 43 | + sources map[types.NamespacedName]string, |
| 44 | +) (bool, *field.Error) { |
| 45 | + if pol.FrontendTLS == nil { |
| 46 | + return false, nil |
| 47 | + } |
| 48 | + if pol.FrontendTLS.HandshakeTimeout == nil && len(pol.FrontendTLS.ALPNProtocols) == 0 { |
| 49 | + return false, nil |
| 50 | + } |
| 51 | + |
| 52 | + gatewayKey, ok := gatewayParentKeyForHTTPRoute(httpRoute, defaultNamespace) |
| 53 | + if !ok { |
| 54 | + return false, field.Invalid( |
| 55 | + field.NewPath("emitter", "agentgateway", "AgentgatewayPolicy", "frontend", "tls"), |
| 56 | + ingressName, |
| 57 | + "frontend TLS policies require a parent Gateway target", |
| 58 | + ) |
| 59 | + } |
| 60 | + |
| 61 | + if existing, ok := ap[gatewayKey]; ok { |
| 62 | + if !frontendTLSPoliciesEqual(existing.Spec.Frontend.TLS, pol.FrontendTLS) { |
| 63 | + return false, field.Invalid( |
| 64 | + field.NewPath("emitter", "agentgateway", "AgentgatewayPolicy", "frontend", "tls"), |
| 65 | + ingressName, |
| 66 | + fmt.Sprintf( |
| 67 | + "frontend TLS settings conflict on Gateway %s/%s with source Ingress %q; agentgateway only supports Gateway-scoped frontend TLS policies", |
| 68 | + gatewayKey.Namespace, |
| 69 | + gatewayKey.Name, |
| 70 | + sources[gatewayKey], |
| 71 | + ), |
| 72 | + ) |
| 73 | + } |
| 74 | + return true, nil |
| 75 | + } |
| 76 | + |
| 77 | + alpn := make([]agentgatewayv1alpha1.TinyString, 0, len(pol.FrontendTLS.ALPNProtocols)) |
| 78 | + for _, value := range pol.FrontendTLS.ALPNProtocols { |
| 79 | + alpn = append(alpn, agentgatewayv1alpha1.TinyString(value)) |
| 80 | + } |
| 81 | + |
| 82 | + ap[gatewayKey] = &agentgatewayv1alpha1.AgentgatewayPolicy{ |
| 83 | + ObjectMeta: metav1.ObjectMeta{ |
| 84 | + Name: fmt.Sprintf("%s-frontend-tls", gatewayKey.Name), |
| 85 | + Namespace: gatewayKey.Namespace, |
| 86 | + }, |
| 87 | + Spec: agentgatewayv1alpha1.AgentgatewayPolicySpec{ |
| 88 | + Frontend: &agentgatewayv1alpha1.Frontend{ |
| 89 | + TLS: &agentgatewayv1alpha1.FrontendTLS{ |
| 90 | + HandshakeTimeout: pol.FrontendTLS.HandshakeTimeout, |
| 91 | + AlpnProtocols: &alpn, |
| 92 | + }, |
| 93 | + }, |
| 94 | + TargetRefs: []shared.LocalPolicyTargetReferenceWithSectionName{{ |
| 95 | + LocalPolicyTargetReference: shared.LocalPolicyTargetReference{ |
| 96 | + Group: gatewayv1.Group("gateway.networking.k8s.io"), |
| 97 | + Kind: gatewayv1.Kind("Gateway"), |
| 98 | + Name: gatewayv1.ObjectName(gatewayKey.Name), |
| 99 | + }, |
| 100 | + }}, |
| 101 | + }, |
| 102 | + } |
| 103 | + ap[gatewayKey].SetGroupVersionKind(AgentgatewayPolicyGVK) |
| 104 | + sources[gatewayKey] = ingressName |
| 105 | + |
| 106 | + return true, nil |
| 107 | +} |
| 108 | + |
| 109 | +func frontendTLSPoliciesEqual( |
| 110 | + existing *agentgatewayv1alpha1.FrontendTLS, |
| 111 | + desired *emitterir.FrontendTLSPolicy, |
| 112 | +) bool { |
| 113 | + if existing == nil || desired == nil { |
| 114 | + return existing == nil && desired == nil |
| 115 | + } |
| 116 | + |
| 117 | + switch { |
| 118 | + case existing.HandshakeTimeout == nil && desired.HandshakeTimeout != nil: |
| 119 | + return false |
| 120 | + case existing.HandshakeTimeout != nil && desired.HandshakeTimeout == nil: |
| 121 | + return false |
| 122 | + case existing.HandshakeTimeout != nil && desired.HandshakeTimeout != nil && |
| 123 | + existing.HandshakeTimeout.Duration != desired.HandshakeTimeout.Duration: |
| 124 | + return false |
| 125 | + } |
| 126 | + |
| 127 | + existingALPN := []agentgatewayv1alpha1.TinyString{} |
| 128 | + if existing.AlpnProtocols != nil { |
| 129 | + existingALPN = *existing.AlpnProtocols |
| 130 | + } |
| 131 | + if len(existingALPN) != len(desired.ALPNProtocols) { |
| 132 | + return false |
| 133 | + } |
| 134 | + for i, protocol := range desired.ALPNProtocols { |
| 135 | + if string(existingALPN[i]) != protocol { |
| 136 | + return false |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return true |
| 141 | +} |
0 commit comments