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

fix: segfault in ingress, persistentvolumeclaim, statefulset #9585

Merged
merged 4 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 12 additions & 7 deletions plugins/inputs/kube_inventory/ingress.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,20 @@ func (ki *KubernetesInventory) gatherIngress(i netv1.Ingress, acc telegraf.Accum
tags["ip"] = ingress.IP

for _, rule := range i.Spec.Rules {
for _, path := range rule.IngressRuleValue.HTTP.Paths {
fields["backend_service_port"] = path.Backend.Service.Port.Number
fields["tls"] = i.Spec.TLS != nil
if rule.IngressRuleValue.HTTP != nil {
srebhan marked this conversation as resolved.
Show resolved Hide resolved
for _, path := range rule.IngressRuleValue.HTTP.Paths {
if path.Backend.Service != nil {
tags["backend_service_name"] = path.Backend.Service.Name
fields["backend_service_port"] = path.Backend.Service.Port.Number
}

tags["backend_service_name"] = path.Backend.Service.Name
tags["path"] = path.Path
tags["host"] = rule.Host
fields["tls"] = i.Spec.TLS != nil

acc.AddFields(ingressMeasurement, fields, tags)
tags["path"] = path.Path
tags["host"] = rule.Host

acc.AddFields(ingressMeasurement, fields, tags)
}
}
}
}
Expand Down
108 changes: 108 additions & 0 deletions plugins/inputs/kube_inventory/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,114 @@ func TestIngress(t *testing.T) {
},
hasError: false,
},
{
name: "no HTTPIngressRuleValue",
handler: &mockHandler{
responseMap: map[string]interface{}{
"/ingress/": netv1.IngressList{
Items: []netv1.Ingress{
{
Status: netv1.IngressStatus{
LoadBalancer: v1.LoadBalancerStatus{
Ingress: []v1.LoadBalancerIngress{
{
Hostname: "chron-1",
IP: "1.0.0.127",
},
},
},
},
Spec: netv1.IngressSpec{
Rules: []netv1.IngressRule{
{
Host: "ui.internal",
IngressRuleValue: netv1.IngressRuleValue{
HTTP: nil,
},
},
},
},
ObjectMeta: metav1.ObjectMeta{
Generation: 12,
Namespace: "ns1",
Name: "ui-lb",
CreationTimestamp: metav1.Time{Time: now},
},
},
},
},
},
},
hasError: false,
},
{
name: "no IngressServiceBackend",
handler: &mockHandler{
responseMap: map[string]interface{}{
"/ingress/": netv1.IngressList{
Items: []netv1.Ingress{
{
Status: netv1.IngressStatus{
LoadBalancer: v1.LoadBalancerStatus{
Ingress: []v1.LoadBalancerIngress{
{
Hostname: "chron-1",
IP: "1.0.0.127",
},
},
},
},
Spec: netv1.IngressSpec{
Rules: []netv1.IngressRule{
{
Host: "ui.internal",
IngressRuleValue: netv1.IngressRuleValue{
HTTP: &netv1.HTTPIngressRuleValue{
Paths: []netv1.HTTPIngressPath{
{
Path: "/",
Backend: netv1.IngressBackend{
Service: nil,
},
},
},
},
},
},
},
},
ObjectMeta: metav1.ObjectMeta{
Generation: 12,
Namespace: "ns1",
Name: "ui-lb",
CreationTimestamp: metav1.Time{Time: now},
},
},
},
},
},
},
output: []telegraf.Metric{
testutil.MustMetric(
"kubernetes_ingress",
map[string]string{
"ingress_name": "ui-lb",
"namespace": "ns1",
"ip": "1.0.0.127",
"hostname": "chron-1",
"host": "ui.internal",
"path": "/",
},
map[string]interface{}{
"tls": false,
"generation": int64(12),
"created": now.UnixNano(),
},
time.Unix(0, 0),
),
},
hasError: false,
},
}

