-
Notifications
You must be signed in to change notification settings - Fork 835
/
seldondeployment_webhook.go
480 lines (392 loc) · 15.6 KB
/
seldondeployment_webhook.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
Copyright 2019 The Seldon Authors.
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 v1
import (
"github.com/seldonio/seldon-core/operator/constants"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
"os"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"strconv"
)
var (
// log is for logging in this package.
seldondeploymentlog = logf.Log.WithName("seldondeployment")
ControllerNamespace = GetEnv("POD_NAMESPACE", "seldon-system")
C client.Client
envPredictiveUnitServicePort = os.Getenv(ENV_PREDICTIVE_UNIT_SERVICE_PORT)
envPredictiveUnitServicePortMetrics = os.Getenv(ENV_PREDICTIVE_UNIT_SERVICE_PORT_METRICS)
envPredictiveUnitMetricsPortName = GetEnv(ENV_PREDICTIVE_UNIT_METRICS_PORT_NAME, constants.DefaultMetricsPortName)
)
// Get an environment variable given by key or return the fallback.
func GetEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
func (r *SeldonDeployment) SetupWebhookWithManager(mgr ctrl.Manager) error {
C = mgr.GetClient()
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
var _ webhook.Defaulter = &SeldonDeployment{}
func GetContainerForPredictiveUnit(p *PredictorSpec, name string) *corev1.Container {
for j := 0; j < len(p.ComponentSpecs); j++ {
cSpec := p.ComponentSpecs[j]
for k := 0; k < len(cSpec.Spec.Containers); k++ {
c := &cSpec.Spec.Containers[k]
if c.Name == name {
return c
}
}
}
return nil
}
func GetPort(name string, ports []corev1.ContainerPort) *corev1.ContainerPort {
for i := 0; i < len(ports); i++ {
if ports[i].Name == name {
return &ports[i]
}
}
return nil
}
// -----
func addDefaultsToGraph(pu *PredictiveUnit) {
if pu.Type == nil && pu.Methods == nil && pu.Implementation == nil {
ty := MODEL
pu.Type = &ty
}
if pu.Implementation == nil {
im := UNKNOWN_IMPLEMENTATION
pu.Implementation = &im
} else if IsPrepack(pu) {
ty := MODEL
pu.Type = &ty
}
for i := 0; i < len(pu.Children); i++ {
addDefaultsToGraph(&pu.Children[i])
}
}
func getUpdatePortNumMap(name string, nextPortNum *int32, portMap map[string]int32) int32 {
if _, present := portMap[name]; !present {
portMap[name] = *nextPortNum
*nextPortNum++
}
return portMap[name]
}
func addMetricsPortAndIncrement(nextMetricsPortNum *int32, con *corev1.Container) {
existingMetricPort := GetPort(envPredictiveUnitMetricsPortName, con.Ports)
if existingMetricPort == nil {
con.Ports = append(con.Ports, corev1.ContainerPort{
Name: envPredictiveUnitMetricsPortName,
ContainerPort: *nextMetricsPortNum,
Protocol: corev1.ProtocolTCP,
})
*nextMetricsPortNum++
}
}
func (r *SeldonDeploymentSpec) setContainerPredictiveUnitDefaults(compSpecIdx int,
portNum int32, nextMetricsPortNum *int32, mldepName string, namespace string,
p *PredictorSpec, pu *PredictiveUnit, con *corev1.Container) {
if pu.Endpoint == nil {
if r.Transport == TransportGrpc {
pu.Endpoint = &Endpoint{Type: GRPC}
} else {
pu.Endpoint = &Endpoint{Type: REST}
}
}
var portType string
if pu.Endpoint.Type == GRPC {
portType = constants.GrpcPortName
} else {
portType = constants.HttpPortName
}
existingPort := GetPort(portType, con.Ports)
if existingPort != nil {
portNum = existingPort.ContainerPort
}
volFound := false
for _, vol := range con.VolumeMounts {
if vol.Name == PODINFO_VOLUME_NAME {
volFound = true
}
}
if !volFound {
con.VolumeMounts = append(con.VolumeMounts, corev1.VolumeMount{
Name: PODINFO_VOLUME_NAME,
MountPath: PODINFO_VOLUME_PATH,
})
}
//Add metrics port if missing
addMetricsPortAndIncrement(nextMetricsPortNum, con)
// Set ports and hostname in predictive unit so engine can read it from SDep
// if this is the first componentSpec then it's the one to put the engine in - note using outer loop counter here
if _, hasSeparateEnginePod := r.Annotations[ANNOTATION_SEPARATE_ENGINE]; compSpecIdx == 0 && !hasSeparateEnginePod {
pu.Endpoint.ServiceHost = constants.DNSLocalHost
} else {
containerServiceValue := GetContainerServiceName(mldepName, *p, con)
pu.Endpoint.ServiceHost = containerServiceValue + "." + namespace + constants.DNSClusterLocalSuffix
}
pu.Endpoint.ServicePort = portNum
}
func (r *SeldonDeploymentSpec) DefaultSeldonDeployment(mldepName string, namespace string) {
var firstPuPortNum int32 = constants.FirstPortNumber
if envPredictiveUnitServicePort != "" {
portNum, err := strconv.Atoi(envPredictiveUnitServicePort)
if err != nil {
seldondeploymentlog.Error(err, "Failed to decode predictive unit service port will use default", "envar", ENV_PREDICTIVE_UNIT_SERVICE_PORT, "value", envPredictiveUnitServicePort)
} else {
firstPuPortNum = int32(portNum)
}
}
nextPortNum := firstPuPortNum
var firstMetricsPuPortNum int32 = constants.FirstMetricsPortNumber
if envPredictiveUnitServicePortMetrics != "" {
portNum, err := strconv.Atoi(envPredictiveUnitServicePortMetrics)
if err != nil {
seldondeploymentlog.Error(err, "Failed to decode PREDICTIVE_UNIT_SERVICE_PORT_METRICS will use default", "value", envPredictiveUnitServicePortMetrics)
} else {
firstMetricsPuPortNum = int32(portNum)
}
}
nextMetricsPortNum := firstMetricsPuPortNum
portMap := map[string]int32{}
for i := 0; i < len(r.Predictors); i++ {
p := r.Predictors[i]
// Add version label for predictor if not present
if p.Labels == nil {
p.Labels = map[string]string{}
}
if _, present := p.Labels["version"]; !present {
p.Labels["version"] = p.Name
}
addDefaultsToGraph(&p.Graph)
for j := 0; j < len(p.ComponentSpecs); j++ {
cSpec := r.Predictors[i].ComponentSpecs[j]
// add service details for each container - looping this way as if containers in same pod and its the engine pod both need to be localhost
for k := 0; k < len(cSpec.Spec.Containers); k++ {
con := &cSpec.Spec.Containers[k]
getUpdatePortNumMap(con.Name, &nextPortNum, portMap)
portNum := portMap[con.Name]
pu := GetPredictiveUnit(&p.Graph, con.Name)
if pu != nil {
r.setContainerPredictiveUnitDefaults(j, portNum, &nextMetricsPortNum, mldepName, namespace, &p, pu, con)
}
}
}
pus := GetPredictiveUnitList(&p.Graph)
//some pus might not have a container spec so pick those up
for l := 0; l < len(pus); l++ {
pu := pus[l]
if IsPrepack(pu) {
con := GetContainerForPredictiveUnit(&p, pu.Name)
existing := con != nil
if !existing {
con = &corev1.Container{
Name: pu.Name,
VolumeMounts: []corev1.VolumeMount{
{
Name: PODINFO_VOLUME_NAME,
MountPath: PODINFO_VOLUME_PATH,
},
},
}
}
getUpdatePortNumMap(pu.Name, &nextPortNum, portMap)
portNum := portMap[pu.Name]
r.setContainerPredictiveUnitDefaults(0, portNum, &nextMetricsPortNum, mldepName, namespace, &p, pu, con)
//Only set image default for non tensorflow graphs
if r.Protocol != ProtocolTensorflow {
serverConfig := GetPrepackServerConfig(string(*pu.Implementation))
if serverConfig != nil {
SetImageNameForPrepackContainer(pu, con, serverConfig)
}
}
// if new Add container to componentSpecs
if !existing {
if len(p.ComponentSpecs) > 0 {
p.ComponentSpecs[0].Spec.Containers = append(p.ComponentSpecs[0].Spec.Containers, *con)
} else {
podSpec := SeldonPodSpec{
Metadata: metav1.ObjectMeta{CreationTimestamp: metav1.Now()},
Spec: corev1.PodSpec{
Containers: []corev1.Container{*con},
},
}
p.ComponentSpecs = []*SeldonPodSpec{&podSpec}
// p is a copy so update the entry
r.Predictors[i] = p
}
}
}
}
r.Predictors[i] = p
}
}
// --- Validating
// Check the predictive units to ensure the graph matches up with defined containers.
func (r *SeldonDeploymentSpec) checkPredictiveUnits(pu *PredictiveUnit, p *PredictorSpec, fldPath *field.Path, allErrs field.ErrorList) field.ErrorList {
if *pu.Implementation == UNKNOWN_IMPLEMENTATION {
if GetContainerForPredictiveUnit(p, pu.Name) == nil {
allErrs = append(allErrs, field.Invalid(fldPath, pu.Name, "Can't find container for Predictive Unit"))
}
if *pu.Type == UNKNOWN_TYPE && (pu.Methods == nil || len(*pu.Methods) == 0) {
allErrs = append(allErrs, field.Invalid(fldPath, pu.Name, "Predictive Unit has no implementation methods defined. Change to a known type or add what methods it defines"))
}
} else if IsPrepack(pu) {
if pu.ModelURI == "" {
allErrs = append(allErrs, field.Invalid(fldPath, pu.Name, "Predictive unit modelUri required when using standalone servers"))
}
c := GetContainerForPredictiveUnit(p, pu.Name)
//Current non tensorflow serving prepack servers can not handle tensorflow protocol
if r.Protocol == ProtocolTensorflow && (*pu.Implementation == PrepackSklearnName || *pu.Implementation == PrepackXgboostName || *pu.Implementation == PrepackMlflowName) {
allErrs = append(allErrs, field.Invalid(fldPath, pu.Name, "Prepackaged server does not handle tendorflow protocol "+string(*pu.Implementation)))
}
if c == nil || c.Image == "" {
ServersConfigs, err := getPredictorServerConfigs()
if err != nil {
seldondeploymentlog.Error(err, "Failed to read prepacked model servers from configmap")
}
_, ok := ServersConfigs[string(*pu.Implementation)]
if !ok {
allErrs = append(allErrs, field.Invalid(fldPath, pu.Name, "No entry in predictors map for "+string(*pu.Implementation)))
}
}
}
if pu.Logger != nil {
if pu.Logger.Mode == "" {
allErrs = append(allErrs, field.Invalid(fldPath, pu.Logger.Mode, "No logger mode specified"))
}
}
for i := 0; i < len(pu.Children); i++ {
allErrs = r.checkPredictiveUnits(&pu.Children[i], p, fldPath.Index(i), allErrs)
}
return allErrs
}
func checkTraffic(spec *SeldonDeploymentSpec, fldPath *field.Path, allErrs field.ErrorList) field.ErrorList {
var trafficSum int32 = 0
var shadows int = 0
for i := 0; i < len(spec.Predictors); i++ {
p := spec.Predictors[i]
trafficSum = trafficSum + p.Traffic
if p.Shadow == true {
shadows += 1
}
}
if trafficSum != 100 && (len(spec.Predictors)-shadows) > 1 {
allErrs = append(allErrs, field.Invalid(fldPath, spec.Predictors[0].Name, "Traffic must sum to 100 for multiple predictors"))
}
if trafficSum > 0 && trafficSum < 100 && len(spec.Predictors) == 1 {
allErrs = append(allErrs, field.Invalid(fldPath, spec.Predictors[0].Name, "Traffic must sum be 100 for a single predictor when set"))
}
return allErrs
}
func sizeOfGraph(p *PredictiveUnit) int {
count := 0
for _, child := range p.Children {
count = count + sizeOfGraph(&child)
}
return count + 1
}
func collectTransports(pu *PredictiveUnit, transportsFound map[EndpointType]bool) {
if pu.Endpoint != nil && pu.Endpoint.Type != "" {
transportsFound[pu.Endpoint.Type] = true
}
for _, c := range pu.Children {
collectTransports(&c, transportsFound)
}
}
func (r *SeldonDeploymentSpec) ValidateSeldonDeployment() error {
var allErrs field.ErrorList
if r.Protocol != "" && !(r.Protocol == ProtocolSeldon || r.Protocol == ProtocolTensorflow) {
fldPath := field.NewPath("spec")
allErrs = append(allErrs, field.Invalid(fldPath, r.Protocol, "Invalid protocol"))
}
if r.Transport != "" && !(r.Transport == TransportRest || r.Transport == TransportGrpc) {
fldPath := field.NewPath("spec")
allErrs = append(allErrs, field.Invalid(fldPath, r.Transport, "Invalid transport"))
}
transports := make(map[EndpointType]bool)
predictorNames := make(map[string]bool)
for i, p := range r.Predictors {
collectTransports(&p.Graph, transports)
_, noEngine := p.Annotations[ANNOTATION_NO_ENGINE]
if noEngine && sizeOfGraph(&p.Graph) > 1 {
fldPath := field.NewPath("spec").Child("predictors").Index(i)
allErrs = append(allErrs, field.Invalid(fldPath, p.Name, "Running without engine only valid for single element graphs"))
}
if _, present := predictorNames[p.Name]; present {
fldPath := field.NewPath("spec").Child("predictors").Index(i)
allErrs = append(allErrs, field.Invalid(fldPath, p.Name, "Duplicate predictor name"))
}
predictorNames[p.Name] = true
allErrs = r.checkPredictiveUnits(&p.Graph, &p, field.NewPath("spec").Child("predictors").Index(i).Child("graph"), allErrs)
}
if len(transports) > 1 {
fldPath := field.NewPath("spec")
allErrs = append(allErrs, field.Invalid(fldPath, "", "Multiple endpoint.types found - can only have 1 type in graph. Please use spec.transport"))
} else if len(transports) == 1 && r.Transport != "" {
for k := range transports {
if (k == REST && r.Transport != TransportRest) || (k == GRPC && r.Transport != TransportGrpc) {
fldPath := field.NewPath("spec")
allErrs = append(allErrs, field.Invalid(fldPath, "", "Mixed transport types found. Remove graph endpoint.types if transport set at deployment level"))
}
}
}
allErrs = checkTraffic(r, field.NewPath("spec"), allErrs)
if len(allErrs) == 0 {
return nil
}
return apierrors.NewInvalid(
schema.GroupKind{Group: "machinelearing.seldon.io", Kind: "SeldonDeployment"},
r.Name, allErrs)
}
/// ---
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// +kubebuilder:webhook:path=/mutate-machinelearning-seldon-io-v1-seldondeployment,mutating=true,failurePolicy=fail,groups=machinelearning.seldon.io,resources=seldondeployments,verbs=create;update,versions=v1,name=v1.mseldondeployment.kb.io
// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *SeldonDeployment) Default() {
seldondeploymentlog.Info("Defaulting v1 web hook called", "name", r.Name)
if r.ObjectMeta.Namespace == "" {
r.ObjectMeta.Namespace = "default"
}
r.Spec.DefaultSeldonDeployment(r.Name, r.ObjectMeta.Namespace)
}
// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
// +kubebuilder:webhook:verbs=create;update,path=/validate-machinelearning-seldon-io-v1-seldondeployment,mutating=false,failurePolicy=fail,groups=machinelearning.seldon.io,resources=seldondeployments,versions=v1,name=v1.vseldondeployment.kb.io
var _ webhook.Validator = &SeldonDeployment{}
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *SeldonDeployment) ValidateCreate() error {
seldondeploymentlog.Info("Validating v1 Webhook called for CREATE", "name", r.Name)
return r.Spec.ValidateSeldonDeployment()
}
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *SeldonDeployment) ValidateUpdate(old runtime.Object) error {
seldondeploymentlog.Info("Validating v1 webhook called for UPDATE", "name", r.Name)
return r.Spec.ValidateSeldonDeployment()
}
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *SeldonDeployment) ValidateDelete() error {
seldondeploymentlog.Info("Validating v1 webhook called for DELETE", "name", r.Name)
// TODO(user): fill in your validation logic upon object deletion.
return nil
}