mirror of
https://github.com/yusing/godoxy.git
synced 2025-07-04 22:14:24 +02:00
fix(docker): wildcard properties not working with FQDN aliases
This commit is contained in:
parent
e906b358fa
commit
4189ffa1db
2 changed files with 25 additions and 5 deletions
|
@ -59,12 +59,12 @@ func ParseLabels(labels map[string]string, aliases ...string) (LabelMap, gperr.E
|
||||||
|
|
||||||
func ExpandWildcard(labels map[string]string, aliases ...string) {
|
func ExpandWildcard(labels map[string]string, aliases ...string) {
|
||||||
// collect all explicit aliases first
|
// collect all explicit aliases first
|
||||||
aliasSet := make(map[string]struct{}, len(labels))
|
aliasSet := make(map[string]int, len(labels))
|
||||||
// wildcardLabels holds mapping suffix -> value derived from wildcard label definitions
|
// wildcardLabels holds mapping suffix -> value derived from wildcard label definitions
|
||||||
wildcardLabels := make(map[string]string)
|
wildcardLabels := make(map[string]string)
|
||||||
|
|
||||||
for _, alias := range aliases {
|
for i, alias := range aliases {
|
||||||
aliasSet[alias] = struct{}{}
|
aliasSet[alias] = i
|
||||||
}
|
}
|
||||||
|
|
||||||
// iterate over a copy of the keys to safely mutate the map while ranging
|
// iterate over a copy of the keys to safely mutate the map while ranging
|
||||||
|
@ -89,7 +89,9 @@ func ExpandWildcard(labels map[string]string, aliases ...string) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// explicit alias label – remember the alias
|
// explicit alias label – remember the alias
|
||||||
aliasSet[alias] = struct{}{}
|
if _, ok := aliasSet[alias]; !ok {
|
||||||
|
aliasSet[alias] = len(aliasSet)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(aliasSet) == 0 || len(wildcardLabels) == 0 {
|
if len(aliasSet) == 0 || len(wildcardLabels) == 0 {
|
||||||
|
@ -98,7 +100,11 @@ func ExpandWildcard(labels map[string]string, aliases ...string) {
|
||||||
|
|
||||||
// expand collected wildcard labels for every alias
|
// expand collected wildcard labels for every alias
|
||||||
for suffix, v := range wildcardLabels {
|
for suffix, v := range wildcardLabels {
|
||||||
for alias := range aliasSet {
|
for alias, i := range aliasSet {
|
||||||
|
// for FQDN aliases, use numeric index instead of the alias name
|
||||||
|
if strings.Contains(alias, ".") {
|
||||||
|
alias = fmt.Sprintf("#%d", i+1)
|
||||||
|
}
|
||||||
key := fmt.Sprintf("%s.%s.%s", NSProxy, alias, suffix)
|
key := fmt.Sprintf("%s.%s.%s", NSProxy, alias, suffix)
|
||||||
if suffix == "" { // this should not happen (root wildcard handled earlier) but keep safe
|
if suffix == "" { // this should not happen (root wildcard handled earlier) but keep safe
|
||||||
key = fmt.Sprintf("%s.%s", NSProxy, alias)
|
key = fmt.Sprintf("%s.%s", NSProxy, alias)
|
||||||
|
|
|
@ -28,6 +28,20 @@ func TestExpandWildcard(t *testing.T) {
|
||||||
}, labels)
|
}, labels)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestExpandWildcardWithFQDNAliases(t *testing.T) {
|
||||||
|
labels := map[string]string{
|
||||||
|
"proxy.c.host": "localhost",
|
||||||
|
"proxy.*.port": "5555",
|
||||||
|
}
|
||||||
|
docker.ExpandWildcard(labels, "a.example.com", "b.example.com")
|
||||||
|
require.Equal(t, map[string]string{
|
||||||
|
"proxy.#1.port": "5555",
|
||||||
|
"proxy.#2.port": "5555",
|
||||||
|
"proxy.c.host": "localhost",
|
||||||
|
"proxy.c.port": "5555",
|
||||||
|
}, labels)
|
||||||
|
}
|
||||||
|
|
||||||
func TestExpandWildcardYAML(t *testing.T) {
|
func TestExpandWildcardYAML(t *testing.T) {
|
||||||
yaml := `
|
yaml := `
|
||||||
host: localhost
|
host: localhost
|
||||||
|
|
Loading…
Add table
Reference in a new issue