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

long syntax for device mapping #676

Merged
merged 1 commit into from
Aug 21, 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: 3 additions & 1 deletion loader/full-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ services:
- "a 7:* rmw"

devices:
- "/dev/ttyUSB0:/dev/ttyUSB0"
- source: /dev/ttyUSB0
target: /dev/ttyUSB0
permissions: rwm

# String or list
# dns: 8.8.8.8
Expand Down
15 changes: 12 additions & 3 deletions loader/full-struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ func services(workingDir, homeDir string) types.Services {
"c 1:3 mr",
"a 7:* rmw",
},
Devices: []string{"/dev/ttyUSB0:/dev/ttyUSB0"},
Devices: []types.DeviceMapping{
{
Source: "/dev/ttyUSB0", Target: "/dev/ttyUSB0", Permissions: "rwm",
}},
DNS: []string{"8.8.8.8", "9.9.9.9"},
DNSSearch: []string{"dc1.example.com", "dc2.example.com"},
DomainName: "foo.com",
Expand Down Expand Up @@ -719,7 +722,9 @@ services:
- c 1:3 mr
- a 7:* rmw
devices:
- /dev/ttyUSB0:/dev/ttyUSB0
- source: /dev/ttyUSB0
target: /dev/ttyUSB0
permissions: rwm
dns:
- 8.8.8.8
- 9.9.9.9
Expand Down Expand Up @@ -1314,7 +1319,11 @@ func fullExampleJSON(workingDir, homeDir string) string {
"a 7:* rmw"
],
"devices": [
"/dev/ttyUSB0:/dev/ttyUSB0"
{
"source": "/dev/ttyUSB0",
"target": "/dev/ttyUSB0",
"permissions": "rwm"
}
],
"dns": [
"8.8.8.8",
Expand Down
48 changes: 48 additions & 0 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3329,3 +3329,51 @@ secrets:
Content: "Shadoks",
}})
}

func TestLoadDeviceMapping(t *testing.T) {
config, err := loadYAML(`
name: load-device-mapping
services:
test:
devices:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also introduce the test case for the object/map case (with an extension)?

- /dev/source:/dev/target:permissions
- /dev/single
`)
assert.NilError(t, err)
assert.DeepEqual(t, config.Services["test"].Devices, []types.DeviceMapping{
{
Source: "/dev/source",
Target: "/dev/target",
Permissions: "permissions",
},
{
Source: "/dev/single",
Target: "/dev/single",
Permissions: "rwm",
},
})
}

func TestLoadDeviceMappingLongSyntax(t *testing.T) {
config, err := loadYAML(`
name: load-device-mapping-long-syntax
services:
test:
devices:
- source: /dev/source
target: /dev/target
permissions: permissions
x-foo: bar
`)
assert.NilError(t, err)
assert.DeepEqual(t, config.Services["test"].Devices, []types.DeviceMapping{
{
Source: "/dev/source",
Target: "/dev/target",
Permissions: "permissions",
Extensions: map[string]any{
"x-foo": "bar",
},
},
})
}
19 changes: 19 additions & 0 deletions override/uncity.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func init() {
unique["services.*.sysctls"] = keyValueIndexer
unique["services.*.tmpfs"] = keyValueIndexer
unique["services.*.volumes"] = volumeIndexer
unique["services.*.devices"] = deviceMappingIndexer
}

// EnforceUnicity removes redefinition of elements declared in a sequence
Expand Down Expand Up @@ -139,6 +140,24 @@ func volumeIndexer(y any, p tree.Path) (string, error) {
return "", nil
}

func deviceMappingIndexer(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 device %s is missing a mount target", p)
}
return target, nil
case string:
arr := strings.Split(value, ":")
if len(arr) == 1 {
return arr[0], nil
}
return arr[1], nil
}
return "", nil
}

func exposeIndexer(a any, path tree.Path) (string, error) {
switch v := a.(type) {
case string:
Expand Down
20 changes: 19 additions & 1 deletion schema/compose-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,25 @@
]
},
"device_cgroup_rules": {"$ref": "#/definitions/list_of_strings"},
"devices": {"type": "array", "items": {"type": "string"}, "uniqueItems": true},
"devices": {
"type": "array",
"items": {
"oneOf": [
{"type": "string"},
{
"type": "object",
"required": ["source"],
"properties": {
"source": {"type": "string"},
"target": {"type": "string"},
"permissions": {"type": "string"}
},
"additionalProperties": false,
"patternProperties": {"^x-": {}}
}
]
}
},
"dns": {"$ref": "#/definitions/string_or_list"},
"dns_opt": {"type": "array","items": {"type": "string"}, "uniqueItems": true},
"dns_search": {"$ref": "#/definitions/string_or_list"},
Expand Down
1 change: 1 addition & 0 deletions transform/canonical.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func init() {
transformers["services.*.extends"] = transformExtends
transformers["services.*.networks"] = transformServiceNetworks
transformers["services.*.volumes.*"] = transformVolumeMount
transformers["services.*.devices.*"] = transformDeviceMapping
transformers["services.*.secrets.*"] = transformFileMount
transformers["services.*.configs.*"] = transformFileMount
transformers["services.*.ports"] = transformPorts
Expand Down
60 changes: 60 additions & 0 deletions transform/device.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
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"
"strings"

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

func transformDeviceMapping(data any, p tree.Path, ignoreParseError bool) (any, error) {
switch v := data.(type) {
case map[string]any:
return v, nil
case string:
src := ""
dst := ""
permissions := "rwm"
arr := strings.Split(v, ":")
switch len(arr) {
case 3:
permissions = arr[2]
fallthrough
case 2:
dst = arr[1]
fallthrough
case 1:
src = arr[0]
default:
if !ignoreParseError {
return nil, fmt.Errorf("confusing device mapping, please use long syntax: %s", v)
}
}
if dst == "" {
dst = src
}
return map[string]any{
"source": src,
"target": dst,
"permissions": permissions,
}, nil
default:
return data, fmt.Errorf("%s: invalid type %T for service volume mount", p, v)
}
}
4 changes: 2 additions & 2 deletions types/derived.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type ServiceConfig struct {
DependsOn DependsOnConfig `yaml:"depends_on,omitempty" json:"depends_on,omitempty"`
Deploy *DeployConfig `yaml:"deploy,omitempty" json:"deploy,omitempty"`
DeviceCgroupRules []string `yaml:"device_cgroup_rules,omitempty" json:"device_cgroup_rules,omitempty"`
Devices []string `yaml:"devices,omitempty" json:"devices,omitempty"`
Devices []DeviceMapping `yaml:"devices,omitempty" json:"devices,omitempty"`
DNS StringList `yaml:"dns,omitempty" json:"dns,omitempty"`
DNSOpts []string `yaml:"dns_opt,omitempty" json:"dns_opt,omitempty"`
DNSSearch StringList `yaml:"dns_search,omitempty" json:"dns_search,omitempty"`
Expand Down Expand Up @@ -301,6 +301,14 @@ type BlkioConfig struct {
Extensions Extensions `yaml:"#extensions,inline,omitempty" json:"-"`
}

type DeviceMapping struct {
Source string `yaml:"source,omitempty" json:"source,omitempty"`
Target string `yaml:"target,omitempty" json:"target,omitempty"`
Permissions string `yaml:"permissions,omitempty" json:"permissions,omitempty"`

Extensions Extensions `yaml:"#extensions,inline,omitempty" json:"-"`
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing Extension? From compse-spec you defined the pattern for Extensions


// WeightDevice is a structure that holds device:weight pair
type WeightDevice struct {
Path string
Expand Down