Skip to content
Open
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
34 changes: 19 additions & 15 deletions container/docker/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,24 @@ func newContainerHandler(
return nil, fmt.Errorf("failed to inspect container %q: %v", id, err)
}

// Obtain the IP address for the container.
var ipAddress string
if ctnr.NetworkSettings != nil && ctnr.HostConfig != nil {
c := ctnr
if ctnr.HostConfig.NetworkMode.IsContainer() {
// If the NetworkMode starts with 'container:' then we need to use the IP address of the container specified.
// 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
containerID := ctnr.HostConfig.NetworkMode.ConnectedContainer()
c, err = client.ContainerInspect(context.Background(), containerID)
if err != nil {
return nil, fmt.Errorf("failed to inspect container %q: %v", containerID, err)
}
}
if nw, ok := c.NetworkSettings.Networks[c.HostConfig.NetworkMode.NetworkName()]; ok {
ipAddress = nw.IPAddress
}
}

// Do not report network metrics for containers that share netns with another container.
includedMetrics := common.RemoveNetMetrics(metrics, ctnr.HostConfig.NetworkMode.IsContainer())

Expand All @@ -197,6 +215,7 @@ func newContainerHandler(
storageDriver: storageDriver,
fsInfo: fsInfo,
rootfsStorageDir: rootfsStorageDir,
ipAddress: ipAddress,
envs: make(map[string]string),
labels: ctnr.Config.Labels,
image: ctnr.Config.Image,
Expand Down Expand Up @@ -224,21 +243,6 @@ func newContainerHandler(
handler.labels["restartcount"] = strconv.Itoa(ctnr.RestartCount)
}

// Obtain the IP address for the container.
// If the NetworkMode starts with 'container:' then we need to use the IP address of the container specified.
// 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
ipAddress := ctnr.NetworkSettings.IPAddress
networkMode := string(ctnr.HostConfig.NetworkMode)
if ipAddress == "" && strings.HasPrefix(networkMode, "container:") {
containerID := strings.TrimPrefix(networkMode, "container:")
c, err := client.ContainerInspect(context.Background(), containerID)
if err != nil {
return nil, fmt.Errorf("failed to inspect container %q: %v", containerID, err)
}
ipAddress = c.NetworkSettings.IPAddress
}
handler.ipAddress = ipAddress

if includedMetrics.Has(container.DiskUsageMetrics) {
handler.fsHandler = &FsHandler{
FsHandler: common.NewFsHandler(common.DefaultPeriod, rootfsStorageDir, otherStorageDir, fsInfo),
Expand Down
34 changes: 19 additions & 15 deletions container/podman/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,24 @@ func newContainerHandler(
return nil, err
}

// Obtain the IP address for the container.
var ipAddress string
if ctnr.NetworkSettings != nil && ctnr.HostConfig != nil {
c := ctnr
if ctnr.HostConfig.NetworkMode.IsContainer() {
// If the NetworkMode starts with 'container:' then we need to use the IP address of the container specified.
// 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
containerID := ctnr.HostConfig.NetworkMode.ConnectedContainer()
c, err = InspectContainer(containerID)
if err != nil {
return nil, fmt.Errorf("failed to inspect container %q: %v", containerID, err)
}
}
if nw, ok := c.NetworkSettings.Networks[c.HostConfig.NetworkMode.NetworkName()]; ok {
ipAddress = nw.IPAddress
}
}

layerID, err := rwLayerID(storageDriver, storageDir, id)
if err != nil {
return nil, err
Expand All @@ -144,6 +162,7 @@ func newContainerHandler(
storageDriver: storageDriver,
fsInfo: fsInfo,
rootfsStorageDir: rootfsStorageDir,
ipAddress: ipAddress,
envs: make(map[string]string),
labels: ctnr.Config.Labels,
image: ctnr.Config.Image,
Expand Down Expand Up @@ -171,21 +190,6 @@ func newContainerHandler(
handler.labels["restartcount"] = strconv.Itoa(ctnr.RestartCount)
}

// Obtain the IP address for the container.
// If the NetworkMode starts with 'container:' then we need to use the IP address of the container specified.
// 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
ipAddress := ctnr.NetworkSettings.IPAddress
networkMode := string(ctnr.HostConfig.NetworkMode)
if ipAddress == "" && strings.HasPrefix(networkMode, "container:") {
containerID := strings.TrimPrefix(networkMode, "container:")
c, err := InspectContainer(containerID)
if err != nil {
return nil, fmt.Errorf("failed to inspect container %q: %v", containerID, err)
}
ipAddress = c.NetworkSettings.IPAddress
}
handler.ipAddress = ipAddress

if metrics.Has(container.DiskUsageMetrics) {
handler.fsHandler = &docker.FsHandler{
FsHandler: common.NewFsHandler(common.DefaultPeriod, rootfsStorageDir, otherStorageDir, fsInfo),
Expand Down
Loading