|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package translator |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/go-logr/logr" |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + k8stypes "k8s.io/apimachinery/pkg/types" |
| 29 | + gatewayv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" |
| 30 | + |
| 31 | + "github.com/apache/apisix-ingress-controller/api/v1alpha1" |
| 32 | + "github.com/apache/apisix-ingress-controller/internal/provider" |
| 33 | +) |
| 34 | + |
| 35 | +func TestTranslateTCPRouteWithL4RoutePolicy(t *testing.T) { |
| 36 | + tests := []struct { |
| 37 | + name string |
| 38 | + policy *v1alpha1.L4RoutePolicy |
| 39 | + wantPlugins []string |
| 40 | + wantNoPlugins bool |
| 41 | + }{ |
| 42 | + { |
| 43 | + name: "attaches plugins from matching L4RoutePolicy", |
| 44 | + policy: makeL4RoutePolicy("default", "tcp-policy", "TCPRoute", "my-tcp", []v1alpha1.Plugin{ |
| 45 | + {Name: "limit-conn", Config: mustJSON(map[string]any{"conn": 100})}, |
| 46 | + {Name: "ip-restriction", Config: mustJSON(map[string]any{"whitelist": []string{"10.0.0.0/8"}})}, |
| 47 | + }), |
| 48 | + wantPlugins: []string{"limit-conn", "ip-restriction"}, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "does not attach plugins from policy targeting different route kind", |
| 52 | + policy: makeL4RoutePolicy("default", "udp-policy", "UDPRoute", "my-tcp", []v1alpha1.Plugin{ |
| 53 | + {Name: "limit-conn", Config: mustJSON(map[string]any{"conn": 100})}, |
| 54 | + }), |
| 55 | + wantNoPlugins: true, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "does not attach plugins from policy targeting different route name", |
| 59 | + policy: makeL4RoutePolicy("default", "tcp-policy", "TCPRoute", "other-tcp", []v1alpha1.Plugin{ |
| 60 | + {Name: "limit-conn", Config: mustJSON(map[string]any{"conn": 100})}, |
| 61 | + }), |
| 62 | + wantNoPlugins: true, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "succeeds with no policy in context", |
| 66 | + policy: nil, |
| 67 | + wantNoPlugins: true, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + for _, tt := range tests { |
| 72 | + t.Run(tt.name, func(t *testing.T) { |
| 73 | + translator := NewTranslator(logr.Discard()) |
| 74 | + tctx := provider.NewDefaultTranslateContext(context.Background()) |
| 75 | + |
| 76 | + if tt.policy != nil { |
| 77 | + key := k8stypes.NamespacedName{Namespace: tt.policy.Namespace, Name: tt.policy.Name} |
| 78 | + tctx.L4RoutePolicies[key] = tt.policy |
| 79 | + } |
| 80 | + |
| 81 | + route := &gatewayv1alpha2.TCPRoute{ |
| 82 | + ObjectMeta: metav1.ObjectMeta{ |
| 83 | + Name: "my-tcp", |
| 84 | + Namespace: "default", |
| 85 | + }, |
| 86 | + Spec: gatewayv1alpha2.TCPRouteSpec{ |
| 87 | + Rules: []gatewayv1alpha2.TCPRouteRule{ |
| 88 | + {BackendRefs: []gatewayv1alpha2.BackendRef{}}, |
| 89 | + }, |
| 90 | + }, |
| 91 | + } |
| 92 | + |
| 93 | + result, err := translator.TranslateTCPRoute(tctx, route) |
| 94 | + require.NoError(t, err) |
| 95 | + require.Len(t, result.Services, 1) |
| 96 | + |
| 97 | + plugins := result.Services[0].Plugins |
| 98 | + if tt.wantNoPlugins { |
| 99 | + assert.Empty(t, plugins) |
| 100 | + } else { |
| 101 | + for _, name := range tt.wantPlugins { |
| 102 | + assert.Contains(t, plugins, name, "expected plugin %q to be attached", name) |
| 103 | + } |
| 104 | + } |
| 105 | + }) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestTranslateUDPRouteWithL4RoutePolicy(t *testing.T) { |
| 110 | + tests := []struct { |
| 111 | + name string |
| 112 | + policy *v1alpha1.L4RoutePolicy |
| 113 | + wantPlugins []string |
| 114 | + wantNoPlugins bool |
| 115 | + }{ |
| 116 | + { |
| 117 | + name: "attaches plugins from matching L4RoutePolicy", |
| 118 | + policy: makeL4RoutePolicy("default", "udp-policy", "UDPRoute", "my-udp", []v1alpha1.Plugin{ |
| 119 | + {Name: "limit-conn", Config: mustJSON(map[string]any{"conn": 50})}, |
| 120 | + }), |
| 121 | + wantPlugins: []string{"limit-conn"}, |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "does not attach plugins from policy targeting TCPRoute", |
| 125 | + policy: makeL4RoutePolicy("default", "tcp-policy", "TCPRoute", "my-udp", []v1alpha1.Plugin{ |
| 126 | + {Name: "limit-conn", Config: mustJSON(map[string]any{"conn": 50})}, |
| 127 | + }), |
| 128 | + wantNoPlugins: true, |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "succeeds with no policy in context", |
| 132 | + policy: nil, |
| 133 | + wantNoPlugins: true, |
| 134 | + }, |
| 135 | + } |
| 136 | + |
| 137 | + for _, tt := range tests { |
| 138 | + t.Run(tt.name, func(t *testing.T) { |
| 139 | + translator := NewTranslator(logr.Discard()) |
| 140 | + tctx := provider.NewDefaultTranslateContext(context.Background()) |
| 141 | + |
| 142 | + if tt.policy != nil { |
| 143 | + key := k8stypes.NamespacedName{Namespace: tt.policy.Namespace, Name: tt.policy.Name} |
| 144 | + tctx.L4RoutePolicies[key] = tt.policy |
| 145 | + } |
| 146 | + |
| 147 | + route := &gatewayv1alpha2.UDPRoute{ |
| 148 | + ObjectMeta: metav1.ObjectMeta{ |
| 149 | + Name: "my-udp", |
| 150 | + Namespace: "default", |
| 151 | + }, |
| 152 | + Spec: gatewayv1alpha2.UDPRouteSpec{ |
| 153 | + Rules: []gatewayv1alpha2.UDPRouteRule{ |
| 154 | + {BackendRefs: []gatewayv1alpha2.BackendRef{}}, |
| 155 | + }, |
| 156 | + }, |
| 157 | + } |
| 158 | + |
| 159 | + result, err := translator.TranslateUDPRoute(tctx, route) |
| 160 | + require.NoError(t, err) |
| 161 | + require.Len(t, result.Services, 1) |
| 162 | + |
| 163 | + plugins := result.Services[0].Plugins |
| 164 | + if tt.wantNoPlugins { |
| 165 | + assert.Empty(t, plugins) |
| 166 | + } else { |
| 167 | + for _, name := range tt.wantPlugins { |
| 168 | + assert.Contains(t, plugins, name, "expected plugin %q to be attached", name) |
| 169 | + } |
| 170 | + } |
| 171 | + }) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +func TestTranslateTLSRouteWithL4RoutePolicy(t *testing.T) { |
| 176 | + tests := []struct { |
| 177 | + name string |
| 178 | + policy *v1alpha1.L4RoutePolicy |
| 179 | + hostnames []string |
| 180 | + wantPlugins []string |
| 181 | + wantNoPlugins bool |
| 182 | + }{ |
| 183 | + { |
| 184 | + name: "attaches plugins from matching L4RoutePolicy", |
| 185 | + policy: makeL4RoutePolicy("default", "tls-policy", "TLSRoute", "my-tls", []v1alpha1.Plugin{ |
| 186 | + {Name: "ip-restriction", Config: mustJSON(map[string]any{"whitelist": []string{"192.168.0.0/16"}})}, |
| 187 | + }), |
| 188 | + hostnames: []string{"example.com"}, |
| 189 | + wantPlugins: []string{"ip-restriction"}, |
| 190 | + }, |
| 191 | + { |
| 192 | + name: "plugins attached once per rule even with multiple SNI hostnames", |
| 193 | + policy: makeL4RoutePolicy("default", "tls-policy", "TLSRoute", "my-tls", []v1alpha1.Plugin{ |
| 194 | + {Name: "limit-conn", Config: mustJSON(map[string]any{"conn": 20})}, |
| 195 | + }), |
| 196 | + hostnames: []string{"foo.example.com", "bar.example.com"}, |
| 197 | + wantPlugins: []string{"limit-conn"}, |
| 198 | + }, |
| 199 | + { |
| 200 | + name: "does not attach plugins from policy targeting TCPRoute", |
| 201 | + policy: makeL4RoutePolicy("default", "tcp-policy", "TCPRoute", "my-tls", []v1alpha1.Plugin{ |
| 202 | + {Name: "limit-conn", Config: mustJSON(map[string]any{"conn": 20})}, |
| 203 | + }), |
| 204 | + hostnames: []string{"example.com"}, |
| 205 | + wantNoPlugins: true, |
| 206 | + }, |
| 207 | + { |
| 208 | + name: "succeeds with no policy in context", |
| 209 | + policy: nil, |
| 210 | + hostnames: []string{"example.com"}, |
| 211 | + wantNoPlugins: true, |
| 212 | + }, |
| 213 | + } |
| 214 | + |
| 215 | + for _, tt := range tests { |
| 216 | + t.Run(tt.name, func(t *testing.T) { |
| 217 | + translator := NewTranslator(logr.Discard()) |
| 218 | + tctx := provider.NewDefaultTranslateContext(context.Background()) |
| 219 | + |
| 220 | + if tt.policy != nil { |
| 221 | + key := k8stypes.NamespacedName{Namespace: tt.policy.Namespace, Name: tt.policy.Name} |
| 222 | + tctx.L4RoutePolicies[key] = tt.policy |
| 223 | + } |
| 224 | + |
| 225 | + hostnames := make([]gatewayv1alpha2.Hostname, 0, len(tt.hostnames)) |
| 226 | + for _, h := range tt.hostnames { |
| 227 | + hostnames = append(hostnames, gatewayv1alpha2.Hostname(h)) |
| 228 | + } |
| 229 | + |
| 230 | + route := &gatewayv1alpha2.TLSRoute{ |
| 231 | + ObjectMeta: metav1.ObjectMeta{ |
| 232 | + Name: "my-tls", |
| 233 | + Namespace: "default", |
| 234 | + }, |
| 235 | + Spec: gatewayv1alpha2.TLSRouteSpec{ |
| 236 | + Hostnames: hostnames, |
| 237 | + Rules: []gatewayv1alpha2.TLSRouteRule{ |
| 238 | + {BackendRefs: []gatewayv1alpha2.BackendRef{}}, |
| 239 | + }, |
| 240 | + }, |
| 241 | + } |
| 242 | + |
| 243 | + result, err := translator.TranslateTLSRoute(tctx, route) |
| 244 | + require.NoError(t, err) |
| 245 | + require.Len(t, result.Services, 1) |
| 246 | + |
| 247 | + // Verify stream routes are created per SNI hostname |
| 248 | + if len(tt.hostnames) > 0 { |
| 249 | + assert.Len(t, result.Services[0].StreamRoutes, len(tt.hostnames)) |
| 250 | + } |
| 251 | + |
| 252 | + plugins := result.Services[0].Plugins |
| 253 | + if tt.wantNoPlugins { |
| 254 | + assert.Empty(t, plugins) |
| 255 | + } else { |
| 256 | + for _, name := range tt.wantPlugins { |
| 257 | + assert.Contains(t, plugins, name, "expected plugin %q to be attached", name) |
| 258 | + } |
| 259 | + // Plugins are on the service, not duplicated per stream route |
| 260 | + assert.Len(t, result.Services, 1, "plugins should be on service level, not duplicated") |
| 261 | + } |
| 262 | + }) |
| 263 | + } |
| 264 | +} |
0 commit comments