|
| 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 | + "encoding/json" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/go-logr/logr" |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" |
| 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 | + adctypes "github.com/apache/apisix-ingress-controller/api/adc" |
| 32 | + "github.com/apache/apisix-ingress-controller/api/v1alpha1" |
| 33 | +) |
| 34 | + |
| 35 | +func makeL4RoutePolicy(namespace, name, targetKind, targetName string, plugins []v1alpha1.Plugin) *v1alpha1.L4RoutePolicy { |
| 36 | + return &v1alpha1.L4RoutePolicy{ |
| 37 | + ObjectMeta: metav1.ObjectMeta{ |
| 38 | + Namespace: namespace, |
| 39 | + Name: name, |
| 40 | + }, |
| 41 | + Spec: v1alpha1.L4RoutePolicySpec{ |
| 42 | + TargetRefs: []gatewayv1alpha2.LocalPolicyTargetReferenceWithSectionName{ |
| 43 | + { |
| 44 | + LocalPolicyTargetReference: gatewayv1alpha2.LocalPolicyTargetReference{ |
| 45 | + Group: gatewayv1alpha2.GroupName, |
| 46 | + Kind: gatewayv1alpha2.Kind(targetKind), |
| 47 | + Name: gatewayv1alpha2.ObjectName(targetName), |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + Plugins: plugins, |
| 52 | + }, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func mustJSON(v any) apiextensionsv1.JSON { |
| 57 | + b, err := json.Marshal(v) |
| 58 | + if err != nil { |
| 59 | + panic(err) |
| 60 | + } |
| 61 | + return apiextensionsv1.JSON{Raw: b} |
| 62 | +} |
| 63 | + |
| 64 | +func TestAttachL4RoutePolicyPlugins_AttachesMatchingPolicy(t *testing.T) { |
| 65 | + tr := NewTranslator(logr.Discard()) |
| 66 | + |
| 67 | + policy := makeL4RoutePolicy("default", "my-policy", "TCPRoute", "my-tcp-route", []v1alpha1.Plugin{ |
| 68 | + {Name: "limit-conn", Config: mustJSON(map[string]any{"conn": 100, "burst": 50})}, |
| 69 | + {Name: "ip-restriction", Config: mustJSON(map[string]any{"whitelist": []string{"10.0.0.0/8"}})}, |
| 70 | + }) |
| 71 | + |
| 72 | + policies := map[k8stypes.NamespacedName]*v1alpha1.L4RoutePolicy{ |
| 73 | + {Namespace: "default", Name: "my-policy"}: policy, |
| 74 | + } |
| 75 | + |
| 76 | + plugins := adctypes.Plugins{} |
| 77 | + tr.AttachL4RoutePolicyPlugins(policies, "default", "my-tcp-route", "TCPRoute", plugins) |
| 78 | + |
| 79 | + assert.Len(t, plugins, 2) |
| 80 | + assert.Contains(t, plugins, "limit-conn") |
| 81 | + assert.Contains(t, plugins, "ip-restriction") |
| 82 | + |
| 83 | + cfg := plugins["limit-conn"].(map[string]any) |
| 84 | + assert.EqualValues(t, 100, cfg["conn"]) |
| 85 | +} |
| 86 | + |
| 87 | +func TestAttachL4RoutePolicyPlugins_NoMatchOnKind(t *testing.T) { |
| 88 | + tr := NewTranslator(logr.Discard()) |
| 89 | + |
| 90 | + policy := makeL4RoutePolicy("default", "udp-policy", "UDPRoute", "my-udp-route", []v1alpha1.Plugin{ |
| 91 | + {Name: "limit-conn", Config: mustJSON(map[string]any{"conn": 10})}, |
| 92 | + }) |
| 93 | + |
| 94 | + policies := map[k8stypes.NamespacedName]*v1alpha1.L4RoutePolicy{ |
| 95 | + {Namespace: "default", Name: "udp-policy"}: policy, |
| 96 | + } |
| 97 | + |
| 98 | + plugins := adctypes.Plugins{} |
| 99 | + // Looking for TCPRoute, but policy targets UDPRoute — should not match. |
| 100 | + tr.AttachL4RoutePolicyPlugins(policies, "default", "my-udp-route", "TCPRoute", plugins) |
| 101 | + |
| 102 | + assert.Empty(t, plugins) |
| 103 | +} |
| 104 | + |
| 105 | +func TestAttachL4RoutePolicyPlugins_NoMatchOnNamespace(t *testing.T) { |
| 106 | + tr := NewTranslator(logr.Discard()) |
| 107 | + |
| 108 | + policy := makeL4RoutePolicy("other-ns", "my-policy", "TCPRoute", "my-tcp-route", []v1alpha1.Plugin{ |
| 109 | + {Name: "limit-conn", Config: mustJSON(map[string]any{"conn": 10})}, |
| 110 | + }) |
| 111 | + |
| 112 | + policies := map[k8stypes.NamespacedName]*v1alpha1.L4RoutePolicy{ |
| 113 | + {Namespace: "other-ns", Name: "my-policy"}: policy, |
| 114 | + } |
| 115 | + |
| 116 | + plugins := adctypes.Plugins{} |
| 117 | + // Route is in "default" namespace, policy is in "other-ns" — should not match. |
| 118 | + tr.AttachL4RoutePolicyPlugins(policies, "default", "my-tcp-route", "TCPRoute", plugins) |
| 119 | + |
| 120 | + assert.Empty(t, plugins) |
| 121 | +} |
| 122 | + |
| 123 | +func TestAttachL4RoutePolicyPlugins_EmptyPlugins(t *testing.T) { |
| 124 | + tr := NewTranslator(logr.Discard()) |
| 125 | + |
| 126 | + policy := makeL4RoutePolicy("default", "empty-policy", "TCPRoute", "my-tcp-route", nil) |
| 127 | + |
| 128 | + policies := map[k8stypes.NamespacedName]*v1alpha1.L4RoutePolicy{ |
| 129 | + {Namespace: "default", Name: "empty-policy"}: policy, |
| 130 | + } |
| 131 | + |
| 132 | + plugins := adctypes.Plugins{} |
| 133 | + tr.AttachL4RoutePolicyPlugins(policies, "default", "my-tcp-route", "TCPRoute", plugins) |
| 134 | + |
| 135 | + assert.Empty(t, plugins) |
| 136 | +} |
| 137 | + |
| 138 | +func TestAttachL4RoutePolicyPlugins_EmptyPolicies(t *testing.T) { |
| 139 | + tr := NewTranslator(logr.Discard()) |
| 140 | + plugins := adctypes.Plugins{} |
| 141 | + tr.AttachL4RoutePolicyPlugins(nil, "default", "my-tcp-route", "TCPRoute", plugins) |
| 142 | + assert.Empty(t, plugins) |
| 143 | +} |
0 commit comments