forked from kgateway-dev/kgateway
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvirtual_services.go
114 lines (97 loc) · 2.84 KB
/
virtual_services.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
package helpers
import (
"github.com/golang/protobuf/proto"
v1 "github.com/solo-io/gloo/projects/gateway/pkg/api/v1"
gloov1 "github.com/solo-io/gloo/projects/gloo/pkg/api/v1"
"github.com/solo-io/gloo/projects/gloo/pkg/api/v1/core/matchers"
"github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
)
// virtualServiceBuilder simplifies the process of generating VirtualServices in tests
type virtualServiceBuilder struct {
name string
namespace string
domains []string
routesByName map[string]*v1.Route
sslConfig *gloov1.SslConfig
}
func NewVirtualServiceBuilder() *virtualServiceBuilder {
return &virtualServiceBuilder{
routesByName: make(map[string]*v1.Route, 10),
}
}
func (b *virtualServiceBuilder) WithSslConfig(sslConfig *gloov1.SslConfig) *virtualServiceBuilder {
b.sslConfig = sslConfig
return b
}
func (b *virtualServiceBuilder) WithName(name string) *virtualServiceBuilder {
b.name = name
return b
}
func (b *virtualServiceBuilder) WithNamespace(namespace string) *virtualServiceBuilder {
b.namespace = namespace
return b
}
func (b *virtualServiceBuilder) WithDomain(domain string) *virtualServiceBuilder {
b.domains = []string{domain}
return b
}
func (b *virtualServiceBuilder) WithDomains(domains []string) *virtualServiceBuilder {
b.domains = domains
return b
}
func (b *virtualServiceBuilder) WithRoute(routeName string, route *v1.Route) *virtualServiceBuilder {
b.routesByName[routeName] = route
return b
}
func (b *virtualServiceBuilder) getOrDefaultRoute(routeName string) *v1.Route {
route, ok := b.routesByName[routeName]
if !ok {
return &v1.Route{
Name: routeName,
}
}
return route
}
func (b *virtualServiceBuilder) WithRouteActionToUpstream(routeName string, upstream *gloov1.Upstream) *virtualServiceBuilder {
route := b.getOrDefaultRoute(routeName)
route.Action = &v1.Route_RouteAction{
RouteAction: &gloov1.RouteAction{
Destination: &gloov1.RouteAction_Single{
Single: &gloov1.Destination{
DestinationType: &gloov1.Destination_Upstream{
Upstream: upstream.GetMetadata().Ref(),
},
},
},
},
}
return b.WithRoute(routeName, route)
}
func (b *virtualServiceBuilder) WithPrefixMatcher(routeName string, prefixMatch string) *virtualServiceBuilder {
route := b.getOrDefaultRoute(routeName)
route.Matchers = []*matchers.Matcher{{
PathSpecifier: &matchers.Matcher_Prefix{
Prefix: prefixMatch,
},
}}
return b.WithRoute(routeName, route)
}
func (b *virtualServiceBuilder) Build() *v1.VirtualService {
var routes []*v1.Route
for _, route := range b.routesByName {
routes = append(routes, route)
}
vs := &v1.VirtualService{
Metadata: &core.Metadata{
Name: b.name,
Namespace: b.namespace,
},
VirtualHost: &v1.VirtualHost{
Domains: b.domains,
Routes: routes,
Options: nil,
},
SslConfig: b.sslConfig,
}
return proto.Clone(vs).(*v1.VirtualService)
}