From c33b9bea3c5fd789c08b49184b0a2daecd3f344a Mon Sep 17 00:00:00 2001 From: Tejas Kokje Date: Fri, 30 Aug 2024 11:28:32 -0700 Subject: [PATCH] Add nil pointers check to prevent crash in k8sclusterreceiver --- .../internal/deployment/deployments.go | 6 +++++- .../k8sclusterreceiver/internal/hpa/hpa.go | 6 +++++- .../persistentvolume/persistentvolume.go | 18 +++++++++++++++--- .../persistentvolumeclaim.go | 14 ++++++++++++-- .../internal/statefulset/statefulsets.go | 6 +++++- 5 files changed, 42 insertions(+), 8 deletions(-) diff --git a/receiver/k8sclusterreceiver/internal/deployment/deployments.go b/receiver/k8sclusterreceiver/internal/deployment/deployments.go index da176dd64985..efe538da3a45 100644 --- a/receiver/k8sclusterreceiver/internal/deployment/deployments.go +++ b/receiver/k8sclusterreceiver/internal/deployment/deployments.go @@ -29,7 +29,11 @@ func Transform(deployment *appsv1.Deployment) *appsv1.Deployment { } func RecordMetrics(mb *imetadata.MetricsBuilder, dep *appsv1.Deployment, ts pcommon.Timestamp) { - mb.RecordK8sDeploymentDesiredDataPoint(ts, int64(*dep.Spec.Replicas)) + replicas := int64(0) + if dep.Spec.Replicas != nil { + replicas = int64(*dep.Spec.Replicas) + } + mb.RecordK8sDeploymentDesiredDataPoint(ts, replicas) mb.RecordK8sDeploymentAvailableDataPoint(ts, int64(dep.Status.AvailableReplicas)) rb := mb.NewResourceBuilder() rb.SetK8sDeploymentName(dep.Name) diff --git a/receiver/k8sclusterreceiver/internal/hpa/hpa.go b/receiver/k8sclusterreceiver/internal/hpa/hpa.go index 15d437e9863e..6e89148958ee 100644 --- a/receiver/k8sclusterreceiver/internal/hpa/hpa.go +++ b/receiver/k8sclusterreceiver/internal/hpa/hpa.go @@ -13,7 +13,11 @@ import ( func RecordMetrics(mb *metadata.MetricsBuilder, hpa *autoscalingv2.HorizontalPodAutoscaler, ts pcommon.Timestamp) { mb.RecordK8sHpaMaxReplicasDataPoint(ts, int64(hpa.Spec.MaxReplicas)) - mb.RecordK8sHpaMinReplicasDataPoint(ts, int64(*hpa.Spec.MinReplicas)) + minReplicas := 0 + if hpa.Spec.MinReplicas != nil { + minReplicas = int(*hpa.Spec.MinReplicas) + } + mb.RecordK8sHpaMinReplicasDataPoint(ts, int64(minReplicas)) mb.RecordK8sHpaCurrentReplicasDataPoint(ts, int64(hpa.Status.CurrentReplicas)) mb.RecordK8sHpaDesiredReplicasDataPoint(ts, int64(hpa.Status.DesiredReplicas)) rb := mb.NewResourceBuilder() diff --git a/receiver/k8sclusterreceiver/internal/persistentvolume/persistentvolume.go b/receiver/k8sclusterreceiver/internal/persistentvolume/persistentvolume.go index a14b01ccdc58..9ba7ebf6fae9 100644 --- a/receiver/k8sclusterreceiver/internal/persistentvolume/persistentvolume.go +++ b/receiver/k8sclusterreceiver/internal/persistentvolume/persistentvolume.go @@ -58,9 +58,21 @@ func RecordMetrics(mb *imetadata.MetricsBuilder, pv *corev1.PersistentVolume, ts rb.SetK8sPersistentvolumeStartTime(pv.GetCreationTimestamp().String()) rb.SetK8sPersistentvolumeStorageClass(pv.Spec.StorageClassName) rb.SetK8sPersistentvolumeType("PersistentVolume") - rb.SetK8sPersistentvolumeVolumeMode(string(*pv.Spec.VolumeMode)) - rb.SetK8sPersistentvolumeclaimUID(string((*pv.Spec.ClaimRef).UID)) - rb.SetK8sPersistentvolumeclaimName((pv.Spec.ClaimRef).Name) + + volumeMode := "unknown" + if pv.Spec.VolumeMode != nil { + volumeMode = string(*pv.Spec.VolumeMode) + } + rb.SetK8sPersistentvolumeVolumeMode(string(volumeMode)) + + volumeClaimRefUID := "unknown" + volumeClaimRefName := "unknown" + if pv.Spec.ClaimRef != nil { + volumeClaimRefUID = string((*pv.Spec.ClaimRef).UID) + volumeClaimRefName = (*pv.Spec.ClaimRef).Name + } + rb.SetK8sPersistentvolumeclaimUID(volumeClaimRefUID) + rb.SetK8sPersistentvolumeclaimName(volumeClaimRefName) rb.SetK8sClusterName("unknown") mb.EmitForResource(metadata.WithResource(rb.Emit())) } diff --git a/receiver/k8sclusterreceiver/internal/persistentvolumeclaim/persistentvolumeclaim.go b/receiver/k8sclusterreceiver/internal/persistentvolumeclaim/persistentvolumeclaim.go index e1821c174b89..d0d94f9be54e 100644 --- a/receiver/k8sclusterreceiver/internal/persistentvolumeclaim/persistentvolumeclaim.go +++ b/receiver/k8sclusterreceiver/internal/persistentvolumeclaim/persistentvolumeclaim.go @@ -61,8 +61,18 @@ func RecordMetrics(mb *imetadata.MetricsBuilder, pvc *corev1.PersistentVolumeCla rb.SetK8sPersistentvolumeclaimLabels(mapToString(pvc.GetLabels(), "&")) rb.SetK8sPersistentvolumeclaimPhase(string(pvc.Status.Phase)) rb.SetK8sPersistentvolumeclaimSelector("") - rb.SetK8sPersistentvolumeclaimStorageClass(*pvc.Spec.StorageClassName) - rb.SetK8sPersistentvolumeclaimVolumeMode(string(*pvc.Spec.VolumeMode)) + + storageClassName := "unknown" + if pvc.Spec.StorageClassName != nil { + storageClassName = *pvc.Spec.StorageClassName + } + rb.SetK8sPersistentvolumeclaimStorageClass(storageClassName) + + volumeMode := "unknown" + if pvc.Spec.VolumeMode != nil { + volumeMode = string(*pvc.Spec.VolumeMode) + } + rb.SetK8sPersistentvolumeclaimVolumeMode(string(volumeMode)) rb.SetK8sPersistentvolumeclaimAccessModes(sliceToString(pvc.Spec.AccessModes, ",")) rb.SetK8sPersistentvolumeclaimFinalizers(strings.Join(pvc.Finalizers, ",")) rb.SetK8sPersistentvolumeclaimStartTime(pvc.GetCreationTimestamp().String()) diff --git a/receiver/k8sclusterreceiver/internal/statefulset/statefulsets.go b/receiver/k8sclusterreceiver/internal/statefulset/statefulsets.go index 4958d9b33540..df4c7f32e52c 100644 --- a/receiver/k8sclusterreceiver/internal/statefulset/statefulsets.go +++ b/receiver/k8sclusterreceiver/internal/statefulset/statefulsets.go @@ -39,7 +39,11 @@ func RecordMetrics(mb *imetadata.MetricsBuilder, ss *appsv1.StatefulSet, ts pcom if ss.Spec.Replicas == nil { return } - mb.RecordK8sStatefulsetDesiredPodsDataPoint(ts, int64(*ss.Spec.Replicas)) + var replicas int64 + if ss.Spec.Replicas != nil { + replicas = int64(*ss.Spec.Replicas) + } + mb.RecordK8sStatefulsetDesiredPodsDataPoint(ts, replicas) mb.RecordK8sStatefulsetReadyPodsDataPoint(ts, int64(ss.Status.ReadyReplicas)) mb.RecordK8sStatefulsetCurrentPodsDataPoint(ts, int64(ss.Status.CurrentReplicas)) mb.RecordK8sStatefulsetUpdatedPodsDataPoint(ts, int64(ss.Status.UpdatedReplicas))