Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion cli/compose/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ func Transform(source any, target any, additionalTransformers ...Transformer) er
config := &mapstructure.DecoderConfig{
DecodeHook: mapstructure.ComposeDecodeHookFunc(
createTransformHook(additionalTransformers...),
mapstructure.StringToTimeDurationHookFunc()),
mapstructure.StringToTimeDurationHookFunc(),
numberToStringHook()),
Result: target,
Metadata: &data,
}
Expand Down Expand Up @@ -356,6 +357,28 @@ func createTransformHook(additionalTransformers ...Transformer) mapstructure.Dec
}
}

// numberToStringHook lets numeric YAML/JSON values land in string fields
// (notably deploy.resources.*.cpus, which compose config emits as numbers).
func numberToStringHook() mapstructure.DecodeHookFunc {
return func(_ reflect.Type, to reflect.Type, data any) (any, error) {
if to.Kind() != reflect.String {
return data, nil
}
switch v := data.(type) {
case string:
return v, nil
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return fmt.Sprint(v), nil
case float32:
return strconv.FormatFloat(float64(v), 'f', -1, 32), nil
case float64:
return strconv.FormatFloat(v, 'f', -1, 64), nil
default:
return data, nil
}
}
}

// keys needs to be converted to strings for jsonschema
func convertToStringKeysRecursive(value any, keyPrefix string) (any, error) {
if mapping, ok := value.(map[string]any); ok {
Expand Down
31 changes: 31 additions & 0 deletions cli/compose/loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,37 @@ func TestInvalidResource(t *testing.T) {
assert.Check(t, is.ErrorContains(err, "Additional property impossible is not allowed"))
}

func TestLoadNumericCPUs(t *testing.T) {
config, err := loadYAML(`
version: "3.13"
services:
foo:
image: busybox
deploy:
resources:
limits:
cpus: 0.5
reservations:
cpus: 1
`)
assert.NilError(t, err)
assert.Check(t, is.Equal("0.5", config.Services[0].Deploy.Resources.Limits.NanoCPUs))
assert.Check(t, is.Equal("1", config.Services[0].Deploy.Resources.Reservations.NanoCPUs))

config, err = loadYAML(`
version: "3.13"
services:
foo:
image: busybox
deploy:
resources:
limits:
cpus: "0.25"
`)
assert.NilError(t, err)
assert.Check(t, is.Equal("0.25", config.Services[0].Deploy.Resources.Limits.NanoCPUs))
}

func TestInvalidExternalAndDriverCombination(t *testing.T) {
_, err := loadYAML(`
version: "3"
Expand Down
2 changes: 1 addition & 1 deletion cli/compose/schema/data/config_schema_v3.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@
"id": "#/definitions/resource",
"type": "object",
"properties": {
"cpus": {"type": "string"},
"cpus": {"type": ["number", "string"]},
"memory": {"type": "string"}
},
"additionalProperties": false
Expand Down
2 changes: 1 addition & 1 deletion cli/compose/schema/data/config_schema_v3.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@
"id": "#/definitions/resource",
"type": "object",
"properties": {
"cpus": {"type": "string"},
"cpus": {"type": ["number", "string"]},
"memory": {"type": "string"}
},
"additionalProperties": false
Expand Down
4 changes: 2 additions & 2 deletions cli/compose/schema/data/config_schema_v3.10.json
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@
"limits": {
"type": "object",
"properties": {
"cpus": {"type": "string"},
"cpus": {"type": ["number", "string"]},
"memory": {"type": "string"},
"pids": {"type": "integer"}
},
Expand All @@ -400,7 +400,7 @@
"reservations": {
"type": "object",
"properties": {
"cpus": {"type": "string"},
"cpus": {"type": ["number", "string"]},
"memory": {"type": "string"},
"generic_resources": {"$ref": "#/definitions/generic_resources"}
},
Expand Down
4 changes: 2 additions & 2 deletions cli/compose/schema/data/config_schema_v3.11.json
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@
"limits": {
"type": "object",
"properties": {
"cpus": {"type": "string"},
"cpus": {"type": ["number", "string"]},
"memory": {"type": "string"},
"pids": {"type": "integer"}
},
Expand All @@ -400,7 +400,7 @@
"reservations": {
"type": "object",
"properties": {
"cpus": {"type": "string"},
"cpus": {"type": ["number", "string"]},
"memory": {"type": "string"},
"generic_resources": {"$ref": "#/definitions/generic_resources"}
},
Expand Down
4 changes: 2 additions & 2 deletions cli/compose/schema/data/config_schema_v3.12.json
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@
"limits": {
"type": "object",
"properties": {
"cpus": {"type": "string"},
"cpus": {"type": ["number", "string"]},
"memory": {"type": "string"},
"pids": {"type": "integer"}
},
Expand All @@ -401,7 +401,7 @@
"reservations": {
"type": "object",
"properties": {
"cpus": {"type": "string"},
"cpus": {"type": ["number", "string"]},
"memory": {"type": "string"},
"generic_resources": {"$ref": "#/definitions/generic_resources"}
},
Expand Down
4 changes: 2 additions & 2 deletions cli/compose/schema/data/config_schema_v3.13.json
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@
"limits": {
"type": "object",
"properties": {
"cpus": {"type": "string"},
"cpus": {"type": ["number", "string"]},
"memory": {"type": "string"},
"pids": {"type": "integer"}
},
Expand All @@ -408,7 +408,7 @@
"reservations": {
"type": "object",
"properties": {
"cpus": {"type": "string"},
"cpus": {"type": ["number", "string"]},
"memory": {"type": "string"},
"generic_resources": {"$ref": "#/definitions/generic_resources"}
},
Expand Down
2 changes: 1 addition & 1 deletion cli/compose/schema/data/config_schema_v3.2.json
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@
"id": "#/definitions/resource",
"type": "object",
"properties": {
"cpus": {"type": "string"},
"cpus": {"type": ["number", "string"]},
"memory": {"type": "string"}
},
"additionalProperties": false
Expand Down
2 changes: 1 addition & 1 deletion cli/compose/schema/data/config_schema_v3.3.json
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@
"id": "#/definitions/resource",
"type": "object",
"properties": {
"cpus": {"type": "string"},
"cpus": {"type": ["number", "string"]},
"memory": {"type": "string"}
},
"additionalProperties": false
Expand Down
2 changes: 1 addition & 1 deletion cli/compose/schema/data/config_schema_v3.4.json
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@
"id": "#/definitions/resource",
"type": "object",
"properties": {
"cpus": {"type": "string"},
"cpus": {"type": ["number", "string"]},
"memory": {"type": "string"}
},
"additionalProperties": false
Expand Down
4 changes: 2 additions & 2 deletions cli/compose/schema/data/config_schema_v3.5.json
Original file line number Diff line number Diff line change
Expand Up @@ -363,15 +363,15 @@
"limits": {
"type": "object",
"properties": {
"cpus": {"type": "string"},
"cpus": {"type": ["number", "string"]},
"memory": {"type": "string"}
},
"additionalProperties": false
},
"reservations": {
"type": "object",
"properties": {
"cpus": {"type": "string"},
"cpus": {"type": ["number", "string"]},
"memory": {"type": "string"},
"generic_resources": {"$ref": "#/definitions/generic_resources"}
},
Expand Down
4 changes: 2 additions & 2 deletions cli/compose/schema/data/config_schema_v3.6.json
Original file line number Diff line number Diff line change
Expand Up @@ -372,15 +372,15 @@
"limits": {
"type": "object",
"properties": {
"cpus": {"type": "string"},
"cpus": {"type": ["number", "string"]},
"memory": {"type": "string"}
},
"additionalProperties": false
},
"reservations": {
"type": "object",
"properties": {
"cpus": {"type": "string"},
"cpus": {"type": ["number", "string"]},
"memory": {"type": "string"},
"generic_resources": {"$ref": "#/definitions/generic_resources"}
},
Expand Down
4 changes: 2 additions & 2 deletions cli/compose/schema/data/config_schema_v3.7.json
Original file line number Diff line number Diff line change
Expand Up @@ -388,15 +388,15 @@
"limits": {
"type": "object",
"properties": {
"cpus": {"type": "string"},
"cpus": {"type": ["number", "string"]},
"memory": {"type": "string"}
},
"additionalProperties": false
},
"reservations": {
"type": "object",
"properties": {
"cpus": {"type": "string"},
"cpus": {"type": ["number", "string"]},
"memory": {"type": "string"},
"generic_resources": {"$ref": "#/definitions/generic_resources"}
},
Expand Down
4 changes: 2 additions & 2 deletions cli/compose/schema/data/config_schema_v3.8.json
Original file line number Diff line number Diff line change
Expand Up @@ -389,15 +389,15 @@
"limits": {
"type": "object",
"properties": {
"cpus": {"type": "string"},
"cpus": {"type": ["number", "string"]},
"memory": {"type": "string"}
},
"additionalProperties": false
},
"reservations": {
"type": "object",
"properties": {
"cpus": {"type": "string"},
"cpus": {"type": ["number", "string"]},
"memory": {"type": "string"},
"generic_resources": {"$ref": "#/definitions/generic_resources"}
},
Expand Down
4 changes: 2 additions & 2 deletions cli/compose/schema/data/config_schema_v3.9.json
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@
"limits": {
"type": "object",
"properties": {
"cpus": {"type": "string"},
"cpus": {"type": ["number", "string"]},
"memory": {"type": "string"},
"pids": {"type": "integer"}
},
Expand All @@ -400,7 +400,7 @@
"reservations": {
"type": "object",
"properties": {
"cpus": {"type": "string"},
"cpus": {"type": ["number", "string"]},
"memory": {"type": "string"},
"generic_resources": {"$ref": "#/definitions/generic_resources"}
},
Expand Down
23 changes: 23 additions & 0 deletions cli/compose/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,29 @@ func TestValidatePorts(t *testing.T) {
}
}

func TestValidateNumericCPUs(t *testing.T) {
config := dict{
"services": dict{
"foo": dict{
"image": "busybox",
"deploy": dict{
"resources": dict{
"limits": dict{
"cpus": 0.5,
},
"reservations": dict{
"cpus": 1,
},
},
},
},
},
}

assert.NilError(t, Validate(config, "3.13"))
assert.NilError(t, Validate(config, "3"))
}

func TestValidateUndefinedTopLevelOption(t *testing.T) {
config := dict{
"version": "3.0",
Expand Down