Skip to content

Commit c9ee03b

Browse files
Feature: support multi hosts in vs (#5430)
* Update controller_utils.go add GetAnnotations() to support multi hosts * Update istio.go add env ANNOTATION_ISTIO_HOST_SEPARATOR to support multi hosts * Update seldondeployment_controller.go Update controller to support multi hosts * Update seldondeployment_explainers.go Update explianer to support multi hosts * Update istio.go Remove seperator defination * Update controller_utils.go Rename GetAnnotation to HostsFromAnnotation, and remove the redundant logic * Update seldondeployment_explainers.go * Update seldondeployment_controller.go * Update operator/controllers/seldondeployment_controller.go * add test and trimp whitespaces --------- Co-authored-by: Rafal Skolasinski <r.j.skolasinski@gmail.com>
1 parent 39d7103 commit c9ee03b

File tree

4 files changed

+86
-3
lines changed

4 files changed

+86
-3
lines changed

operator/controllers/seldondeployment_controller.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func createIstioResources(mlDep *machinelearningv1.SeldonDeployment,
262262
Namespace: namespace,
263263
},
264264
Spec: istio_networking.VirtualService{
265-
Hosts: []string{utils2.GetAnnotation(mlDep, ANNOTATION_ISTIO_HOST, "*")},
265+
Hosts: utils2.HostsFromAnnotation(mlDep, ANNOTATION_ISTIO_HOST, "*"),
266266
Gateways: []string{utils2.GetAnnotation(mlDep, ANNOTATION_ISTIO_GATEWAY, istio_gateway)},
267267
Http: []*istio_networking.HTTPRoute{
268268
{

operator/controllers/seldondeployment_explainers.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ func createExplainerIstioResources(pSvcName string, p *machinelearningv1.Predict
328328
Namespace: namespace,
329329
},
330330
Spec: istio_networking.VirtualService{
331-
Hosts: []string{utils2.GetAnnotation(mlDep, ANNOTATION_ISTIO_HOST, "*")},
331+
Hosts: utils2.HostsFromAnnotation(mlDep, ANNOTATION_ISTIO_HOST, "*"),
332332
Gateways: []string{utils2.GetAnnotation(mlDep, ANNOTATION_ISTIO_GATEWAY, istio_gateway)},
333333
Http: []*istio_networking.HTTPRoute{
334334
{
@@ -349,7 +349,7 @@ func createExplainerIstioResources(pSvcName string, p *machinelearningv1.Predict
349349
Namespace: namespace,
350350
},
351351
Spec: istio_networking.VirtualService{
352-
Hosts: []string{utils2.GetAnnotation(mlDep, ANNOTATION_ISTIO_HOST, "*")},
352+
Hosts: utils2.HostsFromAnnotation(mlDep, ANNOTATION_ISTIO_HOST, "*"),
353353
Gateways: []string{utils2.GetAnnotation(mlDep, ANNOTATION_ISTIO_GATEWAY, istio_gateway)},
354354
Http: []*istio_networking.HTTPRoute{
355355
{

operator/controllers/utils/controller_utils.go

+14
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ func GetAnnotation(mlDep *machinelearningv1.SeldonDeployment, annotationKey stri
5151
}
5252
}
5353

54+
// Get an annotation array for hosts from the Seldon Deployment given by annotationKey or return the array with fallback.
55+
func HostsFromAnnotation(mlDep *machinelearningv1.SeldonDeployment, annotationKey string, fallback string) []string {
56+
annotation := GetAnnotation(mlDep, annotationKey, fallback)
57+
if strings.Contains(annotation, ",") {
58+
hosts := strings.Split(annotation, ",")
59+
for i, host := range hosts {
60+
hosts[i] = strings.TrimSpace(host)
61+
}
62+
return hosts
63+
} else {
64+
return []string{strings.TrimSpace(annotation)}
65+
}
66+
}
67+
5468
// get annotations that start with seldon.io/engine
5569
func GetEngineEnvAnnotations(mlDep *machinelearningv1.SeldonDeployment) []corev1.EnvVar {
5670

operator/controllers/utils/controller_utils_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package utils
22

33
import (
4+
"testing"
5+
46
. "github.com/onsi/ginkgo"
57
. "github.com/onsi/ginkgo/extensions/table"
68
. "github.com/onsi/gomega"
79
machinelearningv1 "github.com/seldonio/seldon-core/operator/apis/machinelearning.seldon.io/v1"
810
)
911

12+
const HOST_ANNOTATION = "host-annotation"
13+
1014
var _ = Describe("Controller utils", func() {
1115
DescribeTable(
1216
"isEmptyExplainer",
@@ -23,3 +27,68 @@ var _ = Describe("Controller utils", func() {
2327
),
2428
)
2529
})
30+
31+
func TestHostsFromAnnotation(t *testing.T) {
32+
var key = HOST_ANNOTATION
33+
var fallback = "*"
34+
35+
tests := []struct {
36+
name string
37+
mldep *machinelearningv1.SeldonDeployment
38+
want []string
39+
}{
40+
{
41+
name: "No host",
42+
mldep: &machinelearningv1.SeldonDeployment{
43+
Spec: machinelearningv1.SeldonDeploymentSpec{
44+
Annotations: map[string]string{},
45+
},
46+
},
47+
want: []string{fallback},
48+
},
49+
{
50+
name: "Single host",
51+
mldep: &machinelearningv1.SeldonDeployment{
52+
Spec: machinelearningv1.SeldonDeploymentSpec{
53+
Annotations: map[string]string{
54+
key: "prod.svc.cluster.local",
55+
},
56+
},
57+
},
58+
want: []string{"prod.svc.cluster.local"},
59+
},
60+
{
61+
name: "Multiple hosts",
62+
mldep: &machinelearningv1.SeldonDeployment{
63+
Spec: machinelearningv1.SeldonDeploymentSpec{
64+
Annotations: map[string]string{
65+
key: "prod.svc.cluster.local,dev.svc.cluster.local",
66+
},
67+
},
68+
},
69+
want: []string{"prod.svc.cluster.local", "dev.svc.cluster.local"},
70+
},
71+
{
72+
name: "Multiple hosts with space",
73+
mldep: &machinelearningv1.SeldonDeployment{
74+
Spec: machinelearningv1.SeldonDeploymentSpec{
75+
Annotations: map[string]string{
76+
key: "prod.svc.cluster.local, dev.svc.cluster.local",
77+
},
78+
},
79+
},
80+
want: []string{"prod.svc.cluster.local", "dev.svc.cluster.local"},
81+
},
82+
}
83+
84+
for _, tt := range tests {
85+
t.Run(tt.name, func(t *testing.T) {
86+
hosts := HostsFromAnnotation(tt.mldep, key, fallback)
87+
for i, host := range hosts {
88+
if host != tt.want[i] {
89+
t.Errorf("got: %v; want %v", host, tt.want[i])
90+
}
91+
}
92+
})
93+
}
94+
}

0 commit comments

Comments
 (0)