Skip to content

Commit

Permalink
Fix issue with container network mode causing unnecessary redeployment (
Browse files Browse the repository at this point in the history
#1290)

* Fix issue with container network mode causing unnecessary redeployment when using `container:<name>` format

- Added network mode normalization to handle container IDs consistently
- Prevents container recreation when only the container ID format changes

---------

Signed-off-by: Dan Webb <dan.webb@damacus.io>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
  • Loading branch information
damacus and Copilot authored Dec 11, 2024
1 parent cc25fe7 commit 918a120
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

- Fix issue with container network mode causing unnecessary redeployment when using `container:<name>` format
- Added network mode normalization to handle container IDs consistently
- Prevents container recreation when only the container ID format changes

## 11.8.0 - *2024-12-11*

- Add volume_prune resource
Expand Down
16 changes: 16 additions & 0 deletions libraries/helpers_network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ def ip_address_from_container_networks(container)
container.info['NetworkSettings']['Networks'].values[0]['IPAMConfig']['IPv4Address']
end
end

def normalize_container_network_mode(mode)
return mode unless mode.is_a?(String) && mode.start_with?('container:')

# Extract container name/id from network mode
container_ref = mode.split(':', 2)[1]
begin
# Try to get the container by name or ID
container = Docker::Container.get(container_ref, {}, connection)
# Return normalized form with full container ID
"container:#{container.id}"
rescue Docker::Error::NotFoundError, Docker::Error::TimeoutError
# If container not found, return original value
mode
end
end
end
end
end
23 changes: 22 additions & 1 deletion resources/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@ def to_snake_case(name)
when 'NanoCpus'
property_name = 'cpus'
value = (value / (10**9)).to_i
when 'NetworkMode'
property_name = 'network_mode'
value = normalize_container_network_mode(value)
else
property_name = to_snake_case(key)
end
Expand Down Expand Up @@ -501,7 +504,7 @@ def load_container_labels
'MemorySwappiness' => new_resource.memory_swappiness,
'MemoryReservation' => new_resource.memory_reservation,
'NanoCpus' => new_resource.cpus,
'NetworkMode' => new_resource.network_mode,
'NetworkMode' => normalize_container_network_mode(new_resource.network_mode),
'OomKillDisable' => new_resource.oom_kill_disable,
'OomScoreAdj' => new_resource.oom_score_adj,
'Privileged' => new_resource.privileged,
Expand Down Expand Up @@ -743,4 +746,22 @@ def ulimits_to_hash
def read_env_file
new_resource.env_file.map { |f| ::File.readlines(f).map(&:strip) }.flatten
end

def normalize_container_network_mode(value)
if value.is_a?(String) && value.start_with?('container:')
# Use the network helper method for container network mode
DockerCookbook::DockerHelpers::Network.normalize_container_network_mode(value)
else
case value
when 'host'
'host'
when 'none'
'none'
when 'default'
'bridge'
else
value
end
end
end
end

0 comments on commit 918a120

Please sign in to comment.