From 9b338cd98efc3b44804817792bdce6d3e251f64d Mon Sep 17 00:00:00 2001 From: Mirko Bez Date: Thu, 16 May 2024 08:46:30 +0200 Subject: [PATCH] [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 --- CHANGELOG.next.asciidoc | 1 + .../kubernetes/_meta/test/stats_summary.json | 16 +++++++- metricbeat/module/kubernetes/types.go | 4 ++ metricbeat/module/kubernetes/volume/data.go | 3 ++ .../module/kubernetes/volume/volume_test.go | 38 ++++++++++++------- 5 files changed, 47 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 0a9583619c8..3beb4ef51e5 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -191,6 +191,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff] - Enable early event encoding in the Elasticsearch output, improving cpu and memory use {pull}38572[38572] - The environment variable `BEATS_ADD_CLOUD_METADATA_PROVIDERS` overrides configured/default `add_cloud_metadata` providers {pull}38669[38669] - Introduce log message for not supported annotations for Hints based autodiscover {pull}38213[38213] +- Add persistent volume claim name to volume if available {pull}38839[38839] *Auditbeat* diff --git a/metricbeat/module/kubernetes/_meta/test/stats_summary.json b/metricbeat/module/kubernetes/_meta/test/stats_summary.json index 1372d24dcfc..244afb29b7e 100644 --- a/metricbeat/module/kubernetes/_meta/test/stats_summary.json +++ b/metricbeat/module/kubernetes/_meta/test/stats_summary.json @@ -122,7 +122,21 @@ "inodes": 473560, "inodesUsed": 9, "name": "default-token-sg8x5" - } + }, + { + "time": "2024-04-09T17:34:17Z", + "availableBytes": 31509590016, + "capacityBytes": 31526391808, + "usedBytes": 24576, + "inodesFree": 1966069, + "inodes": 1966080, + "inodesUsed": 11, + "name": "pvc-demo-vol", + "pvcRef": { + "name": "pvc-demo", + "namespace": "default" + } + } ] } ] diff --git a/metricbeat/module/kubernetes/types.go b/metricbeat/module/kubernetes/types.go index e1915b9602b..082d6dea80e 100644 --- a/metricbeat/module/kubernetes/types.go +++ b/metricbeat/module/kubernetes/types.go @@ -131,6 +131,10 @@ type Summary struct { InodesUsed uint64 `json:"inodesUsed"` Name string `json:"name"` UsedBytes uint64 `json:"usedBytes"` + PvcRef struct { + Name string `json:"name"` + Namespace string `json:"namespace"` + } `json:"pvcRef"` } `json:"volume"` } `json:"pods"` } diff --git a/metricbeat/module/kubernetes/volume/data.go b/metricbeat/module/kubernetes/volume/data.go index 52d801e105c..c6b9983f777 100644 --- a/metricbeat/module/kubernetes/volume/data.go +++ b/metricbeat/module/kubernetes/volume/data.go @@ -75,6 +75,9 @@ func eventMapping(content []byte, logger *logp.Logger) ([]mapstr.M, error) { if volume.Inodes > 0 { kubernetes2.ShouldPut(volumeEvent, "fs.inodes.pct", float64(volume.InodesUsed)/float64(volume.Inodes), logger) } + if volume.PvcRef.Name != "" { + kubernetes2.ShouldPut(volumeEvent, mb.ModuleDataKey+".persistentvolumeclaim.name", volume.PvcRef.Name, logger) + } events = append(events, volumeEvent) } diff --git a/metricbeat/module/kubernetes/volume/volume_test.go b/metricbeat/module/kubernetes/volume/volume_test.go index 719cc48b61d..2df7ab8eed0 100644 --- a/metricbeat/module/kubernetes/volume/volume_test.go +++ b/metricbeat/module/kubernetes/volume/volume_test.go @@ -26,6 +26,7 @@ import ( "github.com/stretchr/testify/assert" + "github.com/elastic/beats/v7/metricbeat/mb" "github.com/elastic/elastic-agent-libs/logp" "github.com/elastic/elastic-agent-libs/mapstr" ) @@ -44,23 +45,32 @@ func TestEventMapping(t *testing.T) { events, err := eventMapping(body, logger) assert.NoError(t, err, "error mapping "+testFile) - assert.Len(t, events, 1, "got wrong number of events") + assert.Len(t, events, 2, "got wrong number of events") - testCases := map[string]interface{}{ - "name": "default-token-sg8x5", - - "fs.available.bytes": 1939689472, - "fs.capacity.bytes": 1939701760, - "fs.used.bytes": 12288, - "fs.used.pct": float64(12288) / float64(1939701760), - "fs.inodes.used": 9, - "fs.inodes.free": 473551, - "fs.inodes.count": 473560, - "fs.inodes.pct": float64(9) / float64(473560), + testCases := []map[string]interface{}{ + // Test for ephemeral volume + { + "name": "default-token-sg8x5", + "fs.available.bytes": 1939689472, + "fs.capacity.bytes": 1939701760, + "fs.used.bytes": 12288, + "fs.used.pct": float64(12288) / float64(1939701760), + "fs.inodes.used": 9, + "fs.inodes.free": 473551, + "fs.inodes.count": 473560, + "fs.inodes.pct": float64(9) / float64(473560), + }, + // Test for the persistent volume claim + { + mb.ModuleDataKey + ".persistentvolumeclaim.name": "pvc-demo", + "name": "pvc-demo-vol", + }, } - for k, v := range testCases { - testValue(t, events[0], k, v) + for i := range testCases { + for k, v := range testCases[i] { + testValue(t, events[i], k, v) + } } }