Skip to content

Commit 5d993f4

Browse files
thaJeztahdims
authored andcommitted
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 a1160cf commit 5d993f4

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
@@ -188,6 +188,24 @@ func newContainerHandler(
188188
return nil, fmt.Errorf("failed to inspect container %q: %v", id, err)
189189
}
190190

191+
// Obtain the IP address for the container.
192+
var ipAddress string
193+
if ctnr.NetworkSettings != nil && ctnr.HostConfig != nil {
194+
c := ctnr
195+
if ctnr.HostConfig.NetworkMode.IsContainer() {
196+
// If the NetworkMode starts with 'container:' then we need to use the IP address of the container specified.
197+
// 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
198+
containerID := ctnr.HostConfig.NetworkMode.ConnectedContainer()
199+
c, err = client.ContainerInspect(context.Background(), containerID)
200+
if err != nil {
201+
return nil, fmt.Errorf("failed to inspect container %q: %v", containerID, err)
202+
}
203+
}
204+
if nw, ok := c.NetworkSettings.Networks[c.HostConfig.NetworkMode.NetworkName()]; ok {
205+
ipAddress = nw.IPAddress
206+
}
207+
}
208+
191209
// Do not report network metrics for containers that share netns with another container.
192210
includedMetrics := common.RemoveNetMetrics(metrics, ctnr.HostConfig.NetworkMode.IsContainer())
193211

@@ -197,6 +215,7 @@ func newContainerHandler(
197215
storageDriver: storageDriver,
198216
fsInfo: fsInfo,
199217
rootfsStorageDir: rootfsStorageDir,
218+
ipAddress: ipAddress,
200219
envs: make(map[string]string),
201220
labels: ctnr.Config.Labels,
202221
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)