forked from kserve/kserve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinferenceservice_validation.go
163 lines (138 loc) · 5.3 KB
/
inferenceservice_validation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
Copyright 2019 kubeflow.org.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1alpha2
import (
"fmt"
"k8s.io/apimachinery/pkg/runtime"
"regexp"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
// Known error messages
const (
MinReplicasShouldBeLessThanMaxError = "MinReplicas cannot be greater than MaxReplicas."
MinReplicasLowerBoundExceededError = "MinReplicas cannot be less than 0."
MaxReplicasLowerBoundExceededError = "MaxReplicas cannot be less than 0."
ParallelismLowerBoundExceededError = "Parallelism cannot be less than 0."
TrafficBoundsExceededError = "TrafficPercent must be between [0, 100]."
TrafficProvidedWithoutCanaryError = "Canary must be specified when CanaryTrafficPercent > 0."
UnsupportedStorageURIFormatError = "storageUri, must be one of: [%s] or match https://{}.blob.core.windows.net/{}/{} or be an absolute or relative local path. StorageUri [%s] is not supported."
InvalidISVCNameFormatError = "The InferenceService \"%s\" is invalid: a InferenceService name must consist of lower case alphanumeric characters or '-', and must start with alphabetical character. (e.g. \"my-name\" or \"abc-123\", regex used for validation is '%s')"
)
// Validation for isvc name
const (
IsvcNameFmt string = "[a-z]([-a-z0-9]*[a-z0-9])?"
)
var (
SupportedStorageURIPrefixList = []string{"gs://", "s3://", "pvc://", "file://", "https://", "http://"}
AzureBlobURL = "blob.core.windows.net"
AzureBlobURIRegEx = "https://(.+?).blob.core.windows.net/(.+)"
IsvcRegexp = regexp.MustCompile("^" + IsvcNameFmt + "$")
)
var _ webhook.Validator = &InferenceService{}
// ValidateCreate implements https://godoc.org/sigs.k8s.io/controller-runtime/pkg/webhook/admission#Validator
func (isvc *InferenceService) ValidateCreate() error {
client, err := client.New(config.GetConfigOrDie(), client.Options{})
if err != nil {
panic("Failed to create client in validator")
}
return isvc.validate(client)
}
// ValidateUpdate implements https://godoc.org/sigs.k8s.io/controller-runtime/pkg/webhook/admission#Validator
func (isvc *InferenceService) ValidateUpdate(old runtime.Object) error {
client, err := client.New(config.GetConfigOrDie(), client.Options{})
if err != nil {
panic("Failed to create client in validator")
}
return isvc.validate(client)
}
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (isvc *InferenceService) ValidateDelete() error {
return nil
}
func (isvc *InferenceService) validate(client client.Client) error {
logger.Info("Validating InferenceService", "namespace", isvc.Namespace, "name", isvc.Name)
if err := validateInferenceService(isvc, client); err != nil {
logger.Info("Failed to validate InferenceService", "namespace", isvc.Namespace, "name", isvc.Name,
"error", err.Error())
return err
}
logger.Info("Successfully validated InferenceService", "namespace", isvc.Namespace, "name", isvc.Name)
return nil
}
func validateInferenceService(isvc *InferenceService, client client.Client) error {
if isvc == nil {
return fmt.Errorf("Unable to validate, InferenceService is nil")
}
if err := validateInferenceServiceName(isvc); err != nil {
return err
}
endpoints := []*EndpointSpec{
&isvc.Spec.Default,
isvc.Spec.Canary,
}
for _, endpoint := range endpoints {
if err := validateEndpoint(endpoint, client); err != nil {
return err
}
}
if err := validateCanaryTrafficPercent(isvc.Spec); err != nil {
return err
}
return nil
}
func validateEndpoint(endpoint *EndpointSpec, client client.Client) error {
if endpoint == nil {
return nil
}
configMap, err := GetInferenceServicesConfig(client)
if err != nil {
return err
}
// validate predictor
if err := endpoint.Predictor.Validate(configMap); err != nil {
return err
}
// validate transformer
if endpoint.Transformer != nil {
if err := endpoint.Transformer.Validate(configMap); err != nil {
return err
}
}
// validate explainer
if endpoint.Explainer != nil {
if err := endpoint.Explainer.Validate(configMap); err != nil {
return err
}
}
return nil
}
func validateCanaryTrafficPercent(spec InferenceServiceSpec) error {
if spec.CanaryTrafficPercent == nil {
return nil
}
if spec.Canary == nil && *spec.CanaryTrafficPercent != 0 {
return fmt.Errorf(TrafficProvidedWithoutCanaryError)
}
if *spec.CanaryTrafficPercent < 0 || *spec.CanaryTrafficPercent > 100 {
return fmt.Errorf(TrafficBoundsExceededError)
}
return fmt.Errorf("Canary rollout is no longer supported on v1alpha2, please convert to v1beta1 to use the feature.")
}
func validateInferenceServiceName(isvc *InferenceService) error {
if !IsvcRegexp.MatchString(isvc.Name) {
return fmt.Errorf(InvalidISVCNameFormatError, isvc.Name, IsvcNameFmt)
}
return nil
}