|
| 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 | + "strings" |
| 21 | + |
| 22 | + "github.com/kgateway-dev/ingress2gateway/pkg/i2gw/intermediate" |
| 23 | + "github.com/kgateway-dev/kgateway/v2/api/v1alpha1/kgateway" |
| 24 | + "github.com/kgateway-dev/kgateway/v2/api/v1alpha1/shared" |
| 25 | + corev1 "k8s.io/api/core/v1" |
| 26 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 27 | + "k8s.io/apimachinery/pkg/types" |
| 28 | + "k8s.io/utils/ptr" |
| 29 | + gwv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 30 | +) |
| 31 | + |
| 32 | +// applyBackendTLSPolicy projects the BackendTLS IR policy into one or more |
| 33 | +// Kgateway BackendConfigPolicies. |
| 34 | +// |
| 35 | +// Semantics: |
| 36 | +// - We create at most one BackendConfigPolicy per Service. |
| 37 | +// - That policy's Spec.TLS is configured with client certificates, CA certificates, |
| 38 | +// SNI hostname, and verification settings from the Policy.BackendTLS. |
| 39 | +// - TargetRefs are populated with all core Service backends that this Policy covers |
| 40 | +// (based on RuleBackendSources). |
| 41 | +func applyBackendTLSPolicy( |
| 42 | + pol intermediate.Policy, |
| 43 | + httpRouteKey types.NamespacedName, |
| 44 | + httpRouteCtx intermediate.HTTPRouteContext, |
| 45 | + backendCfg map[types.NamespacedName]*kgateway.BackendConfigPolicy, |
| 46 | +) bool { |
| 47 | + if pol.BackendTLS == nil { |
| 48 | + return false |
| 49 | + } |
| 50 | + |
| 51 | + backendTLS := pol.BackendTLS |
| 52 | + |
| 53 | + // Parse secret name (format: "namespace/secretName" or just "secretName") |
| 54 | + // Note: LocalObjectReference doesn't support namespace, so the secret |
| 55 | + // must be in the same namespace as the BackendConfigPolicy |
| 56 | + secretName := backendTLS.SecretName |
| 57 | + if parts := strings.SplitN(backendTLS.SecretName, "/", 2); len(parts) == 2 { |
| 58 | + // If namespace is specified but different from policy namespace, use just the secret name |
| 59 | + // and assume it's in the same namespace as the policy |
| 60 | + secretName = parts[1] |
| 61 | + } |
| 62 | + |
| 63 | + for _, idx := range pol.RuleBackendSources { |
| 64 | + if idx.Rule >= len(httpRouteCtx.Spec.Rules) { |
| 65 | + continue |
| 66 | + } |
| 67 | + rule := httpRouteCtx.Spec.Rules[idx.Rule] |
| 68 | + if idx.Backend >= len(rule.BackendRefs) { |
| 69 | + continue |
| 70 | + } |
| 71 | + |
| 72 | + br := rule.BackendRefs[idx.Backend] |
| 73 | + |
| 74 | + if br.BackendRef.Group != nil && *br.BackendRef.Group != "" { |
| 75 | + continue |
| 76 | + } |
| 77 | + if br.BackendRef.Kind != nil && *br.BackendRef.Kind != "Service" { |
| 78 | + continue |
| 79 | + } |
| 80 | + |
| 81 | + svcName := string(br.BackendRef.Name) |
| 82 | + if svcName == "" { |
| 83 | + continue |
| 84 | + } |
| 85 | + |
| 86 | + svcKey := types.NamespacedName{ |
| 87 | + Namespace: httpRouteKey.Namespace, |
| 88 | + Name: svcName, |
| 89 | + } |
| 90 | + |
| 91 | + // Create or reuse BackendConfigPolicy per Service |
| 92 | + bcp, exists := backendCfg[svcKey] |
| 93 | + if !exists { |
| 94 | + // Use a generic name that works for all backend config features |
| 95 | + policyName := svcName + "-backend-config" |
| 96 | + bcp = &kgateway.BackendConfigPolicy{ |
| 97 | + ObjectMeta: metav1.ObjectMeta{ |
| 98 | + Name: policyName, |
| 99 | + Namespace: httpRouteKey.Namespace, |
| 100 | + }, |
| 101 | + Spec: kgateway.BackendConfigPolicySpec{ |
| 102 | + TargetRefs: []shared.LocalPolicyTargetReference{ |
| 103 | + { |
| 104 | + Group: "", |
| 105 | + Kind: "Service", |
| 106 | + Name: gwv1.ObjectName(svcName), |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + } |
| 111 | + bcp.SetGroupVersionKind(BackendConfigPolicyGVK) |
| 112 | + backendCfg[svcKey] = bcp |
| 113 | + } |
| 114 | + |
| 115 | + // Configure TLS settings |
| 116 | + if bcp.Spec.TLS == nil { |
| 117 | + bcp.Spec.TLS = &kgateway.TLS{} |
| 118 | + } |
| 119 | + |
| 120 | + // Set SNI hostname if specified |
| 121 | + if backendTLS.Hostname != "" { |
| 122 | + bcp.Spec.TLS.Sni = ptr.To(backendTLS.Hostname) |
| 123 | + } |
| 124 | + |
| 125 | + // Set verification: InsecureSkipVerify is false when verify is on, true when verify is off |
| 126 | + if !backendTLS.Verify { |
| 127 | + bcp.Spec.TLS.InsecureSkipVerify = ptr.To(true) |
| 128 | + } else if secretName != "" { |
| 129 | + // Set secret reference (contains tls.crt, tls.key, and optionally ca.crt) |
| 130 | + bcp.Spec.TLS.SecretRef = &corev1.LocalObjectReference{ |
| 131 | + Name: secretName, |
| 132 | + } |
| 133 | + // Note: LocalObjectReference doesn't support namespace, so the secret |
| 134 | + // must be in the same namespace as the BackendConfigPolicy |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + return true |
| 139 | +} |
0 commit comments