Skip to content

Commit d435839

Browse files
committed
container/(docker|podman): rewrite obtaining IP-address
The top-level IPAddress field is deprecated in the API, so instead, the IP-address should be obtained from the container's primary network. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 782cf80 commit d435839

File tree

2 files changed

+42
-30
lines changed

2 files changed

+42
-30
lines changed

container/docker/handler.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,26 @@ func newContainerHandler(
186186
return nil, fmt.Errorf("failed to inspect container %q: %v", id, err)
187187
}
188188

189+
// Obtain the IP address for the container.
190+
var ipAddress string
191+
if ctnr.NetworkSettings != nil && ctnr.HostConfig != nil {
192+
c := ctnr
193+
if ctnr.HostConfig.NetworkMode.IsContainer() {
194+
// If the NetworkMode starts with 'container:' then we need to use the IP address of the container specified.
195+
// This happens in cases such as kubernetes where the containers doesn't have an IP address itself and we need to use the pod's address
196+
containerID := ctnr.HostConfig.NetworkMode.ConnectedContainer()
197+
c, err = client.ContainerInspect(context.Background(), containerID)
198+
if err != nil {
199+
return nil, fmt.Errorf("failed to inspect container %q: %v", containerID, err)
200+
}
201+
}
202+
for n, nw := range c.NetworkSettings.Networks {
203+
if n == c.HostConfig.NetworkMode.NetworkName() {
204+
ipAddress = nw.IPAddress
205+
}
206+
}
207+
}
208+
189209
// Do not report network metrics for containers that share netns with another container.
190210
includedMetrics := common.RemoveNetMetrics(metrics, ctnr.HostConfig.NetworkMode.IsContainer())
191211

@@ -195,6 +215,7 @@ func newContainerHandler(
195215
storageDriver: storageDriver,
196216
fsInfo: fsInfo,
197217
rootfsStorageDir: rootfsStorageDir,
218+
ipAddress: ipAddress,
198219
envs: make(map[string]string),
199220
labels: ctnr.Config.Labels,
200221
image: ctnr.Config.Image,
@@ -224,21 +245,6 @@ func newContainerHandler(
224245
handler.labels["restartcount"] = strconv.Itoa(ctnr.RestartCount)
225246
}
226247

227-
// Obtain the IP address for the container.
228-
// If the NetworkMode starts with 'container:' then we need to use the IP address of the container specified.
229-
// This happens in cases such as kubernetes where the containers doesn't have an IP address itself and we need to use the pod's address
230-
ipAddress := ctnr.NetworkSettings.IPAddress
231-
networkMode := string(ctnr.HostConfig.NetworkMode)
232-
if ipAddress == "" && strings.HasPrefix(networkMode, "container:") {
233-
containerID := strings.TrimPrefix(networkMode, "container:")
234-
c, err := client.ContainerInspect(context.Background(), containerID)
235-
if err != nil {
236-
return nil, fmt.Errorf("failed to inspect container %q: %v", containerID, err)
237-
}
238-
ipAddress = c.NetworkSettings.IPAddress
239-
}
240-
handler.ipAddress = ipAddress
241-
242248
if includedMetrics.Has(container.DiskUsageMetrics) {
243249
handler.fsHandler = &FsHandler{
244250
FsHandler: common.NewFsHandler(common.DefaultPeriod, rootfsStorageDir, otherStorageDir, fsInfo),

container/podman/handler.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,26 @@ func newContainerHandler(
123123
return nil, err
124124
}
125125

126+
// Obtain the IP address for the container.
127+
var ipAddress string
128+
if ctnr.NetworkSettings != nil && ctnr.HostConfig != nil {
129+
c := ctnr
130+
if ctnr.HostConfig.NetworkMode.IsContainer() {
131+
// If the NetworkMode starts with 'container:' then we need to use the IP address of the container specified.
132+
// This happens in cases such as kubernetes where the containers doesn't have an IP address itself and we need to use the pod's address
133+
containerID := ctnr.HostConfig.NetworkMode.ConnectedContainer()
134+
c, err = InspectContainer(containerID)
135+
if err != nil {
136+
return nil, fmt.Errorf("failed to inspect container %q: %v", containerID, err)
137+
}
138+
}
139+
for n, nw := range c.NetworkSettings.Networks {
140+
if n == c.HostConfig.NetworkMode.NetworkName() {
141+
ipAddress = nw.IPAddress
142+
}
143+
}
144+
}
145+
126146
rwLayerID, err := rwLayerID(storageDriver, storageDir, id)
127147
if err != nil {
128148
return nil, err
@@ -143,6 +163,7 @@ func newContainerHandler(
143163
storageDriver: storageDriver,
144164
fsInfo: fsInfo,
145165
rootfsStorageDir: rootfsStorageDir,
166+
ipAddress: ipAddress,
146167
envs: make(map[string]string),
147168
labels: ctnr.Config.Labels,
148169
image: ctnr.Config.Image,
@@ -170,21 +191,6 @@ func newContainerHandler(
170191
handler.labels["restartcount"] = strconv.Itoa(ctnr.RestartCount)
171192
}
172193

173-
// Obtain the IP address for the container.
174-
// If the NetworkMode starts with 'container:' then we need to use the IP address of the container specified.
175-
// This happens in cases such as kubernetes where the containers doesn't have an IP address itself and we need to use the pod's address
176-
ipAddress := ctnr.NetworkSettings.IPAddress
177-
networkMode := string(ctnr.HostConfig.NetworkMode)
178-
if ipAddress == "" && strings.HasPrefix(networkMode, "container:") {
179-
containerID := strings.TrimPrefix(networkMode, "container:")
180-
c, err := InspectContainer(containerID)
181-
if err != nil {
182-
return nil, fmt.Errorf("failed to inspect container %q: %v", containerID, err)
183-
}
184-
ipAddress = c.NetworkSettings.IPAddress
185-
}
186-
handler.ipAddress = ipAddress
187-
188194
if metrics.Has(container.DiskUsageMetrics) {
189195
handler.fsHandler = &docker.FsHandler{
190196
FsHandler: common.NewFsHandler(common.DefaultPeriod, rootfsStorageDir, otherStorageDir, fsInfo),

0 commit comments

Comments
 (0)