Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions pkg/epp/backend/metrics/pod_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"sync"
"sync/atomic"
"time"
"unsafe"

"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
Expand All @@ -36,8 +35,8 @@ const (
)

type podMetrics struct {
pod unsafe.Pointer // stores a *Pod
metrics unsafe.Pointer // stores a *Metrics
pod atomic.Pointer[Pod]
metrics atomic.Pointer[Metrics]
pmc PodMetricsClient
ds Datastore
interval time.Duration
Expand All @@ -58,15 +57,15 @@ func (pm *podMetrics) String() string {
}

func (pm *podMetrics) GetPod() *Pod {
return (*Pod)(atomic.LoadPointer(&pm.pod))
return pm.pod.Load()
}

func (pm *podMetrics) GetMetrics() *Metrics {
return (*Metrics)(atomic.LoadPointer(&pm.metrics))
return pm.metrics.Load()
}

func (pm *podMetrics) UpdatePod(in *corev1.Pod) {
atomic.StorePointer(&pm.pod, unsafe.Pointer(toInternalPod(in)))
pm.pod.Store(toInternalPod(in))
}

func toInternalPod(in *corev1.Pod) *Pod {
Expand Down Expand Up @@ -128,7 +127,7 @@ func (pm *podMetrics) refreshMetrics() error {
if updated != nil {
updated.UpdateTime = time.Now()
pm.logger.V(logutil.TRACE).Info("Refreshed metrics", "updated", updated)
atomic.StorePointer(&pm.metrics, unsafe.Pointer(updated))
pm.metrics.Store(updated)
}

return nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/epp/backend/metrics/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"
"sync"
"time"
"unsafe"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -43,8 +42,6 @@ type PodMetricsFactory struct {

func (f *PodMetricsFactory) NewPodMetrics(parentCtx context.Context, in *corev1.Pod, ds Datastore) PodMetrics {
pm := &podMetrics{
pod: unsafe.Pointer(toInternalPod(in)),
metrics: unsafe.Pointer(newMetrics()),
pmc: f.pmc,
ds: ds,
interval: f.refreshMetricsInterval,
Expand All @@ -53,6 +50,9 @@ func (f *PodMetricsFactory) NewPodMetrics(parentCtx context.Context, in *corev1.
done: make(chan struct{}),
logger: log.FromContext(parentCtx),
}
pm.pod.Store(toInternalPod(in))
pm.metrics.Store(newMetrics())

pm.startRefreshLoop()
return pm
}
Expand Down