github.com/goharbor/go-client@v0.210.0/pkg/harbor/client.go (about)

     1  package harbor
     2  
     3  import (
     4  	"crypto/tls"
     5  	"net"
     6  	"net/http"
     7  	"net/url"
     8  	"time"
     9  
    10  	"github.com/go-openapi/runtime"
    11  	httptransport "github.com/go-openapi/runtime/client"
    12  	v2client "github.com/goharbor/go-client/pkg/sdk/v2.0/client"
    13  )
    14  
    15  // InsecureTransport provides a insecure RoundTripper and disable the HTTP2 try.
    16  var InsecureTransport http.RoundTripper = &http.Transport{
    17  	Proxy: http.ProxyFromEnvironment,
    18  	DialContext: (&net.Dialer{
    19  		Timeout:   30 * time.Second, // nolint:gomnd
    20  		KeepAlive: 30 * time.Second, // nolint:gomnd
    21  		DualStack: true,
    22  	}).DialContext,
    23  	TLSClientConfig: &tls.Config{
    24  		InsecureSkipVerify: true, // nolint:gosec
    25  	},
    26  	MaxIdleConns:          100,              // nolint:gomnd
    27  	IdleConnTimeout:       90 * time.Second, // nolint:gomnd
    28  	TLSHandshakeTimeout:   10 * time.Second, // nolint:gomnd
    29  	ExpectContinueTimeout: 1 * time.Second,
    30  }
    31  
    32  // Config contains configs for constructing a client
    33  type Config struct {
    34  	// URL is the base URL of the upstream server
    35  	URL *url.URL
    36  	// Transport is an inner transport for the client
    37  	Transport http.RoundTripper
    38  	// AuthInfo is for authentication
    39  	AuthInfo runtime.ClientAuthInfoWriter
    40  }
    41  
    42  // ClientSetConfig contains config for creating a ClientSet
    43  type ClientSetConfig struct {
    44  	URL      string
    45  	Insecure bool
    46  	Username string
    47  	Password string
    48  }
    49  
    50  // ClientSet contains clients for V2
    51  type ClientSet struct {
    52  	v2Client *v2client.HarborAPI
    53  }
    54  
    55  // ToV2Config convert the Config to v2client's Config
    56  func (c *Config) ToV2Config() v2client.Config {
    57  	var (
    58  		u url.URL                      = url.URL{}
    59  		t http.RoundTripper            = c.Transport
    60  		a runtime.ClientAuthInfoWriter = c.AuthInfo
    61  	)
    62  
    63  	if c.URL != nil {
    64  		u = *c.URL
    65  	}
    66  
    67  	if len(u.Host) == 0 {
    68  		u.Host = v2client.DefaultHost
    69  	}
    70  	if len(u.Path) == 0 {
    71  		u.Path = v2client.DefaultBasePath
    72  	}
    73  	if len(u.Scheme) == 0 {
    74  		// default to https if not set
    75  		u.Scheme = v2client.DefaultSchemes[1]
    76  	}
    77  
    78  	if t == nil {
    79  		t = InsecureTransport
    80  	}
    81  
    82  	return v2client.Config{
    83  		URL:       &u,
    84  		Transport: t,
    85  		AuthInfo:  a,
    86  	}
    87  }
    88  
    89  func NewClientSet(csc *ClientSetConfig) (*ClientSet, error) {
    90  	if csc == nil {
    91  		csc = &ClientSetConfig{}
    92  	}
    93  
    94  	u, err := url.Parse(csc.URL)
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  
    99  	c := &Config{
   100  		URL:      u,
   101  		AuthInfo: httptransport.BasicAuth(csc.Username, csc.Password),
   102  	}
   103  
   104  	if csc.Insecure {
   105  		c.Transport = InsecureTransport
   106  	}
   107  
   108  	cs := &ClientSet{}
   109  	cs.v2Client = v2client.New(c.ToV2Config())
   110  
   111  	return cs, nil
   112  }
   113  
   114  // V2 return V2Client
   115  func (c *ClientSet) V2() *v2client.HarborAPI {
   116  	return c.v2Client
   117  }