forked from kgateway-dev/kgateway
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathroute_tables.go
81 lines (69 loc) · 1.99 KB
/
route_tables.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
package helpers
import (
"github.com/golang/protobuf/proto"
v1 "github.com/solo-io/gloo/projects/gateway/pkg/api/v1"
"github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
)
// RouteTableBuilder simplifies the process of generating RouteTables in tests
type RouteTableBuilder struct {
name string
namespace string
routesByName map[string]*v1.Route
}
// BuilderFromRouteTable creates a new RouteTableBuilder from an existing RouteTable
func BuilderFromRouteTable(rt *v1.RouteTable) *RouteTableBuilder {
builder := &RouteTableBuilder{
name: rt.GetMetadata().GetName(),
namespace: rt.GetMetadata().GetNamespace(),
routesByName: make(map[string]*v1.Route, len(rt.GetRoutes())),
}
for _, r := range rt.GetRoutes() {
builder.WithRoute(r.GetName(), r)
}
return builder
}
// NewRouteTableBuilder creates an empty RouteTableBuilder
func NewRouteTableBuilder() *RouteTableBuilder {
return &RouteTableBuilder{
routesByName: make(map[string]*v1.Route, 10),
}
}
func (b *RouteTableBuilder) WithName(name string) *RouteTableBuilder {
b.name = name
return b
}
func (b *RouteTableBuilder) WithNamespace(namespace string) *RouteTableBuilder {
b.namespace = namespace
return b
}
func (b *RouteTableBuilder) WithRoute(name string, route *v1.Route) *RouteTableBuilder {
b.routesByName[name] = route
return b
}
func (b *RouteTableBuilder) Clone() *RouteTableBuilder {
if b == nil {
return nil
}
clone := new(RouteTableBuilder)
clone.name = b.name
clone.namespace = b.namespace
clone.routesByName = make(map[string]*v1.Route, len(b.routesByName))
for key, value := range b.routesByName {
clone.routesByName[key] = value.Clone().(*v1.Route)
}
return clone
}
func (b *RouteTableBuilder) Build() *v1.RouteTable {
var routes []*v1.Route
for _, route := range b.routesByName {
routes = append(routes, route)
}
rt := &v1.RouteTable{
Metadata: &core.Metadata{
Name: b.name,
Namespace: b.namespace,
},
Routes: routes,
}
return proto.Clone(rt).(*v1.RouteTable)
}