generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathe2e_suite_test.go
353 lines (297 loc) · 13.1 KB
/
e2e_suite_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
Copyright 2024 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package e2e
import (
"context"
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/onsi/ginkgo/v2"
"github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
infextv1a2 "sigs.k8s.io/gateway-api-inference-extension/api/v1alpha2"
testutils "sigs.k8s.io/gateway-api-inference-extension/test/utils"
)
const (
// defaultExistsTimeout is the default timeout for a resource to exist in the api server.
defaultExistsTimeout = 30 * time.Second
// defaultReadyTimeout is the default timeout for a resource to report a ready state.
defaultReadyTimeout = 3 * time.Minute
// defaultModelReadyTimeout is the default timeout for the model server deployment to report a ready state.
defaultModelReadyTimeout = 10 * time.Minute
// defaultInterval is the default interval to check if a resource exists or ready conditions.
defaultInterval = time.Millisecond * 250
// defaultCurlInterval is the default interval to run the test curl command.
defaultCurlInterval = time.Second * 5
// nsName is the name of the Namespace used for tests.
// TODO [danehans]: Must be "default" until https://github.com/kubernetes-sigs/gateway-api-inference-extension/issues/227 is fixed
nsName = "default"
// modelServerName is the name of the model server test resources.
modelServerName = "vllm-llama2-7b-pool"
// modelName is the test model name.
modelName = "tweet-summary"
// envoyName is the name of the envoy proxy test resources.
envoyName = "envoy"
// envoyPort is the listener port number of the test envoy proxy.
envoyPort = "8081"
// inferExtName is the name of the inference extension test resources.
inferExtName = "inference-gateway-ext-proc"
// clientManifest is the manifest for the client test resources.
clientManifest = "../testdata/client.yaml"
// modelServerManifest is the manifest for the model server test resources.
modelServerManifest = "../../config/manifests/vllm/deployment.yaml"
// modelServerSecretManifest is the manifest for the model server secret resource.
modelServerSecretManifest = "../testdata/model-secret.yaml"
// inferPoolManifest is the manifest for the inference pool CRD.
inferPoolManifest = "../../config/crd/bases/inference.networking.x-k8s.io_inferencepools.yaml"
// inferModelManifest is the manifest for the inference model CRD.
inferModelManifest = "../../config/crd/bases/inference.networking.x-k8s.io_inferencemodels.yaml"
// inferExtManifest is the manifest for the inference extension test resources.
inferExtManifest = "../../config/manifests/ext_proc.yaml"
// envoyManifest is the manifest for the envoy proxy test resources.
envoyManifest = "../testdata/envoy.yaml"
)
var (
ctx context.Context
cli client.Client
// Required for exec'ing in curl pod
kubeCli *kubernetes.Clientset
scheme = runtime.NewScheme()
cfg = config.GetConfigOrDie()
)
func TestAPIs(t *testing.T) {
gomega.RegisterFailHandler(ginkgo.Fail)
ginkgo.RunSpecs(t,
"End To End Test Suite",
)
}
var _ = ginkgo.BeforeSuite(func() {
ginkgo.By("Setting up the test suite")
setupSuite()
ginkgo.By("Creating test infrastructure")
setupInfra()
})
func setupInfra() {
crds := map[string]string{
"inferencepools.inference.networking.x-k8s.io": inferPoolManifest,
"inferencemodels.inference.networking.x-k8s.io": inferModelManifest,
}
createCRDs(cli, crds)
createInferExt(cli, inferExtManifest)
createClient(cli, clientManifest)
createEnvoy(cli, envoyManifest)
// Run this step last, as it requires additional time for the model server to become ready.
createModelServer(cli, modelServerSecretManifest, modelServerManifest)
}
var _ = ginkgo.AfterSuite(func() {
ginkgo.By("Performing global cleanup")
cleanupResources()
})
// setupSuite initializes the test suite by setting up the Kubernetes client,
// loading required API schemes, and validating configuration.
func setupSuite() {
ctx = context.Background()
gomega.ExpectWithOffset(1, cfg).NotTo(gomega.BeNil())
err := clientgoscheme.AddToScheme(scheme)
gomega.ExpectWithOffset(1, err).NotTo(gomega.HaveOccurred())
err = apiextv1.AddToScheme(scheme)
gomega.ExpectWithOffset(1, err).NotTo(gomega.HaveOccurred())
err = infextv1a2.AddToScheme(scheme)
gomega.ExpectWithOffset(1, err).NotTo(gomega.HaveOccurred())
cli, err = client.New(cfg, client.Options{Scheme: scheme})
gomega.Expect(err).NotTo(gomega.HaveOccurred())
gomega.Expect(cli).NotTo(gomega.BeNil())
kubeCli, err = kubernetes.NewForConfig(cfg)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
}
func cleanupResources() {
gomega.Expect(testutils.DeleteClusterResources(ctx, cli)).To(gomega.Succeed())
gomega.Expect(testutils.DeleteNamespacedResources(ctx, cli, nsName)).To(gomega.Succeed())
}
func cleanupInferModelResources() {
gomega.Expect(testutils.DeleteInferenceModelResources(ctx, cli, nsName)).To(gomega.Succeed())
}
func getTimeout(key string, fallback time.Duration) time.Duration {
if value, ok := os.LookupEnv(key); ok {
if parsed, err := time.ParseDuration(value); err == nil {
return parsed
}
}
return fallback
}
var (
existsTimeout = getTimeout("EXISTS_TIMEOUT", defaultExistsTimeout)
readyTimeout = getTimeout("READY_TIMEOUT", defaultReadyTimeout)
modelReadyTimeout = getTimeout("MODEL_READY_TIMEOUT", defaultModelReadyTimeout)
interval = defaultInterval
curlInterval = defaultCurlInterval
)
// namespaceExists ensures that a specified namespace exists and is ready for use.
func namespaceExists(k8sClient client.Client, ns string) {
ginkgo.By("Ensuring namespace exists: " + ns)
testutils.EventuallyExists(ctx, func() error {
return k8sClient.Get(ctx, types.NamespacedName{Name: ns}, &corev1.Namespace{})
}, existsTimeout, interval)
}
// createCRDs creates the Inference Extension CRDs used for testing.
func createCRDs(k8sClient client.Client, crds map[string]string) {
for name, path := range crds {
ginkgo.By("Creating CRD resource from manifest: " + path)
applyYAMLFile(k8sClient, path)
// Wait for the CRD to exist.
crd := &apiextv1.CustomResourceDefinition{}
testutils.EventuallyExists(ctx, func() error {
return k8sClient.Get(ctx, types.NamespacedName{Name: name}, crd)
}, existsTimeout, interval)
// Wait for the CRD to be established.
testutils.CRDEstablished(ctx, k8sClient, crd, readyTimeout, interval)
}
}
// createClient creates the client pod used for testing from the given filePath.
func createClient(k8sClient client.Client, filePath string) {
ginkgo.By("Creating client resources from manifest: " + filePath)
applyYAMLFile(k8sClient, filePath)
// Wait for the pod to exist.
pod := &corev1.Pod{}
testutils.EventuallyExists(ctx, func() error {
return k8sClient.Get(ctx, types.NamespacedName{Namespace: nsName, Name: "curl"}, pod)
}, existsTimeout, interval)
// Wait for the pod to be ready.
testutils.PodReady(ctx, k8sClient, pod, readyTimeout, interval)
}
// createModelServer creates the model server resources used for testing from the given filePaths.
func createModelServer(k8sClient client.Client, secretPath, deployPath string) {
ginkgo.By("Ensuring the HF_TOKEN environment variable is set")
token := os.Getenv("HF_TOKEN")
gomega.Expect(token).NotTo(gomega.BeEmpty(), "HF_TOKEN is not set")
inManifests := readYaml(secretPath)
ginkgo.By("Replacing placeholder secret data with HF_TOKEN environment variable")
outManifests := []string{}
for _, m := range inManifests {
outManifests = append(outManifests, strings.Replace(m, "$HF_TOKEN", token, 1))
}
ginkgo.By("Creating model server secret resource from manifest: " + deployPath)
createObjsFromYaml(k8sClient, outManifests)
// Wait for the secret to exist before proceeding with test.
testutils.EventuallyExists(ctx, func() error {
return k8sClient.Get(ctx, types.NamespacedName{Namespace: nsName, Name: "hf-token"}, &corev1.Secret{})
}, existsTimeout, interval)
ginkgo.By("Creating model server resources from manifest: " + deployPath)
applyYAMLFile(k8sClient, deployPath)
// Wait for the deployment to exist.
deploy := &appsv1.Deployment{}
testutils.EventuallyExists(ctx, func() error {
return k8sClient.Get(ctx, types.NamespacedName{Namespace: nsName, Name: modelServerName}, deploy)
}, existsTimeout, interval)
// Wait for the deployment to be available.
testutils.DeploymentAvailable(ctx, k8sClient, deploy, modelReadyTimeout, interval)
}
// createEnvoy creates the envoy proxy resources used for testing from the given filePath.
func createEnvoy(k8sClient client.Client, filePath string) {
ginkgo.By("Creating envoy proxy resources from manifest: " + filePath)
applyYAMLFile(k8sClient, filePath)
// Wait for the configmap to exist before proceeding with test.
cfgMap := &corev1.ConfigMap{}
testutils.EventuallyExists(ctx, func() error {
return k8sClient.Get(ctx, types.NamespacedName{Namespace: nsName, Name: envoyName}, cfgMap)
}, existsTimeout, interval)
// Wait for the deployment to exist.
deploy := &appsv1.Deployment{}
testutils.EventuallyExists(ctx, func() error {
return k8sClient.Get(ctx, types.NamespacedName{Namespace: nsName, Name: envoyName}, deploy)
}, existsTimeout, interval)
// Wait for the deployment to be available.
testutils.DeploymentAvailable(ctx, k8sClient, deploy, readyTimeout, interval)
// Wait for the service to exist.
testutils.EventuallyExists(ctx, func() error {
return k8sClient.Get(ctx, types.NamespacedName{Namespace: nsName, Name: envoyName}, &corev1.Service{})
}, existsTimeout, interval)
}
// createInferExt creates the inference extension resources used for testing from the given filePath.
func createInferExt(k8sClient client.Client, filePath string) {
ginkgo.By("Creating inference extension resources from manifest: " + filePath)
applyYAMLFile(k8sClient, filePath)
// Wait for the clusterrole to exist.
testutils.EventuallyExists(ctx, func() error {
return k8sClient.Get(ctx, types.NamespacedName{Name: "pod-read"}, &rbacv1.ClusterRole{})
}, existsTimeout, interval)
// Wait for the clusterrolebinding to exist.
testutils.EventuallyExists(ctx, func() error {
return k8sClient.Get(ctx, types.NamespacedName{Name: "pod-read-binding"}, &rbacv1.ClusterRoleBinding{})
}, existsTimeout, interval)
// Wait for the deployment to exist.
deploy := &appsv1.Deployment{}
testutils.EventuallyExists(ctx, func() error {
return k8sClient.Get(ctx, types.NamespacedName{Namespace: nsName, Name: inferExtName}, deploy)
}, existsTimeout, interval)
// Wait for the deployment to be available.
testutils.DeploymentAvailable(ctx, k8sClient, deploy, modelReadyTimeout, interval)
// Wait for the service to exist.
testutils.EventuallyExists(ctx, func() error {
return k8sClient.Get(ctx, types.NamespacedName{Namespace: nsName, Name: inferExtName}, &corev1.Service{})
}, existsTimeout, interval)
}
// applyYAMLFile reads a file containing YAML (possibly multiple docs)
// and applies each object to the cluster.
func applyYAMLFile(k8sClient client.Client, filePath string) {
// Create the resources from the manifest file
createObjsFromYaml(k8sClient, readYaml(filePath))
}
func readYaml(filePath string) []string {
ginkgo.By("Reading YAML file: " + filePath)
yamlBytes, err := os.ReadFile(filePath)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
// Split multiple docs, if needed
return strings.Split(string(yamlBytes), "\n---")
}
func createObjsFromYaml(k8sClient client.Client, docs []string) {
// For each doc, decode and create
decoder := serializer.NewCodecFactory(scheme).UniversalDeserializer()
for _, doc := range docs {
trimmed := strings.TrimSpace(doc)
if trimmed == "" {
continue
}
// Decode into a runtime.Object
obj, gvk, decodeErr := decoder.Decode([]byte(trimmed), nil, nil)
gomega.Expect(decodeErr).NotTo(gomega.HaveOccurred(),
"Failed to decode YAML document to a Kubernetes object")
ginkgo.By(fmt.Sprintf("Decoded GVK: %s", gvk))
unstrObj, ok := obj.(*unstructured.Unstructured)
if !ok {
// Fallback if it's a typed object
unstrObj = &unstructured.Unstructured{}
// Convert typed to unstructured
err := scheme.Convert(obj, unstrObj, nil)
gomega.Expect(err).NotTo(gomega.HaveOccurred())
}
unstrObj.SetNamespace(nsName)
// Create the object
err := k8sClient.Create(ctx, unstrObj)
gomega.Expect(err).NotTo(gomega.HaveOccurred(),
"Failed to create object from YAML")
}
}