|
15 | 15 | package gateway |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "fmt" |
18 | 19 | "testing" |
19 | 20 |
|
20 | 21 | corev1 "k8s.io/api/core/v1" |
21 | 22 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 | + "k8s.io/apimachinery/pkg/runtime" |
| 24 | + "k8s.io/apimachinery/pkg/util/intstr" |
22 | 25 | inferencev1 "sigs.k8s.io/gateway-api-inference-extension/api/v1" |
23 | 26 |
|
24 | 27 | "istio.io/istio/pilot/pkg/features" |
25 | 28 | "istio.io/istio/pkg/config/constants" |
26 | | - "istio.io/istio/pkg/kube/krt" |
27 | 29 | "istio.io/istio/pkg/test" |
28 | 30 | "istio.io/istio/pkg/test/util/assert" |
29 | 31 | ) |
30 | 32 |
|
31 | 33 | func TestReconcileInferencePool(t *testing.T) { |
32 | 34 | test.SetForTest(t, &features.EnableGatewayAPIInferenceExtension, true) |
33 | | - pool := &inferencev1.InferencePool{ |
34 | | - ObjectMeta: metav1.ObjectMeta{ |
35 | | - Name: "test-pool", |
36 | | - Namespace: "default", |
| 35 | + |
| 36 | + testCases := []struct { |
| 37 | + name string |
| 38 | + inferencePool *inferencev1.InferencePool |
| 39 | + shadowService *corev1.Service // name is optional, if not provided, it will be generated |
| 40 | + expectedAnnotations map[string]string |
| 41 | + expectedLabels map[string]string |
| 42 | + expectedServiceName string |
| 43 | + expectedTargetPort int32 |
| 44 | + }{ |
| 45 | + { |
| 46 | + name: "basic shadow service creation", |
| 47 | + inferencePool: &inferencev1.InferencePool{ |
| 48 | + ObjectMeta: metav1.ObjectMeta{ |
| 49 | + Name: "test-pool", |
| 50 | + Namespace: "default", |
| 51 | + }, |
| 52 | + Spec: inferencev1.InferencePoolSpec{ |
| 53 | + TargetPorts: []inferencev1.Port{ |
| 54 | + { |
| 55 | + Number: inferencev1.PortNumber(8080), |
| 56 | + }, |
| 57 | + }, |
| 58 | + Selector: inferencev1.LabelSelector{ |
| 59 | + MatchLabels: map[inferencev1.LabelKey]inferencev1.LabelValue{ |
| 60 | + "app": "test", |
| 61 | + }, |
| 62 | + }, |
| 63 | + EndpointPickerRef: inferencev1.EndpointPickerRef{ |
| 64 | + Name: "dummy", |
| 65 | + Port: &inferencev1.Port{ |
| 66 | + Number: inferencev1.PortNumber(5421), |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + expectedLabels: map[string]string{ |
| 72 | + constants.InternalServiceSemantics: constants.ServiceSemanticsInferencePool, |
| 73 | + InferencePoolRefLabel: "test-pool", |
| 74 | + }, |
| 75 | + expectedTargetPort: 8080, |
37 | 76 | }, |
38 | | - Spec: inferencev1.InferencePoolSpec{ |
39 | | - TargetPorts: []inferencev1.Port{ |
40 | | - { |
41 | | - Number: inferencev1.PortNumber(8080), |
| 77 | + { |
| 78 | + name: "user label and annotation preservation", |
| 79 | + inferencePool: &inferencev1.InferencePool{ |
| 80 | + ObjectMeta: metav1.ObjectMeta{ |
| 81 | + Name: "preserve-pool", |
| 82 | + Namespace: "default", |
| 83 | + }, |
| 84 | + Spec: inferencev1.InferencePoolSpec{ |
| 85 | + TargetPorts: []inferencev1.Port{ |
| 86 | + { |
| 87 | + Number: inferencev1.PortNumber(8080), |
| 88 | + }, |
| 89 | + }, |
| 90 | + Selector: inferencev1.LabelSelector{ |
| 91 | + MatchLabels: map[inferencev1.LabelKey]inferencev1.LabelValue{ |
| 92 | + "app": "test", |
| 93 | + }, |
| 94 | + }, |
| 95 | + EndpointPickerRef: inferencev1.EndpointPickerRef{ |
| 96 | + Name: "dummy", |
| 97 | + Port: &inferencev1.Port{ |
| 98 | + Number: inferencev1.PortNumber(5421), |
| 99 | + }, |
| 100 | + }, |
42 | 101 | }, |
43 | 102 | }, |
44 | | - Selector: inferencev1.LabelSelector{ |
45 | | - MatchLabels: map[inferencev1.LabelKey]inferencev1.LabelValue{ |
46 | | - "app": "test", |
| 103 | + shadowService: &corev1.Service{ |
| 104 | + ObjectMeta: metav1.ObjectMeta{ |
| 105 | + Namespace: "default", |
| 106 | + Labels: map[string]string{ |
| 107 | + InferencePoolRefLabel: "preserve-pool", |
| 108 | + "user.example.com/my-label": "user-value", |
| 109 | + "another.domain.com/label": "another-value", |
| 110 | + }, |
| 111 | + Annotations: map[string]string{ |
| 112 | + "user.example.com/my-annotation": "user-annotation-value", |
| 113 | + "monitoring.example.com/scrape": "true", |
| 114 | + }, |
| 115 | + }, |
| 116 | + Spec: corev1.ServiceSpec{ |
| 117 | + Selector: map[string]string{"app": "test"}, |
| 118 | + Type: corev1.ServiceTypeClusterIP, |
| 119 | + ClusterIP: corev1.ClusterIPNone, |
| 120 | + Ports: []corev1.ServicePort{ |
| 121 | + { |
| 122 | + Protocol: "TCP", |
| 123 | + Port: 54321, |
| 124 | + TargetPort: intstr.FromInt(8080), |
| 125 | + }, |
| 126 | + }, |
47 | 127 | }, |
48 | 128 | }, |
49 | | - EndpointPickerRef: inferencev1.EndpointPickerRef{ |
50 | | - Name: "dummy", |
51 | | - Port: &inferencev1.Port{ |
52 | | - Number: inferencev1.PortNumber(5421), |
| 129 | + expectedAnnotations: map[string]string{ |
| 130 | + "user.example.com/my-annotation": "user-annotation-value", |
| 131 | + "monitoring.example.com/scrape": "true", |
| 132 | + }, |
| 133 | + expectedLabels: map[string]string{ |
| 134 | + constants.InternalServiceSemantics: constants.ServiceSemanticsInferencePool, |
| 135 | + InferencePoolRefLabel: "preserve-pool", |
| 136 | + "user.example.com/my-label": "user-value", |
| 137 | + "another.domain.com/label": "another-value", |
| 138 | + }, |
| 139 | + expectedTargetPort: 8080, |
| 140 | + }, |
| 141 | + { |
| 142 | + name: "very long inferencepool name", |
| 143 | + inferencePool: &inferencev1.InferencePool{ |
| 144 | + ObjectMeta: metav1.ObjectMeta{ |
| 145 | + Name: "very-long-inference-pool-name-that-should-be-truncated-properly", |
| 146 | + Namespace: "default", |
53 | 147 | }, |
| 148 | + Spec: inferencev1.InferencePoolSpec{ |
| 149 | + TargetPorts: []inferencev1.Port{ |
| 150 | + { |
| 151 | + Number: inferencev1.PortNumber(9090), |
| 152 | + }, |
| 153 | + }, |
| 154 | + Selector: inferencev1.LabelSelector{ |
| 155 | + MatchLabels: map[inferencev1.LabelKey]inferencev1.LabelValue{ |
| 156 | + "app": "longname", |
| 157 | + }, |
| 158 | + }, |
| 159 | + EndpointPickerRef: inferencev1.EndpointPickerRef{ |
| 160 | + Name: "dummy", |
| 161 | + Port: &inferencev1.Port{ |
| 162 | + Number: inferencev1.PortNumber(5421), |
| 163 | + }, |
| 164 | + }, |
| 165 | + }, |
| 166 | + }, |
| 167 | + expectedLabels: map[string]string{ |
| 168 | + constants.InternalServiceSemantics: constants.ServiceSemanticsInferencePool, |
| 169 | + InferencePoolRefLabel: "very-long-inference-pool-name-that-should-be-truncated-properly", |
54 | 170 | }, |
| 171 | + expectedServiceName: "very-long-inference-pool-name-that-should-be-trunca-ip-6d24df6a", |
| 172 | + expectedTargetPort: 9090, |
55 | 173 | }, |
56 | 174 | } |
57 | | - controller := setupController(t, |
58 | | - &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "default"}}, |
59 | | - NewGateway("test-gw", InNamespace(DefaultTestNS), WithGatewayClass("istio")), |
60 | | - NewHTTPRoute("test-route", InNamespace(DefaultTestNS), |
61 | | - WithParentRefAndStatus("test-gw", DefaultTestNS, IstioController), |
62 | | - WithBackendRef("test-pool", DefaultTestNS), |
63 | | - ), |
64 | | - pool, |
65 | | - ) |
66 | 175 |
|
67 | | - dumpOnFailure(t, krt.GlobalDebugHandler) |
| 176 | + for _, tc := range testCases { |
| 177 | + t.Run(tc.name, func(t *testing.T) { |
| 178 | + objects := []runtime.Object{ |
| 179 | + &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: "default"}}, |
| 180 | + NewGateway(tc.name+"-gw", InNamespace("default"), WithGatewayClass("istio")), |
| 181 | + NewHTTPRoute(tc.name+"-route", InNamespace("default"), |
| 182 | + WithParentRefAndStatus(tc.name+"-gw", "default", "istio.io/gateway-controller"), |
| 183 | + WithBackendRef(tc.inferencePool.Name, "default"), |
| 184 | + ), |
| 185 | + tc.inferencePool, |
| 186 | + } |
| 187 | + if tc.shadowService != nil { |
| 188 | + // Generate the service name if not provided |
| 189 | + if tc.shadowService.Name == "" { |
| 190 | + generatedName, err := InferencePoolServiceName(tc.inferencePool.Name) |
| 191 | + assert.NoError(t, err) |
| 192 | + tc.shadowService.Name = generatedName |
| 193 | + } |
| 194 | + objects = append(objects, tc.shadowService) |
| 195 | + } |
| 196 | + controller := setupController(t, objects...) |
68 | 197 |
|
69 | | - // Verify the service was created |
70 | | - var service *corev1.Service |
71 | | - var err error |
72 | | - assert.EventuallyEqual(t, func() bool { |
73 | | - svcName := "test-pool-ip-" + generateHash("test-pool", hashSize) |
74 | | - service, err = controller.client.Kube().CoreV1().Services("default").Get(t.Context(), svcName, metav1.GetOptions{}) |
75 | | - if err != nil { |
76 | | - t.Logf("Service %s not found yet: %v", svcName, err) |
77 | | - return false |
78 | | - } |
79 | | - return service != nil |
80 | | - }, true) |
| 198 | + var service *corev1.Service |
| 199 | + expectedSvcName, err := InferencePoolServiceName(tc.inferencePool.Name) |
| 200 | + if tc.expectedServiceName != "" { |
| 201 | + assert.Equal(t, expectedSvcName, tc.expectedServiceName, fmt.Sprintf("Service name should be '%s'", tc.expectedServiceName)) |
| 202 | + } |
| 203 | + assert.NoError(t, err) |
| 204 | + assert.EventuallyEqual(t, func() bool { |
| 205 | + var err error |
| 206 | + service, err = controller.client.Kube().CoreV1().Services("default").Get(t.Context(), expectedSvcName, metav1.GetOptions{}) |
| 207 | + if err != nil { |
| 208 | + t.Logf("Service %s not found yet: %v", expectedSvcName, err) |
| 209 | + return false |
| 210 | + } |
| 211 | + return service != nil && service.Labels[constants.InternalServiceSemantics] == constants.ServiceSemanticsInferencePool |
| 212 | + }, true) |
81 | 213 |
|
82 | | - assert.Equal(t, service.ObjectMeta.Labels[constants.InternalServiceSemantics], constants.ServiceSemanticsInferencePool) |
83 | | - assert.Equal(t, service.ObjectMeta.Labels[InferencePoolRefLabel], pool.Name) |
84 | | - assert.Equal(t, service.OwnerReferences[0].Name, pool.Name) |
85 | | - assert.Equal(t, service.Spec.Ports[0].TargetPort.IntVal, int32(8080)) |
86 | | - assert.Equal(t, service.Spec.Ports[0].Port, int32(54321)) // dummyPort + i |
| 214 | + for key, expectedValue := range tc.expectedLabels { |
| 215 | + assert.Equal(t, service.Labels[key], expectedValue, fmt.Sprintf("Label '%s' should have value '%s'", key, expectedValue)) |
| 216 | + } |
| 217 | + for key, expectedValue := range tc.expectedAnnotations { |
| 218 | + assert.Equal(t, service.Annotations[key], expectedValue, fmt.Sprintf("Annotation '%s' should have value '%s'", key, expectedValue)) |
| 219 | + } |
| 220 | + assert.Equal(t, service.Spec.Ports[0].Port, int32(54321)) // dummyPort + i |
| 221 | + assert.Equal(t, service.Spec.Ports[0].TargetPort.IntVal, tc.expectedTargetPort) |
| 222 | + assert.Equal(t, service.OwnerReferences[0].Name, tc.inferencePool.Name) |
| 223 | + }) |
| 224 | + } |
87 | 225 | } |
0 commit comments