Skip to content

Commit

Permalink
fix detection of k8s volume names mounted with subPath
Browse files Browse the repository at this point in the history
  • Loading branch information
def committed May 13, 2022
1 parent 10b9282 commit dfb7f3e
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 43 deletions.
22 changes: 22 additions & 0 deletions common/volumes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package common

import (
"regexp"
)

var (
k8sVolumeDir = regexp.MustCompile(`.+/(volumes/kubernetes.io~([^/]+)|volume-subpaths)/([^/]+)`)
)

func ParseKubernetesVolumeSource(source string) string {
groups := k8sVolumeDir.FindStringSubmatch(source)
if len(groups) != 4 {
return ""
}
provisioner, volume := groups[2], groups[3]
switch provisioner {
case "secret", "configmap", "empty-dir", "projected":
return ""
}
return volume
}
29 changes: 29 additions & 0 deletions common/volumes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package common

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestParseKubernetesVolumeSource(t *testing.T) {
assert.Equal(t,
"pvc-90af9c02-ec70-446a-a16a-3ce17d4f42b4",
ParseKubernetesVolumeSource("/var/lib/kubelet/pods/ed56844b-2ad5-4858-9305-abea47bd39fc/volumes/kubernetes.io~csi/pvc-90af9c02-ec70-446a-a16a-3ce17d4f42b4/mount"),
)
assert.Equal(t,
"pvc-0307b722-e448-4d73-9d75-091ebf367264",
ParseKubernetesVolumeSource("/var/lib/kubelet/pods/adf669ca-c3f8-49de-9ad4-9dd66721dc0d/volume-subpaths/pvc-0307b722-e448-4d73-9d75-091ebf367264/pg/0"),
)
assert.Equal(t,
"",
ParseKubernetesVolumeSource("/var/lib/kubelet/pods/adf669ca-c3f8-49de-9ad4-9dd66721dc0d/volumes/kubernetes.io~projected/kube-api-access-jvvq6"),
)
assert.Equal(t,
"",
ParseKubernetesVolumeSource("/var/lib/kubelet/pods/7cac0d4b-0562-4a25-bbd9-601c60048eb9/etc-hosts"))

assert.Equal(t,
"",
ParseKubernetesVolumeSource("/var/lib/kubelet/pods/db6e284a-fbab-4629-8f17-9f5fea38bea7/volumes/kubernetes.io~configmap/config-volume"),
)
}
10 changes: 2 additions & 8 deletions containers/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,12 @@ type ContainerID string
type ContainerMetadata struct {
name string
labels map[string]string
volumes map[string]Volume
volumes map[string]string
logPath string
logDecoder logparser.Decoder
hostListens map[string][]netaddr.IPPort
}

type Volume struct {
provisioner string
volume string
}

