diff --git a/pkg/kubelet/cm/container_manager_linux.go b/pkg/kubelet/cm/container_manager_linux.go index c08aa56f24491..c65053cd678d7 100644 --- a/pkg/kubelet/cm/container_manager_linux.go +++ b/pkg/kubelet/cm/container_manager_linux.go @@ -317,7 +317,7 @@ func NewContainerManager(mountUtil mount.Interface, cadvisorInterface cadvisor.I if err != nil { return nil, err } - metrics.Register(cm.draManager.NewMetricsCollector()) + metrics.RegisterCollectors(cm.draManager.NewMetricsCollector()) } cm.kubeClient = kubeClient diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index 8276ddbae91ea..f72dfa0a3093a 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -1632,7 +1632,8 @@ func (kl *Kubelet) StartGarbageCollection() { // Note that the modules here must not depend on modules that are not initialized here. func (kl *Kubelet) initializeModules(ctx context.Context) error { // Prometheus metrics. - metrics.Register( + metrics.Register() + metrics.RegisterCollectors( collectors.NewVolumeStatsCollector(kl), collectors.NewLogMetricsCollector(kl.StatsProvider.ListPodStats), ) diff --git a/pkg/kubelet/metrics/collectors/volume_stats.go b/pkg/kubelet/metrics/collectors/volume_stats.go index b565bf1d376f5..b139cf5eaecb1 100644 --- a/pkg/kubelet/metrics/collectors/volume_stats.go +++ b/pkg/kubelet/metrics/collectors/volume_stats.go @@ -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) } } } diff --git a/pkg/kubelet/metrics/metrics.go b/pkg/kubelet/metrics/metrics.go index 03731586dc3f6..d960989d2bd17 100644 --- a/pkg/kubelet/metrics/metrics.go +++ b/pkg/kubelet/metrics/metrics.go @@ -1194,7 +1194,7 @@ var ( var registerMetrics sync.Once // Register registers all metrics. -func Register(collectors ...metrics.StableCollector) { +func Register() { // Register the metrics. registerMetrics.Do(func() { legacyregistry.MustRegister(FirstNetworkPodStartSLIDuration) @@ -1274,10 +1274,6 @@ func Register(collectors ...metrics.StableCollector) { legacyregistry.MustRegister(OrphanPodCleanedVolumes) legacyregistry.MustRegister(OrphanPodCleanedVolumesErrors) - for _, collector := range collectors { - legacyregistry.CustomMustRegister(collector) - } - if utilfeature.DefaultFeatureGate.Enabled(features.GracefulNodeShutdown) && utilfeature.DefaultFeatureGate.Enabled(features.GracefulNodeShutdownBasedOnPodPriority) { legacyregistry.MustRegister(GracefulShutdownStartTime) @@ -1314,6 +1310,10 @@ func Register(collectors ...metrics.StableCollector) { }) } +func RegisterCollectors(collectors ...metrics.StableCollector) { + legacyregistry.CustomMustRegister(collectors...) +} + // GetGather returns the gatherer. It used by test case outside current package. func GetGather() metrics.Gatherer { return legacyregistry.DefaultGatherer diff --git a/pkg/kubelet/server/stats/volume_stat_calculator.go b/pkg/kubelet/server/stats/volume_stat_calculator.go index 671c72ca4b51d..fc6bea1adc7e3 100644 --- a/pkg/kubelet/server/stats/volume_stat_calculator.go +++ b/pkg/kubelet/server/stats/volume_stat_calculator.go @@ -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 }