kubelet/metrics: misc optimization

This commit is contained in:
胡玮文
2025-09-04 19:29:01 +08:00
parent 55e8cdeb95
commit 98c6293623
3 changed files with 5 additions and 8 deletions

View File

@@ -107,7 +107,7 @@ func (collector *volumeStatsCollector) CollectWithStability(ch chan<- metrics.Me
lv = append([]string{pvcRef.Namespace, pvcRef.Name}, lv...)
ch <- metrics.NewLazyConstMetric(desc, metrics.GaugeValue, v, lv...)
}
allPVCs := sets.Set[string]{}
allPVCs := sets.Set[stats.PVCReference]{}
for _, podStat := range podStats {
if podStat.VolumeStats == nil {
continue
@@ -118,8 +118,7 @@ func (collector *volumeStatsCollector) CollectWithStability(ch chan<- metrics.Me
// ignore if no PVC reference
continue
}
pvcUniqStr := pvcRef.Namespace + "/" + pvcRef.Name
if allPVCs.Has(pvcUniqStr) {
if allPVCs.Has(*pvcRef) {
// ignore if already collected
continue
}
@@ -132,7 +131,7 @@ func (collector *volumeStatsCollector) CollectWithStability(ch chan<- metrics.Me
if volumeStat.VolumeHealthStats != nil {
addGauge(volumeStatsHealthAbnormalDesc, pvcRef, convertBoolToFloat64(volumeStat.VolumeHealthStats.Abnormal))
}
allPVCs.Insert(pvcUniqStr)
allPVCs.Insert(*pvcRef)
}
}
}

View File

@@ -1274,9 +1274,7 @@ func Register(collectors ...metrics.StableCollector) {
legacyregistry.MustRegister(OrphanPodCleanedVolumes)
legacyregistry.MustRegister(OrphanPodCleanedVolumesErrors)
for _, collector := range collectors {
legacyregistry.CustomMustRegister(collector)
}
legacyregistry.CustomMustRegister(collectors...)
if utilfeature.DefaultFeatureGate.Enabled(features.GracefulNodeShutdown) &&
utilfeature.DefaultFeatureGate.Enabled(features.GracefulNodeShutdownBasedOnPodPriority) {

View File

@@ -125,7 +125,7 @@ func (s *volumeStatCalculator) calcAndStoreStats() {
}
// Get volume specs for the pod - key'd by volume name
volumesSpec := make(map[string]v1.Volume)
volumesSpec := make(map[string]v1.Volume, len(s.pod.Spec.Volumes))
for _, v := range s.pod.Spec.Volumes {
volumesSpec[v.Name] = v
}