@@ -25,17 +25,14 @@ import (
25
25
discovery "k8s.io/api/discovery/v1"
26
26
"k8s.io/apimachinery/pkg/types"
27
27
endpointsliceutil "k8s.io/endpointslice/util"
28
- "k8s.io/utils/pointer "
28
+ "k8s.io/utils/ptr "
29
29
)
30
30
31
31
func TestNumEndpointsAndSlices (t * testing.T ) {
32
32
c := NewCache (int32 (100 ))
33
33
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 )}})
39
36
40
37
spCacheEfficient := NewServicePortCache ()
41
38
spCacheEfficient .Set (pmKey80 , EfficiencyInfo {Endpoints : 45 , Slices : 1 })
@@ -64,11 +61,8 @@ func TestNumEndpointsAndSlices(t *testing.T) {
64
61
func TestPlaceHolderSlice (t * testing.T ) {
65
62
c := NewCache (int32 (100 ))
66
63
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 )}})
72
66
73
67
sp := NewServicePortCache ()
74
68
sp .Set (pmKey80 , EfficiencyInfo {Endpoints : 0 , Slices : 1 })
@@ -113,79 +107,76 @@ func TestCache_ServicesByTrafficDistribution(t *testing.T) {
113
107
// Mutate and make assertions
114
108
115
109
desc := "service1 starts using trafficDistribution=PreferClose"
116
- cache .UpdateTrafficDistributionForService (service1 , ptrTo (corev1 .ServiceTrafficDistributionPreferClose ))
110
+ cache .UpdateTrafficDistributionForService (service1 , ptr . To (corev1 .ServiceTrafficDistributionPreferClose ))
117
111
mustHaveServicesByTrafficDistribution (map [string ]map [types.NamespacedName ]bool {
118
112
corev1 .ServiceTrafficDistributionPreferClose : {service1 : true },
119
113
}, desc )
120
114
121
115
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 ))
123
117
mustHaveServicesByTrafficDistribution (map [string ]map [types.NamespacedName ]bool { // No delta
124
118
corev1 .ServiceTrafficDistributionPreferClose : {service1 : true },
125
119
}, desc )
126
120
127
121
desc = "service2 starts using trafficDistribution=PreferClose"
128
- cache .UpdateTrafficDistributionForService (service2 , ptrTo (corev1 .ServiceTrafficDistributionPreferClose ))
122
+ cache .UpdateTrafficDistributionForService (service2 , ptr . To (corev1 .ServiceTrafficDistributionPreferClose ))
129
123
mustHaveServicesByTrafficDistribution (map [string ]map [types.NamespacedName ]bool {
130
124
corev1 .ServiceTrafficDistributionPreferClose : {service1 : true , service2 : true }, // Delta
131
125
}, desc )
132
126
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 " ))
135
129
mustHaveServicesByTrafficDistribution (map [string ]map [types.NamespacedName ]bool {
136
130
corev1 .ServiceTrafficDistributionPreferClose : {service1 : true , service2 : true },
137
- trafficDistributionImplementationSpecific : {service3 : true }, // Delta
131
+ "FutureValue" : {service3 : true }, // Delta
138
132
}, desc )
139
133
140
134
desc = "service4 starts using trafficDistribution=nil"
141
135
cache .UpdateTrafficDistributionForService (service4 , nil )
142
136
mustHaveServicesByTrafficDistribution (map [string ]map [types.NamespacedName ]bool { // No delta
143
137
corev1 .ServiceTrafficDistributionPreferClose : {service1 : true , service2 : true },
144
- trafficDistributionImplementationSpecific : {service3 : true },
138
+ "FutureValue" : {service3 : true },
145
139
}, desc )
146
140
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 " ))
149
143
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
152
147
}, desc )
153
148
154
149
desc = "service3 gets deleted"
155
150
cache .DeleteService (service3 )
156
151
mustHaveServicesByTrafficDistribution (map [string ]map [types.NamespacedName ]bool {
157
152
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 },
166
155
}, desc )
167
156
168
- desc = "service1 transitions trafficDistribution: InvalidValue -> nil"
157
+ desc = "service1 transitions trafficDistribution: PreferClose -> nil"
169
158
cache .UpdateTrafficDistributionForService (service1 , nil )
170
159
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 },
173
163
}, desc )
174
164
175
- desc = "service2 transitions trafficDistribution: InvalidValue -> nil"
165
+ desc = "service2 transitions trafficDistribution: AnotherFutureValue -> nil"
176
166
cache .UpdateTrafficDistributionForService (service2 , nil )
177
167
mustHaveServicesByTrafficDistribution (map [string ]map [types.NamespacedName ]bool {
178
168
corev1 .ServiceTrafficDistributionPreferClose : {},
179
- trafficDistributionImplementationSpecific : {}, // Delta
169
+ "FutureValue" : {},
170
+ "AnotherFutureValue" : {}, // Delta
180
171
}, desc )
181
172
182
173
}
183
174
184
175
func benchmarkUpdateServicePortCache (b * testing.B , num int ) {
185
176
c := NewCache (int32 (100 ))
186
177
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 )}})
189
180
spCache := & ServicePortCache {items : map [endpointsliceutil.PortMapKey ]EfficiencyInfo {
190
181
httpKey : {
191
182
Endpoints : 182 ,
@@ -224,7 +215,3 @@ func BenchmarkUpdateServicePortCache10000(b *testing.B) {
224
215
func BenchmarkUpdateServicePortCache100000 (b * testing.B ) {
225
216
benchmarkUpdateServicePortCache (b , 100000 )
226
217
}
227
-
228
- func ptrTo [T any ](obj T ) * T {
229
- return & obj
230
- }
0 commit comments