github.com/redhat-appstudio/e2e-tests@v0.0.0-20230619105049-9a422b2094d7/pkg/utils/common/route.go (about) 1 package common 2 3 import ( 4 "context" 5 "crypto/tls" 6 "fmt" 7 "net/http" 8 9 routev1 "github.com/openshift/api/route/v1" 10 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 11 "k8s.io/apimachinery/pkg/types" 12 "k8s.io/apimachinery/pkg/util/wait" 13 ) 14 15 // GetOpenshiftRoute returns the route for a given component name 16 func (h *SuiteController) GetOpenshiftRoute(routeName string, routeNamespace string) (*routev1.Route, error) { 17 namespacedName := types.NamespacedName{ 18 Name: routeName, 19 Namespace: routeNamespace, 20 } 21 22 route := &routev1.Route{} 23 err := h.KubeRest().Get(context.TODO(), namespacedName, route) 24 if err != nil { 25 return &routev1.Route{}, err 26 } 27 return route, nil 28 } 29 30 // GetOpenshiftRouteByComponentName returns a route associated with the given component 31 // Routes that belong to a given component will have the following label: 'app.kubernetes.io/name: <component-name>' 32 func (h *SuiteController) GetOpenshiftRouteByComponentName(componentName string, componentNamespace string) (*routev1.Route, error) { 33 listOptions := metav1.ListOptions{ 34 LabelSelector: fmt.Sprintf("app.kubernetes.io/name=%s", componentName), 35 } 36 routeList, err := h.CustomClient.RouteClient().RouteV1().Routes(componentNamespace).List(context.Background(), listOptions) 37 if err != nil { 38 return &routev1.Route{}, err 39 } 40 if len(routeList.Items) == 0 { 41 return &routev1.Route{}, fmt.Errorf("unable to find routes with label %v:%v", "app.kubernetes.io/name", componentName) 42 } 43 return &routeList.Items[0], nil 44 } 45 46 func (h *SuiteController) RouteHostnameIsAccessible(routeName, namespace string) wait.ConditionFunc { 47 return func() (bool, error) { 48 namespacedName := types.NamespacedName{ 49 Name: routeName, 50 Namespace: namespace, 51 } 52 route := &routev1.Route{} 53 if err := h.KubeRest().Get(context.TODO(), namespacedName, route); err != nil { 54 return false, nil 55 } 56 57 tr := &http.Transport{ 58 TLSClientConfig: &tls.Config{ 59 InsecureSkipVerify: true, 60 }, 61 } 62 client := http.Client{Transport: tr} 63 res, err := client.Get("https://" + route.Spec.Host) 64 if err != nil || res.StatusCode > 299 { 65 return false, nil 66 } 67 68 return true, nil 69 } 70 }