Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prefer canonical transform vs DecodeMapStructure #692

Merged
merged 1 commit into from
Oct 4, 2024
Merged
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
4 changes: 2 additions & 2 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2205,7 +2205,7 @@ services:
count: 2
device_ids: ["my-device-id"]
`)
assert.ErrorContains(t, err, `invalid "count" and "device_ids" are attributes are exclusive`)
assert.ErrorContains(t, err, `"count" and "device_ids" attributes are exclusive`)
}

func TestServiceDeviceRequestCapabilitiesMandatory(t *testing.T) {
Expand All @@ -2221,7 +2221,7 @@ services:
- driver: nvidia
count: 2
`)
assert.ErrorContains(t, err, `"capabilities" attribute is mandatory for device request definition`)
assert.ErrorContains(t, err, `capabilities is required`)
}

func TestServicePullPolicy(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion schema/compose-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,10 @@
"options":{"$ref": "#/definitions/list_or_dict"}
},
"additionalProperties": false,
"patternProperties": {"^x-": {}}
"patternProperties": {"^x-": {}},
"required": [
"capabilities"
]
}
},

Expand Down
1 change: 1 addition & 0 deletions transform/canonical.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func init() {
transformers["services.*.build.secrets.*"] = transformFileMount
transformers["services.*.build.additional_contexts"] = transformKeyValue
transformers["services.*.depends_on"] = transformDependsOn
transformers["services.*.deploy.resources.reservations.devices.*"] = transformDeviceRequest
transformers["services.*.env_file"] = transformEnvFile
transformers["services.*.extends"] = transformExtends
transformers["services.*.networks"] = transformServiceNetworks
Expand Down
40 changes: 40 additions & 0 deletions transform/devices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
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 transform

import (
"fmt"

"github.com/compose-spec/compose-go/v2/tree"
)

func transformDeviceRequest(data any, p tree.Path, ignoreParseError bool) (any, error) {
switch v := data.(type) {
case map[string]any:
_, hasCount := v["count"]
_, hasIds := v["device_ids"]
if hasCount && hasIds {
return nil, fmt.Errorf(`%s: "count" and "device_ids" attributes are exclusive`, p)
}
if !hasCount && !hasIds {
v["count"] = "all"
}
return transformMapping(v, p, ignoreParseError)
default:
return data, fmt.Errorf("%s: invalid type %T for device request", p, v)
}
}
52 changes: 0 additions & 52 deletions types/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,55 +51,3 @@ func (c *DeviceCount) DecodeMapstructure(value interface{}) error {
}
return nil
}

func (d *DeviceRequest) DecodeMapstructure(value interface{}) error {
v, ok := value.(map[string]any)
if !ok {
return fmt.Errorf("invalid device request type %T", value)
}
if _, okCaps := v["capabilities"]; !okCaps {
return fmt.Errorf(`"capabilities" attribute is mandatory for device request definition`)
}
if _, okCount := v["count"]; okCount {
if _, okDeviceIds := v["device_ids"]; okDeviceIds {
return fmt.Errorf(`invalid "count" and "device_ids" are attributes are exclusive`)
}
}
d.Count = DeviceCount(-1)

capabilities := v["capabilities"]
caps := StringList{}
if err := caps.DecodeMapstructure(capabilities); err != nil {
return err
}
d.Capabilities = caps
if driver, ok := v["driver"]; ok {
if val, ok := driver.(string); ok {
d.Driver = val
} else {
return fmt.Errorf("invalid type for driver value: %T", driver)
}
}
if count, ok := v["count"]; ok {
if err := d.Count.DecodeMapstructure(count); err != nil {
return err
}
}
if deviceIDs, ok := v["device_ids"]; ok {
ids := StringList{}
if err := ids.DecodeMapstructure(deviceIDs); err != nil {
return err
}
d.IDs = ids
d.Count = DeviceCount(len(ids))
}

d.Options = Mapping{}
if options, ok := v["options"].(map[string]any); ok {
for k, v := range options {
d.Options[k] = fmt.Sprint(v)
}
}
return nil

}