cli/compose/schema: make version optional, default to "latest"

The compose spec (https://compose-spec.io) defines the version to be optional,
and implementations of the spec to check for supported attributes instead.

While this change does not switch the `docker stack` implementation to use the
compose-spec, it makes it function more similar. Previously, omitting a version
number would either produce an error (as the field was required), or switched
the handling to assume it was version 1.0 (which is deprecated).

With this change, compose files without a version number will be handled as
the latest version supported by `docker stack` (currently 3.10). This allows
users that work with docker-compose or docker compose (v2) to deploy their
compose file, without having to re-add a version number. Fields that are
not supported by stackes (schema 3.10) will still produce an error.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2021-08-16 14:00:37 +02:00
parent a9fd697737
commit a7778806a0
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
4 changed files with 16 additions and 5 deletions

View File

@ -422,6 +422,14 @@ func TestV1Unsupported(t *testing.T) {
foo:
image: busybox
`)
assert.ErrorContains(t, err, "(root) Additional property foo is not allowed")
_, err = loadYAML(`
version: "1.0"
foo:
image: busybox
`)
assert.ErrorContains(t, err, "unsupported Compose file version: 1.0")
}

View File

@ -2,11 +2,11 @@
"$schema": "http://json-schema.org/draft-04/schema#",
"id": "config_schema_v3.10.json",
"type": "object",
"required": ["version"],
"properties": {
"version": {
"type": "string"
"type": "string",
"default": "3.10"
},
"services": {

View File

@ -11,7 +11,7 @@ import (
)
const (
defaultVersion = "1.0"
defaultVersion = "3.10"
versionField = "version"
)
@ -39,7 +39,8 @@ func init() {
gojsonschema.FormatCheckers.Add("duration", durationFormatChecker{})
}
// Version returns the version of the config, defaulting to version 1.0
// Version returns the version of the config, defaulting to the latest "3.x"
// version (3.10).
func Version(config map[string]interface{}) string {
version, ok := config[versionField]
if !ok {

View File

@ -21,7 +21,8 @@ func TestValidate(t *testing.T) {
assert.NilError(t, Validate(config, "3.0"))
assert.NilError(t, Validate(config, "3"))
assert.ErrorContains(t, Validate(config, ""), "unsupported Compose file version: 1.0")
assert.NilError(t, Validate(config, ""))
assert.ErrorContains(t, Validate(config, "1.0"), "unsupported Compose file version: 1.0")
assert.ErrorContains(t, Validate(config, "12345"), "unsupported Compose file version: 12345")
}
@ -99,6 +100,7 @@ func TestValidateCredentialSpecs(t *testing.T) {
{version: "3.8"},
{version: "3.9"},
{version: "3.10"},
{version: ""},
}
for _, tc := range tests {