@@ -19,6 +19,7 @@ import (
1919 operatorcontroller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller"
2020 operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
2121
22+ admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
2223 appsv1 "k8s.io/api/apps/v1"
2324 corev1 "k8s.io/api/core/v1"
2425 rbacv1 "k8s.io/api/rbac/v1"
@@ -149,6 +150,53 @@ func deleteExistingCRD(t *testing.T, crdName string) error {
149150 return nil
150151}
151152
153+ // deleteVAP delete given Validating Admission Policy.
154+ func deleteVAP (t * testing.T , vapName string ) error {
155+ t .Helper ()
156+
157+ vap := & admissionregistrationv1.ValidatingAdmissionPolicy {}
158+ newVAP := & admissionregistrationv1.ValidatingAdmissionPolicy {}
159+ name := types.NamespacedName {Name : vapName }
160+
161+ // Retrieve the object to be deleted.
162+ if err := wait .PollUntilContextTimeout (context .Background (), 1 * time .Second , 30 * time .Second , false , func (context context.Context ) (bool , error ) {
163+ if err := kclient .Get (context , name , vap ); err != nil {
164+ t .Logf ("failed to get vap %q: %v, retrying ..." , vapName , err )
165+ return false , nil
166+ }
167+ return true , nil
168+ }); err != nil {
169+ return fmt .Errorf ("failed to get vap %q: %w" , vapName , err )
170+ }
171+
172+ if err := kclient .Delete (context .Background (), vap ); err != nil {
173+ return fmt .Errorf ("failed to delete vap %q: %w" , vapName , err )
174+ }
175+
176+ // Verify VAP was not recreated.
177+ if err := wait .PollUntilContextTimeout (context .Background (), 1 * time .Second , 30 * time .Second , false , func (ctx context.Context ) (bool , error ) {
178+ if err := kclient .Get (ctx , name , newVAP ); err != nil {
179+ if kerrors .IsNotFound (err ) {
180+ // VAP does not exist as expected.
181+ return true , nil
182+ }
183+ t .Logf ("failed to get vap %q: %v, retrying ..." , vapName , err )
184+ return false , nil
185+ }
186+ // Check if new VAP got recreated.
187+ if newVAP != nil && newVAP .UID != vap .UID {
188+ return true , fmt .Errorf ("vap %q got recreated" , vapName )
189+ }
190+ t .Logf ("vap %q still exists, retrying ..." , vapName )
191+ return false , nil
192+ }); err != nil {
193+ return fmt .Errorf ("failed to verify deletion of vap %q: %v" , vapName , err )
194+ }
195+
196+ t .Logf ("deleted vap %q" , vapName )
197+ return nil
198+ }
199+
152200// createHttpRoute checks if the HTTPRoute can be created.
153201// If it can't an error is returned.
154202func createHttpRoute (namespace , routeName , parentNamespace , hostname , backendRefname string , gateway * gatewayapiv1.Gateway ) (* gatewayapiv1.HTTPRoute , error ) {
@@ -272,6 +320,71 @@ func buildHTTPRoute(routeName, namespace, parentgateway, parentNamespace, hostna
272320 }
273321}
274322
323+ // buildGWAPICRDFromName initializes the GatewayAPI CRD deducing most of its required fields from the given name.
324+ func buildGWAPICRDFromName (name string ) * apiextensionsv1.CustomResourceDefinition {
325+ var (
326+ plural = strings .Split (name , "." )[0 ]
327+ group , _ = strings .CutPrefix (name , plural + "." )
328+ scope = apiextensionsv1 .NamespaceScoped
329+ // removing trailing "s"
330+ singular = plural [0 : len (plural )- 1 ]
331+ kind string
332+ )
333+
334+ switch plural {
335+ case "gatewayclasses" :
336+ singular = "gatewayclass"
337+ kind = "GatewayClass"
338+ scope = apiextensionsv1 .ClusterScoped
339+ case "gateways" :
340+ kind = "Gateway"
341+ case "httproutes" :
342+ kind = "HTTPRoute"
343+ case "referencegrants" :
344+ kind = "ReferenceGrant"
345+ }
346+
347+ return & apiextensionsv1.CustomResourceDefinition {
348+ ObjectMeta : metav1.ObjectMeta {
349+ Name : plural + "." + group ,
350+ Annotations : map [string ]string {
351+ "api-approved.kubernetes.io" : "https://github.com/kubernetes-sigs/gateway-api/pull/2466" ,
352+ },
353+ },
354+ Spec : apiextensionsv1.CustomResourceDefinitionSpec {
355+ Group : group ,
356+ Names : apiextensionsv1.CustomResourceDefinitionNames {
357+ Singular : singular ,
358+ Plural : plural ,
359+ Kind : kind ,
360+ },
361+ Scope : scope ,
362+ Versions : []apiextensionsv1.CustomResourceDefinitionVersion {
363+ {
364+ Name : "v1" ,
365+ Storage : true ,
366+ Served : true ,
367+ Schema : & apiextensionsv1.CustomResourceValidation {
368+ OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
369+ Type : "object" ,
370+ },
371+ },
372+ },
373+ {
374+ Name : "v1beta1" ,
375+ Storage : false ,
376+ Served : true ,
377+ Schema : & apiextensionsv1.CustomResourceValidation {
378+ OpenAPIV3Schema : & apiextensionsv1.JSONSchemaProps {
379+ Type : "object" ,
380+ },
381+ },
382+ },
383+ },
384+ },
385+ }
386+ }
387+
275388// assertSubscription checks if the Subscription of the given name exists and returns an error if not.
276389func assertSubscription (t * testing.T , namespace , subName string ) error {
277390 t .Helper ()
@@ -651,3 +764,37 @@ func assertDNSRecord(t *testing.T, recordName types.NamespacedName) error {
651764 })
652765 return err
653766}
767+
768+ // assertVAP checks if the VAP of the given name exists, and returns an error if not.
769+ func assertVAP (t * testing.T , name string ) error {
770+ t .Helper ()
771+ vap := & admissionregistrationv1.ValidatingAdmissionPolicy {}
772+ return wait .PollUntilContextTimeout (context .Background (), 1 * time .Second , 1 * time .Minute , false , func (context context.Context ) (bool , error ) {
773+ if err := kclient .Get (context , types.NamespacedName {Name : name }, vap ); err != nil {
774+ t .Logf ("failed to get vap %q: %v, retrying..." , name , err )
775+ return false , nil
776+ }
777+ return true , nil
778+ })
779+ }
780+
781+ // scaleDeployment scales a deployment with the given name to the specified number of replicas.
782+ func scaleDeployment (t * testing.T , namespace , name string , replicas int32 ) error {
783+ t .Helper ()
784+
785+ nsName := types.NamespacedName {Namespace : namespace , Name : name }
786+ return wait .PollUntilContextTimeout (context .Background (), 1 * time .Second , 30 * time .Second , false , func (context context.Context ) (bool , error ) {
787+ depl := & appsv1.Deployment {}
788+ if err := kclient .Get (context , nsName , depl ); err != nil {
789+ t .Logf ("failed to get deployment %q: %v, retrying..." , nsName , err )
790+ return false , nil
791+ }
792+ depl .Spec .Replicas = & replicas
793+ if err := kclient .Update (context , depl ); err != nil {
794+ t .Logf ("failed to update deployment %q: %v, retrying..." , nsName , err )
795+ return false , nil
796+ }
797+ t .Logf ("scaled deployment %q to %d replica(s)" , nsName , replicas )
798+ return true , nil
799+ })
800+ }
0 commit comments