File: merge.go

package info (click to toggle)
golang-github-compose-spec-compose-go 2.4.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,628 kB
  • sloc: makefile: 36; sh: 8
file content (292 lines) | stat: -rw-r--r-- 8,264 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
   Copyright 2020 The Compose Specification Authors.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package override

import (
	"cmp"
	"fmt"
	"strings"

	"github.com/compose-spec/compose-go/v2/tree"
	"golang.org/x/exp/slices"
)

// Merge applies overrides to a config model
func Merge(right, left map[string]any) (map[string]any, error) {
	merged, err := mergeYaml(right, left, tree.NewPath())
	if err != nil {
		return nil, err
	}
	return merged.(map[string]any), nil
}

type merger func(any, any, tree.Path) (any, error)

// mergeSpecials defines the custom rules applied by compose when merging yaml trees
var mergeSpecials = map[tree.Path]merger{}

func init() {
	mergeSpecials["networks.*.ipam.config"] = mergeIPAMConfig
	mergeSpecials["networks.*.labels"] = mergeToSequence
	mergeSpecials["volumes.*.labels"] = mergeToSequence
	mergeSpecials["services.*.annotations"] = mergeToSequence
	mergeSpecials["services.*.build"] = mergeBuild
	mergeSpecials["services.*.build.args"] = mergeToSequence
	mergeSpecials["services.*.build.additional_contexts"] = mergeToSequence
	mergeSpecials["services.*.build.extra_hosts"] = mergeExtraHosts
	mergeSpecials["services.*.build.labels"] = mergeToSequence
	mergeSpecials["services.*.command"] = override
	mergeSpecials["services.*.depends_on"] = mergeDependsOn
	mergeSpecials["services.*.deploy.labels"] = mergeToSequence
	mergeSpecials["services.*.dns"] = mergeToSequence
	mergeSpecials["services.*.dns_opt"] = mergeToSequence
	mergeSpecials["services.*.dns_search"] = mergeToSequence
	mergeSpecials["services.*.entrypoint"] = override
	mergeSpecials["services.*.env_file"] = mergeToSequence
	mergeSpecials["services.*.label_file"] = mergeToSequence
	mergeSpecials["services.*.environment"] = mergeToSequence
	mergeSpecials["services.*.extra_hosts"] = mergeExtraHosts
	mergeSpecials["services.*.healthcheck.test"] = override
	mergeSpecials["services.*.labels"] = mergeToSequence
	mergeSpecials["services.*.logging"] = mergeLogging
	mergeSpecials["services.*.networks"] = mergeNetworks
	mergeSpecials["services.*.sysctls"] = mergeToSequence
	mergeSpecials["services.*.tmpfs"] = mergeToSequence
	mergeSpecials["services.*.ulimits.*"] = mergeUlimit
}

// mergeYaml merges map[string]any yaml trees handling special rules
func mergeYaml(e any, o any, p tree.Path) (any, error) {
	for pattern, merger := range mergeSpecials {
		if p.Matches(pattern) {
			merged, err := merger(e, o, p)
			if err != nil {
				return nil, err
			}
			return merged, nil
		}
	}
	if o == nil {
		return e, nil
	}
	switch value := e.(type) {
	case map[string]any:
		other, ok := o.(map[string]any)
		if !ok {
			return nil, fmt.Errorf("cannot override %s", p)
		}
		return mergeMappings(value, other, p)
	case []any:
		other, ok := o.([]any)
		if !ok {
			return nil, fmt.Errorf("cannot override %s", p)
		}
		return append(value, other...), nil
	default:
		return o, nil
	}
}

func mergeMappings(mapping map[string]any, other map[string]any, p tree.Path) (map[string]any, error) {
	for k, v := range other {
		e, ok := mapping[k]
		if !ok || strings.HasPrefix(k, "x-") {
			mapping[k] = v
			continue
		}
		next := p.Next(k)
		merged, err := mergeYaml(e, v, next)
		if err != nil {
			return nil, err
		}
		mapping[k] = merged
	}
	return mapping, nil
}

