2018-07-06 14:53:13 +02:00
|
|
|
package chartserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2018-07-11 11:31:34 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
2018-09-19 08:45:52 +02:00
|
|
|
"os"
|
2018-07-11 11:31:34 +02:00
|
|
|
"strings"
|
2018-07-06 14:53:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
contentTypeHeader = "content-type"
|
|
|
|
contentTypeJSON = "application/json"
|
|
|
|
)
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// Extract error object '{"error": "****---***"}' from the content if existing
|
|
|
|
// nil error will be returned if it does exist
|
2018-07-23 11:41:40 +02:00
|
|
|
func extractError(content []byte) error {
|
|
|
|
if len(content) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
errorObj := make(map[string]string)
|
|
|
|
err := json.Unmarshal(content, &errorObj)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if errText, ok := errorObj["error"]; ok {
|
|
|
|
return errors.New(errText)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// Parse the redis configuration to the beego cache pattern
|
|
|
|
// Config pattern is "address:port[,weight,password,db_index]"
|
2018-07-11 11:31:34 +02:00
|
|
|
func parseRedisConfig(redisConfigV string) (string, error) {
|
|
|
|
if len(redisConfigV) == 0 {
|
|
|
|
return "", errors.New("empty redis config")
|
|
|
|
}
|
|
|
|
|
|
|
|
redisConfig := make(map[string]string)
|
|
|
|
redisConfig["key"] = cacheCollectionName
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// Try best to parse the configuration segments.
|
|
|
|
// If the related parts are missing, assign default value.
|
|
|
|
// The default database index for UI process is 0.
|
2018-08-23 07:59:13 +02:00
|
|
|
configSegments := strings.Split(redisConfigV, ",")
|
|
|
|
for i, segment := range configSegments {
|
|
|
|
if i > 3 {
|
2018-09-05 10:16:31 +02:00
|
|
|
// ignore useless segments
|
2018-08-23 07:59:13 +02:00
|
|
|
break
|
2018-07-11 11:31:34 +02:00
|
|
|
}
|
|
|
|
|
2018-08-23 07:59:13 +02:00
|
|
|
switch i {
|
2018-09-05 10:16:31 +02:00
|
|
|
// address:port
|
2018-08-23 07:59:13 +02:00
|
|
|
case 0:
|
|
|
|
redisConfig["conn"] = segment
|
2018-09-05 10:16:31 +02:00
|
|
|
// password, may not exist
|
2018-08-23 07:59:13 +02:00
|
|
|
case 2:
|
|
|
|
redisConfig["password"] = segment
|
2018-09-05 10:16:31 +02:00
|
|
|
// database index, may not exist
|
2018-08-23 07:59:13 +02:00
|
|
|
case 3:
|
|
|
|
redisConfig["dbNum"] = segment
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// Assign default value
|
2018-08-23 07:59:13 +02:00
|
|
|
if len(redisConfig["dbNum"]) == 0 {
|
2018-07-11 11:31:34 +02:00
|
|
|
redisConfig["dbNum"] = "0"
|
|
|
|
}
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// Try to validate the connection address
|
2018-07-11 11:31:34 +02:00
|
|
|
fullAddr := redisConfig["conn"]
|
|
|
|
if strings.Index(fullAddr, "://") == -1 {
|
2018-09-05 10:16:31 +02:00
|
|
|
// Append schema
|
2018-07-11 11:31:34 +02:00
|
|
|
fullAddr = fmt.Sprintf("redis://%s", fullAddr)
|
|
|
|
}
|
2018-09-05 10:16:31 +02:00
|
|
|
// Validate it by url
|
2018-07-11 11:31:34 +02:00
|
|
|
_, err := url.Parse(fullAddr)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// Convert config map to string
|
2018-07-11 11:31:34 +02:00
|
|
|
cfgData, err := json.Marshal(redisConfig)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(cfgData), nil
|
|
|
|
}
|
2018-09-19 08:45:52 +02:00
|
|
|
|
|
|
|
// What's the cache driver if it is set
|
|
|
|
func parseCacheDriver() (string, bool) {
|
|
|
|
driver, ok := os.LookupEnv(cacheDriverENVKey)
|
|
|
|
return strings.ToLower(driver), ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get and parse the configuration for the chart cache
|
|
|
|
func getCacheConfig() (*ChartCacheConfig, error) {
|
|
|
|
driver, isSet := parseCacheDriver()
|
|
|
|
if !isSet {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if driver != cacheDriverMem && driver != cacheDriverRedis {
|
|
|
|
return nil, fmt.Errorf("cache driver '%s' is not supported, only support 'memory' and 'redis'", driver)
|
|
|
|
}
|
|
|
|
|
|
|
|
if driver == cacheDriverMem {
|
|
|
|
return &ChartCacheConfig{
|
|
|
|
DriverType: driver,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
redisConfigV := os.Getenv(redisENVKey)
|
|
|
|
redisCfg, err := parseRedisConfig(redisConfigV)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse redis configurations from '%s' with error: %s", redisCfg, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ChartCacheConfig{
|
|
|
|
DriverType: driver,
|
|
|
|
Config: redisCfg,
|
|
|
|
}, nil
|
|
|
|
}
|