@@ -41,6 +41,8 @@ import (
4141 "k8s.io/client-go/tools/remotecommand"
4242 "sigs.k8s.io/controller-runtime/pkg/client"
4343 "sigs.k8s.io/controller-runtime/pkg/client/config"
44+ "sigs.k8s.io/kustomize/api/krusty"
45+ "sigs.k8s.io/kustomize/kyaml/filesys"
4446
4547 v1 "sigs.k8s.io/gateway-api-inference-extension/api/v1"
4648 "sigs.k8s.io/gateway-api-inference-extension/apix/v1alpha2"
@@ -381,44 +383,20 @@ func EventuallyExists(testConfig *TestConfig, getResource func() error) {
381383 }, testConfig .ExistsTimeout , testConfig .Interval ).Should (gomega .Succeed ())
382384}
383385
384- // CreateObjsFromYaml creates K8S objects from yaml and waits for them to be instantiated
385- func CreateObjsFromYaml (testConfig * TestConfig , docs []string ) []string {
386+ func createAndVerifyObjs (testConfig * TestConfig , objs []* unstructured.Unstructured ) []string {
386387 objNames := []string {}
387-
388- // For each doc, decode and create
389- decoder := serializer .NewCodecFactory (testConfig .Scheme ).UniversalDeserializer ()
390- for _ , doc := range docs {
391- trimmed := strings .TrimSpace (doc )
392- if trimmed == "" {
393- continue
394- }
395- // Decode into a runtime.Object
396- obj , gvk , decodeErr := decoder .Decode ([]byte (trimmed ), nil , nil )
397- gomega .Expect (decodeErr ).NotTo (gomega .HaveOccurred (),
398- "Failed to decode YAML document to a Kubernetes object" )
399-
400- ginkgo .By (fmt .Sprintf ("Decoded GVK: %s" , gvk ))
401-
402- unstrObj , ok := obj .(* unstructured.Unstructured )
403- if ! ok {
404- // Fallback if it's a typed object
405- unstrObj = & unstructured.Unstructured {}
406- // Convert typed to unstructured
407- err := testConfig .Scheme .Convert (obj , unstrObj , nil )
408- gomega .Expect (err ).NotTo (gomega .HaveOccurred ())
409- }
410-
388+ for _ , unstrObj := range objs {
389+ ginkgo .By (fmt .Sprintf ("Processing GVK: %s" , unstrObj .GroupVersionKind ()))
411390 unstrObj .SetNamespace (testConfig .NsName )
391+
412392 kind := unstrObj .GetKind ()
413393 name := unstrObj .GetName ()
414394 objNames = append (objNames , kind + "/" + name )
415395
416- // Create the object
417396 err := testConfig .K8sClient .Create (testConfig .Context , unstrObj , & client.CreateOptions {})
418397 gomega .Expect (err ).NotTo (gomega .HaveOccurred (),
419- "Failed to create object from YAML" )
398+ fmt . Sprintf ( "Failed to create %s %s" , kind , name ) )
420399
421- // Wait for the created object to exist.
422400 clientObj := getClientObject (kind )
423401 EventuallyExists (testConfig , func () error {
424402 return testConfig .K8sClient .Get (testConfig .Context ,
@@ -427,19 +405,66 @@ func CreateObjsFromYaml(testConfig *TestConfig, docs []string) []string {
427405
428406 switch kind {
429407 case "CustomResourceDefinition" :
430- // Wait for the CRD to be established.
431408 CRDEstablished (testConfig , clientObj .(* apiextv1.CustomResourceDefinition ))
432409 case "Deployment" :
433- // Wait for the deployment to be available.
434410 DeploymentAvailable (testConfig , clientObj .(* appsv1.Deployment ))
435411 case "Pod" :
436- // Wait for the pod to be ready.
437412 PodReady (testConfig , clientObj .(* corev1.Pod ))
438413 }
439414 }
440415 return objNames
441416}
442417
418+ func CreateCrdsFromKustomize (testConfig * TestConfig , kustomizePath string ) []string {
419+ ginkgo .By (fmt .Sprintf ("Running Kustomize build on: %s" , kustomizePath ))
420+
421+ fSys := filesys .MakeFsOnDisk ()
422+ opts := krusty .MakeDefaultOptions ()
423+ opts .PluginConfig = krusty .MakeDefaultOptions ().PluginConfig
424+ k := krusty .MakeKustomizer (opts )
425+
426+ resMap , err := k .Run (fSys , kustomizePath )
427+ gomega .Expect (err ).NotTo (gomega .HaveOccurred (), "Failed to run kustomize build" )
428+
429+ var objs []* unstructured.Unstructured
430+ for _ , res := range resMap .Resources () {
431+ resMap , err := res .Map ()
432+ gomega .Expect (err ).NotTo (gomega .HaveOccurred (), "Failed to run kustomize get map" )
433+ objs = append (objs , & unstructured.Unstructured {Object : resMap })
434+ }
435+ return createAndVerifyObjs (testConfig , objs )
436+ }
437+
438+ // CreateObjsFromYaml creates K8S objects from yaml and waits for them to be instantiated
439+ func CreateObjsFromYaml (testConfig * TestConfig , docs []string ) []string {
440+ var objs []* unstructured.Unstructured
441+ decoder := serializer .NewCodecFactory (testConfig .Scheme ).UniversalDeserializer ()
442+
443+ for _ , doc := range docs {
444+ trimmed := strings .TrimSpace (doc )
445+ if trimmed == "" {
446+ continue
447+ }
448+ // Decode into a runtime.Object
449+ obj , gvk , decodeErr := decoder .Decode ([]byte (trimmed ), nil , nil )
450+ gomega .Expect (decodeErr ).NotTo (gomega .HaveOccurred (),
451+ "Failed to decode YAML document to a Kubernetes object" )
452+
453+ ginkgo .By (fmt .Sprintf ("Decoded GVK: %s" , gvk ))
454+
455+ unstrObj , ok := obj .(* unstructured.Unstructured )
456+ if ! ok {
457+ // Fallback if it's a typed object
458+ unstrObj = & unstructured.Unstructured {}
459+ // Convert typed to unstructured
460+ err := testConfig .Scheme .Convert (obj , unstrObj , nil )
461+ gomega .Expect (err ).NotTo (gomega .HaveOccurred ())
462+ }
463+ objs = append (objs , unstrObj )
464+ }
465+ return createAndVerifyObjs (testConfig , objs )
466+ }
467+
443468// DeleteObjects deletes set of Kubernetes objects in the form of kind/name
444469func DeleteObjects (testConfig * TestConfig , kindAndNames []string ) {
445470 for _ , kindAndName := range kindAndNames {
@@ -462,6 +487,7 @@ func DeleteObjects(testConfig *TestConfig, kindAndNames []string) {
462487// ApplyYAMLFile reads a file containing YAML (possibly multiple docs)
463488// and applies each object to the cluster.
464489func ApplyYAMLFile (testConfig * TestConfig , filePath string ) []string {
490+ // return CreateObjsFromKustomize(testConfig, filePath)
465491 // Create the resources from the manifest file
466492 return CreateObjsFromYaml (testConfig , ReadYaml (filePath ))
467493}
0 commit comments