/
general.go
69 lines (54 loc) · 1.38 KB
/
general.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
65
66
67
68
69
package commands
import (
"crypto/tls"
"net"
"net/http"
"net/url"
"os"
"time"
"github.com/openfaas/faas-cli/proxy"
"github.com/openfaas/go-sdk"
)
var (
commandTimeout = 60 * time.Second
)
func GetDefaultCLITransport(tlsInsecure bool, timeout *time.Duration) *http.Transport {
if timeout != nil || tlsInsecure {
tr := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DisableKeepAlives: false,
}
if timeout != nil {
tr.DialContext = (&net.Dialer{
Timeout: *timeout,
}).DialContext
tr.IdleConnTimeout = 120 * time.Millisecond
tr.ExpectContinueTimeout = 1500 * time.Millisecond
}
if tlsInsecure {
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: tlsInsecure}
}
tr.DisableKeepAlives = false
return tr
}
return nil
}
func GetDefaultSDKClient() (*sdk.Client, error) {
gatewayAddress := getGatewayURL(gateway, defaultGateway, "", os.Getenv(openFaaSURLEnvironment))
gatewayURL, err := url.Parse(gatewayAddress)
if err != nil {
return nil, err
}
transport := GetDefaultCLITransport(tlsInsecure, &commandTimeout)
httpClient := &http.Client{}
httpClient.Timeout = commandTimeout
if transport != nil {
httpClient.Transport = transport
}
clientAuth, err := proxy.NewCLIAuth(token, gatewayAddress)
if err != nil {
return nil, err
}
client := sdk.NewClient(gatewayURL, clientAuth, http.DefaultClient)
return client, nil
}