Handle networks.driver_opts for a service

These are endpoint-specific driver options...

services:
  myservice:
    image: myimage
    networks:
      mynet:
        driver_opts:
          "option1": "value1"

The API has had support for a long time, it's only recently been
added to compose (unreleased right now).

Signed-off-by: Rob Murray <rob.murray@docker.com>
This commit is contained in:
Rob Murray 2024-06-07 09:13:21 +01:00
parent a731722652
commit 94f9de5928
8 changed files with 38 additions and 6 deletions

View File

@ -215,16 +215,19 @@ func convertServiceNetworks(
return nil, errors.Errorf("undefined network %q", networkName) return nil, errors.Errorf("undefined network %q", networkName)
} }
var aliases []string var aliases []string
var driverOpts map[string]string
if network != nil { if network != nil {
aliases = network.Aliases aliases = network.Aliases
driverOpts = network.DriverOpts
} }
target := namespace.Scope(networkName) target := namespace.Scope(networkName)
if networkConfig.Name != "" { if networkConfig.Name != "" {
target = networkConfig.Name target = networkConfig.Name
} }
netAttachConfig := swarm.NetworkAttachmentConfig{ netAttachConfig := swarm.NetworkAttachmentConfig{
Target: target, Target: target,
Aliases: aliases, Aliases: aliases,
DriverOpts: driverOpts,
} }
// Only add default aliases to user defined networks. Other networks do // Only add default aliases to user defined networks. Other networks do
// not support aliases. // not support aliases.

View File

@ -240,6 +240,10 @@ func TestConvertServiceNetworks(t *testing.T) {
networks := map[string]*composetypes.ServiceNetworkConfig{ networks := map[string]*composetypes.ServiceNetworkConfig{
"front": { "front": {
Aliases: []string{"something"}, Aliases: []string{"something"},
DriverOpts: map[string]string{
"driver.opt1": "optval1",
"driver.opt2": "optval2",
},
}, },
"back": { "back": {
Aliases: []string{"other"}, Aliases: []string{"other"},
@ -257,6 +261,10 @@ func TestConvertServiceNetworks(t *testing.T) {
{ {
Target: "fronttier", Target: "fronttier",
Aliases: []string{"something", "service"}, Aliases: []string{"something", "service"},
DriverOpts: map[string]string{
"driver.opt1": "optval1",
"driver.opt2": "optval2",
},
}, },
} }

View File

@ -207,6 +207,9 @@ services:
aliases: aliases:
- alias1 - alias1
- alias3 - alias3
driver_opts:
"driveropt1": "optval1"
"driveropt2": "optval2"
other-network: other-network:
ipv4_address: 172.16.238.10 ipv4_address: 172.16.238.10
ipv6_address: 2001:3984:3989::10 ipv6_address: 2001:3984:3989::10

View File

@ -190,6 +190,10 @@ func services(workingDir, homeDir string) []types.ServiceConfig {
Aliases: []string{"alias1", "alias3"}, Aliases: []string{"alias1", "alias3"},
Ipv4Address: "", Ipv4Address: "",
Ipv6Address: "", Ipv6Address: "",
DriverOpts: map[string]string{
"driveropt1": "optval1",
"driveropt2": "optval2",
},
}, },
"other-network": { "other-network": {
Ipv4Address: "172.16.238.10", Ipv4Address: "172.16.238.10",

View File

@ -285,7 +285,11 @@
"aliases": [ "aliases": [
"alias1", "alias1",
"alias3" "alias3"
] ],
"driver_opts": {
"driveropt1": "optval1",
"driveropt2": "optval2"
}
} }
}, },
"pid": "host", "pid": "host",

View File

@ -152,6 +152,9 @@ services:
aliases: aliases:
- alias1 - alias1
- alias3 - alias3
driver_opts:
driveropt1: optval1
driveropt2: optval2
pid: host pid: host
ports: ports:
- mode: ingress - mode: ingress

View File

@ -197,6 +197,12 @@
"type": "object", "type": "object",
"properties": { "properties": {
"aliases": {"$ref": "#/definitions/list_of_strings"}, "aliases": {"$ref": "#/definitions/list_of_strings"},
"driver_opts": {
"type": "object",
"patternProperties": {
"^.+$": { "type": ["string", "number"] }
}
},
"ipv4_address": {"type": "string"}, "ipv4_address": {"type": "string"},
"ipv6_address": {"type": "string"} "ipv6_address": {"type": "string"}
}, },

View File

@ -374,9 +374,10 @@ type PlacementPreferences struct {
// ServiceNetworkConfig is the network configuration for a service // ServiceNetworkConfig is the network configuration for a service
type ServiceNetworkConfig struct { type ServiceNetworkConfig struct {
Aliases []string `yaml:",omitempty" json:"aliases,omitempty"` Aliases []string `yaml:",omitempty" json:"aliases,omitempty"`
Ipv4Address string `mapstructure:"ipv4_address" yaml:"ipv4_address,omitempty" json:"ipv4_address,omitempty"` DriverOpts map[string]string `mapstructure:"driver_opts" yaml:"driver_opts,omitempty" json:"driver_opts,omitempty"`
Ipv6Address string `mapstructure:"ipv6_address" yaml:"ipv6_address,omitempty" json:"ipv6_address,omitempty"` Ipv4Address string `mapstructure:"ipv4_address" yaml:"ipv4_address,omitempty" json:"ipv4_address,omitempty"`
Ipv6Address string `mapstructure:"ipv6_address" yaml:"ipv6_address,omitempty" json:"ipv6_address,omitempty"`
} }
// ServicePortConfig is the port configuration for a service // ServicePortConfig is the port configuration for a service