@@ -20,6 +20,7 @@ import (
2020 v1 "github.com/openshift/api/operatoringress/v1"
2121 operatorcontroller "github.com/openshift/cluster-ingress-operator/pkg/operator/controller"
2222 util "github.com/openshift/cluster-ingress-operator/pkg/util"
23+ olmv1 "github.com/operator-framework/api/pkg/operators/v1"
2324 operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
2425
2526 "github.com/google/go-cmp/cmp"
@@ -30,6 +31,7 @@ import (
3031 apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
3132 kerrors "k8s.io/apimachinery/pkg/api/errors"
3233 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34+ "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3335 "k8s.io/apimachinery/pkg/types"
3436 "k8s.io/apimachinery/pkg/util/wait"
3537
@@ -180,6 +182,105 @@ func deleteExistingVAP(t *testing.T, vapName string) error {
180182 return nil
181183}
182184
185+ // cleanupGateway deletes the given gateway and waits for the corresponding Istio proxy deployment to disappear.
186+ func cleanupGateway (t * testing.T , namespace , name , gcname string ) error {
187+ t .Helper ()
188+
189+ gw := & gatewayapiv1.Gateway {ObjectMeta : metav1.ObjectMeta {Namespace : namespace , Name : name }}
190+ if err := kclient .Delete (context .Background (), gw ); err != nil && ! kerrors .IsNotFound (err ) {
191+ return fmt .Errorf (`failed to delete gateway "%s/%s": %w` , namespace , name , err )
192+ }
193+ t .Logf (`Deleted gateway "%s/%s"` , namespace , name )
194+
195+ depl := & appsv1.Deployment {}
196+ istioName := types.NamespacedName {Namespace : operatorcontroller .DefaultOperandNamespace , Name : name + "-" + gcname }
197+ if err := wait .PollUntilContextTimeout (context .Background (), 1 * time .Second , 1 * time .Minute , false , func (context context.Context ) (bool , error ) {
198+ if err := kclient .Get (context , istioName , depl ); err != nil {
199+ if ! kerrors .IsNotFound (err ) {
200+ t .Logf ("Failed to get deployment %q, retrying..." , istioName )
201+ return false , nil
202+ }
203+ } else {
204+ t .Logf ("Deployment %q still exists, retrying..." , istioName )
205+ return false , nil
206+ }
207+ return true , nil
208+ }); err != nil {
209+ return fmt .Errorf ("timed out waiting for deployment %v to disappear" , istioName )
210+ }
211+ t .Logf ("Deleted deployment %q" , istioName )
212+
213+ return nil
214+ }
215+
216+ // cleanupGatewayClass deletes the given gatewayclass and waits for the corresponding Istiod deployment to disappear.
217+ func cleanupGatewayClass (t * testing.T , name string ) error {
218+ t .Helper ()
219+
220+ gc := & gatewayapiv1.GatewayClass {ObjectMeta : metav1.ObjectMeta {Name : name }}
221+ if err := kclient .Delete (context .Background (), gc ); err != nil && ! kerrors .IsNotFound (err ) {
222+ return fmt .Errorf ("failed to delete gatewayclass %q: %w" , name , err )
223+ }
224+ t .Logf ("Deleted gatewayclass %q" , name )
225+
226+ depl := & appsv1.Deployment {}
227+ istiodName := types.NamespacedName {Namespace : operatorcontroller .DefaultOperandNamespace , Name : "istiod-" + name }
228+ if err := wait .PollUntilContextTimeout (context .Background (), 1 * time .Second , 1 * time .Minute , false , func (context context.Context ) (bool , error ) {
229+ if err := kclient .Get (context , istiodName , depl ); err != nil {
230+ if ! kerrors .IsNotFound (err ) {
231+ t .Logf ("Failed to get deployment %q, retrying..." , istiodName )
232+ return false , nil
233+ }
234+ } else {
235+ t .Logf ("Deployment %q still exists, retrying..." , istiodName )
236+ return false , nil
237+ }
238+ return true , nil
239+ }); err != nil {
240+ return fmt .Errorf ("timed out waiting for deployment %v to disappear" , istiodName )
241+ }
242+ t .Logf ("Deleted deployment %q" , istiodName )
243+
244+ return nil
245+ }
246+
247+ // cleanupOLMOperator deletes all components associated with the given OLM operator, including the operator resource itself.
248+ func cleanupOLMOperator (t * testing.T , namespace , name string ) error {
249+ operatorName := types.NamespacedName {Namespace : namespace , Name : name }
250+ operator := & olmv1.Operator {}
251+ if err := wait .PollUntilContextTimeout (context .Background (), 1 * time .Second , 1 * time .Minute , false , func (context context.Context ) (bool , error ) {
252+ if err := kclient .Get (context , operatorName , operator ); err != nil {
253+ t .Logf ("Failed to get operator %q, retrying..." , operatorName )
254+ return false , nil
255+ }
256+ return true , nil
257+ }); err != nil {
258+ return fmt .Errorf ("timed out getting operator %v" , operatorName )
259+ }
260+
261+ if operator .Status .Components != nil {
262+ for _ , compRef := range operator .Status .Components .Refs {
263+ if compRef .Kind != "CustomResourceDefinition" {
264+ unstructuredObj := & unstructured.Unstructured {}
265+ unstructuredObj .SetAPIVersion (compRef .APIVersion )
266+ unstructuredObj .SetKind (compRef .Kind )
267+ unstructuredObj .SetName (compRef .Name )
268+ unstructuredObj .SetNamespace (namespace )
269+ if err := kclient .Delete (context .Background (), unstructuredObj ); err != nil && ! kerrors .IsNotFound (err ) {
270+ return fmt .Errorf (`failed to delete operator component "%s/%s/%s/%s": %w` , compRef .APIVersion , compRef .Kind , namespace , compRef .Name , err )
271+ }
272+ t .Logf (`Deleted operator component "%s/%s/%s/%s"` , compRef .APIVersion , compRef .Kind , namespace , compRef .Name )
273+ }
274+ }
275+ }
276+ if err := kclient .Delete (context .Background (), operator ); err != nil && ! kerrors .IsNotFound (err ) {
277+ return fmt .Errorf ("failed to delete operator %q: %w" , operatorName , err )
278+ }
279+ t .Logf ("Deleted operator %q" , operatorName )
280+
281+ return nil
282+ }
283+
183284// createHttpRoute checks if the HTTPRoute can be created.
184285// If it can't an error is returned.
185286func createHttpRoute (namespace , routeName , parentNamespace , hostname , backendRefname string , gateway * gatewayapiv1.Gateway ) (* gatewayapiv1.HTTPRoute , error ) {
0 commit comments