-
Notifications
You must be signed in to change notification settings - Fork 18
/
client.go
117 lines (99 loc) · 2.49 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package harbor
import (
"crypto/tls"
"net"
"net/http"
"net/url"
"time"
"github.com/go-openapi/runtime"
httptransport "github.com/go-openapi/runtime/client"
v2client "github.com/goharbor/go-client/pkg/sdk/v2.0/client"
)
// InsecureTransport provides a insecure RoundTripper and disable the HTTP2 try.
var InsecureTransport http.RoundTripper = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second, // nolint:gomnd
KeepAlive: 30 * time.Second, // nolint:gomnd
DualStack: true,
}).DialContext,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true, // nolint:gosec
},
MaxIdleConns: 100, // nolint:gomnd
IdleConnTimeout: 90 * time.Second, // nolint:gomnd
TLSHandshakeTimeout: 10 * time.Second, // nolint:gomnd
ExpectContinueTimeout: 1 * time.Second,
}
// Config contains configs for constructing a client
type Config struct {
// URL is the base URL of the upstream server
URL *url.URL
// Transport is an inner transport for the client
Transport http.RoundTripper
// AuthInfo is for authentication
AuthInfo runtime.ClientAuthInfoWriter
}
// ClientSetConfig contains config for creating a ClientSet
type ClientSetConfig struct {
URL string
Insecure bool
Username string
Password string
}
// ClientSet contains clients for V2
type ClientSet struct {
v2Client *v2client.HarborAPI
}
// ToV2Config convert the Config to v2client's Config
func (c *Config) ToV2Config() v2client.Config {
var (
u url.URL = url.URL{}
t http.RoundTripper = c.Transport
a runtime.ClientAuthInfoWriter = c.AuthInfo
)
if c.URL != nil {
u = *c.URL
}
if len(u.Host) == 0 {
u.Host = v2client.DefaultHost
}
if len(u.Path) == 0 {
u.Path = v2client.DefaultBasePath
}
if len(u.Scheme) == 0 {
// default to https if not set
u.Scheme = v2client.DefaultSchemes[1]
}
if t == nil {
t = InsecureTransport
}
return v2client.Config{
URL: &u,
Transport: t,
AuthInfo: a,
}
}
func NewClientSet(csc *ClientSetConfig) (*ClientSet, error) {
if csc == nil {
csc = &ClientSetConfig{}
}
u, err := url.Parse(csc.URL)
if err != nil {
return nil, err
}
c := &Config{
URL: u,
AuthInfo: httptransport.BasicAuth(csc.Username, csc.Password),
}
if csc.Insecure {
c.Transport = InsecureTransport
}
cs := &ClientSet{}
cs.v2Client = v2client.New(c.ToV2Config())
return cs, nil
}
// V2 return V2Client
func (c *ClientSet) V2() *v2client.HarborAPI {
return c.v2Client
}