Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent segfault in kube_inventory #9549

Merged
merged 1 commit into from
Jul 28, 2021
Merged
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
8 changes: 5 additions & 3 deletions plugins/inputs/kube_inventory/persistentvolumeclaim.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ func (ki *KubernetesInventory) gatherPersistentVolumeClaim(pvc corev1.Persistent
"phase": string(pvc.Status.Phase),
"storageclass": *pvc.Spec.StorageClassName,
}
for key, val := range pvc.Spec.Selector.MatchLabels {
if ki.selectorFilter.Match(key) {
tags["selector_"+key] = val
if pvc.Spec.Selector != nil {
for key, val := range pvc.Spec.Selector.MatchLabels {
if ki.selectorFilter.Match(key) {
tags["selector_"+key] = val
}
}
}

Expand Down
46 changes: 46 additions & 0 deletions plugins/inputs/kube_inventory/persistentvolumeclaim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down