mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-22 16:28:30 +02:00
fix(label): expand wildcard labels before unmarshaling and add corresponding test
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user