@@ -18,6 +18,7 @@ package validation
18
18
19
19
import (
20
20
"fmt"
21
+ "slices"
21
22
22
23
corev1 "k8s.io/api/core/v1"
23
24
apiequality "k8s.io/apimachinery/pkg/api/equality"
@@ -33,15 +34,15 @@ import (
33
34
)
34
35
35
36
var (
36
- supportedAddressTypes = sets .NewString (
37
- string ( discovery .AddressTypeIPv4 ) ,
38
- string ( discovery .AddressTypeIPv6 ) ,
39
- string ( discovery .AddressTypeFQDN ) ,
37
+ supportedAddressTypes = sets .New (
38
+ discovery .AddressTypeIPv4 ,
39
+ discovery .AddressTypeIPv6 ,
40
+ discovery .AddressTypeFQDN ,
40
41
)
41
- supportedPortProtocols = sets .NewString (
42
- string ( api .ProtocolTCP ) ,
43
- string ( api .ProtocolUDP ) ,
44
- string ( api .ProtocolSCTP ) ,
42
+ supportedPortProtocols = sets .New (
43
+ api .ProtocolTCP ,
44
+ api .ProtocolUDP ,
45
+ api .ProtocolSCTP ,
45
46
)
46
47
maxTopologyLabels = 16
47
48
maxAddresses = 100
@@ -172,24 +173,26 @@ func validatePorts(endpointPorts []discovery.EndpointPort, fldPath *field.Path)
172
173
return allErrs
173
174
}
174
175
175
- portNames := sets.String {}
176
+ // Even though a sets.Set would be more idiomatic, we use a []string here to avoid
177
+ // extra allocations (especially since there are presumably only a few ports anyway).
178
+ portNames := make ([]string , 0 , len (endpointPorts ))
176
179
for i , endpointPort := range endpointPorts {
177
180
idxPath := fldPath .Index (i )
178
181
179
182
if len (* endpointPort .Name ) > 0 {
180
183
allErrs = append (allErrs , apivalidation .ValidateDNS1123Label (* endpointPort .Name , idxPath .Child ("name" ))... )
181
184
}
182
185
183
- if portNames . Has ( * endpointPort .Name ) {
186
+ if slices . Contains ( portNames , * endpointPort .Name ) {
184
187
allErrs = append (allErrs , field .Duplicate (idxPath .Child ("name" ), endpointPort .Name ))
185
188
} else {
186
- portNames . Insert ( * endpointPort .Name )
189
+ portNames = append ( portNames , * endpointPort .Name )
187
190
}
188
191
189
192
if endpointPort .Protocol == nil {
190
193
allErrs = append (allErrs , field .Required (idxPath .Child ("protocol" ), "" ))
191
- } else if ! supportedPortProtocols .Has (string ( * endpointPort .Protocol ) ) {
192
- allErrs = append (allErrs , field .NotSupported (idxPath .Child ("protocol" ), * endpointPort .Protocol , supportedPortProtocols .List ()))
194
+ } else if ! supportedPortProtocols .Has (* endpointPort .Protocol ) {
195
+ allErrs = append (allErrs , field .NotSupported (idxPath .Child ("protocol" ), * endpointPort .Protocol , sets .List (supportedPortProtocols )))
193
196
}
194
197
195
198
if endpointPort .AppProtocol != nil {
@@ -205,8 +208,8 @@ func validateAddressType(addressType discovery.AddressType) field.ErrorList {
205
208
206
209
if addressType == "" {
207
210
allErrs = append (allErrs , field .Required (field .NewPath ("addressType" ), "" ))
208
- } else if ! supportedAddressTypes .Has (string ( addressType ) ) {
209
- allErrs = append (allErrs , field .NotSupported (field .NewPath ("addressType" ), addressType , supportedAddressTypes .List ()))
211
+ } else if ! supportedAddressTypes .Has (addressType ) {
212
+ allErrs = append (allErrs , field .NotSupported (field .NewPath ("addressType" ), addressType , sets .List (supportedAddressTypes )))
210
213
}
211
214
212
215
return allErrs
@@ -221,13 +224,15 @@ func validateHints(endpointHints *discovery.EndpointHints, fldPath *field.Path)
221
224
return allErrs
222
225
}
223
226
224
- zoneNames := sets.String {}
227
+ // Even though a sets.Set would be more idiomatic, we use a []string here to avoid
228
+ // extra allocations (especially since there is normally only one zone anyway).
229
+ zoneNames := make ([]string , 0 , len (endpointHints .ForZones ))
225
230
for i , forZone := range endpointHints .ForZones {
226
231
zonePath := fzPath .Index (i ).Child ("name" )
227
- if zoneNames . Has ( forZone .Name ) {
232
+ if slices . Contains ( zoneNames , forZone .Name ) {
228
233
allErrs = append (allErrs , field .Duplicate (zonePath , forZone .Name ))
229
234
} else {
230
- zoneNames . Insert ( forZone .Name )
235
+ zoneNames = append ( zoneNames , forZone .Name )
231
236
}
232
237
233
238
for _ , msg := range validation .IsValidLabelValue (forZone .Name ) {
0 commit comments