fix(label): expand wildcard labels before unmarshaling and add corresponding test

This commit is contained in:
yusing
2025-06-09 20:46:39 +08:00
parent 421aaecba4
commit 25fbcc4ab9
3 changed files with 60 additions and 19 deletions

View File

@@ -1,6 +1,9 @@
package docker
import (
"fmt"
"strings"
"github.com/yusing/go-proxy/internal/gperr"
"github.com/yusing/go-proxy/internal/utils/strutils"
)
@@ -13,6 +16,8 @@ func ParseLabels(labels map[string]string) (LabelMap, gperr.Error) {
nestedMap := make(LabelMap)
errs := gperr.NewBuilder("labels error")
ExpandWildcard(labels)
for lbl, value := range labels {
parts := strutils.SplitRune(lbl, '.')
if parts[0] != NSProxy {
@@ -50,3 +55,32 @@ func ParseLabels(labels map[string]string) (LabelMap, gperr.Error) {
return nestedMap, errs.Error()
}
func ExpandWildcard(labels map[string]string) {
aliasLabels := make([]string, 0, len(labels))
wildcardLabels := make(map[string]string)
for lbl, value := range labels {
parts := strings.SplitN(lbl, ".", 3) // Split into proxy, alias, rest
if parts[0] != NSProxy || len(parts) < 2 {
continue
}
alias := parts[1] // alias or wildcard alias
if alias == WildcardAlias {
delete(labels, lbl)
if len(parts) < 3 { // invalid wildcard label (no suffix)
continue
}
wildcardLabels[parts[2]] = value
} else {
// Extract just the alias part (first segment after proxy)
aliasLabels = append(aliasLabels, alias)
}
}
for lbl, v := range wildcardLabels {
for _, alias := range aliasLabels {
labels[fmt.Sprintf("%s.%s.%s", NSProxy, alias, lbl)] = v
}
}
}