mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-07-10 05:27:07 +08:00
vendor: update compose-go to v2.0.0-rc.3
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
This commit is contained in:
27
vendor/github.com/compose-spec/compose-go/v2/override/extends.go
generated
vendored
Normal file
27
vendor/github.com/compose-spec/compose-go/v2/override/extends.go
generated
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
/*
|
||||
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 "github.com/compose-spec/compose-go/v2/tree"
|
||||
|
||||
func ExtendService(base, override map[string]any) (map[string]any, error) {
|
||||
yaml, err := mergeYaml(base, override, tree.NewPath("services.x"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return yaml.(map[string]any), nil
|
||||
}
|
252
vendor/github.com/compose-spec/compose-go/v2/override/merge.go
generated
vendored
Normal file
252
vendor/github.com/compose-spec/compose-go/v2/override/merge.go
generated
vendored
Normal file
@ -0,0 +1,252 @@
|
||||
/*
|
||||
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 (
|
||||
"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["services.*.annotations"] = mergeToSequence
|
||||
mergeSpecials["services.*.build"] = mergeBuild
|
||||
mergeSpecials["services.*.build.args"] = mergeToSequence
|
||||
mergeSpecials["services.*.build.additional_contexts"] = mergeToSequence
|
||||
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.*.environment"] = mergeToSequence
|
||||
mergeSpecials["services.*.extra_hosts"] = mergeToSequence
|
||||
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 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:
|
||||
seq := make([]any, len(v))
|
||||
i := 0
|
||||
for k, v := range v {
|
||||
if v == nil {
|
||||
seq[i] = k
|
||||
} else {
|
||||
seq[i] = fmt.Sprintf("%s=%v", k, v)
|
||||
}
|
||||
i++
|
||||
}
|
||||
slices.SortFunc(seq, func(a, b any) bool {
|
||||
return 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 any) map[string]any {
|
||||
switch v := a.(type) {
|
||||
case map[string]any:
|
||||
return v
|
||||
case []any:
|
||||
converted := map[string]any{}
|
||||
for _, s := range v {
|
||||
converted[s.(string)] = defaultValue
|
||||
}
|
||||
return converted
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func override(_ any, other any, _ tree.Path) (any, error) {
|
||||
return other, nil
|
||||
}
|
204
vendor/github.com/compose-spec/compose-go/v2/override/uncity.go
generated
vendored
Normal file
204
vendor/github.com/compose-spec/compose-go/v2/override/uncity.go
generated
vendored
Normal file
@ -0,0 +1,204 @@
|
||||
/*
|
||||
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 (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/compose-spec/compose-go/v2/format"
|
||||
"github.com/compose-spec/compose-go/v2/tree"
|
||||
)
|
||||
|
||||
type indexer func(any, tree.Path) (string, error)
|
||||
|
||||
// mergeSpecials defines the custom rules applied by compose when merging yaml trees
|
||||
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
|
||||
unique["services.*.build.extra_hosts"] = keyValueIndexer
|
||||
unique["services.*.build.platform"] = keyValueIndexer
|
||||
unique["services.*.build.tags"] = keyValueIndexer
|
||||
unique["services.*.build.labels"] = keyValueIndexer
|
||||
unique["services.*.cap_add"] = keyValueIndexer
|
||||
unique["services.*.cap_drop"] = keyValueIndexer
|
||||
unique["services.*.configs"] = mountIndexer("")
|
||||
unique["services.*.deploy.labels"] = keyValueIndexer
|
||||
unique["services.*.dns"] = keyValueIndexer
|
||||
unique["services.*.dns_opt"] = keyValueIndexer
|
||||
unique["services.*.dns_search"] = keyValueIndexer
|
||||
unique["services.*.environment"] = keyValueIndexer
|
||||
unique["services.*.env_file"] = envFileIndexer
|
||||
unique["services.*.expose"] = exposeIndexer
|
||||
unique["services.*.extra_hosts"] = keyValueIndexer
|
||||
unique["services.*.labels"] = keyValueIndexer
|
||||
unique["services.*.links"] = keyValueIndexer
|
||||
unique["services.*.networks.*.aliases"] = keyValueIndexer
|
||||
unique["services.*.networks.*.link_local_ips"] = keyValueIndexer
|
||||
unique["services.*.ports"] = portIndexer
|
||||
unique["services.*.profiles"] = keyValueIndexer
|
||||
unique["services.*.secrets"] = mountIndexer("/run/secrets")
|
||||
unique["services.*.sysctls"] = keyValueIndexer
|
||||
unique["services.*.tmpfs"] = keyValueIndexer
|
||||
unique["services.*.volumes"] = volumeIndexer
|
||||
}
|
||||
|
||||
// EnforceUnicity removes redefinition of elements declared in a sequence
|
||||
func EnforceUnicity(value map[string]any) (map[string]any, error) {
|
||||
uniq, err := enforceUnicity(value, tree.NewPath())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return uniq.(map[string]any), nil
|
||||
}
|
||||
|
||||
func enforceUnicity(value any, p tree.Path) (any, error) {
|
||||
switch v := value.(type) {
|
||||
case map[string]any:
|
||||
for k, e := range v {
|
||||
u, err := enforceUnicity(e, p.Next(k))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v[k] = u
|
||||
}
|
||||
return v, nil
|
||||
case []any:
|
||||
for pattern, indexer := range unique {
|
||||
if p.Matches(pattern) {
|
||||
seq := []any{}
|
||||
keys := map[string]int{}
|
||||
for i, entry := range v {
|
||||
key, err := indexer(entry, p.Next(fmt.Sprintf("[%d]", i)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if j, ok := keys[key]; ok {
|
||||
seq[j] = entry
|
||||
} else {
|
||||
seq = append(seq, entry)
|
||||
keys[key] = len(seq) - 1
|
||||
}
|
||||
}
|
||||
return seq, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func keyValueIndexer(y any, _ tree.Path) (string, error) {
|
||||
value := y.(string)
|
||||
key, _, found := strings.Cut(value, "=")
|
||||
if !found {
|
||||
return value, nil
|
||||
}
|
||||
return key, nil
|
||||
}
|
||||
|
||||
func volumeIndexer(y any, p tree.Path) (string, error) {
|
||||
switch value := y.(type) {
|
||||
case map[string]any:
|
||||
target, ok := value["target"].(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("service volume %s is missing a mount target", p)
|
||||
}
|
||||
return target, nil
|
||||
case string:
|
||||
volume, err := format.ParseVolume(value)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return volume.Target, nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func exposeIndexer(a any, path tree.Path) (string, error) {
|
||||
switch v := a.(type) {
|
||||
case string:
|
||||
return v, nil
|
||||
case int:
|
||||
return strconv.Itoa(v), nil
|
||||
default:
|
||||
return "", fmt.Errorf("%s: unsupported expose value %s", path, a)
|
||||
}
|
||||
}
|
||||
|
||||
func mountIndexer(defaultPath string) indexer {
|
||||
return func(a any, path tree.Path) (string, error) {
|
||||
switch v := a.(type) {
|
||||
case string:
|
||||
return fmt.Sprintf("%s/%s", defaultPath, v), nil
|
||||
case map[string]any:
|
||||
t, ok := v["target"]
|
||||
if ok {
|
||||
return t.(string), nil
|
||||
}
|
||||
return fmt.Sprintf("%s/%s", defaultPath, v["source"]), nil
|
||||
default:
|
||||
return "", fmt.Errorf("%s: unsupported expose value %s", path, a)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func portIndexer(y any, p tree.Path) (string, error) {
|
||||
switch value := y.(type) {
|
||||
case int:
|
||||
return strconv.Itoa(value), nil
|
||||
case map[string]any:
|
||||
target, ok := value["target"]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("service ports %s is missing a target port", p)
|
||||
}
|
||||
published, ok := value["published"]
|
||||
if !ok {
|
||||
// try to parse it as an int
|
||||
if pub, ok := value["published"]; ok {
|
||||
published = fmt.Sprintf("%d", pub)
|
||||
}
|
||||
}
|
||||
host, ok := value["host_ip"]
|
||||
if !ok {
|
||||
host = "0.0.0.0"
|
||||
}
|
||||
protocol, ok := value["protocol"]
|
||||
if !ok {
|
||||
protocol = "tcp"
|
||||
}
|
||||
return fmt.Sprintf("%s:%s:%d/%s", host, published, target, protocol), nil
|
||||
case string:
|
||||
return value, nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func envFileIndexer(y any, _ tree.Path) (string, error) {
|
||||
switch value := y.(type) {
|
||||
case string:
|
||||
return value, nil
|
||||
case map[string]any:
|
||||
return value["path"].(string), nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
Reference in New Issue
Block a user