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

fix #40622

Signed-off-by: xiaofanluan <xiaofan.luan@zilliz.com>
pull/40643/head
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
1 changed files with 14 additions and 0 deletions

View File

@ -24,15 +24,20 @@ import (
"github.com/milvus-io/milvus/pkg/v2/util/typeutil"
)
// DefaultEnvPrefix is the default prefix for environment variables
const DefaultEnvPrefix = "MILVUS_CONF_"
type EnvSource struct {
configs *typeutil.ConcurrentMap[string, string]
KeyFormatter func(string) string
prefix string
}
func NewEnvSource(KeyFormatter func(string) string) EnvSource {
es := EnvSource{
configs: typeutil.NewConcurrentMap[string, string](),
KeyFormatter: KeyFormatter,
prefix: DefaultEnvPrefix,
}
for _, value := range os.Environ() {
@ -43,6 +48,15 @@ func NewEnvSource(KeyFormatter func(string) string) EnvSource {
envKey := KeyFormatter(key)
es.configs.Insert(key, 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
}