Skip to content

Commit

Permalink
Add nil pointers check to prevent crash in k8sclusterreceiver
Browse files Browse the repository at this point in the history
  • Loading branch information
tejaskokje-mw committed Aug 31, 2024
1 parent 6d8ef62 commit c33b9be
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion receiver/k8sclusterreceiver/internal/hpa/hpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit c33b9be

Please sign in to comment.