Skip to content

Commit 4a35592

Browse files
committed
removing unsafe lib by switching to atomic.Pointer
1 parent af06345 commit 4a35592

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

pkg/epp/backend/metrics/pod_metrics.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"sync"
2323
"sync/atomic"
2424
"time"
25-
"unsafe"
2625

2726
"github.com/go-logr/logr"
2827
corev1 "k8s.io/api/core/v1"
@@ -36,8 +35,8 @@ const (
3635
)
3736

3837
type podMetrics struct {
39-
pod unsafe.Pointer // stores a *Pod
40-
metrics unsafe.Pointer // stores a *Metrics
38+
pod atomic.Pointer[Pod]
39+
metrics atomic.Pointer[Metrics]
4140
pmc PodMetricsClient
4241
ds Datastore
4342
interval time.Duration
@@ -58,15 +57,15 @@ func (pm *podMetrics) String() string {
5857
}
5958

6059
func (pm *podMetrics) GetPod() *Pod {
61-
return (*Pod)(atomic.LoadPointer(&pm.pod))
60+
return pm.pod.Load()
6261
}
6362

6463
func (pm *podMetrics) GetMetrics() *Metrics {
65-
return (*Metrics)(atomic.LoadPointer(&pm.metrics))
64+
return pm.metrics.Load()
6665
}
6766

6867
func (pm *podMetrics) UpdatePod(in *corev1.Pod) {
69-
atomic.StorePointer(&pm.pod, unsafe.Pointer(toInternalPod(in)))
68+
pm.pod.Store(toInternalPod(in))
7069
}
7170

7271
func toInternalPod(in *corev1.Pod) *Pod {
@@ -125,7 +124,7 @@ func (pm *podMetrics) refreshMetrics() error {
125124

126125
pm.logger.V(logutil.TRACE).Info("Refreshed metrics", "updated", updated)
127126

128-
atomic.StorePointer(&pm.metrics, unsafe.Pointer(updated))
127+
pm.metrics.Store(updated)
129128
return nil
130129
}
131130

pkg/epp/backend/metrics/types.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"fmt"
2323
"sync"
2424
"time"
25-
"unsafe"
2625

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

4443
func (f *PodMetricsFactory) NewPodMetrics(parentCtx context.Context, in *corev1.Pod, ds Datastore) PodMetrics {
4544
pm := &podMetrics{
46-
pod: unsafe.Pointer(toInternalPod(in)),
47-
metrics: unsafe.Pointer(newMetrics()),
4845
pmc: f.pmc,
4946
ds: ds,
5047
interval: f.refreshMetricsInterval,
@@ -53,6 +50,9 @@ func (f *PodMetricsFactory) NewPodMetrics(parentCtx context.Context, in *corev1.
5350
done: make(chan struct{}),
5451
logger: log.FromContext(parentCtx),
5552
}
53+
pm.pod.Store(toInternalPod(in))
54+
pm.metrics.Store(newMetrics())
55+
5656
pm.startRefreshLoop()
5757
return pm
5858
}

0 commit comments

Comments
 (0)