Skip to content

Commit 9b338cd

Browse files
authored
[Metricbeat][Kubernetes Volume] Add pvc reference to distinguish ephemeral from persistent volumes (#38839)
* Kubernetes add pvc reference in the kubernetes.volume dataset if present * Reuse field kubernetes.persistentvolumeclaim instead of creating a new one * Add only persistentvolumeclaim name and no namespace (that is redundant with the kubernetes.namespace) * Add test to see if persistentvolumeclaim name is extracted * Add a separate test for pvc * Add changelog entry for the PR * Add the persistentvolume claim field * Run mage update to update the fields * Remove the duplicate definition of the field kubernetes.persistentvolumeclaim.name * Remove newline * Fix key from kubernetes.volume.persistentvolumeclaim.name to kubernetes.persistentvolume * Fix unit test after changing the populated field name * Removed printf function * Remove empty lines added by mistake
1 parent c7829d3 commit 9b338cd

File tree

5 files changed

+47
-15
lines changed

5 files changed

+47
-15
lines changed

CHANGELOG.next.asciidoc

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]
191191
- Enable early event encoding in the Elasticsearch output, improving cpu and memory use {pull}38572[38572]
192192
- The environment variable `BEATS_ADD_CLOUD_METADATA_PROVIDERS` overrides configured/default `add_cloud_metadata` providers {pull}38669[38669]
193193
- Introduce log message for not supported annotations for Hints based autodiscover {pull}38213[38213]
194+
- Add persistent volume claim name to volume if available {pull}38839[38839]
194195

195196

196197
*Auditbeat*

metricbeat/module/kubernetes/_meta/test/stats_summary.json

+15-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,21 @@
122122
"inodes": 473560,
123123
"inodesUsed": 9,
124124
"name": "default-token-sg8x5"
125-
}
125+
},
126+
{
127+
"time": "2024-04-09T17:34:17Z",
128+
"availableBytes": 31509590016,
129+
"capacityBytes": 31526391808,
130+
"usedBytes": 24576,
131+
"inodesFree": 1966069,
132+
"inodes": 1966080,
133+
"inodesUsed": 11,
134+
"name": "pvc-demo-vol",
135+
"pvcRef": {
136+
"name": "pvc-demo",
137+
"namespace": "default"
138+
}
139+
}
126140
]
127141
}
128142
]

metricbeat/module/kubernetes/types.go

+4
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ type Summary struct {
131131
InodesUsed uint64 `json:"inodesUsed"`
132132
Name string `json:"name"`
133133
UsedBytes uint64 `json:"usedBytes"`
134+
PvcRef struct {
135+
Name string `json:"name"`
136+
Namespace string `json:"namespace"`
137+
} `json:"pvcRef"`
134138
} `json:"volume"`
135139
} `json:"pods"`
136140
}

metricbeat/module/kubernetes/volume/data.go

+3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ func eventMapping(content []byte, logger *logp.Logger) ([]mapstr.M, error) {
7575
if volume.Inodes > 0 {
7676
kubernetes2.ShouldPut(volumeEvent, "fs.inodes.pct", float64(volume.InodesUsed)/float64(volume.Inodes), logger)
7777
}
78+
if volume.PvcRef.Name != "" {
79+
kubernetes2.ShouldPut(volumeEvent, mb.ModuleDataKey+".persistentvolumeclaim.name", volume.PvcRef.Name, logger)
80+
}
7881
events = append(events, volumeEvent)
7982
}
8083

metricbeat/module/kubernetes/volume/volume_test.go

+24-14
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/stretchr/testify/assert"
2828

29+
"github.com/elastic/beats/v7/metricbeat/mb"
2930
"github.com/elastic/elastic-agent-libs/logp"
3031
"github.com/elastic/elastic-agent-libs/mapstr"
3132
)
@@ -44,23 +45,32 @@ func TestEventMapping(t *testing.T) {
4445
events, err := eventMapping(body, logger)
4546
assert.NoError(t, err, "error mapping "+testFile)
4647

47-
assert.Len(t, events, 1, "got wrong number of events")
48+
assert.Len(t, events, 2, "got wrong number of events")
4849

49-
testCases := map[string]interface{}{
50-
"name": "default-token-sg8x5",
51-
52-
"fs.available.bytes": 1939689472,
53-
"fs.capacity.bytes": 1939701760,
54-
"fs.used.bytes": 12288,
55-
"fs.used.pct": float64(12288) / float64(1939701760),
56-
"fs.inodes.used": 9,
57-
"fs.inodes.free": 473551,
58-
"fs.inodes.count": 473560,
59-
"fs.inodes.pct": float64(9) / float64(473560),
50+
testCases := []map[string]interface{}{
51+
// Test for ephemeral volume
52+
{
53+
"name": "default-token-sg8x5",
54+
"fs.available.bytes": 1939689472,
55+
"fs.capacity.bytes": 1939701760,
56+
"fs.used.bytes": 12288,
57+
"fs.used.pct": float64(12288) / float64(1939701760),
58+
"fs.inodes.used": 9,
59+
"fs.inodes.free": 473551,
60+
"fs.inodes.count": 473560,
61+
"fs.inodes.pct": float64(9) / float64(473560),
62+
},
63+
// Test for the persistent volume claim
64+
{
65+
mb.ModuleDataKey + ".persistentvolumeclaim.name": "pvc-demo",
66+
"name": "pvc-demo-vol",
67+
},
6068
}
6169

62-
for k, v := range testCases {
63-
testValue(t, events[0], k, v)
70+
for i := range testCases {
71+
for k, v := range testCases[i] {
72+
testValue(t, events[i], k, v)
73+
}
6474
}
6575
}
6676

0 commit comments

Comments
 (0)