Skip to content

Commit 202b563

Browse files
committed
Check for duplicate endpoints in a single cortex.yaml file
1 parent b4d1a71 commit 202b563

File tree

2 files changed

+51
-15
lines changed

2 files changed

+51
-15
lines changed

pkg/operator/operator/errors.go

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ const (
3434
ErrMalformedConfig
3535
ErrNoAPIs
3636
ErrDuplicateName
37+
ErrDuplicateEndpointInOneDeploy
38+
ErrDuplicateEndpoint
3739
ErrSpecifyAllOrNone
3840
ErrOneOfPrerequisitesNotDefined
3941
ErrMinReplicasGreaterThanMax
@@ -50,7 +52,6 @@ const (
5052
ErrFieldNotSupportedByPredictorType
5153
ErrNoAvailableNodeComputeLimit
5254
ErrAPINotDeployed
53-
ErrDuplicateEndpoint
5455
)
5556

5657
var _errorKinds = []string{
@@ -60,6 +61,8 @@ var _errorKinds = []string{
6061
"err_malformed_config",
6162
"err_no_apis",
6263
"err_duplicate_name",
64+
"err_duplicate_endpoint_in_one_deploy",
65+
"err_duplicate_endpoint",
6366
"err_specify_all_or_none",
6467
"err_one_of_prerequisites_not_defined",
6568
"err_min_replicas_greater_than_max",
@@ -76,10 +79,9 @@ var _errorKinds = []string{
7679
"err_field_not_supported_by_predictor_type",
7780
"err_no_available_node_compute_limit",
7881
"err_api_not_deployed",
79-
"err_duplicate_endpoint",
8082
}
8183

82-
var _ = [1]int{}[int(ErrDuplicateEndpoint)-(len(_errorKinds)-1)] // Ensure list length matches
84+
var _ = [1]int{}[int(ErrAPINotDeployed)-(len(_errorKinds)-1)] // Ensure list length matches
8385

8486
func (t ErrorKind) String() string {
8587
return _errorKinds[t]
@@ -164,6 +166,25 @@ func ErrorDuplicateName(apis []userconfig.API) error {
164166
})
165167
}
166168

169+
func ErrorDuplicateEndpointInOneDeploy(apis []userconfig.API) error {
170+
names := make([]string, len(apis))
171+
for i, api := range apis {
172+
names[i] = api.Name
173+
}
174+
175+
return errors.WithStack(Error{
176+
Kind: ErrDuplicateEndpointInOneDeploy,
177+
message: fmt.Sprintf("endpoint %s must be unique across apis (defined in %s)", s.UserStr(*apis[0].Endpoint), s.StrsAnd(names)),
178+
})
179+
}
180+
181+
func ErrorDuplicateEndpoint(apiName string) error {
182+
return errors.WithStack(Error{
183+
Kind: ErrDuplicateEndpoint,
184+
message: fmt.Sprintf("endpoint is already being used by the %s api", apiName),
185+
})
186+
}
187+
167188
func ErrorSpecifyAllOrNone(val string, vals ...string) error {
168189
allVals := append(vals, val)
169190
message := fmt.Sprintf("please specify all or none of %s", s.UserStrsAnd(allVals))
@@ -299,10 +320,3 @@ func ErrorAPINotDeployed(apiName string) error {
299320
message: fmt.Sprintf("%s api is not deployed", apiName), // note: if modifying this string, search the codebase for it and change all occurrences
300321
})
301322
}
302-
303-
func ErrorDuplicateEndpoint(apiName string) error {
304-
return errors.WithStack(Error{
305-
Kind: ErrDuplicateEndpoint,
306-
message: fmt.Sprintf("endpoint is already being used by the %s api", apiName),
307-
})
308-
}

pkg/operator/operator/validations.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,6 @@ func validateAPIs(apis []userconfig.API, projectFileMap map[string][]byte) error
278278
return ErrorNoAPIs()
279279
}
280280

281-
dups := findDuplicateNames(apis)
282-
if len(dups) > 0 {
283-
return ErrorDuplicateName(dups)
284-
}
285-
286281
virtualServices, maxMem, err := getValidationK8sResources()
287282
if err != nil {
288283
return err
@@ -293,6 +288,17 @@ func validateAPIs(apis []userconfig.API, projectFileMap map[string][]byte) error
293288
return err
294289
}
295290
}
291+
292+
dups := findDuplicateNames(apis)
293+
if len(dups) > 0 {
294+
return ErrorDuplicateName(dups)
295+
}
296+
297+
dups = findDuplicateEndpoints(apis)
298+
if len(dups) > 0 {
299+
return ErrorDuplicateEndpointInOneDeploy(dups)
300+
}
301+
296302
return nil
297303
}
298304

@@ -585,6 +591,22 @@ func findDuplicateNames(apis []userconfig.API) []userconfig.API {
585591
return nil
586592
}
587593

594+
func findDuplicateEndpoints(apis []userconfig.API) []userconfig.API {
595+
endpoints := make(map[string][]userconfig.API)
596+
597+
for _, api := range apis {
598+
endpoints[*api.Endpoint] = append(endpoints[*api.Endpoint], api)
599+
}
600+
601+
for endpoint := range endpoints {
602+
if len(endpoints[endpoint]) > 1 {
603+
return endpoints[endpoint]
604+
}
605+
}
606+
607+
return nil
608+
}
609+
588610
func getValidationK8sResources() ([]kunstructured.Unstructured, *kresource.Quantity, error) {
589611
var virtualServices []kunstructured.Unstructured
590612
var maxMem *kresource.Quantity

0 commit comments

Comments
 (0)