Adding Hints to EndpointSlice API

This commit is contained in:
Rob Scott
2021-03-05 12:05:05 -08:00
parent fe43948d44
commit 11f0944dbc
34 changed files with 1847 additions and 126 deletions

View File

@@ -45,6 +45,7 @@ var (
maxAddresses = 100
maxPorts = 20000
maxEndpoints = 1000
maxZoneHints = 8
)
// ValidateEndpointSliceName can be used to check whether the given endpoint
@@ -125,6 +126,10 @@ func validateEndpoints(endpoints []discovery.Endpoint, addrType discovery.Addres
if endpoint.Hostname != nil {
allErrs = append(allErrs, apivalidation.ValidateDNS1123Label(*endpoint.Hostname, idxPath.Child("hostname"))...)
}
if endpoint.Hints != nil {
allErrs = append(allErrs, validateHints(endpoint.Hints, idxPath.Child("hints"))...)
}
}
return allErrs
@@ -179,3 +184,29 @@ func validateAddressType(addressType discovery.AddressType) field.ErrorList {
return allErrs
}
func validateHints(endpointHints *discovery.EndpointHints, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
fzPath := fldPath.Child("forZones")
if len(endpointHints.ForZones) > maxZoneHints {
allErrs = append(allErrs, field.TooMany(fzPath, len(endpointHints.ForZones), maxZoneHints))
return allErrs
}
zoneNames := sets.String{}
for i, forZone := range endpointHints.ForZones {
zonePath := fzPath.Index(i).Child("name")
if zoneNames.Has(forZone.Name) {
allErrs = append(allErrs, field.Duplicate(zonePath, forZone.Name))
} else {
zoneNames.Insert(forZone.Name)
}
for _, msg := range validation.IsValidLabelValue(forZone.Name) {
allErrs = append(allErrs, field.Invalid(zonePath, forZone.Name, msg))
}
}
return allErrs
}

View File

@@ -203,6 +203,23 @@ func TestValidateEndpointSlice(t *testing.T) {
}},
},
},
"valid-hints": {
expectedErrors: 0,
endpointSlice: &discovery.EndpointSlice{
ObjectMeta: standardMeta,
AddressType: discovery.AddressTypeIPv4,
Ports: []discovery.EndpointPort{{
Name: utilpointer.StringPtr("http"),
Protocol: protocolPtr(api.ProtocolTCP),
}},
Endpoints: []discovery.Endpoint{{
Addresses: generateIPAddresses(1),
Hints: &discovery.EndpointHints{
ForZones: []discovery.ForZone{{Name: "zone-a"}},
},
}},
},
},
// expected failures
"duplicate-port-name": {
@@ -451,6 +468,71 @@ func TestValidateEndpointSlice(t *testing.T) {
}},
},
},
"invalid-hints": {
expectedErrors: 1,
endpointSlice: &discovery.EndpointSlice{
ObjectMeta: standardMeta,
AddressType: discovery.AddressTypeIPv4,
Ports: []discovery.EndpointPort{{
Name: utilpointer.StringPtr("http"),
Protocol: protocolPtr(api.ProtocolTCP),
}},
Endpoints: []discovery.Endpoint{{
Addresses: generateIPAddresses(1),
Hints: &discovery.EndpointHints{
ForZones: []discovery.ForZone{{Name: "inv@lid"}},
},
}},
},
},
"overlapping-hints": {
expectedErrors: 1,
endpointSlice: &discovery.EndpointSlice{
ObjectMeta: standardMeta,
AddressType: discovery.AddressTypeIPv4,
Ports: []discovery.EndpointPort{{
Name: utilpointer.StringPtr("http"),
Protocol: protocolPtr(api.ProtocolTCP),
}},
Endpoints: []discovery.Endpoint{{
Addresses: generateIPAddresses(1),
Hints: &discovery.EndpointHints{
ForZones: []discovery.ForZone{
{Name: "zone-a"},
{Name: "zone-b"},
{Name: "zone-a"},
},
},
}},
},
},
"too-many-hints": {
expectedErrors: 1,
endpointSlice: &discovery.EndpointSlice{
ObjectMeta: standardMeta,
AddressType: discovery.AddressTypeIPv4,
Ports: []discovery.EndpointPort{{
Name: utilpointer.StringPtr("http"),
Protocol: protocolPtr(api.ProtocolTCP),
}},
Endpoints: []discovery.Endpoint{{
Addresses: generateIPAddresses(1),
Hints: &discovery.EndpointHints{
ForZones: []discovery.ForZone{
{Name: "zone-a"},
{Name: "zone-b"},
{Name: "zone-c"},
{Name: "zone-d"},
{Name: "zone-e"},
{Name: "zone-f"},
{Name: "zone-g"},
{Name: "zone-h"},
{Name: "zone-i"},
},
},
}},
},
},
"empty-everything": {
expectedErrors: 3,
endpointSlice: &discovery.EndpointSlice{},