@@ -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,67 @@ 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 ("Running Kustomize build on: " + 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+ resources := resMap .Resources ()
430+ objs := make ([]* unstructured.Unstructured , 0 , len (resources ))
431+ for _ , res := range resources {
432+ resMap , err := res .Map ()
433+ gomega .Expect (err ).NotTo (gomega .HaveOccurred (), "Failed to run kustomize get map" )
434+ objs = append (objs , & unstructured.Unstructured {Object : resMap })
435+ }
436+ return createAndVerifyObjs (testConfig , objs )
437+ }
438+
439+ // CreateObjsFromYaml creates K8S objects from yaml and waits for them to be instantiated
440+ func CreateObjsFromYaml (testConfig * TestConfig , docs []string ) []string {
441+ objs := make ([]* unstructured.Unstructured , 0 , len (docs ))
442+ decoder := serializer .NewCodecFactory (testConfig .Scheme ).UniversalDeserializer ()
443+
444+ for _ , doc := range docs {
445+ trimmed := strings .TrimSpace (doc )
446+ if trimmed == "" {
447+ continue
448+ }
449+ // Decode into a runtime.Object
450+ obj , gvk , decodeErr := decoder .Decode ([]byte (trimmed ), nil , nil )
451+ gomega .Expect (decodeErr ).NotTo (gomega .HaveOccurred (),
452+ "Failed to decode YAML document to a Kubernetes object" )
453+
454+ ginkgo .By (fmt .Sprintf ("Decoded GVK: %s" , gvk ))
455+
456+ unstrObj , ok := obj .(* unstructured.Unstructured )
457+ if ! ok {
458+ // Fallback if it's a typed object
459+ unstrObj = & unstructured.Unstructured {}
460+ // Convert typed to unstructured
461+ err := testConfig .Scheme .Convert (obj , unstrObj , nil )
462+ gomega .Expect (err ).NotTo (gomega .HaveOccurred ())
463+ }
464+ objs = append (objs , unstrObj )
465+ }
466+ return createAndVerifyObjs (testConfig , objs )
467+ }
468+
443469// DeleteObjects deletes set of Kubernetes objects in the form of kind/name
444470func DeleteObjects (testConfig * TestConfig , kindAndNames []string ) {
445471 for _ , kindAndName := range kindAndNames {
@@ -462,6 +488,7 @@ func DeleteObjects(testConfig *TestConfig, kindAndNames []string) {
462488// ApplyYAMLFile reads a file containing YAML (possibly multiple docs)
463489// and applies each object to the cluster.
464490func ApplyYAMLFile (testConfig * TestConfig , filePath string ) []string {
491+ // return CreateObjsFromKustomize(testConfig, filePath)
465492 // Create the resources from the manifest file
466493 return CreateObjsFromYaml (testConfig , ReadYaml (filePath ))
467494}
0 commit comments