|
| 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 | + |
| 22 | + "github.com/kgateway-dev/ingress2gateway/pkg/i2gw/intermediate" |
| 23 | + "github.com/kgateway-dev/ingress2gateway/pkg/i2gw/notifications" |
| 24 | + "k8s.io/apimachinery/pkg/types" |
| 25 | +) |
| 26 | + |
| 27 | +// backendProtoPatchKey de-dupes notifications across rules/routes/policies. |
| 28 | +type backendProtoPatchKey struct { |
| 29 | + Namespace string |
| 30 | + Service string |
| 31 | + Port int32 |
| 32 | + AppProtocol string |
| 33 | +} |
| 34 | + |
| 35 | +// emitBackendProtocolPatchNotifications emits an INFO notification with the correct kubectl patch |
| 36 | +// command to set ServicePort.appProtocol on an existing Service. |
| 37 | +// |
| 38 | +// IMPORTANT: |
| 39 | +// - We intentionally do NOT emit a Service object to avoid overwriting user-managed Service config. |
| 40 | +// - We also skip backends that have been rewritten to a kgateway Backend (service-upstream case), |
| 41 | +// because the generated Backend will carry appProtocol instead. |
| 42 | +func emitBackendProtocolPatchNotifications( |
| 43 | + pol intermediate.Policy, |
| 44 | + sourceIngressName string, |
| 45 | + httpRouteKey types.NamespacedName, |
| 46 | + httpCtx intermediate.HTTPRouteContext, |
| 47 | + seen map[backendProtoPatchKey]struct{}, |
| 48 | +) { |
| 49 | + if pol.BackendProtocol == nil { |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + // Map ingress-nginx backend-protocol → ServicePort.appProtocol |
| 54 | + var appProto string |
| 55 | + switch *pol.BackendProtocol { |
| 56 | + case intermediate.BackendProtocolGRPC: |
| 57 | + appProto = "grpc" |
| 58 | + default: |
| 59 | + // Nothing to do for unsupported/unknown mappings. |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + for _, idx := range pol.RuleBackendSources { |
| 64 | + if idx.Rule >= len(httpCtx.Spec.Rules) { |
| 65 | + continue |
| 66 | + } |
| 67 | + rule := httpCtx.Spec.Rules[idx.Rule] |
| 68 | + if idx.Backend >= len(rule.BackendRefs) { |
| 69 | + continue |
| 70 | + } |
| 71 | + |
| 72 | + br := rule.BackendRefs[idx.Backend] |
| 73 | + |
| 74 | + // If already rewritten to a kgateway Backend, skip; appProtocol will be applied there. |
| 75 | + if br.BackendRef.Group != nil && *br.BackendRef.Group != "" { |
| 76 | + continue |
| 77 | + } |
| 78 | + if br.BackendRef.Kind != nil && *br.BackendRef.Kind != "" && *br.BackendRef.Kind != "Service" { |
| 79 | + continue |
| 80 | + } |
| 81 | + if br.BackendRef.Name == "" || br.BackendRef.Port == nil { |
| 82 | + continue |
| 83 | + } |
| 84 | + |
| 85 | + svcName := string(br.BackendRef.Name) |
| 86 | + // If service-upstream is enabled for this backend, the emitter will generate a |
| 87 | + // kgateway Backend with appProtocol, so do not suggest patching the Service. |
| 88 | + if len(pol.Backends) > 0 { |
| 89 | + if _, ok := pol.Backends[backendKeyForService(httpRouteKey.Namespace, svcName)]; ok { |
| 90 | + continue |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + port := int32(*br.BackendRef.Port) |
| 95 | + |
| 96 | + key := backendProtoPatchKey{ |
| 97 | + Namespace: httpRouteKey.Namespace, |
| 98 | + Service: svcName, |
| 99 | + Port: port, |
| 100 | + AppProtocol: appProto, |
| 101 | + } |
| 102 | + if _, ok := seen[key]; ok { |
| 103 | + continue |
| 104 | + } |
| 105 | + seen[key] = struct{}{} |
| 106 | + |
| 107 | + // Use strategic merge patch (safe for core types) so only the matching port entry is updated. |
| 108 | + // This is far safer than emitting a Service manifest that users might blindly apply. |
| 109 | + patch := fmt.Sprintf(`{"spec":{"ports":[{"port":%d,"appProtocol":"%s"}]}}`, port, appProto) |
| 110 | + cmd := fmt.Sprintf( |
| 111 | + "kubectl patch service %s -n %s --type=strategic -p '%s'", |
| 112 | + svcName, |
| 113 | + httpRouteKey.Namespace, |
| 114 | + patch, |
| 115 | + ) |
| 116 | + |
| 117 | + msg := fmt.Sprintf( |
| 118 | + `Ingress %q uses nginx.ingress.kubernetes.io/backend-protocol=%q for Service %s/%s port %d. |
| 119 | +
|
| 120 | +To avoid overwriting existing Service configuration, ingress2gateway does not emit a Service for this annotation. |
| 121 | +Apply the equivalent behavior by patching your existing Service port's appProtocol: |
| 122 | +
|
| 123 | + %s`, |
| 124 | + sourceIngressName, |
| 125 | + *pol.BackendProtocol, |
| 126 | + httpRouteKey.Namespace, |
| 127 | + svcName, |
| 128 | + port, |
| 129 | + cmd, |
| 130 | + ) |
| 131 | + |
| 132 | + notifications.NotificationAggr.DispatchNotification( |
| 133 | + notifications.NewNotification(notifications.InfoNotification, msg), |
| 134 | + "ingress-nginx", |
| 135 | + ) |
| 136 | + } |
| 137 | +} |
0 commit comments