8000 fix: k8s resource nil point by Abingcbc · Pull Request #2216 · alibaba/loongcollector · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: k8s resource nil point #2216

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 ou 8000 r terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 16, 2025
Merged

Conversation

Abingcbc
Copy link
Collaborator

问题

image
K8s资源中,这类虽然有默认值的指针,在通过client-go获取时仍然可能是空的。

解决方案

  1. 检查是否是空指针
  2. 添加panicRecover,保证至少不crash

@@ -23,7 +23,11 @@ func (m *metaCollector) processDeploymentEntity(data *k8smeta.ObjectWrapper, met
log.Contents.Add("labels", m.processEntityJSONObject(obj.Labels))
log.Contents.Add("annotations", m.processEntityJSONObject(obj.Annotations))
log.Contents.Add("match_labels", m.processEntityJSONObject(obj.Spec.Selector.MatchLabels))
log.Contents.Add("replicas", strconv.FormatInt(int64(*obj.Spec.Replicas), 10))
if obj.Spec.Replicas != nil {
log.Contents.Add("replicas", strconv.FormatInt(int64(*obj.Spec.Replicas), 10))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if else有点多,能否封装下函数

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@@ -209,7 +209,11 @@ func (m *metaCollector) processPersistentVolumeEntity(data *k8smeta.ObjectWrappe
log.Contents.Add("status", string(obj.Status.Phase))
log.Contents.Add("storage_class_name", obj.Spec.StorageClassName)
log.Contents.Add("persistent_volume_reclaim_policy", string(obj.Spec.PersistentVolumeReclaimPolicy))
log.Contents.Add("volume_mode", string(*obj.Spec.VolumeMode))
if obj.Spec.VolumeMode != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为什么还是if esle

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个字段的类型是type VolumeMode string 特殊的,没有可以复用的地方

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其他保留if else的都是这个原因

@Abingcbc Abingcbc merged commit 508790d into alibaba:main May 16, 2025
26 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0