github.com/redhat-appstudio/e2e-tests@v0.0.0-20240520140907-9709f6f59323/pkg/clients/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.Background(), 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 string, 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.Background(), 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 } 71 72 // Checks that the deployed route endpoint is actually reachable and returns 200 73 func (h *SuiteController) RouteEndpointIsAccessible(route *routev1.Route, endpoint string) error { 74 if len(route.Spec.Host) > 0 { 75 protocol := "https" 76 // Insecure route -> use http 77 if route.Spec.TLS == nil { 78 protocol = "http" 79 } 80 routeUrl := protocol + "://" + route.Spec.Host + endpoint 81 82 http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} 83 resp, err := http.Get(routeUrl) 84 if err != nil { 85 return err 86 } 87 if resp.StatusCode != 200 { 88 return fmt.Errorf("route responded with '%d' status code", resp.StatusCode) 89 } 90 } else { 91 return fmt.Errorf("route is invalid: '%s'", route.Spec.Host) 92 } 93 94 return nil 95 }