/
streamer.go
64 lines (55 loc) · 1.67 KB
/
streamer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package stream
import (
"context"
"fmt"
"io"
"net/url"
"strings"
"github.com/operator-framework/catalogd/api/core/v1alpha1"
"k8s.io/apimachinery/pkg/api/meta"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
)
type CatalogContentStreamer interface {
StreamCatalogContents(ctx context.Context, catalog v1alpha1.Catalog) (io.ReadCloser, error)
}
type instance struct {
client corev1.CoreV1Interface
}
func New(client corev1.CoreV1Interface) CatalogContentStreamer {
return &instance{
client: client,
}
}
func (c *instance) StreamCatalogContents(ctx context.Context, catalog v1alpha1.Catalog) (io.ReadCloser, error) {
if !meta.IsStatusConditionTrue(catalog.Status.Conditions, v1alpha1.TypeUnpacked) {
return nil, fmt.Errorf("catalog %q is not unpacked", catalog.Name)
}
url, err := url.Parse(catalog.Status.ContentURL)
if err != nil {
return nil, fmt.Errorf("parsing catalog content url for catalog %q: %w", catalog.Name, err)
}
// url is expected to be in the format of
// http://{service_name}.{namespace}.svc/{catalog_name}/all.json
// so to get the namespace and name of the service we grab only
// the hostname and split it on the '.' character
ns := strings.Split(url.Hostname(), ".")[1]
name := strings.Split(url.Hostname(), ".")[0]
port := url.Port()
// the ProxyGet() call below needs an explicit port value, so if
// value from url.Port() is empty, we assume port 80.
if port == "" {
port = "80"
}
rw := c.client.Services(ns).ProxyGet(
url.Scheme,
name,
port,
url.Path,
map[string]string{},
)
rc, err := rw.Stream(ctx)
if err != nil {
return nil, fmt.Errorf("getting catalog contents for catalog %q: %w", catalog.Name, err)
}
return rc, nil
}