Skip to content

Commit

Permalink
Merge pull request sous-chefs#1042 from smcavallo/network_alias_fix
Browse files Browse the repository at this point in the history
fix issues with network_mode
  • Loading branch information
tas50 authored Dec 9, 2018
2 parents 3b9d146 + 9bdc837 commit 1373ff2
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 15 deletions.
43 changes: 28 additions & 15 deletions libraries/docker_container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -422,13 +422,26 @@ def to_snake_case(name)
restart_maximum_retry_count container.info['HostConfig']['RestartPolicy']['MaximumRetryCount']
volumes_binds container.info['HostConfig']['Binds']
ro_rootfs container.info['HostConfig']['ReadonlyRootfs']
if container.info['NetworkSettings'] &&
container.info['NetworkSettings']['Networks'] &&
container.info['NetworkSettings']['Networks'][new_resource.network_mode] &&
container.info['NetworkSettings']['Networks'][new_resource.network_mode]['IPAMConfig'] &&
container.info['NetworkSettings']['Networks'][new_resource.network_mode]['IPAMConfig']['IPv4Address']
ip_address ip_address_from_container_networks(container) unless ip_address_from_container_networks(container).nil?
end

ip_address container.info['NetworkSettings']['Networks'][new_resource.network_mode]['IPAMConfig']['IPv4Address']
# Gets the ip address from the existing container
# current docker api of 1.16 does not have ['NetworkSettings']['Networks']
# For docker > 1.21 - use ['NetworkSettings']['Networks']
#
# @param container [Docker::Container] A container object
# @returns [String] An ip_address
def ip_address_from_container_networks(container)
# We use the first value in 'Networks'
# We can't assume it will be 'bridged'
# It might also not match the new_resource value
if container.info['NetworkSettings'] &&
container.info['NetworkSettings']['Networks'] &&
container.info['NetworkSettings']['Networks'].values[0] &&
container.info['NetworkSettings']['Networks'].values[0]['IPAMConfig'] &&
container.info['NetworkSettings']['Networks'].values[0]['IPAMConfig']['IPv4Address']
# Return the ip address listed
container.info['NetworkSettings']['Networks'].values[0]['IPAMConfig']['IPv4Address']
end
end

Expand Down Expand Up @@ -696,15 +709,15 @@ def validate_container_create

if new_resource.network_mode == 'container' &&
(
!(new_reosurce.hostname.nil? || new_reosurce.hostname.empty?) ||
!(new_reosurce.dns.nil? || new_reosurce.dns.empty?) ||
!(new_reosurce.dns_search.nil? || new_reosurce.dns_search.empty?) ||
!(new_reosurce.mac_address.nil? || new_reosurce.mac_address.empty?) ||
!(new_reosurce.extra_hosts.nil? || new_reosurce.extra_hosts.empty?) ||
!(new_reosurce.exposed_ports.nil? || new_reosurce.exposed_ports.empty?) ||
!(new_reosurce.port_bindings.nil? || new_reosurce.port_bindings.empty?) ||
!(new_reosurce.publish_all_ports.nil? || new_reosurce.publish_all_ports.empty?) ||
!new_reosurce.port.nil?
!(new_resource.hostname.nil? || new_resource.hostname.empty?) ||
!(new_resource.dns.nil? || new_resource.dns.empty?) ||
!(new_resource.dns_search.nil? || new_resource.dns_search.empty?) ||
!(new_resource.mac_address.nil? || new_resource.mac_address.empty?) ||
!(new_resource.extra_hosts.nil? || new_resource.extra_hosts.empty?) ||
!(new_resource.exposed_ports.nil? || new_resource.exposed_ports.empty?) ||
!(new_resource.port_bindings.nil? || new_resource.port_bindings.empty?) ||
!(new_resource.publish_all_ports.nil? || new_resource.publish_all_ports.empty?) ||
!new_resource.port.nil?
)
raise Chef::Exceptions::ValidationFailed, 'Cannot specify hostname, dns, dns_search, mac_address, extra_hosts, exposed_ports, port_bindings, publish_all_ports, port when network_mode is container.'
end
Expand Down
55 changes: 55 additions & 0 deletions spec/libraries/container_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Load all the libraries
require 'chef'
require 'docker'
Dir['libraries/*.rb'].each { |f| require File.expand_path(f) }

describe DockerCookbook::DockerContainer do
let(:resource) { DockerCookbook::DockerContainer.new('hello_world') }

it 'has a default action of [:run]' do
expect(resource.action).to eql([:run])
end

describe 'gets ip_address_from_container_networks' do
let(:options) { { 'id' => rand(10_000).to_s } }
subject do
Docker::Container.send(:new, Docker.connection, options)
end
# https://docs.docker.com/engine/api/version-history/#v121-api-changes
context 'when docker API < 1.21' do
let(:ip_address) { '10.0.0.1' }
let(:options) do
{
'id' => rand(10_000).to_s,
'IPAddress' => ip_address
}
end
it 'gets ip_address as nil' do
actual = resource.ip_address_from_container_networks(subject)
expect { resource.ip_address_from_container_networks(subject) }.not_to raise_error
expect(actual).to eq(nil)
end
end
context 'when docker API > 1.21' do
let(:ip_address) { '10.0.0.1' }
let(:options) do
{
'id' => rand(10_000).to_s,
'NetworkSettings' => {
'Networks' => {
'bridge' => {
'IPAMConfig' => {
'IPv4Address' => ip_address
}
}
}
}
}
end
it 'gets ip_address' do
actual = resource.ip_address_from_container_networks(subject)
expect(actual).to eq(ip_address)
end
end
end
end

0 comments on commit 1373ff2

Please sign in to comment.