Prevent segfault in persistent volume claims (#9549)

This commit is contained in:
Alexander Krantz 2021-07-28 13:50:18 -07:00 committed by GitHub
parent 5843b27d75
commit fdec5f1f31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 3 deletions

View File

@ -39,11 +39,13 @@ func (ki *KubernetesInventory) gatherPersistentVolumeClaim(pvc corev1.Persistent
"phase": string(pvc.Status.Phase),
"storageclass": *pvc.Spec.StorageClassName,
}
if pvc.Spec.Selector != nil {
for key, val := range pvc.Spec.Selector.MatchLabels {
if ki.selectorFilter.Match(key) {
tags["selector_"+key] = val
}
}
}
acc.AddFields(persistentVolumeClaimMeasurement, fields, tags)
}

View File

@ -88,6 +88,52 @@ func TestPersistentVolumeClaim(t *testing.T) {
},
hasError: false,
},
{
name: "no label selectors",
hasError: false,
handler: &mockHandler{
responseMap: map[string]interface{}{
"/persistentvolumeclaims/": &corev1.PersistentVolumeClaimList{
Items: []corev1.PersistentVolumeClaim{
{
Status: corev1.PersistentVolumeClaimStatus{
Phase: "bound",
},
Spec: corev1.PersistentVolumeClaimSpec{
VolumeName: "pvc-dc870fd6-1e08-11e8-b226-02aa4bc06eb8",
StorageClassName: toStrPtr("ebs-1"),
Selector: nil,
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "ns1",
Name: "pc1",
Labels: map[string]string{
"lab1": "v1",
"lab2": "v2",
},
CreationTimestamp: metav1.Time{Time: now},
},
},
},
},
},
},
output: []telegraf.Metric{
testutil.MustMetric(
"kubernetes_persistentvolumeclaim",
map[string]string{
"pvc_name": "pc1",
"namespace": "ns1",
"storageclass": "ebs-1",
"phase": "bound",
},
map[string]interface{}{
"phase_type": 0,
},
time.Unix(0, 0),
),
},
},
}
for _, v := range tests {