Skip to content
Merged
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
12 changes: 7 additions & 5 deletions backend/app/service/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,14 @@ func (u *ContainerService) ContainerInfo(req dto.OperationWithName) (*dto.Contai
networkSettings := oldContainer.NetworkSettings
bridgeNetworkSettings := networkSettings.Networks[data.Network]
if bridgeNetworkSettings.IPAMConfig != nil {
ipv4Address := bridgeNetworkSettings.IPAMConfig.IPv4Address
data.Ipv4 = ipv4Address
ipv6Address := bridgeNetworkSettings.IPAMConfig.IPv6Address
data.Ipv6 = ipv6Address
if data.Network != "bridge" {
data.Ipv4 = bridgeNetworkSettings.IPAMConfig.IPv4Address
data.Ipv6 = bridgeNetworkSettings.IPAMConfig.IPv6Address
}
} else {
data.Ipv4 = bridgeNetworkSettings.IPAddress
if data.Network != "bridge" {
data.Ipv4 = bridgeNetworkSettings.IPAddress
}
}

data.Cmd = oldContainer.Config.Cmd
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The given code appears to have some redundancy and unnecessary logic that can be simplified. Specifically, when dealing with the bridge network, both IPv4 and IPv6 addresses are being checked for null values before assigning them to data.Ipv4 and data.Ipv6, respectively. This logic is repeated twice.

Here's an optimized version of the function:

func (u *ContainerService) ContainerInfo(req dto.OperationWithName) (*dto.ContainerInfo, error) {
	oldContainer, err := u.getOldContainer(req.Name)
	if err != nil {
		return nil, err
	}

	networkSettings := oldContainer.NetworkSettings
	brBridgeNetworkSettings := networkSettings.Networks["bridge"]

	if brBridgeNetworkSettings.IPAMConfig == nil || data.Network == "bridge" {
		data.Ipv4 = bridgingNetworkSettings.IPAMConfig.IPv4Address
		data.Ipv6 = bridgingNetworkSettings.IPAMConfig.IPv6Address
	} else {
		data.Ipv4 = bridgingNetworkSettings.IPAddress
	}

	data.Cmd = oldContainer.Config.Cmd

// Continue processing...

return &info, nil

Key Changes:

  1. Simplified Logic: Removed the redundant checks for IPAMConfig and only assign IPs if either the container is on a different network than "bridge" or there is no IPAM configuration associated with the "bridge".
  2. Corrected Variable Names: Fixed a typo in bridgingNetworkSettings to brBridgeNetworkSettings.
  3. Ensured that data.Ipv4 and data.Ipv6 are properly assigned based on whether the container is on a different network or not.

These changes should improve readability and potentially reduce complexity while maintaining correctness and functionality.

Expand Down
Loading