feature: add a prefix on environment config (#40623)

fix #40622

Signed-off-by: xiaofanluan <xiaofan.luan@zilliz.com>
This commit is contained in:
Xiaofan 2025-03-13 16:44:07 +08:00 committed by GitHub
parent f6fb4bc442
commit 7210fc9780
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -24,15 +24,20 @@ import (
"github.com/milvus-io/milvus/pkg/v2/util/typeutil" "github.com/milvus-io/milvus/pkg/v2/util/typeutil"
) )
// DefaultEnvPrefix is the default prefix for environment variables
const DefaultEnvPrefix = "MILVUS_CONF_"
type EnvSource struct { type EnvSource struct {
configs *typeutil.ConcurrentMap[string, string] configs *typeutil.ConcurrentMap[string, string]
KeyFormatter func(string) string KeyFormatter func(string) string
prefix string
} }
func NewEnvSource(KeyFormatter func(string) string) EnvSource { func NewEnvSource(KeyFormatter func(string) string) EnvSource {
es := EnvSource{ es := EnvSource{
configs: typeutil.NewConcurrentMap[string, string](), configs: typeutil.NewConcurrentMap[string, string](),
KeyFormatter: KeyFormatter, KeyFormatter: KeyFormatter,
prefix: DefaultEnvPrefix,
} }
for _, value := range os.Environ() { for _, value := range os.Environ() {
@ -43,6 +48,15 @@ func NewEnvSource(KeyFormatter func(string) string) EnvSource {
envKey := KeyFormatter(key) envKey := KeyFormatter(key)
es.configs.Insert(key, value) es.configs.Insert(key, value)
es.configs.Insert(envKey, value) es.configs.Insert(envKey, value)
// Handle prefixed environment variables
// e.g., MILVUS_MINIO_PORT will override MINIO_PORT
if strings.HasPrefix(key, es.prefix) {
originalKey := key[len(es.prefix):]
originalEnvKey := KeyFormatter(originalKey)
es.configs.Insert(originalKey, value)
es.configs.Insert(originalEnvKey, value)
}
} }
return es return es
} }