Skip to content

Commit 8ea473e

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 <[email protected]>
1 parent b0a4e1d commit 8ea473e

File tree

2 files changed

+38
-30
lines changed

2 files changed

+38
-30
lines changed

container/docker/handler.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,24 @@ 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+
if nw, ok := c.NetworkSettings.Networks[c.HostConfig.NetworkMode.NetworkName()]; ok {
203+
ipAddress = nw.IPAddress
204+
}
205+
}
206+
189207
// Do not report network metrics for containers that share netns with another container.
190208
includedMetrics := common.RemoveNetMetrics(metrics, ctnr.HostConfig.NetworkMode.IsContainer())
191209

@@ -195,6 +213,7 @@ func newContainerHandler(
195213
storageDriver: storageDriver,
196214
fsInfo: fsInfo,
197215
rootfsStorageDir: rootfsStorageDir,
216+
ipAddress: ipAddress,
198217
envs: make(map[string]string),
199218
labels: ctnr.Config.Labels,
200219
image: ctnr.Config.Image,
@@ -224,21 +243,6 @@ func newContainerHandler(
224243
handler.labels["restartcount"] = strconv.Itoa(ctnr.RestartCount)
225244
}
226245

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-
242246
if includedMetrics.Has(container.DiskUsageMetrics) {
243247
handler.fsHandler = &FsHandler{
244248
FsHandler: common.NewFsHandler(common.DefaultPeriod, rootfsStorageDir, otherStorageDir, fsInfo),

container/podman/handler.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,24 @@ func newContainerHandler(
124124
return nil, err
125125
}
126126

127+
// Obtain the IP address for the container.
128+
var ipAddress string
129+
if ctnr.NetworkSettings != nil && ctnr.HostConfig != nil {
130+
c := ctnr
131+
if ctnr.HostConfig.NetworkMode.IsContainer() {
132+
// If the NetworkMode starts with 'container:' then we need to use the IP address of the container specified.
133+
// 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
134+
containerID := ctnr.HostConfig.NetworkMode.ConnectedContainer()
135+
c, err = InspectContainer(containerID)
136+
if err != nil {
137+
return nil, fmt.Errorf("failed to inspect container %q: %v", containerID, err)
138+
}
139+
}
140+
if nw, ok := c.NetworkSettings.Networks[c.HostConfig.NetworkMode.NetworkName()]; ok {
141+
ipAddress = nw.IPAddress
142+
}
143+
}
144+
127145
layerID, err := rwLayerID(storageDriver, storageDir, id)
128146
if err != nil {
129147
return nil, err
@@ -144,6 +162,7 @@ func newContainerHandler(
144162
storageDriver: storageDriver,
145163
fsInfo: fsInfo,
146164
rootfsStorageDir: rootfsStorageDir,
165+
ipAddress: ipAddress,
147166
envs: make(map[string]string),
148167
labels: ctnr.Config.Labels,
149168
image: ctnr.Config.Image,
@@ -171,21 +190,6 @@ func newContainerHandler(
171190
handler.labels["restartcount"] = strconv.Itoa(ctnr.RestartCount)
172191
}
173192

174-
// Obtain the IP address for the container.
175-
// If the NetworkMode starts with 'container:' then we need to use the IP address of the container specified.
176-
// 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
177-
ipAddress := ctnr.NetworkSettings.IPAddress
178-
networkMode := string(ctnr.HostConfig.NetworkMode)
179-
if ipAddress == "" && strings.HasPrefix(networkMode, "container:") {
180-
containerID := strings.TrimPrefix(networkMode, "container:")
181-
c, err := InspectContainer(containerID)
182-
if err != nil {
183-
return nil, fmt.Errorf("failed to inspect container %q: %v", containerID, err)
184-
}
185-
ipAddress = c.NetworkSettings.IPAddress
186-
}
187-
handler.ipAddress = ipAddress
188-
189193
if metrics.Has(container.DiskUsageMetrics) {
190194
handler.fsHandler = &docker.FsHandler{
191195
FsHandler: common.NewFsHandler(common.DefaultPeriod, rootfsStorageDir, otherStorageDir, fsInfo),

0 commit comments

Comments
 (0)