forked from SeldonIO/seldon-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller_utils.go
80 lines (69 loc) · 2.4 KB
/
controller_utils.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
package controllers
import (
"encoding/base64"
"encoding/json"
"os"
"sort"
"strings"
machinelearningv1 "github.com/seldonio/seldon-core/operator/apis/machinelearning/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// Get the Namespace from the SeldonDeployment. Returns "default" if none found.
func getNamespace(deployment *machinelearningv1.SeldonDeployment) string {
if len(deployment.ObjectMeta.Namespace) > 0 {
return deployment.ObjectMeta.Namespace
} else {
return "default"
}
}
// Translte the PredictorSpec p in to base64 encoded JSON to feed to engine in env var.
func getEngineVarJson(p *machinelearningv1.PredictorSpec) (string, error) {
pcopy := p.DeepCopy()
// engine doesn't need to know about metadata or explainer
// leaving these out means they're not part of diffs on main predictor deployments
for _, compSpec := range pcopy.ComponentSpecs {
compSpec.Metadata.CreationTimestamp = metav1.Time{}
}
pcopy.Explainer = nil
str, err := json.Marshal(pcopy)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(str), nil
}
// 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
}
// Get an annotation from the Seldon Deployment given by annotationKey or return the fallback.
func getAnnotation(mlDep *machinelearningv1.SeldonDeployment, annotationKey string, fallback string) string {
if annotation, hasAnnotation := mlDep.Spec.Annotations[annotationKey]; hasAnnotation {
return annotation
} else {
return fallback
}
}
//get annotations that start with seldon.io/engine
func getEngineEnvAnnotations(mlDep *machinelearningv1.SeldonDeployment) []corev1.EnvVar {
envVars := make([]corev1.EnvVar, 0)
var keys []string
for k, _ := range mlDep.Spec.Annotations {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
//prefix indicates engine annotation but "seldon.io/engine-separate-pod" isn't an env one
if strings.HasPrefix(k, "seldon.io/engine-") && k != machinelearningv1.ANNOTATION_SEPARATE_ENGINE {
name := strings.TrimPrefix(k, "seldon.io/engine-")
var replacer = strings.NewReplacer("-", "_")
name = replacer.Replace(name)
name = strings.ToUpper(name)
envVars = append(envVars, corev1.EnvVar{Name: name, Value: mlDep.Spec.Annotations[k]})
}
}
return envVars
}