@@ -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
3131func 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) {
6461func 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,79 +107,76 @@ 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
133- desc = "service3 starts using trafficDistribution=InvalidValue "
134- cache .UpdateTrafficDistributionForService (service3 , ptrTo ( "InvalidValue " ))
127+ desc = "service3 starts using trafficDistribution=FutureValue "
128+ cache .UpdateTrafficDistributionForService (service3 , ptr . To ( "FutureValue " ))
135129 mustHaveServicesByTrafficDistribution (map [string ]map [types.NamespacedName ]bool {
136130 corev1 .ServiceTrafficDistributionPreferClose : {service1 : true , service2 : true },
137- trafficDistributionImplementationSpecific : {service3 : true }, // Delta
131+ "FutureValue" : {service3 : true }, // Delta
138132 }, desc )
139133
140134 desc = "service4 starts using trafficDistribution=nil"
141135 cache .UpdateTrafficDistributionForService (service4 , nil )
142136 mustHaveServicesByTrafficDistribution (map [string ]map [types.NamespacedName ]bool { // No delta
143137 corev1 .ServiceTrafficDistributionPreferClose : {service1 : true , service2 : true },
144- trafficDistributionImplementationSpecific : {service3 : true },
138+ "FutureValue" : {service3 : true },
145139 }, desc )
146140
147- desc = "service2 transitions trafficDistribution: PreferClose -> InvalidValue "
148- cache .UpdateTrafficDistributionForService (service2 , ptrTo ( "InvalidValue " ))
141+ desc = "service2 transitions trafficDistribution: PreferClose -> AnotherFutureValue "
142+ cache .UpdateTrafficDistributionForService (service2 , ptr . To ( "AnotherFutureValue " ))
149143 mustHaveServicesByTrafficDistribution (map [string ]map [types.NamespacedName ]bool {
150- corev1 .ServiceTrafficDistributionPreferClose : {service1 : true }, // Delta
151- trafficDistributionImplementationSpecific : {service3 : true , service2 : true }, // Delta
144+ corev1 .ServiceTrafficDistributionPreferClose : {service1 : true }, // Delta
145+ "FutureValue" : {service3 : true },
146+ "AnotherFutureValue" : {service2 : true }, // Delta
152147 }, desc )
153148
154149 desc = "service3 gets deleted"
155150 cache .DeleteService (service3 )
156151 mustHaveServicesByTrafficDistribution (map [string ]map [types.NamespacedName ]bool {
157152 corev1 .ServiceTrafficDistributionPreferClose : {service1 : true },
158- trafficDistributionImplementationSpecific : {service2 : true }, // Delta
159- }, desc )
160-
161- desc = "service1 transitions trafficDistribution: PreferClose -> empty"
162- cache .UpdateTrafficDistributionForService (service1 , ptrTo ("" ))
163- mustHaveServicesByTrafficDistribution (map [string ]map [types.NamespacedName ]bool {
164- corev1 .ServiceTrafficDistributionPreferClose : {}, // Delta
165- trafficDistributionImplementationSpecific : {service1 : true , service2 : true }, // Delta
153+ "FutureValue" : {}, // Delta
154+ "AnotherFutureValue" : {service2 : true },
166155 }, desc )
167156
168- desc = "service1 transitions trafficDistribution: InvalidValue -> nil"
157+ desc = "service1 transitions trafficDistribution: PreferClose -> nil"
169158 cache .UpdateTrafficDistributionForService (service1 , nil )
170159 mustHaveServicesByTrafficDistribution (map [string ]map [types.NamespacedName ]bool {
171- corev1 .ServiceTrafficDistributionPreferClose : {},
172- trafficDistributionImplementationSpecific : {service2 : true }, // Delta
160+ corev1 .ServiceTrafficDistributionPreferClose : {}, // Delta
161+ "FutureValue" : {},
162+ "AnotherFutureValue" : {service2 : true },
173163 }, desc )
174164
175- desc = "service2 transitions trafficDistribution: InvalidValue -> nil"
165+ desc = "service2 transitions trafficDistribution: AnotherFutureValue -> nil"
176166 cache .UpdateTrafficDistributionForService (service2 , nil )
177167 mustHaveServicesByTrafficDistribution (map [string ]map [types.NamespacedName ]bool {
178168 corev1 .ServiceTrafficDistributionPreferClose : {},
179- trafficDistributionImplementationSpecific : {}, // Delta
169+ "FutureValue" : {},
170+ "AnotherFutureValue" : {}, // Delta
180171 }, desc )
181172
182173}
183174
184175func benchmarkUpdateServicePortCache (b * testing.B , num int ) {
185176 c := NewCache (int32 (100 ))
186177 ns := "benchmark"
187- httpKey := endpointsliceutil .NewPortMapKey ([]discovery.EndpointPort {{Port : pointer . Int32 (80 )}})
188- httpsKey := endpointsliceutil .NewPortMapKey ([]discovery.EndpointPort {{Port : pointer . Int32 (443 )}})
178+ httpKey := endpointsliceutil .NewPortMapKey ([]discovery.EndpointPort {{Port : ptr. To [ int32 ] (80 )}})
179+ httpsKey := endpointsliceutil .NewPortMapKey ([]discovery.EndpointPort {{Port : ptr. To [ int32 ] (443 )}})
189180 spCache := & ServicePortCache {items : map [endpointsliceutil.PortMapKey ]EfficiencyInfo {
190181 httpKey : {
191182 Endpoints : 182 ,
@@ -224,7 +215,3 @@ func BenchmarkUpdateServicePortCache10000(b *testing.B) {
224215func BenchmarkUpdateServicePortCache100000 (b * testing.B ) {
225216 benchmarkUpdateServicePortCache (b , 100000 )
226217}
227-
228- func ptrTo [T any ](obj T ) * T {
229- return & obj
230- }
0 commit comments