Skip to content

Commit

Permalink
add custom merge function for IPAM config
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Lours <[email protected]>
  • Loading branch information
glours committed Jan 26, 2024
1 parent 467fb48 commit ba6a986
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/docker/go-units v0.5.0
github.com/google/go-cmp v0.5.9
github.com/mattn/go-shellwords v1.0.12
github.com/mitchellh/copystructure v1.2.0
github.com/mitchellh/mapstructure v1.5.0
github.com/opencontainers/go-digest v1.0.0
github.com/pkg/errors v0.9.1
Expand All @@ -22,7 +23,6 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
Expand Down
11 changes: 11 additions & 0 deletions override/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type merger func(any, any, tree.Path) (any, error)
var mergeSpecials = map[tree.Path]merger{}

func init() {
mergeSpecials["networks.*.ipam.config"] = mergeIPAMConfig
mergeSpecials["services.*.annotations"] = mergeToSequence
mergeSpecials["services.*.build"] = mergeBuild
mergeSpecials["services.*.build.args"] = mergeToSequence
Expand Down Expand Up @@ -194,6 +195,16 @@ func mergeUlimit(_ any, o any, p tree.Path) (any, error) {
return o, nil
}

func mergeIPAMConfig(c any, o any, path tree.Path) (any, error) {
right := convertIntoMapping(c.([]any)[0], nil)
left := convertIntoMapping(o.([]any)[0], nil)
merged, err := mergeMappings(right, left, path)
if err != nil {
return nil, err
}
return []any{merged}, nil
}

func convertIntoMapping(a any, defaultValue any) map[string]any {
switch v := a.(type) {
case map[string]any:
Expand Down
81 changes: 78 additions & 3 deletions override/merge_networks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"testing"
)

func Test_mergeYamlNetworkSequence(t *testing.T) {
func Test_mergeYamlServiceNetworkSequence(t *testing.T) {
assertMergeYaml(t, `
services:
test:
Expand All @@ -43,7 +43,7 @@ services:
`)
}

func Test_mergeYamlNetworksMapping(t *testing.T) {
func Test_mergeYamlServiceNetworksMapping(t *testing.T) {
assertMergeYaml(t, `
services:
test:
Expand Down Expand Up @@ -92,7 +92,7 @@ services:
`)
}

func Test_mergeYamlNetworkstMixed(t *testing.T) {
func Test_mergeYamlServiceNetworksMixed(t *testing.T) {
assertMergeYaml(t, `
services:
test:
Expand Down Expand Up @@ -137,3 +137,78 @@ services:
- alias3
`)
}

func Test_mergeYamlNetworks(t *testing.T) {
assertMergeYaml(t, `
services:
test:
image: foo
networks:
network1:
ipam:
config:
- subnet: 172.28.0.0/16
ip_range: 172.28.5.0/24
gateway: 172.28.5.254
aux_addresses:
host1: 172.28.1.5
host2: 172.28.1.6
host3: 172.28.1.7
options:
foo: bar
baz: "0"
labels:
com.example.description: "Financial transaction network"
com.example.department: "Finance"
com.example.label-with-empty-value: ""
`, `
services:
test:
image: foo
networks:
network1:
ipam:
config:
- subnet: 172.28.10.0/16
ip_range: 172.28.5.1/24
gateway: 172.28.5.254
aux_addresses:
host1: 172.28.1.5
host2: 172.28.1.4
host4: 172.28.1.10
options:
bar: foo
baz: "0"
labels:
com.example.description: "Financial transaction network"
com.example.department-new: "New"
com.example.label-with-empty-value: ""
network2:
`, `
services:
test:
image: foo
networks:
network1:
ipam:
config:
- subnet: 172.28.10.0/16
ip_range: 172.28.5.1/24
gateway: 172.28.5.254
aux_addresses:
host1: 172.28.1.5
host2: 172.28.1.4
host3: 172.28.1.7
host4: 172.28.1.10
options:
foo: bar
bar: foo
baz: "0"
labels:
com.example.description: "Financial transaction network"
com.example.department: "Finance"
com.example.label-with-empty-value: ""
com.example.department-new: "New"
network2:
`)
}
2 changes: 2 additions & 0 deletions override/uncity.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type indexer func(any, tree.Path) (string, error)
var unique = map[tree.Path]indexer{}

func init() {
unique["networks.*.labels"] = keyValueIndexer
unique["networks.*.ipam.options"] = keyValueIndexer
unique["services.*.annotations"] = keyValueIndexer
unique["services.*.build.args"] = keyValueIndexer
unique["services.*.build.additional_contexts"] = keyValueIndexer
Expand Down

0 comments on commit ba6a986

Please sign in to comment.