make subnet subsys dynamic and simplify callhome (#15927)

This commit is contained in:
Harshavardhana
2022-10-27 00:20:01 -07:00
committed by GitHub
parent 86420a1f46
commit ec77d28e62
7 changed files with 136 additions and 60 deletions

View File

@@ -18,6 +18,7 @@
package callhome
import (
"sync"
"time"
"github.com/minio/minio/internal/config"
@@ -42,6 +43,9 @@ var DefaultKVS = config.KVS{
},
}
// callhomeCycleDefault is the default interval between two callhome cycles (24hrs)
const callhomeCycleDefault = 24 * time.Hour
// Config represents the subnet related configuration
type Config struct {
// Flag indicating whether callhome is enabled.
@@ -51,6 +55,37 @@ type Config struct {
Frequency time.Duration `json:"frequency"`
}
var configLock sync.RWMutex
// Enabled - indicates if callhome is enabled or not
func (c *Config) Enabled() bool {
configLock.RLock()
defer configLock.RUnlock()
return c.Enable
}
// FrequencyDur - returns the currently configured callhome frequency
func (c *Config) FrequencyDur() time.Duration {
configLock.RLock()
defer configLock.RUnlock()
if c.Frequency == 0 {
return callhomeCycleDefault
}
return c.Frequency
}
// Update updates new callhome frequency
func (c *Config) Update(ncfg Config) {
configLock.Lock()
defer configLock.Unlock()
c.Enable = ncfg.Enable
c.Frequency = ncfg.Frequency
}
// LookupConfig - lookup config and override with valid environment settings if any.
func LookupConfig(kvs config.KVS) (cfg Config, err error) {
if err = config.CheckValidKeys(config.CallhomeSubSys, kvs, DefaultKVS); err != nil {

View File

@@ -20,7 +20,9 @@ package subnet
import (
"net/http"
"net/url"
"os"
"strings"
"sync"
"github.com/minio/minio/internal/config"
"github.com/minio/pkg/env"
@@ -49,24 +51,62 @@ type Config struct {
License string `json:"license"`
// The subnet api key
APIKey string `json:"api_key"`
APIKey string `json:"apiKey"`
// The HTTP(S) proxy URL to use for connecting to SUBNET
ProxyURL *xnet.URL `json:"proxy_url"`
Proxy string `json:"proxy"`
// Transport configured with proxy_url if set optionally.
transport http.RoundTripper
}
var configLock sync.RWMutex
// Registered indicates if cluster is registered or not
func (c *Config) Registered() bool {
configLock.RLock()
defer configLock.RUnlock()
return len(c.APIKey) > 0
}
// ApplyEnv - applies the current subnet config to Console UI specific environment variables.
func (c *Config) ApplyEnv() {
configLock.RLock()
defer configLock.RUnlock()
if c.License != "" {
os.Setenv("CONSOLE_SUBNET_LICENSE", c.License)
}
if c.APIKey != "" {
os.Setenv("CONSOLE_SUBNET_API_KEY", c.APIKey)
}
if c.Proxy != "" {
os.Setenv("CONSOLE_SUBNET_PROXY", c.Proxy)
}
}
// Update - in-place update with new license and registration information.
func (c *Config) Update(ncfg Config) {
configLock.Lock()
defer configLock.Unlock()
c.License = ncfg.License
c.APIKey = ncfg.APIKey
c.Proxy = ncfg.Proxy
c.transport = ncfg.transport
}
// LookupConfig - lookup config and override with valid environment settings if any.
func LookupConfig(kvs config.KVS, transport http.RoundTripper) (cfg Config, err error) {
if err = config.CheckValidKeys(config.SubnetSubSys, kvs, DefaultKVS); err != nil {
return cfg, err
}
var proxyURL *xnet.URL
proxy := env.Get(config.EnvMinIOSubnetProxy, kvs.Get(config.Proxy))
if len(proxy) > 0 {
cfg.ProxyURL, err = xnet.ParseHTTPURL(proxy)
proxyURL, err = xnet.ParseHTTPURL(proxy)
if err != nil {
return cfg, err
}
@@ -75,6 +115,7 @@ func LookupConfig(kvs config.KVS, transport http.RoundTripper) (cfg Config, err
cfg.License = strings.TrimSpace(env.Get(config.EnvMinIOSubnetLicense, kvs.Get(config.License)))
cfg.APIKey = strings.TrimSpace(env.Get(config.EnvMinIOSubnetAPIKey, kvs.Get(config.APIKey)))
cfg.Proxy = proxy
if transport == nil {
// when transport is nil, it means we are just validating the
@@ -83,9 +124,9 @@ func LookupConfig(kvs config.KVS, transport http.RoundTripper) (cfg Config, err
}
// Make sure to clone the transport before editing the ProxyURL
if cfg.ProxyURL != nil {
if proxyURL != nil {
ctransport := transport.(*http.Transport).Clone()
ctransport.Proxy = http.ProxyURL((*url.URL)(cfg.ProxyURL))
ctransport.Proxy = http.ProxyURL((*url.URL)(proxyURL))
cfg.transport = ctransport
} else {
cfg.transport = transport

View File

@@ -35,8 +35,8 @@ const (
// Post submit 'payload' to specified URL
func (c Config) Post(reqURL string, payload interface{}) (string, error) {
if len(c.APIKey) == 0 {
return "", errors.New("Deployment is not registered with SUBNET. Please register the deployment via 'mc support register ALIAS'")
if !c.Registered() {
return "", errors.New("Deployment is not registered with SUBNET. Please register the deployment via 'mc license register ALIAS'")
}
body, err := json.Marshal(payload)
if err != nil {
@@ -47,7 +47,10 @@ func (c Config) Post(reqURL string, payload interface{}) (string, error) {
return "", err
}
configLock.RLock()
r.Header.Set("Authorization", c.APIKey)
configLock.RUnlock()
r.Header.Set("Content-Type", "application/json")
client := &http.Client{