for _, v := range tests {
Expand Down
10 changes: 6 additions & 4 deletions plugins/inputs/kube_inventory/persistentvolumeclaim.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ func (ki *KubernetesInventory) gatherPersistentVolumeClaim(pvc corev1.Persistent
"phase_type": phaseType,
}
tags := map[string]string{
"pvc_name": pvc.Name,
"namespace": pvc.Namespace,
"phase": string(pvc.Status.Phase),
"storageclass": *pvc.Spec.StorageClassName,
"pvc_name": pvc.Name,
"namespace": pvc.Namespace,
"phase": string(pvc.Status.Phase),
}
if pvc.Spec.StorageClassName != nil {
tags["storageclass"] = *pvc.Spec.StorageClassName
}
if pvc.Spec.Selector != nil {
for key, val := range pvc.Spec.Selector.MatchLabels {
Expand Down
52 changes: 52 additions & 0 deletions plugins/inputs/kube_inventory/persistentvolumeclaim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,58 @@ func TestPersistentVolumeClaim(t *testing.T) {
),
},
},
{
name: "no storage class name",
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: nil,
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"select1": "s1",
"select2": "s2",
},
},
},
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",
"phase": "bound",
"selector_select1": "s1",
"selector_select2": "s2",
},
map[string]interface{}{
"phase_type": 0,
},
time.Unix(0, 0),
),
},
hasError: false,
},
}

for _, v := range tests {
Expand Down
12 changes: 8 additions & 4 deletions plugins/inputs/kube_inventory/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,20 @@ func (ki *KubernetesInventory) gatherStatefulSet(s v1.StatefulSet, acc telegraf.
"replicas_current": status.CurrentReplicas,
"replicas_ready": status.ReadyReplicas,
"replicas_updated": status.UpdatedReplicas,
"spec_replicas": *s.Spec.Replicas,
"observed_generation": s.Status.ObservedGeneration,
}
if s.Spec.Replicas != nil {
fields["spec_replicas"] = *s.Spec.Replicas
}
tags := map[string]string{
"statefulset_name": s.Name,
"namespace": s.Namespace,
}
for key, val := range s.Spec.Selector.MatchLabels {
if ki.selectorFilter.Match(key) {
tags["selector_"+key] = val
if s.Spec.Selector != nil {
for key, val := range s.Spec.Selector.MatchLabels {
if ki.selectorFilter.Match(key) {
tags["selector_"+key] = val
}
}
}

Expand Down
108 changes: 108 additions & 0 deletions plugins/inputs/kube_inventory/statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,114 @@ func TestStatefulSet(t *testing.T) {
},
hasError: false,
},
{
name: "no label selector",
handler: &mockHandler{
responseMap: map[string]interface{}{
"/statefulsets/": &v1.StatefulSetList{
Items: []v1.StatefulSet{
{
Status: v1.StatefulSetStatus{
Replicas: 2,
CurrentReplicas: 4,
ReadyReplicas: 1,
UpdatedReplicas: 3,
ObservedGeneration: 119,
},
Spec: v1.StatefulSetSpec{
Replicas: toInt32Ptr(3),
Selector: nil,
},
ObjectMeta: metav1.ObjectMeta{
Generation: 332,
Namespace: "ns1",
Name: "sts1",
CreationTimestamp: metav1.Time{Time: now},
},
},
},
},
},
},
output: []telegraf.Metric{
testutil.MustMetric(
"kubernetes_statefulset",
map[string]string{
"namespace": "ns1",
"statefulset_name": "sts1",
},
map[string]interface{}{
"generation": int64(332),
"observed_generation": int64(119),
"created": now.UnixNano(),
"spec_replicas": int32(3),
"replicas": int32(2),
"replicas_current": int32(4),
"replicas_ready": int32(1),
"replicas_updated": int32(3),
},
time.Unix(0, 0),
),
},
hasError: false,
},
{
name: "no desired number of replicas",
handler: &mockHandler{
responseMap: map[string]interface{}{
"/statefulsets/": &v1.StatefulSetList{
Items: []v1.StatefulSet{
{
Status: v1.StatefulSetStatus{
Replicas: 2,
CurrentReplicas: 4,
ReadyReplicas: 1,
UpdatedReplicas: 3,
ObservedGeneration: 119,
},
Spec: v1.StatefulSetSpec{
Replicas: nil,
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"select1": "s1",
"select2": "s2",
},
},
},
ObjectMeta: metav1.ObjectMeta{
Generation: 332,
Namespace: "ns1",
Name: "sts1",
CreationTimestamp: metav1.Time{Time: now},
},
},
},
},
},
},
output: []telegraf.Metric{
testutil.MustMetric(
"kubernetes_statefulset",
map[string]string{
"namespace": "ns1",
"statefulset_name": "sts1",
"selector_select1": "s1",
"selector_select2": "s2",
},
map[string]interface{}{
"generation": int64(332),
"observed_generation": int64(119),
"created": now.UnixNano(),
"replicas": int32(2),
"replicas_current": int32(4),
"replicas_ready": int32(1),
"replicas_updated": int32(3),
},
time.Unix(0, 0),
),
},
hasError: false,
},
}

for _, v := range tests {
Expand Down