/
client.go
55 lines (45 loc) · 1.24 KB
/
client.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
package cycloneserver
import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
websocketutil "github.com/caicloud/cyclone/pkg/util/websocket"
)
const (
cycloneAPIVersion = "/apis/v1alpha1"
apiPathForLogStream = "/workflowruns/%s/streamlogs"
)
// Client ...
type Client interface {
PushLogStream(ns, workflowrun, stage, container string, reader io.Reader, close <-chan struct{}) error
}
type client struct {
baseURL string
client *http.Client
}
// NewClient ...
func NewClient(cycloneServer string) Client {
baseURL := strings.TrimRight(cycloneServer, "/")
if !strings.Contains(baseURL, "://") {
baseURL = "http://" + baseURL
}
return &client{
baseURL: baseURL,
client: http.DefaultClient,
}
}
// PushLogStream ...
func (c *client) PushLogStream(ns, workflowrun, stage, container string, reader io.Reader, close <-chan struct{}) error {
path := fmt.Sprintf(apiPathForLogStream, workflowrun)
host := strings.TrimPrefix(c.baseURL, "http://")
host = strings.TrimPrefix(host, "https://")
requestURL := url.URL{
Host: host,
Path: cycloneAPIVersion + path,
RawQuery: fmt.Sprintf("namespace=%s&stage=%s&container=%s", ns, stage, container),
Scheme: "ws",
}
return websocketutil.SendStream(requestURL.String(), reader, close)
}