Skip to content

Commit

Permalink
long syntax for device mapping
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
  • Loading branch information
ndeloof committed Aug 19, 2024
1 parent 2ac541e commit a9798f8
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 5 deletions.
5 changes: 4 additions & 1 deletion 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
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:
- /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
56 changes: 56 additions & 0 deletions transform/device.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
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) {

Check failure on line 26 in transform/device.go

View workflow job for this annotation

GitHub Actions / test (1.22, ubuntu-latest)

unused-parameter: parameter 'ignoreParseError' seems to be unused, consider removing or renaming it as _ (revive)

Check failure on line 26 in transform/device.go

View workflow job for this annotation

GitHub Actions / test (1.21, ubuntu-latest)

unused-parameter: parameter 'ignoreParseError' seems to be unused, consider removing or renaming it as _ (revive)

Check failure on line 26 in transform/device.go

View workflow job for this annotation

GitHub Actions / test (1.21, macos-latest)

unused-parameter: parameter 'ignoreParseError' seems to be unused, consider removing or renaming it as _ (revive)
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]
}
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:"-"`
}

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

0 comments on commit a9798f8

Please sign in to comment.