Skip to content

Commit

Permalink
make extra_hosts values uniques
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Lours <[email protected]>
  • Loading branch information
glours committed Sep 26, 2024
1 parent 70e91bb commit d3fd7d9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
40 changes: 31 additions & 9 deletions override/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func init() {
mergeSpecials["services.*.build"] = mergeBuild
mergeSpecials["services.*.build.args"] = mergeToSequence
mergeSpecials["services.*.build.additional_contexts"] = mergeToSequence
mergeSpecials["services.*.build.extra_hosts"] = mergeToSequence
mergeSpecials["services.*.build.extra_hosts"] = mergeExtraHosts
mergeSpecials["services.*.build.labels"] = mergeToSequence
mergeSpecials["services.*.command"] = override
mergeSpecials["services.*.depends_on"] = mergeDependsOn
Expand All @@ -58,7 +58,7 @@ func init() {
mergeSpecials["services.*.entrypoint"] = override
mergeSpecials["services.*.env_file"] = mergeToSequence
mergeSpecials["services.*.environment"] = mergeToSequence
mergeSpecials["services.*.extra_hosts"] = mergeToSequence
mergeSpecials["services.*.extra_hosts"] = mergeExtraHosts
mergeSpecials["services.*.healthcheck.test"] = override
mergeSpecials["services.*.labels"] = mergeToSequence
mergeSpecials["services.*.logging"] = mergeLogging
Expand Down Expand Up @@ -163,6 +163,22 @@ func mergeNetworks(c any, o any, path tree.Path) (any, error) {
return mergeMappings(right, left, path)
}

func mergeExtraHosts(c any, o any, _ tree.Path) (any, error) {
right := convertIntoSequence(c)
left := convertIntoSequence(o)
// Rewrite content of left slice to remove duplicate elements
i := 0
for _, v := range left {
if !slices.Contains(right, v) {
left[i] = v
i++
}
}
// keep only not duplicated elements from left slice
left = left[:i]
return append(right, left...), nil
}

func mergeToSequence(c any, o any, _ tree.Path) (any, error) {
right := convertIntoSequence(c)
left := convertIntoSequence(o)
Expand All @@ -172,15 +188,21 @@ func mergeToSequence(c any, o any, _ tree.Path) (any, error) {
func convertIntoSequence(value any) []any {
switch v := value.(type) {
case map[string]any:
seq := make([]any, len(v))
i := 0
for k, v := range v {
if v == nil {
seq[i] = k
var seq []any
for k, val := range v {
if val == nil {
seq = append(seq, k)
} else {
seq[i] = fmt.Sprintf("%s=%v", k, v)
switch vl := val.(type) {
// if val is an array we need to add the key with each value one by one
case []any:
for _, vlv := range vl {
seq = append(seq, fmt.Sprintf("%s=%v", k, vlv))
}
default:
seq = append(seq, fmt.Sprintf("%s=%v", k, val))
}
}
i++
}
slices.SortFunc(seq, func(a, b any) int {
return cmp.Compare(a.(string), b.(string))
Expand Down
8 changes: 8 additions & 0 deletions override/merge_extra_hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ services:
test:
extra_hosts:
- localhost=127.0.0.1
- example.com=1.2.3.4
- example.com=4.3.2.1
`, `
services:
test:
image: foo
extra_hosts:
- example.com=1.2.3.4
- localhost=127.0.0.1
- example.com=4.3.2.1
`)
}

Expand All @@ -54,12 +57,14 @@ services:
test:
extra_hosts:
"localhost": "127.0.0.1"
"example.com": ["1.2.3.4", "4.3.2.1"]
`, `
services:
test:
image: foo
extra_hosts:
- example.com=1.2.3.4
- example.com=4.3.2.1
- localhost=127.0.0.1
`)
}
Expand All @@ -76,12 +81,15 @@ services:
test:
extra_hosts:
- localhost=127.0.0.1
- example.com=1.2.3.4
- example.com=4.3.2.1
`, `
services:
test:
image: foo
extra_hosts:
- example.com=1.2.3.4
- localhost=127.0.0.1
- example.com=4.3.2.1
`)
}
3 changes: 2 additions & 1 deletion schema/compose-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,8 @@
"patternProperties": {
".+": {
"type": ["string", "array"]
}
},
"uniqueItems": false
},
"additionalProperties": false
},
Expand Down

0 comments on commit d3fd7d9

Please sign in to comment.