Skip to content

Commit

Permalink
Add support for pid_mode
Browse files Browse the repository at this point in the history
Also adds a bit of coverage for network_mode.
  • Loading branch information
aughr committed Oct 12, 2018
1 parent e172b22 commit c81e386
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@ In `host` and `container...` network modes, you may specify a
for container health checks. The mapping itself, while still passed via the API,
will be ignored by Docker.

### PID modes

You may specify the PID mode you would like a container to use via:

```ruby
set :pid_mode, 'pidmode'
```

Docker (and therefore Centurion) supports one of nothing (the default), `host`,
and `container:<container-id>` for this argument.

### CGroup Resource Constraints

Limits on memory and CPU can be specified with the `memory` and `cpu_shares`
Expand Down
10 changes: 5 additions & 5 deletions lib/centurion/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Centurion
class Service
extend ::Capistrano::DSL

attr_accessor :command, :dns, :extra_hosts, :image, :name, :volumes, :port_bindings, :network_mode, :cap_adds, :cap_drops, :ipc_mode, :security_opt
attr_accessor :command, :dns, :extra_hosts, :image, :name, :volumes, :port_bindings, :network_mode, :pid_mode, :cap_adds, :cap_drops, :ipc_mode, :security_opt
attr_reader :memory, :cpu_shares, :env_vars, :labels

def initialize(name)
Expand Down Expand Up @@ -35,6 +35,7 @@ def self.from_env
s.volumes = fetch(:binds, [])
s.port_bindings = fetch(:port_bindings, [])
s.network_mode = fetch(:network_mode, 'bridge')
s.pid_mode = fetch(:pid_mode, nil)
s.command = fetch(:command, nil)
s.memory = fetch(:memory, 0)
s.cpu_shares = fetch(:cpu_shares, 0)
Expand Down Expand Up @@ -76,10 +77,6 @@ def cap_drops=(capabilites)
@cap_drops = capabilites
end

def network_mode=(mode)
@network_mode = mode
end

def memory=(bytes)
if !bytes || !is_a_uint64?(bytes)
raise ArgumentError, "invalid value for cgroup memory constraint: #{bytes}, value must be a between 0 and 18446744073709551615"
Expand Down Expand Up @@ -159,6 +156,9 @@ def build_host_config(restart_policy = nil)
# Set the network mode
host_config['NetworkMode'] = network_mode

# Set the pid mode if specified
host_config['PidMode'] = pid_mode if pid_mode

# DNS if specified
host_config['Dns'] = dns if dns

Expand Down
2 changes: 1 addition & 1 deletion lib/centurion/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Centurion
VERSION = '1.9.2'
VERSION = '1.10.0'
end
43 changes: 43 additions & 0 deletions spec/service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
set(:port_bindings, [ Centurion::Service::PortBinding.new(12340, 80, 'tcp') ])
set(:labels, labels)
set(:security_opt, ['seccomp=unconfined'])
set(:network_mode, 'host')
set(:pid_mode, 'host')

svc = Centurion::Service.from_env
expect(svc.name).to eq('mycontainer')
Expand All @@ -29,6 +31,8 @@
expect(svc.port_bindings.first.container_port).to eq(80)
expect(svc.labels).to eq(labels)
expect(svc.security_opt).to eq(['seccomp=unconfined'])
expect(svc.network_mode).to eq('host')
expect(svc.pid_mode).to eq('host')
end

it 'starts with a command' do
Expand Down Expand Up @@ -64,6 +68,14 @@
expect(service.dns).to eq('redis.example.com')
end

it 'has a default network mode' do
expect(service.network_mode).to eq('bridge')
end

it 'has no default pid mode' do
expect(service.pid_mode).to be(nil)
end

it 'boots from a docker image' do
service.image = 'registry.hub.docker.com/library/redis'
expect(service.image).to eq('registry.hub.docker.com/library/redis')
Expand Down Expand Up @@ -309,4 +321,35 @@
})
end

it 'builds docker configuration for container-linked pids' do
service.pid_mode = 'container:a2e8937b'
expect(service.build_host_config(Centurion::Service::RestartPolicy.new('on-failure', 50))).to eq({
'Binds' => [],
'CapAdd' => [],
'CapDrop' => [],
'NetworkMode' => 'bridge',
'PidMode' => 'container:a2e8937b',
'PortBindings' => {},
'RestartPolicy' => {
'Name' => 'on-failure',
'MaximumRetryCount' => 50
}
})
end

it 'builds docker configuration for host pids' do
service.pid_mode = 'host'
expect(service.build_host_config(Centurion::Service::RestartPolicy.new('on-failure', 50))).to eq({
'Binds' => [],
'CapAdd' => [],
'CapDrop' => [],
'NetworkMode' => 'bridge',
'PidMode' => 'host',
'PortBindings' => {},
'RestartPolicy' => {
'Name' => 'on-failure',
'MaximumRetryCount' => 50
}
})
end
end

0 comments on commit c81e386

Please sign in to comment.