From a7778806a0e3930b4d27810149be358e07bc3861 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 16 Aug 2021 14:00:37 +0200 Subject: [PATCH] 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 --- cli/compose/loader/loader_test.go | 8 ++++++++ cli/compose/schema/data/config_schema_v3.10.json | 4 ++-- cli/compose/schema/schema.go | 5 +++-- cli/compose/schema/schema_test.go | 4 +++- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/cli/compose/loader/loader_test.go b/cli/compose/loader/loader_test.go index db57dba24a..052dd6f0bd 100644 --- a/cli/compose/loader/loader_test.go +++ b/cli/compose/loader/loader_test.go @@ -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") } diff --git a/cli/compose/schema/data/config_schema_v3.10.json b/cli/compose/schema/data/config_schema_v3.10.json index 53e8ba890a..d7efc208ce 100644 --- a/cli/compose/schema/data/config_schema_v3.10.json +++ b/cli/compose/schema/data/config_schema_v3.10.json @@ -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": { diff --git a/cli/compose/schema/schema.go b/cli/compose/schema/schema.go index f8a1abaf65..a662f4aaf5 100644 --- a/cli/compose/schema/schema.go +++ b/cli/compose/schema/schema.go @@ -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 { diff --git a/cli/compose/schema/schema_test.go b/cli/compose/schema/schema_test.go index e31828d4ba..ed14e1d0ae 100644 --- a/cli/compose/schema/schema_test.go +++ b/cli/compose/schema/schema_test.go @@ -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 {