-
Notifications
You must be signed in to change notification settings - Fork 474
/
virtual_service_data.go
112 lines (101 loc) · 3.22 KB
/
virtual_service_data.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
package data
import (
"time"
"google.golang.org/protobuf/types/known/durationpb"
api_networking_v1beta1 "istio.io/api/networking/v1beta1"
networking_v1beta1 "istio.io/client-go/pkg/apis/networking/v1beta1"
)
func CreateEmptyVirtualService(name string, namespace string, hosts []string) *networking_v1beta1.VirtualService {
vs := networking_v1beta1.VirtualService{}
vs.Name = name
vs.Namespace = namespace
vs.Spec.Hosts = hosts
return &vs
}
// TODO Naming etc
func CreateVirtualService() *networking_v1beta1.VirtualService {
return AddHttpRoutesToVirtualService(CreateHttpRouteDestination("reviews", "v1", -1),
AddTcpRoutesToVirtualService(CreateTcpRoute("reviews", "v1", -1),
CreateEmptyVirtualService("reviews", "test", []string{"reviews"}),
),
)
}
func AddHttpRoutesToVirtualService(route *api_networking_v1beta1.HTTPRouteDestination, vs *networking_v1beta1.VirtualService) *networking_v1beta1.VirtualService {
if len(vs.Spec.Http) == 0 {
vs.Spec.Http = []*api_networking_v1beta1.HTTPRoute{
{
Route: []*api_networking_v1beta1.HTTPRouteDestination{
route,
},
},
}
} else {
vs.Spec.Http[0].Route = append(vs.Spec.Http[0].Route, route)
}
return vs
}
func AddTcpRoutesToVirtualService(route *api_networking_v1beta1.TCPRoute, vs *networking_v1beta1.VirtualService) *networking_v1beta1.VirtualService {
vs.Spec.Tcp = append(vs.Spec.Tcp, route)
return vs
}
func AddTlsRoutesToVirtualService(route *api_networking_v1beta1.TLSRoute, vs *networking_v1beta1.VirtualService) *networking_v1beta1.VirtualService {
vs.Spec.Tls = append(vs.Spec.Tls, route)
return vs
}
func CreateHttpRouteDestination(host string, subset string, weight int32) *api_networking_v1beta1.HTTPRouteDestination {
httpRouteDestination := &api_networking_v1beta1.HTTPRouteDestination{
Destination: &api_networking_v1beta1.Destination{
Host: host,
Subset: subset,
},
Weight: weight,
}
return httpRouteDestination
}
func CreateTcpRoute(host string, subset string, weight int32) *api_networking_v1beta1.TCPRoute {
route := api_networking_v1beta1.TCPRoute{
Route: []*api_networking_v1beta1.RouteDestination{
{
Destination: &api_networking_v1beta1.Destination{
Host: host,
Subset: subset,
},
Weight: weight,
},
},
}
return &route
}
func CreateTlsRoute(host string, subset string, weight int32) *api_networking_v1beta1.TLSRoute {
route := api_networking_v1beta1.TLSRoute{
// TODO Add "Match", currently Route host is needed
Route: []*api_networking_v1beta1.RouteDestination{
{
Destination: &api_networking_v1beta1.Destination{
Host: host,
Subset: subset,
},
Weight: weight,
},
},
}
return &route
}
// Example from https://istio.io/docs/reference/config/istio.networking.v1alpha3/#Destination
func CreateVirtualServiceWithServiceEntryTarget() *networking_v1beta1.VirtualService {
vs := CreateEmptyVirtualService("my-wiki-rule", "wikipedia", []string{"wikipedia.org"})
timeout, _ := time.ParseDuration("5s")
vs.Spec.Http = []*api_networking_v1beta1.HTTPRoute{
{
Timeout: durationpb.New(timeout),
Route: []*api_networking_v1beta1.HTTPRouteDestination{
{
Destination: &api_networking_v1beta1.Destination{
Host: "wikipedia.org",
},
},
},
},
}
return vs
}