Skip to content

Commit 6ca82f9

Browse files
committed
Update the use of sets in EndpointSlice validation
Don't use sets for validating port name and zone hint uniqueness, since constructing a new set each time is likely to be less efficient than just doing a linear search. Keep the sets for supportedAddressTypes and supportedPortProtocols (since they're only constructed once) but switch to the generic set API.
1 parent 73f54b6 commit 6ca82f9

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

pkg/apis/discovery/validation/validation.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package validation
1818

1919
import (
2020
"fmt"
21+
"slices"
2122

2223
corev1 "k8s.io/api/core/v1"
2324
apiequality "k8s.io/apimachinery/pkg/api/equality"
@@ -33,15 +34,15 @@ import (
3334
)
3435

3536
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,
4041
)
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,
4546
)
4647
maxTopologyLabels = 16
4748
maxAddresses = 100
@@ -172,24 +173,26 @@ func validatePorts(endpointPorts []discovery.EndpointPort, fldPath *field.Path)
172173
return allErrs
173174
}
174175

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))
176179
for i, endpointPort := range endpointPorts {
177180
idxPath := fldPath.Index(i)
178181

179182
if len(*endpointPort.Name) > 0 {
180183
allErrs = append(allErrs, apivalidation.ValidateDNS1123Label(*endpointPort.Name, idxPath.Child("name"))...)
181184
}
182185

183-
if portNames.Has(*endpointPort.Name) {
186+
if slices.Contains(portNames, *endpointPort.Name) {
184187
allErrs = append(allErrs, field.Duplicate(idxPath.Child("name"), endpointPort.Name))
185188
} else {
186-
portNames.Insert(*endpointPort.Name)
189+
portNames = append(portNames, *endpointPort.Name)
187190
}
188191

189192
if endpointPort.Protocol == nil {
190193
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)))
193196
}
194197

195198
if endpointPort.AppProtocol != nil {
@@ -205,8 +208,8 @@ func validateAddressType(addressType discovery.AddressType) field.ErrorList {
205208

206209
if addressType == "" {
207210
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)))
210213
}
211214

212215
return allErrs
@@ -221,13 +224,15 @@ func validateHints(endpointHints *discovery.EndpointHints, fldPath *field.Path)
221224
return allErrs
222225
}
223226

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))
225230
for i, forZone := range endpointHints.ForZones {
226231
zonePath := fzPath.Index(i).Child("name")
227-
if zoneNames.Has(forZone.Name) {
232+
if slices.Contains(zoneNames, forZone.Name) {
228233
allErrs = append(allErrs, field.Duplicate(zonePath, forZone.Name))
229234
} else {
230-
zoneNames.Insert(forZone.Name)
235+
zoneNames = append(zoneNames, forZone.Name)
231236
}
232237

233238
for _, msg := range validation.IsValidLabelValue(forZone.Name) {

0 commit comments

Comments
 (0)