Skip to content

Commit

Permalink
Add support for devices in swarm
Browse files Browse the repository at this point in the history
Signed-off-by: zikaeroh <[email protected]>
  • Loading branch information
zikaeroh committed Dec 24, 2022
1 parent 139e924 commit 465e2f5
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 75 deletions.
2 changes: 2 additions & 0 deletions cli/command/service/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func newCreateCommand(dockerCli command.Cli) *cobra.Command {
flags.SetAnnotation(flagSysCtl, "version", []string{"1.40"})
flags.Var(&opts.ulimits, flagUlimit, "Ulimit options")
flags.SetAnnotation(flagUlimit, "version", []string{"1.41"})
flags.Var(&opts.devices, flagDevice, "Devices to add")
flags.SetAnnotation(flagDevice, "version", []string{"1.42"})

flags.Var(cliopts.NewListOptsRef(&opts.resources.resGenericResources, ValidateSingleGenericResource), "generic-resource", "User defined resources")
flags.SetAnnotation(flagHostAdd, "version", []string{"1.32"})
Expand Down
16 changes: 16 additions & 0 deletions cli/command/service/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ Ulimits:
{{- range $k, $v := .ContainerUlimits }}
{{ $k }}: {{ $v }}
{{- end }}{{ end }}
{{- if .ContainerDevices }}
Devices:
{{- range $port := .ContainerDevices }}
PathOnHost = {{ $port.PathOnHost }}
PathInContainer = {{ $port.PathInContainer }}
CgroupPermissions = {{ $port.CgroupPermissions }}
{{- end }} {{ end -}}
{{- if .ContainerMounts }}
Mounts:
{{- end }}
Expand Down Expand Up @@ -487,6 +494,15 @@ func (ctx *serviceInspectContext) HasContainerUlimits() bool {
return len(ctx.Service.Spec.TaskTemplate.ContainerSpec.Ulimits) > 0
}


func (ctx *serviceInspectContext) ContainerDevices() []container.DeviceMapping {
return ctx.Service.Spec.TaskTemplate.ContainerSpec.Devices;
}

func (ctx *serviceInspectContext) HasContainerDevices() bool {
return len(ctx.Service.Spec.TaskTemplate.ContainerSpec.Devices) > 0
}

func (ctx *serviceInspectContext) HasResources() bool {
return ctx.Service.Spec.TaskTemplate.Resources != nil
}
Expand Down
3 changes: 3 additions & 0 deletions cli/command/service/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ type serviceOptions struct {
capAdd opts.ListOpts
capDrop opts.ListOpts
ulimits opts.UlimitOpt
devices opts.ListOpts

resources resourceOptions
stopGrace opts.DurationOpt
Expand Down Expand Up @@ -559,6 +560,7 @@ func newServiceOptions() *serviceOptions {
capAdd: opts.NewListOpts(nil),
capDrop: opts.NewListOpts(nil),
ulimits: *opts.NewUlimitOpt(nil),
devices: opts.NewListOpts(nil),
}
}

Expand Down Expand Up @@ -1024,6 +1026,7 @@ const (
flagUlimit = "ulimit"
flagUlimitAdd = "ulimit-add"
flagUlimitRemove = "ulimit-rm"
flagDevice = "device"
)

func validateAPIVersion(c swarm.ServiceSpec, serverAPIVersion string) error {
Expand Down
33 changes: 33 additions & 0 deletions cli/compose/convert/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ func Service(
}
}

devices, err := convertDevices(service.Devices)
if err != nil {
return swarm.ServiceSpec{}, err
}

capAdd, capDrop := opts.EffectiveCapAddCapDrop(service.CapAdd, service.CapDrop)

serviceSpec := swarm.ServiceSpec{
Expand Down Expand Up @@ -153,6 +158,7 @@ func Service(
CapabilityAdd: capAdd,
CapabilityDrop: capDrop,
Ulimits: convertUlimits(service.Ulimits),
Devices: devices,
},
LogDriver: logDriver,
Resources: resources,
Expand Down Expand Up @@ -719,3 +725,30 @@ func convertUlimits(origUlimits map[string]*composetypes.UlimitsConfig) []*units
})
return ulimits
}

func convertDevices(devices []string) ([]container.DeviceMapping, error) {
newDevices := make([]container.DeviceMapping, len(devices))
for i, device := range devices {
parts := strings.Split(device, ":")
if len(parts) < 1 || len(parts) > 3 {
return nil, errors.New("failed to parse device")
}

mapping := container.DeviceMapping{
PathOnHost: parts[0],
PathInContainer: parts[0],
}

if len(parts) > 1 {
mapping.PathInContainer = parts[1]
}

if len(parts) == 3 {
mapping.CgroupPermissions = parts[2]
}

newDevices[i] = mapping
}

return newDevices, nil
}
1 change: 0 additions & 1 deletion cli/compose/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var UnsupportedProperties = []string{
"build",
"cgroupns_mode",
"cgroup_parent",
"devices",
"domainname",
"external_links",
"ipc",
Expand Down
Loading

0 comments on commit 465e2f5

Please sign in to comment.