Skip to content

Commit 2d4453a

Browse files
danwinshipk8s-publishing-bot
authored andcommitted
Port k8s.io/endpointslice from utils/pointer to utils/ptr
Kubernetes-commit: fdddd8d18c97a69eaa20a87cd58af2f1a4f6598c
1 parent 117db8a commit 2d4453a

10 files changed

+346
-362
lines changed

metrics/cache_test.go

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,14 @@ import (
2525
discovery "k8s.io/api/discovery/v1"
2626
"k8s.io/apimachinery/pkg/types"
2727
endpointsliceutil "k8s.io/endpointslice/util"
28-
"k8s.io/utils/pointer"
28+
"k8s.io/utils/ptr"
2929
)
3030

3131
func TestNumEndpointsAndSlices(t *testing.T) {
3232
c := NewCache(int32(100))
3333

34-
p80 := int32(80)
35-
p443 := int32(443)
36-
37-
pmKey80443 := endpointsliceutil.NewPortMapKey([]discovery.EndpointPort{{Port: &p80}, {Port: &p443}})
38-
pmKey80 := endpointsliceutil.NewPortMapKey([]discovery.EndpointPort{{Port: &p80}})
34+
pmKey80443 := endpointsliceutil.NewPortMapKey([]discovery.EndpointPort{{Port: ptr.To[int32](80)}, {Port: ptr.To[int32](443)}})
35+
pmKey80 := endpointsliceutil.NewPortMapKey([]discovery.EndpointPort{{Port: ptr.To[int32](80)}})
3936

4037
spCacheEfficient := NewServicePortCache()
4138
spCacheEfficient.Set(pmKey80, EfficiencyInfo{Endpoints: 45, Slices: 1})
@@ -64,11 +61,8 @@ func TestNumEndpointsAndSlices(t *testing.T) {
6461
func TestPlaceHolderSlice(t *testing.T) {
6562
c := NewCache(int32(100))
6663

67-
p80 := int32(80)
68-
p443 := int32(443)
69-
70-
pmKey80443 := endpointsliceutil.NewPortMapKey([]discovery.EndpointPort{{Port: &p80}, {Port: &p443}})
71-
pmKey80 := endpointsliceutil.NewPortMapKey([]discovery.EndpointPort{{Port: &p80}})
64+
pmKey80443 := endpointsliceutil.NewPortMapKey([]discovery.EndpointPort{{Port: ptr.To[int32](80)}, {Port: ptr.To[int32](443)}})
65+
pmKey80 := endpointsliceutil.NewPortMapKey([]discovery.EndpointPort{{Port: ptr.To[int32](80)}})
7266

7367
sp := NewServicePortCache()
7468
sp.Set(pmKey80, EfficiencyInfo{Endpoints: 0, Slices: 1})
@@ -113,25 +107,25 @@ func TestCache_ServicesByTrafficDistribution(t *testing.T) {
113107
// Mutate and make assertions
114108

115109
desc := "service1 starts using trafficDistribution=PreferClose"
116-
cache.UpdateTrafficDistributionForService(service1, ptrTo(corev1.ServiceTrafficDistributionPreferClose))
110+
cache.UpdateTrafficDistributionForService(service1, ptr.To(corev1.ServiceTrafficDistributionPreferClose))
117111
mustHaveServicesByTrafficDistribution(map[string]map[types.NamespacedName]bool{
118112
corev1.ServiceTrafficDistributionPreferClose: {service1: true},
119113
}, desc)
120114

121115
desc = "service1 starts using trafficDistribution=PreferClose, retries of similar mutation should be idempotent"
122-
cache.UpdateTrafficDistributionForService(service1, ptrTo(corev1.ServiceTrafficDistributionPreferClose))
116+
cache.UpdateTrafficDistributionForService(service1, ptr.To(corev1.ServiceTrafficDistributionPreferClose))
123117
mustHaveServicesByTrafficDistribution(map[string]map[types.NamespacedName]bool{ // No delta
124118
corev1.ServiceTrafficDistributionPreferClose: {service1: true},
125119
}, desc)
126120

127121
desc = "service2 starts using trafficDistribution=PreferClose"
128-
cache.UpdateTrafficDistributionForService(service2, ptrTo(corev1.ServiceTrafficDistributionPreferClose))
122+
cache.UpdateTrafficDistributionForService(service2, ptr.To(corev1.ServiceTrafficDistributionPreferClose))
129123
mustHaveServicesByTrafficDistribution(map[string]map[types.NamespacedName]bool{
130124
corev1.ServiceTrafficDistributionPreferClose: {service1: true, service2: true}, // Delta
131125
}, desc)
132126

133127
desc = "service3 starts using trafficDistribution=InvalidValue"
134-
cache.UpdateTrafficDistributionForService(service3, ptrTo("InvalidValue"))
128+
cache.UpdateTrafficDistributionForService(service3, ptr.To("InvalidValue"))
135129
mustHaveServicesByTrafficDistribution(map[string]map[types.NamespacedName]bool{
136130
corev1.ServiceTrafficDistributionPreferClose: {service1: true, service2: true},
137131
trafficDistributionImplementationSpecific: {service3: true}, // Delta
@@ -145,7 +139,7 @@ func TestCache_ServicesByTrafficDistribution(t *testing.T) {
145139
}, desc)
146140

147141
desc = "service2 transitions trafficDistribution: PreferClose -> InvalidValue"
148-
cache.UpdateTrafficDistributionForService(service2, ptrTo("InvalidValue"))
142+
cache.UpdateTrafficDistributionForService(service2, ptr.To("InvalidValue"))
149143
mustHaveServicesByTrafficDistribution(map[string]map[types.NamespacedName]bool{
150144
corev1.ServiceTrafficDistributionPreferClose: {service1: true}, // Delta
151145
trafficDistributionImplementationSpecific: {service3: true, service2: true}, // Delta
@@ -159,7 +153,7 @@ func TestCache_ServicesByTrafficDistribution(t *testing.T) {
159153
}, desc)
160154

161155
desc = "service1 transitions trafficDistribution: PreferClose -> empty"
162-
cache.UpdateTrafficDistributionForService(service1, ptrTo(""))
156+
cache.UpdateTrafficDistributionForService(service1, ptr.To(""))
163157
mustHaveServicesByTrafficDistribution(map[string]map[types.NamespacedName]bool{
164158
corev1.ServiceTrafficDistributionPreferClose: {}, // Delta
165159
trafficDistributionImplementationSpecific: {service1: true, service2: true}, // Delta
@@ -184,8 +178,8 @@ func TestCache_ServicesByTrafficDistribution(t *testing.T) {
184178
func benchmarkUpdateServicePortCache(b *testing.B, num int) {
185179
c := NewCache(int32(100))
186180
ns := "benchmark"
187-
httpKey := endpointsliceutil.NewPortMapKey([]discovery.EndpointPort{{Port: pointer.Int32(80)}})
188-
httpsKey := endpointsliceutil.NewPortMapKey([]discovery.EndpointPort{{Port: pointer.Int32(443)}})
181+
httpKey := endpointsliceutil.NewPortMapKey([]discovery.EndpointPort{{Port: ptr.To[int32](80)}})
182+
httpsKey := endpointsliceutil.NewPortMapKey([]discovery.EndpointPort{{Port: ptr.To[int32](443)}})
189183
spCache := &ServicePortCache{items: map[endpointsliceutil.PortMapKey]EfficiencyInfo{
190184
httpKey: {
191185
Endpoints: 182,
@@ -224,7 +218,3 @@ func BenchmarkUpdateServicePortCache10000(b *testing.B) {
224218
func BenchmarkUpdateServicePortCache100000(b *testing.B) {
225219
benchmarkUpdateServicePortCache(b, 100000)
226220
}
227-
228-
func ptrTo[T any](obj T) *T {
229-
return &obj
230-
}

reconciler_test.go

Lines changed: 64 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import (
4444
"k8s.io/endpointslice/topologycache"
4545
endpointsliceutil "k8s.io/endpointslice/util"
4646
"k8s.io/klog/v2/ktesting"
47-
"k8s.io/utils/pointer"
47+
"k8s.io/utils/ptr"
4848
)
4949

5050
const (
@@ -189,12 +189,12 @@ func TestReconcile1Pod(t *testing.T) {
189189
{
190190
Addresses: []string{"1.2.3.4"},
191191
Conditions: discovery.EndpointConditions{
192-
Ready: pointer.Bool(true),
193-
Serving: pointer.Bool(true),
194-
Terminating: pointer.Bool(false),
192+
Ready: ptr.To(true),
193+
Serving: ptr.To(true),
194+
Terminating: ptr.To(false),
195195
},
196-
Zone: pointer.String("us-central1-a"),
197-
NodeName: pointer.String("node-1"),
196+
Zone: ptr.To("us-central1-a"),
197+
NodeName: ptr.To("node-1"),
198198
TargetRef: &corev1.ObjectReference{
199199
Kind: "Pod",
200200
Namespace: namespace,
@@ -215,12 +215,12 @@ func TestReconcile1Pod(t *testing.T) {
215215
{
216216
Addresses: []string{"1.2.3.4"},
217217
Conditions: discovery.EndpointConditions{
218-
Ready: pointer.Bool(true),
219-
Serving: pointer.Bool(true),
220-
Terminating: pointer.Bool(false),
218+
Ready: ptr.To(true),
219+
Serving: ptr.To(true),
220+
Terminating: ptr.To(false),
221221
},
222-
Zone: pointer.String("us-central1-a"),
223-
NodeName: pointer.String("node-1"),
222+
Zone: ptr.To("us-central1-a"),
223+
NodeName: ptr.To("node-1"),
224224
TargetRef: &corev1.ObjectReference{
225225
Kind: "Pod",
226226
Namespace: namespace,
@@ -242,12 +242,12 @@ func TestReconcile1Pod(t *testing.T) {
242242
{
243243
Addresses: []string{"1.2.3.4"},
244244
Conditions: discovery.EndpointConditions{
245-
Ready: pointer.Bool(true),
246-
Serving: pointer.Bool(true),
247-
Terminating: pointer.Bool(false),
245+
Ready: ptr.To(true),
246+
Serving: ptr.To(true),
247+
Terminating: ptr.To(false),
248248
},
249-
Zone: pointer.String("us-central1-a"),
250-
NodeName: pointer.String("node-1"),
249+
Zone: ptr.To("us-central1-a"),
250+
NodeName: ptr.To("node-1"),
251251
TargetRef: &corev1.ObjectReference{
252252
Kind: "Pod",
253253
Namespace: namespace,
@@ -260,12 +260,12 @@ func TestReconcile1Pod(t *testing.T) {
260260
expectedEndpoint: discovery.Endpoint{
261261
Addresses: []string{"1.2.3.4"},
262262
Conditions: discovery.EndpointConditions{
263-
Ready: pointer.Bool(true),
264-
Serving: pointer.Bool(true),
265-
Terminating: pointer.Bool(false),
263+
Ready: ptr.To(true),
264+
Serving: ptr.To(true),
265+
Terminating: ptr.To(false),
266266
},
267-
Zone: pointer.String("us-central1-a"),
268-
NodeName: pointer.String("node-1"),
267+
Zone: ptr.To("us-central1-a"),
268+
NodeName: ptr.To("node-1"),
269269
TargetRef: &corev1.ObjectReference{
270270
Kind: "Pod",
271271
Namespace: namespace,
@@ -284,12 +284,12 @@ func TestReconcile1Pod(t *testing.T) {
284284
{
285285
Addresses: []string{"1.2.3.4"},
286286
Conditions: discovery.EndpointConditions{
287-
Ready: pointer.Bool(true),
288-
Serving: pointer.Bool(true),
289-
Terminating: pointer.Bool(false),
287+
Ready: ptr.To(true),
288+
Serving: ptr.To(true),
289+
Terminating: ptr.To(false),
290290
},
291-
Zone: pointer.String("us-central1-a"),
292-
NodeName: pointer.String("node-1"),
291+
Zone: ptr.To("us-central1-a"),
292+
NodeName: ptr.To("node-1"),
293293
TargetRef: &corev1.ObjectReference{
294294
Kind: "Pod",
295295
Namespace: namespace,
@@ -302,12 +302,12 @@ func TestReconcile1Pod(t *testing.T) {
302302
expectedEndpoint: discovery.Endpoint{
303303
Addresses: []string{"1.2.3.4"},
304304
Conditions: discovery.EndpointConditions{
305-
Ready: pointer.Bool(true),
306-
Serving: pointer.Bool(true),
307-
Terminating: pointer.Bool(false),
305+
Ready: ptr.To(true),
306+
Serving: ptr.To(true),
307+
Terminating: ptr.To(false),
308308
},
309-
Zone: pointer.String("us-central1-a"),
310-
NodeName: pointer.String("node-1"),
309+
Zone: ptr.To("us-central1-a"),
310+
NodeName: ptr.To("node-1"),
311311
TargetRef: &corev1.ObjectReference{
312312
Kind: "Pod",
313313
Namespace: namespace,
@@ -328,12 +328,12 @@ func TestReconcile1Pod(t *testing.T) {
328328
{
329329
Addresses: []string{"1.2.3.4"},
330330
Conditions: discovery.EndpointConditions{
331-
Ready: pointer.Bool(true),
332-
Serving: pointer.Bool(true),
333-
Terminating: pointer.Bool(false),
331+
Ready: ptr.To(true),
332+
Serving: ptr.To(true),
333+
Terminating: ptr.To(false),
334334
},
335-
Zone: pointer.String("us-central1-a"),
336-
NodeName: pointer.String("node-1"),
335+
Zone: ptr.To("us-central1-a"),
336+
NodeName: ptr.To("node-1"),
337337
TargetRef: &corev1.ObjectReference{
338338
Kind: "Pod",
339339
Namespace: namespace,
@@ -346,12 +346,12 @@ func TestReconcile1Pod(t *testing.T) {
346346
expectedEndpoint: discovery.Endpoint{
347347
Addresses: []string{"1.2.3.4"},
348348
Conditions: discovery.EndpointConditions{
349-
Ready: pointer.Bool(true),
350-
Serving: pointer.Bool(true),
351-
Terminating: pointer.Bool(false),
349+
Ready: ptr.To(true),
350+
Serving: ptr.To(true),
351+
Terminating: ptr.To(false),
352352
},
353-
Zone: pointer.String("us-central1-a"),
354-
NodeName: pointer.String("node-1"),
353+
Zone: ptr.To("us-central1-a"),
354+
NodeName: ptr.To("node-1"),
355355
TargetRef: &corev1.ObjectReference{
356356
Kind: "Pod",
357357
Namespace: namespace,
@@ -372,12 +372,12 @@ func TestReconcile1Pod(t *testing.T) {
372372
{
373373
Addresses: []string{"1234::5678:0:0:9abc:def0"},
374374
Conditions: discovery.EndpointConditions{
375-
Ready: pointer.Bool(true),
376-
Serving: pointer.Bool(true),
377-
Terminating: pointer.Bool(false),
375+
Ready: ptr.To(true),
376+
Serving: ptr.To(true),
377+
Terminating: ptr.To(false),
378378
},
379-
Zone: pointer.String("us-central1-a"),
380-
NodeName: pointer.String("node-1"),
379+
Zone: ptr.To("us-central1-a"),
380+
NodeName: ptr.To("node-1"),
381381
TargetRef: &corev1.ObjectReference{
382382
Kind: "Pod",
383383
Namespace: namespace,
@@ -400,12 +400,12 @@ func TestReconcile1Pod(t *testing.T) {
400400
{
401401
Addresses: []string{"1234::5678:0:0:9abc:def0"},
402402
Conditions: discovery.EndpointConditions{
403-
Ready: pointer.Bool(true),
404-
Serving: pointer.Bool(true),
405-
Terminating: pointer.Bool(false),
403+
Ready: ptr.To(true),
404+
Serving: ptr.To(true),
405+
Terminating: ptr.To(false),
406406
},
407-
Zone: pointer.String("us-central1-a"),
408-
NodeName: pointer.String("node-1"),
407+
Zone: ptr.To("us-central1-a"),
408+
NodeName: ptr.To("node-1"),
409409
TargetRef: &corev1.ObjectReference{
410410
Kind: "Pod",
411411
Namespace: namespace,
@@ -427,12 +427,12 @@ func TestReconcile1Pod(t *testing.T) {
427427
{
428428
Addresses: []string{"1234::5678:0:0:9abc:def0"},
429429
Conditions: discovery.EndpointConditions{
430-
Ready: pointer.Bool(true),
431-
Serving: pointer.Bool(true),
432-
Terminating: pointer.Bool(false),
430+
Ready: ptr.To(true),
431+
Serving: ptr.To(true),
432+
Terminating: ptr.To(false),
433433
},
434-
Zone: pointer.String("us-central1-a"),
435-
NodeName: pointer.String("node-1"),
434+
Zone: ptr.To("us-central1-a"),
435+
NodeName: ptr.To("node-1"),
436436
TargetRef: &corev1.ObjectReference{
437437
Kind: "Pod",
438438
Namespace: namespace,
@@ -444,12 +444,12 @@ func TestReconcile1Pod(t *testing.T) {
444444
{
445445
Addresses: []string{"1.2.3.4"},
446446
Conditions: discovery.EndpointConditions{
447-
Ready: pointer.Bool(true),
448-
Serving: pointer.Bool(true),
449-
Terminating: pointer.Bool(false),
447+
Ready: ptr.To(true),
448+
Serving: ptr.To(true),
449+
Terminating: ptr.To(false),
450450
},
451-
Zone: pointer.String("us-central1-a"),
452-
NodeName: pointer.String("node-1"),
451+
Zone: ptr.To("us-central1-a"),
452+
NodeName: ptr.To("node-1"),
453453
TargetRef: &corev1.ObjectReference{
454454
Kind: "Pod",
455455
Namespace: namespace,
@@ -1252,14 +1252,13 @@ func TestReconcileEndpointSlicesNamedPorts(t *testing.T) {
12521252
expectUnorderedSlicesWithLengths(t, fetchedSlices, []int{60, 60, 60, 60, 60})
12531253

12541254
// generate data structures for expected slice ports and address types
1255-
protoTCP := corev1.ProtocolTCP
12561255
expectedSlices := []discovery.EndpointSlice{}
12571256
for i := range fetchedSlices {
12581257
expectedSlices = append(expectedSlices, discovery.EndpointSlice{
12591258
Ports: []discovery.EndpointPort{{
1260-
Name: pointer.String(""),
1261-
Protocol: &protoTCP,
1262-
Port: pointer.Int32(int32(8080 + i)),
1259+
Name: ptr.To(""),
1260+
Protocol: ptr.To(corev1.ProtocolTCP),
1261+
Port: ptr.To[int32](int32(8080 + i)),
12631262
}},
12641263
AddressType: discovery.AddressTypeIPv4,
12651264
})

topologycache/sliceinfo_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"testing"
2222

2323
discovery "k8s.io/api/discovery/v1"
24-
"k8s.io/utils/pointer"
24+
"k8s.io/utils/ptr"
2525
)
2626

2727
func Test_getTotalReadyEndpoints(t *testing.T) {
@@ -93,14 +93,14 @@ func sliceWithNEndpoints(ready, unready int) *discovery.EndpointSlice {
9393
for i := 0; i < ready; i++ {
9494
endpoints[i] = discovery.Endpoint{
9595
Addresses: []string{fmt.Sprintf("10.1.2.%d", i)},
96-
Conditions: discovery.EndpointConditions{Ready: pointer.Bool(true)},
96+
Conditions: discovery.EndpointConditions{Ready: ptr.To(true)},
9797
}
9898
}
9999

100100
for i := 0; i < unready; i++ {
101101
endpoints[ready+i] = discovery.Endpoint{
102102
Addresses: []string{fmt.Sprintf("10.1.2.%d", ready+i)},
103-
Conditions: discovery.EndpointConditions{Ready: pointer.Bool(false)},
103+
Conditions: discovery.EndpointConditions{Ready: ptr.To(false)},
104104
}
105105
}
106106

0 commit comments

Comments
 (0)