// logging driver options are merged only when both compose file define the same driver
func mergeLogging(c any, o any, p tree.Path) (any, error) {
	config := c.(map[string]any)
	other := o.(map[string]any)
	// we override logging config if source and override have the same driver set, or none
	d, ok1 := other["driver"]
	o, ok2 := config["driver"]
	if d == o || !ok1 || !ok2 {
		return mergeMappings(config, other, p)
	}
	return other, nil
}

func mergeBuild(c any, o any, path tree.Path) (any, error) {
	toBuild := func(c any) map[string]any {
		switch v := c.(type) {
		case string:
			return map[string]any{
				"context": v,
			}
		case map[string]any:
			return v
		}
		return nil
	}
	return mergeMappings(toBuild(c), toBuild(o), path)
}

func mergeDependsOn(c any, o any, path tree.Path) (any, error) {
	right := convertIntoMapping(c, map[string]any{
		"condition": "service_started",
		"required":  true,
	})
	left := convertIntoMapping(o, map[string]any{
		"condition": "service_started",
		"required":  true,
	})
	return mergeMappings(right, left, path)
}

func mergeNetworks(c any, o any, path tree.Path) (any, error) {
	right := convertIntoMapping(c, nil)
	left := convertIntoMapping(o, nil)
	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)
	return append(right, left...), nil
}

func convertIntoSequence(value any) []any {
	switch v := value.(type) {
	case map[string]any:
		var seq []any
		for k, val := range v {
			if val == nil {
				seq = append(seq, k)
			} else {
				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))
				}
			}
		}
		slices.SortFunc(seq, func(a, b any) int {
			return cmp.Compare(a.(string), b.(string))
		})
		return seq
	case []any:
		return v
	case string:
		return []any{v}
	}
	return nil
}

func mergeUlimit(_ any, o any, p tree.Path) (any, error) {
	over, ismapping := o.(map[string]any)
	if base, ok := o.(map[string]any); ok && ismapping {
		return mergeMappings(base, over, p)
	}
	return o, nil
}

func mergeIPAMConfig(c any, o any, path tree.Path) (any, error) {
	var ipamConfigs []any
	for _, original := range c.([]any) {
		right := convertIntoMapping(original, nil)
		for _, override := range o.([]any) {
			left := convertIntoMapping(override, nil)
			if left["subnet"] != right["subnet"] {
				// check if left is already in ipamConfigs, add it if not and continue with the next config
				if !slices.ContainsFunc(ipamConfigs, func(a any) bool {
					return a.(map[string]any)["subnet"] == left["subnet"]
				}) {
					ipamConfigs = append(ipamConfigs, left)
					continue
				}
			}
			merged, err := mergeMappings(right, left, path)
			if err != nil {
				return nil, err
			}
			// find index of potential previous config with the same subnet in ipamConfigs
			indexIfExist := slices.IndexFunc(ipamConfigs, func(a any) bool {
				return a.(map[string]any)["subnet"] == merged["subnet"]
			})
			// if a previous config is already in ipamConfigs, replace it
			if indexIfExist >= 0 {
				ipamConfigs[indexIfExist] = merged
			} else {
				// or add the new config to ipamConfigs
				ipamConfigs = append(ipamConfigs, merged)
			}
		}
	}
	return ipamConfigs, nil
}

func convertIntoMapping(a any, defaultValue map[string]any) map[string]any {
	switch v := a.(type) {
	case map[string]any:
		return v
	case []any:
		converted := map[string]any{}
		for _, s := range v {
			if defaultValue == nil {
				converted[s.(string)] = nil
			} else {
				// Create a new map for each key
				converted[s.(string)] = copyMap(defaultValue)
			}
		}
		return converted
	}
	return nil
}

func copyMap(m map[string]any) map[string]any {
	c := make(map[string]any)
	for k, v := range m {
		c[k] = v
	}
	return c
}

func override(_ any, other any, _ tree.Path) (any, error) {
	return other, nil
}