Skip to content

Commit

Permalink
Handle networks.driver_opts for a service
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
robmry committed Jun 6, 2024
1 parent 9b61bbb commit 6bad170
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 6 deletions.
7 changes: 5 additions & 2 deletions cli/compose/convert/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,19 @@ func convertServiceNetworks(
return nil, errors.Errorf("undefined network %q", networkName)
}
var aliases []string
var driverOpts map[string]string
if network != nil {
aliases = network.Aliases
driverOpts = network.DriverOpts
}
target := namespace.Scope(networkName)
if networkConfig.Name != "" {
target = networkConfig.Name
}
netAttachConfig := swarm.NetworkAttachmentConfig{
Target: target,
Aliases: aliases,
Target: target,
Aliases: aliases,
DriverOpts: driverOpts,
}
// Only add default aliases to user defined networks. Other networks do
// not support aliases.
Expand Down
8 changes: 8 additions & 0 deletions cli/compose/convert/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ func TestConvertServiceNetworks(t *testing.T) {
networks := map[string]*composetypes.ServiceNetworkConfig{
"front": {
Aliases: []string{"something"},
DriverOpts: map[string]string{
"driver.opt1": "optval1",
"driver.opt2": "optval2",
},
},
"back": {
Aliases: []string{"other"},
Expand All @@ -257,6 +261,10 @@ func TestConvertServiceNetworks(t *testing.T) {
{
Target: "fronttier",
Aliases: []string{"something", "service"},
DriverOpts: map[string]string{
"driver.opt1": "optval1",
"driver.opt2": "optval2",
},
},
}

Expand Down
3 changes: 3 additions & 0 deletions cli/compose/loader/full-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ services:
aliases:
- alias1
- alias3
driver_opts:
"driveropt1": "optval1"
"driveropt2": "optval2"
other-network:
ipv4_address: 172.16.238.10
ipv6_address: 2001:3984:3989::10
Expand Down
4 changes: 4 additions & 0 deletions cli/compose/loader/full-struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ func services(workingDir, homeDir string) []types.ServiceConfig {
Aliases: []string{"alias1", "alias3"},
Ipv4Address: "",
Ipv6Address: "",
DriverOpts: map[string]string{
"driveropt1": "optval1",
"driveropt2": "optval2",
},
},
"other-network": {
Ipv4Address: "172.16.238.10",
Expand Down
6 changes: 5 additions & 1 deletion cli/compose/loader/testdata/full-example.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,11 @@
"aliases": [
"alias1",
"alias3"
]
],
"driver_opts": {
"driveropt1": "optval1",
"driveropt2": "optval2"
}
}
},
"pid": "host",
Expand Down
3 changes: 3 additions & 0 deletions cli/compose/loader/testdata/full-example.yaml.golden
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ services:
aliases:
- alias1
- alias3
driver_opts:
driveropt1: optval1
driveropt2: optval2
pid: host
ports:
- mode: ingress
Expand Down
6 changes: 6 additions & 0 deletions cli/compose/schema/data/config_schema_v3.12.json
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@
"type": "object",
"properties": {
"aliases": {"$ref": "#/definitions/list_of_strings"},
"driver_opts": {
"type": "object",
"patternProperties": {
"^.+$": { "type": ["string", "number"] }
}
},
"ipv4_address": {"type": "string"},
"ipv6_address": {"type": "string"}
},
Expand Down
7 changes: 4 additions & 3 deletions cli/compose/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,10 @@ type PlacementPreferences struct {

// ServiceNetworkConfig is the network configuration for a service
type ServiceNetworkConfig struct {
Aliases []string `yaml:",omitempty" json:"aliases,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"`
Aliases []string `yaml:",omitempty" json:"aliases,omitempty"`
DriverOpts map[string]string `mapstructure:"driver_opts" yaml:"driver_opts,omitempty" json:"driver_opts,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
Expand Down

0 comments on commit 6bad170

Please sign in to comment.