Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with container network mode causing unnecessary redeployment #1290

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading