Add new site config sub-system intended to replace region (#13672)

- New sub-system has "region" and "name" fields.

- `region` subsystem is marked as deprecated, however still works, unless the
new region parameter under `site` is set - in this case, the region subsystem is
ignored. `region` subsystem is hidden from top-level help (i.e. from `mc admin
config set myminio`), but appears when specifically requested (i.e. with `mc
admin config set myminio region`).

- MINIO_REGION, MINIO_REGION_NAME are supported as legacy environment variables for server region.

- Adds MINIO_SITE_REGION as the current environment variable to configure the
server region and MINIO_SITE_NAME for the site name.
This commit is contained in:
Aditya Manthramurthy
2021-11-25 13:06:25 -08:00
committed by GitHub
parent 81bf0c66c6
commit 4ce6d35e30
27 changed files with 197 additions and 82 deletions

View File

@@ -55,6 +55,8 @@ const (
EnableOn = madmin.EnableOn
EnableOff = madmin.EnableOff
RegionKey = "region"
NameKey = "name"
RegionName = "name"
AccessKey = "access_key"
SecretKey = "secret_key"
@@ -69,6 +71,7 @@ const (
IdentityLDAPSubSys = "identity_ldap"
IdentityTLSSubSys = "identity_tls"
CacheSubSys = "cache"
SiteSubSys = "site"
RegionSubSys = "region"
EtcdSubSys = "etcd"
StorageClassSubSys = "storage_class"
@@ -104,6 +107,7 @@ const (
// SubSystems - all supported sub-systems
var SubSystems = set.CreateStringSet(
CredentialsSubSys,
SiteSubSys,
RegionSubSys,
EtcdSubSys,
CacheSubSys,
@@ -144,6 +148,7 @@ var SubSystemsDynamic = set.CreateStringSet(
// SubSystemsSingleTargets - subsystems which only support single target.
var SubSystemsSingleTargets = set.CreateStringSet([]string{
CredentialsSubSys,
SiteSubSys,
RegionSubSys,
EtcdSubSys,
CacheSubSys,
@@ -202,6 +207,19 @@ func RegisterHelpSubSys(helpKVSMap map[string]HelpKVS) {
}
}
// HelpDeprecatedSubSysMap - help for all deprecated sub-systems, that may be
// removed in the future.
var HelpDeprecatedSubSysMap map[string]HelpKV
// RegisterHelpDeprecatedSubSys - saves input help KVS for deprecated
// sub-systems globally. Should be called only once at init.
func RegisterHelpDeprecatedSubSys(helpDeprecatedKVMap map[string]HelpKV) {
HelpDeprecatedSubSysMap = map[string]HelpKV{}
for k, v := range helpDeprecatedKVMap {
HelpDeprecatedSubSysMap[k] = v
}
}
// KV - is a shorthand of each key value.
type KV struct {
Key string `json:"key"`
@@ -449,6 +467,17 @@ var (
},
}
DefaultSiteKVS = KVS{
KV{
Key: NameKey,
Value: "",
},
KV{
Key: RegionKey,
Value: "",
},
}
DefaultRegionKVS = KVS{
KV{
Key: RegionName,
@@ -471,26 +500,66 @@ func LookupCreds(kv KVS) (auth.Credentials, error) {
return auth.CreateCredentials(accessKey, secretKey)
}
// Site - holds site info - name and region.
type Site struct {
Name string
Region string
}
var validRegionRegex = regexp.MustCompile("^[a-zA-Z][a-zA-Z0-9-_-]+$")
// LookupRegion - get current region.
func LookupRegion(kv KVS) (string, error) {
if err := CheckValidKeys(RegionSubSys, kv, DefaultRegionKVS); err != nil {
return "", err
// validSiteNameRegex - allows lowercase letters, digits and '-', starts with
// letter. At least 2 characters long.
var validSiteNameRegex = regexp.MustCompile("^[a-z][a-z0-9-]+$")
// LookupSite - get site related configuration. Loads configuration from legacy
// region sub-system as well.
func LookupSite(siteKV KVS, regionKV KVS) (s Site, err error) {
if err = CheckValidKeys(SiteSubSys, siteKV, DefaultSiteKVS); err != nil {
return
}
region := env.Get(EnvRegion, "")
if region == "" {
region = env.Get(EnvRegionName, kv.Get(RegionName))
env.Get(EnvRegionName, "")
}
if region == "" {
region = env.Get(EnvSiteRegion, siteKV.Get(RegionKey))
}
if region == "" {
// No region config found in the site-subsystem. So lookup the legacy
// region sub-system.
if err = CheckValidKeys(RegionSubSys, regionKV, DefaultRegionKVS); err != nil {
// An invalid key was found in the region sub-system.
// Since the region sub-system cannot be (re)set as it
// is legacy, we return an error to tell the user to
// reset the region via the new command.
err = Errorf("could not load region from legacy configuration as it was invalid - use 'mc admin config set myminio site region=myregion name=myname' to set a region and name (%v)", err)
return
}
region = regionKV.Get(RegionName)
}
if region != "" {
if validRegionRegex.MatchString(region) {
return region, nil
if !validRegionRegex.MatchString(region) {
err = Errorf(
"region '%s' is invalid, expected simple characters such as [us-east-1, myregion...]",
region)
return
}
return "", Errorf(
"region '%s' is invalid, expected simple characters such as [us-east-1, myregion...]",
region)
s.Region = region
}
return "", nil
name := env.Get(EnvSiteName, siteKV.Get(NameKey))
if name != "" {
if !validSiteNameRegex.MatchString(name) {
err = Errorf(
"site name '%s' is invalid, expected simple characters such as [cal-rack0, myname...]",
name)
return
}
s.Name = name
}
return
}
// CheckValidKeys - checks if inputs KVS has the necessary keys,
@@ -626,9 +695,14 @@ func (c Config) GetKVS(s string, defaultKVS map[string]KVS) (Targets, error) {
KVS: kvs,
})
} else {
hkvs := HelpSubSysMap[""]
// Use help for sub-system to preserve the order.
for _, hkv := range hkvs {
// Use help for sub-system to preserve the order. Add deprecated
// keys at the end (in some order).
kvsOrder := append([]HelpKV{}, HelpSubSysMap[""]...)
for _, v := range HelpDeprecatedSubSysMap {
kvsOrder = append(kvsOrder, v)
}
for _, hkv := range kvsOrder {
if !strings.HasPrefix(hkv.Key, subSysPrefix) {
continue
}

View File

@@ -31,12 +31,14 @@ const (
EnvBrowser = "MINIO_BROWSER"
EnvDomain = "MINIO_DOMAIN"
EnvRegionName = "MINIO_REGION_NAME"
EnvPublicIPs = "MINIO_PUBLIC_IPS"
EnvFSOSync = "MINIO_FS_OSYNC"
EnvArgs = "MINIO_ARGS"
EnvDNSWebhook = "MINIO_DNS_WEBHOOK_ENDPOINT"
EnvSiteName = "MINIO_SITE_NAME"
EnvSiteRegion = "MINIO_SITE_REGION"
EnvMinIOSubnetLicense = "MINIO_SUBNET_LICENSE"
EnvMinIOServerURL = "MINIO_SERVER_URL"
EnvMinIOBrowserRedirectURL = "MINIO_BROWSER_REDIRECT_URL"
@@ -51,7 +53,8 @@ const (
EnvKESClientCert = "MINIO_KMS_KES_CERT_FILE"
EnvKESServerCA = "MINIO_KMS_KES_CAPATH"
EnvEndpoints = "MINIO_ENDPOINTS" // legacy
EnvWorm = "MINIO_WORM" // legacy
EnvRegion = "MINIO_REGION" // legacy
EnvEndpoints = "MINIO_ENDPOINTS" // legacy
EnvWorm = "MINIO_WORM" // legacy
EnvRegion = "MINIO_REGION" // legacy
EnvRegionName = "MINIO_REGION_NAME" // legacy
)

View File

@@ -49,13 +49,34 @@ func (hkvs HelpKVS) Lookup(key string) (HelpKV, bool) {
// DefaultComment used across all sub-systems.
const DefaultComment = "optionally add a comment to this setting"
// Region and Worm help is documented in default config
// Region help is documented in default config
var (
SiteHelp = HelpKVS{
HelpKV{
Key: NameKey,
Type: "string",
Description: `name for the site e.g. "cal-rack0"`,
Optional: true,
},
HelpKV{
Key: RegionKey,
Type: "string",
Description: `name of the location of the server e.g. "us-west-1"`,
Optional: true,
},
HelpKV{
Key: Comment,
Type: "sentence",
Description: DefaultComment,
Optional: true,
},
}
RegionHelp = HelpKVS{
HelpKV{
Key: RegionName,
Type: "string",
Description: `name of the location of the server e.g. "us-west-rack2"`,
Description: `[DEPRECATED] name of the location of the server e.g. "us-west-rack2"`,
Optional: true,
},
HelpKV{