From 065899e95da0dbb96a8def5902f398a843b3a774 Mon Sep 17 00:00:00 2001 From: ylink-lfs Date: Sat, 26 Jul 2025 23:52:44 +0800 Subject: [PATCH] chore: use ptr.Deref to replace int deref utils --- pkg/kubelet/stats/cri_stats_provider.go | 20 ++++++++++---------- pkg/kubelet/stats/helper.go | 8 -------- test/utils/runners.go | 13 +++---------- 3 files changed, 13 insertions(+), 28 deletions(-) diff --git a/pkg/kubelet/stats/cri_stats_provider.go b/pkg/kubelet/stats/cri_stats_provider.go index edfe92d17be..e076ceca4dd 100644 --- a/pkg/kubelet/stats/cri_stats_provider.go +++ b/pkg/kubelet/stats/cri_stats_provider.go @@ -640,8 +640,8 @@ func (p *criStatsProvider) addPodCPUMemoryStats( } ps.CPU.Time = cs.CPU.Time - usageCoreNanoSeconds := getUint64Value(cs.CPU.UsageCoreNanoSeconds) + getUint64Value(ps.CPU.UsageCoreNanoSeconds) - usageNanoCores := getUint64Value(cs.CPU.UsageNanoCores) + getUint64Value(ps.CPU.UsageNanoCores) + usageCoreNanoSeconds := ptr.Deref(cs.CPU.UsageCoreNanoSeconds, 0) + ptr.Deref(ps.CPU.UsageCoreNanoSeconds, 0) + usageNanoCores := ptr.Deref(cs.CPU.UsageNanoCores, 0) + ptr.Deref(ps.CPU.UsageNanoCores, 0) ps.CPU.UsageCoreNanoSeconds = &usageCoreNanoSeconds ps.CPU.UsageNanoCores = &usageNanoCores // Pod level PSI stats cannot be calculated from container level @@ -653,12 +653,12 @@ func (p *criStatsProvider) addPodCPUMemoryStats( } ps.Memory.Time = cs.Memory.Time - availableBytes := getUint64Value(cs.Memory.AvailableBytes) + getUint64Value(ps.Memory.AvailableBytes) - usageBytes := getUint64Value(cs.Memory.UsageBytes) + getUint64Value(ps.Memory.UsageBytes) - workingSetBytes := getUint64Value(cs.Memory.WorkingSetBytes) + getUint64Value(ps.Memory.WorkingSetBytes) - rSSBytes := getUint64Value(cs.Memory.RSSBytes) + getUint64Value(ps.Memory.RSSBytes) - pageFaults := getUint64Value(cs.Memory.PageFaults) + getUint64Value(ps.Memory.PageFaults) - majorPageFaults := getUint64Value(cs.Memory.MajorPageFaults) + getUint64Value(ps.Memory.MajorPageFaults) + availableBytes := ptr.Deref(cs.Memory.AvailableBytes, 0) + ptr.Deref(ps.Memory.AvailableBytes, 0) + usageBytes := ptr.Deref(cs.Memory.UsageBytes, 0) + ptr.Deref(ps.Memory.UsageBytes, 0) + workingSetBytes := ptr.Deref(cs.Memory.WorkingSetBytes, 0) + ptr.Deref(ps.Memory.WorkingSetBytes, 0) + rSSBytes := ptr.Deref(cs.Memory.RSSBytes, 0) + ptr.Deref(ps.Memory.RSSBytes, 0) + pageFaults := ptr.Deref(cs.Memory.PageFaults, 0) + ptr.Deref(ps.Memory.PageFaults, 0) + majorPageFaults := ptr.Deref(cs.Memory.MajorPageFaults, 0) + ptr.Deref(ps.Memory.MajorPageFaults, 0) ps.Memory.AvailableBytes = &availableBytes ps.Memory.UsageBytes = &usageBytes ps.Memory.WorkingSetBytes = &workingSetBytes @@ -687,8 +687,8 @@ func (p *criStatsProvider) addSwapStats( if ps.Swap == nil { ps.Swap = &statsapi.SwapStats{Time: cs.Swap.Time} } - swapAvailableBytes := getUint64Value(cs.Swap.SwapAvailableBytes) + getUint64Value(ps.Swap.SwapAvailableBytes) - swapUsageBytes := getUint64Value(cs.Swap.SwapUsageBytes) + getUint64Value(ps.Swap.SwapUsageBytes) + swapAvailableBytes := ptr.Deref(cs.Swap.SwapAvailableBytes, 0) + ptr.Deref(ps.Swap.SwapAvailableBytes, 0) + swapUsageBytes := ptr.Deref(cs.Swap.SwapUsageBytes, 0) + ptr.Deref(ps.Swap.SwapUsageBytes, 0) ps.Swap.SwapAvailableBytes = &swapAvailableBytes ps.Swap.SwapUsageBytes = &swapUsageBytes } diff --git a/pkg/kubelet/stats/helper.go b/pkg/kubelet/stats/helper.go index cb7460a4166..93465aafec0 100644 --- a/pkg/kubelet/stats/helper.go +++ b/pkg/kubelet/stats/helper.go @@ -423,14 +423,6 @@ func buildRootfsStats(cstat *cadvisorapiv2.ContainerStats, imageFs *cadvisorapiv } } -func getUint64Value(value *uint64) uint64 { - if value == nil { - return 0 - } - - return *value -} - func calcEphemeralStorage(containers []statsapi.ContainerStats, volumes []statsapi.VolumeStats, rootFsInfo *cadvisorapiv2.FsInfo, podLogStats *statsapi.FsStats, etcHostsStats *statsapi.FsStats, isCRIStatsProvider bool) *statsapi.FsStats { result := &statsapi.FsStats{ diff --git a/test/utils/runners.go b/test/utils/runners.go index b04af70fda6..617a264cb40 100644 --- a/test/utils/runners.go +++ b/test/utils/runners.go @@ -54,13 +54,6 @@ const ( nonExist = "NonExist" ) -func removePtr(replicas *int32) int32 { - if replicas == nil { - return 0 - } - return *replicas -} - func waitUntilPodIsScheduled(ctx context.Context, c clientset.Interface, name, namespace string, timeout time.Duration) (*v1.Pod, error) { // Wait until it's scheduled p, err := c.CoreV1().Pods(namespace).Get(ctx, name, metav1.GetOptions{ResourceVersion: "0"}) @@ -351,7 +344,7 @@ func (config *DeploymentConfig) create() error { if err := CreateDeploymentWithRetries(config.Client, config.Namespace, deployment); err != nil { return fmt.Errorf("error creating deployment: %v", err) } - config.RCConfigLog("Created deployment with name: %v, namespace: %v, replica count: %v", deployment.Name, config.Namespace, removePtr(deployment.Spec.Replicas)) + config.RCConfigLog("Created deployment with name: %v, namespace: %v, replica count: %v", deployment.Name, config.Namespace, ptr.Deref(deployment.Spec.Replicas, 0)) return nil } @@ -418,7 +411,7 @@ func (config *ReplicaSetConfig) create() error { if err := CreateReplicaSetWithRetries(config.Client, config.Namespace, rs); err != nil { return fmt.Errorf("error creating replica set: %v", err) } - config.RCConfigLog("Created replica set with name: %v, namespace: %v, replica count: %v", rs.Name, config.Namespace, removePtr(rs.Spec.Replicas)) + config.RCConfigLog("Created replica set with name: %v, namespace: %v, replica count: %v", rs.Name, config.Namespace, ptr.Deref(rs.Spec.Replicas, 0)) return nil } @@ -494,7 +487,7 @@ func (config *RCConfig) create() error { if err := CreateRCWithRetries(config.Client, config.Namespace, rc); err != nil { return fmt.Errorf("error creating replication controller: %v", err) } - config.RCConfigLog("Created replication controller with name: %v, namespace: %v, replica count: %v", rc.Name, config.Namespace, removePtr(rc.Spec.Replicas)) + config.RCConfigLog("Created replication controller with name: %v, namespace: %v, replica count: %v", rc.Name, config.Namespace, ptr.Deref(rc.Spec.Replicas, 0)) return nil }