type Delays struct {
cpu time.Duration
disk time.Duration
Expand Down Expand Up @@ -194,8 +189,7 @@ func (c *Container) Collect(ch chan<- prometheus.Metric) {
continue
}
for mountPoint, fsStat := range mounts {
v := c.metadata.volumes[mountPoint]
dls := []string{mountPoint, dev.Name, v.provisioner, v.volume}
dls := []string{mountPoint, dev.Name, c.metadata.volumes[mountPoint]}
ch <- gauge(metrics.DiskSize, float64(fsStat.CapacityBytes), dls...)
ch <- gauge(metrics.DiskUsed, float64(fsStat.UsedBytes), dls...)
ch <- gauge(metrics.DiskReserved, float64(fsStat.ReservedBytes), dls...)
Expand Down
7 changes: 3 additions & 4 deletions containers/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/containerd/containerd"
"github.com/containerd/containerd/oci"
"github.com/containerd/containerd/pkg/cri/constants"
"github.com/coroot/coroot-node-agent/common"
"github.com/coroot/coroot-node-agent/proc"
"github.com/coroot/logparser"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -45,17 +46,15 @@ func ContainerdInspect(containerID string) (*ContainerMetadata, error) {

res := &ContainerMetadata{
labels: c.Labels,
volumes: map[string]Volume{},
volumes: map[string]string{},
}

var spec oci.Spec
if err := json.Unmarshal(c.Spec.Value, &spec); err != nil {
klog.Warningln(err)
} else {
for _, m := range spec.Mounts {
if provisioner, volume := parseVolumeSource(m.Source); provisioner != "" && volume != "" {
res.volumes[m.Destination] = Volume{provisioner: provisioner, volume: volume}
}
res.volumes[m.Destination] = common.ParseKubernetesVolumeSource(m.Source)
}
}

Expand Down
7 changes: 3 additions & 4 deletions containers/dockerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package containers
import (
"context"
"fmt"
"github.com/coroot/coroot-node-agent/common"
"github.com/coroot/coroot-node-agent/proc"
"github.com/coroot/logparser"
"github.com/docker/docker/client"
Expand Down Expand Up @@ -44,13 +45,11 @@ func DockerdInspect(containerID string) (*ContainerMetadata, error) {
res := &ContainerMetadata{
name: strings.TrimPrefix(c.Name, "/"),
labels: c.Config.Labels,
volumes: map[string]Volume{},
volumes: map[string]string{},
hostListens: map[string][]netaddr.IPPort{},
}
for _, m := range c.Mounts {
if provisioner, volume := parseVolumeSource(m.Source); provisioner != "" && volume != "" {
res.volumes[m.Destination] = Volume{provisioner: provisioner, volume: volume}
}
res.volumes[m.Destination] = common.ParseKubernetesVolumeSource(m.Source)
}
if c.LogPath != "" && c.HostConfig.LogConfig.Type == "json-file" {
res.logPath = c.LogPath
Expand Down
14 changes: 7 additions & 7 deletions containers/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ var metrics = struct {
OOMKills: metric("container_oom_kills_total", "Total number of times the container was terminated by the OOM killer"),

DiskDelay: metric("container_resources_disk_delay_seconds_total", "Total time duration processes of the container have been waiting fot I/Os to complete"),
DiskSize: metric("container_resources_disk_size_bytes", "Total capacity of the volume", "mount_point", "device", "provisioner", "volume"),
DiskUsed: metric("container_resources_disk_used_bytes", "Used capacity of the volume", "mount_point", "device", "provisioner", "volume"),
DiskReserved: metric("container_resources_disk_reserved_bytes", "Reserved capacity of the volume", "mount_point", "device", "provisioner", "volume"),
DiskReadOps: metric("container_resources_disk_reads_total", "Total number of reads completed successfully by the container", "mount_point", "device", "provisioner", "volume"),
DiskReadBytes: metric("container_resources_disk_read_bytes_total", "Total number of bytes read from the disk by the container", "mount_point", "device", "provisioner", "volume"),
DiskWriteOps: metric("container_resources_disk_writes_total", "Total number of writes completed successfully by the container", "mount_point", "device", "provisioner", "volume"),
DiskWriteBytes: metric("container_resources_disk_written_bytes_total", "Total number of bytes written to the disk by the container", "mount_point", "device", "provisioner", "volume"),
DiskSize: metric("container_resources_disk_size_bytes", "Total capacity of the volume", "mount_point", "device", "volume"),
DiskUsed: metric("container_resources_disk_used_bytes", "Used capacity of the volume", "mount_point", "device", "volume"),
DiskReserved: metric("container_resources_disk_reserved_bytes", "Reserved capacity of the volume", "mount_point", "device", "volume"),
DiskReadOps: metric("container_resources_disk_reads_total", "Total number of reads completed successfully by the container", "mount_point", "device", "volume"),
DiskReadBytes: metric("container_resources_disk_read_bytes_total", "Total number of bytes read from the disk by the container", "mount_point", "device", "volume"),
DiskWriteOps: metric("container_resources_disk_writes_total", "Total number of writes completed successfully by the container", "mount_point", "device", "volume"),
DiskWriteBytes: metric("container_resources_disk_written_bytes_total", "Total number of bytes written to the disk by the container", "mount_point", "device", "volume"),

NetListenInfo: metric("container_net_tcp_listen_info", "Listen address of the container", "listen_addr", "proxy"),
NetConnectsSuccessful: metric("container_net_tcp_successful_connects_total", "Total number of successful TCP connects", "destination", "actual_destination"),
Expand Down
20 changes: 0 additions & 20 deletions containers/volumes.go

This file was deleted.

0 comments on commit dfb7f3e

Please sign in to comment.