2018-07-02 13:34:04 +02:00
|
|
|
package chartserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2018-07-11 11:31:34 +02:00
|
|
|
"fmt"
|
2018-07-02 13:34:04 +02:00
|
|
|
"net/url"
|
2018-07-11 11:31:34 +02:00
|
|
|
"os"
|
|
|
|
|
2018-08-23 09:02:20 +02:00
|
|
|
hlog "github.com/goharbor/harbor/src/common/utils/log"
|
2018-07-11 11:31:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
userName = "chart_controller"
|
2018-09-12 08:38:29 +02:00
|
|
|
passwordKey = "CORE_SECRET"
|
2018-07-02 13:34:04 +02:00
|
|
|
)
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// Credential keeps the username and password for the basic auth
|
2018-07-11 11:31:34 +02:00
|
|
|
type Credential struct {
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// Controller is used to handle flows of related requests based on the corresponding handlers
|
|
|
|
// A reverse proxy will be created and managed to proxy the related traffics between API and
|
|
|
|
// backend chart server
|
2018-07-02 13:34:04 +02:00
|
|
|
type Controller struct {
|
2018-09-19 08:45:52 +02:00
|
|
|
// Proxy used to to transfer the traffic of requests
|
|
|
|
// It's mainly used to talk to the backend chart server
|
|
|
|
trafficProxy *ProxyEngine
|
2018-07-02 13:34:04 +02:00
|
|
|
|
2018-09-19 08:45:52 +02:00
|
|
|
// Parse and process the chart version to provide required info data
|
|
|
|
chartOperator *ChartOperator
|
2018-07-02 13:34:04 +02:00
|
|
|
|
2018-09-19 08:45:52 +02:00
|
|
|
// HTTP client used to call the realted APIs of the backend chart repositories
|
|
|
|
apiClient *ChartClient
|
2018-07-02 13:34:04 +02:00
|
|
|
|
2018-09-19 08:45:52 +02:00
|
|
|
// The access endpoint of the backend chart repository server
|
|
|
|
backendServerAddress *url.URL
|
2018-08-03 11:50:03 +02:00
|
|
|
|
2018-09-19 08:45:52 +02:00
|
|
|
// Cache the chart data
|
|
|
|
chartCache *ChartCache
|
2018-07-02 13:34:04 +02:00
|
|
|
}
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// NewController is constructor of the chartserver.Controller
|
2018-07-02 13:34:04 +02:00
|
|
|
func NewController(backendServer *url.URL) (*Controller, error) {
|
|
|
|
if backendServer == nil {
|
|
|
|
return nil, errors.New("failed to create chartserver.Controller: backend sever address is required")
|
|
|
|
}
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// Try to create credential
|
2018-07-11 11:31:34 +02:00
|
|
|
cred := &Credential{
|
|
|
|
Username: userName,
|
|
|
|
Password: os.Getenv(passwordKey),
|
|
|
|
}
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// Creat cache
|
2018-07-11 11:31:34 +02:00
|
|
|
cacheCfg, err := getCacheConfig()
|
|
|
|
if err != nil {
|
2018-09-05 10:16:31 +02:00
|
|
|
// just log the error
|
|
|
|
// will not break the whole flow if failed to create cache
|
2018-07-11 11:31:34 +02:00
|
|
|
hlog.Errorf("failed to get cache configuration with error: %s", err)
|
|
|
|
}
|
|
|
|
cache := NewChartCache(cacheCfg)
|
|
|
|
if !cache.IsEnabled() {
|
|
|
|
hlog.Info("No cache is enabled for chart caching")
|
|
|
|
}
|
2018-07-08 05:17:55 +02:00
|
|
|
|
2018-07-02 13:34:04 +02:00
|
|
|
return &Controller{
|
2018-09-19 08:45:52 +02:00
|
|
|
backendServerAddress: backendServer,
|
|
|
|
// Use customized reverse proxy
|
|
|
|
trafficProxy: NewProxyEngine(backendServer, cred),
|
|
|
|
// Initialize chart operator for use
|
|
|
|
chartOperator: &ChartOperator{},
|
|
|
|
// Create http client with customized timeouts
|
|
|
|
apiClient: NewChartClient(cred),
|
|
|
|
chartCache: cache,
|
2018-07-02 13:34:04 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-09-19 08:45:52 +02:00
|
|
|
// APIPrefix returns the API prefix path of calling backend chart service.
|
|
|
|
func (c *Controller) APIPrefix(namespace string) string {
|
|
|
|
return fmt.Sprintf("%s/api/%s/charts", c.backendServerAddress.String(), namespace)
|
2018-07-11 11:31:34 +02:00
|
|
|
}
|