1
1
package utils
2
2
3
3
import (
4
+ "context"
5
+ "fmt"
6
+ "io"
7
+ "net/url"
4
8
"os/exec"
9
+ "strings"
5
10
"testing"
11
+
12
+ "k8s.io/client-go/kubernetes"
13
+
14
+ catalogdv1 "github.com/operator-framework/operator-controller/catalogd/api/v1"
6
15
)
7
16
8
17
// FindK8sClient returns the first available Kubernetes CLI client from the system,
@@ -21,3 +30,40 @@ func FindK8sClient(t *testing.T) string {
21
30
t .Fatal ("k8s client not found" )
22
31
return ""
23
32
}
33
+
34
+ func ReadTestCatalogServerContents (ctx context.Context , catalog * catalogdv1.ClusterCatalog , kubeClient kubernetes.Interface ) ([]byte , error ) {
35
+ if catalog == nil {
36
+ return nil , fmt .Errorf ("cannot read nil catalog" )
37
+ }
38
+ if catalog .Status .URLs == nil {
39
+ return nil , fmt .Errorf ("catalog %q has no catalog urls" , catalog .Name )
40
+ }
41
+ url , err := url .Parse (catalog .Status .URLs .Base )
42
+ if err != nil {
43
+ return nil , fmt .Errorf ("error parsing clustercatalog url %q: %v" , catalog .Status .URLs .Base , err )
44
+ }
45
+ // url is expected to be in the format of
46
+ // http://{service_name}.{namespace}.svc/catalogs/{catalog_name}/
47
+ // so to get the namespace and name of the service we grab only
48
+ // the hostname and split it on the '.' character
49
+ ns := strings .Split (url .Hostname (), "." )[1 ]
50
+ name := strings .Split (url .Hostname (), "." )[0 ]
51
+ port := url .Port ()
52
+ // the ProxyGet() call below needs an explicit port value, so if
53
+ // value from url.Port() is empty, we assume port 443.
54
+ if port == "" {
55
+ if url .Scheme == "https" {
56
+ port = "443"
57
+ } else {
58
+ port = "80"
59
+ }
60
+ }
61
+ resp := kubeClient .CoreV1 ().Services (ns ).ProxyGet (url .Scheme , name , port , url .JoinPath ("api" , "v1" , "all" ).Path , map [string ]string {})
62
+ rc , err := resp .Stream (ctx )
63
+ if err != nil {
64
+ return nil , err
65
+ }
66
+ defer rc .Close ()
67
+
68
+ return io .ReadAll (rc )
69
+ }
0 commit comments