-
Notifications
You must be signed in to change notification settings - Fork 432
/
route_table.go
72 lines (60 loc) · 1.98 KB
/
route_table.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
package selectionutils
import (
"context"
"github.com/rotisserie/eris"
"github.com/solo-io/gloo/projects/gloo/pkg/defaults"
gatewayv1 "github.com/solo-io/gloo/projects/gateway/pkg/api/v1"
"github.com/solo-io/solo-kit/pkg/api/v1/clients"
"github.com/solo-io/solo-kit/pkg/api/v1/resources/core"
sk_errors "github.com/solo-io/solo-kit/pkg/errors"
)
type RouteTableSelector interface {
SelectOrBuildRouteTable(ctx context.Context, ref *core.ResourceRef) (*gatewayv1.RouteTable, error)
}
type routeTableSelector struct {
client gatewayv1.RouteTableClient
podNamespace string
}
var _ RouteTableSelector = &routeTableSelector{}
func NewRouteTableSelector(client gatewayv1.RouteTableClient, podNamespace string) *routeTableSelector {
return &routeTableSelector{
client: client,
podNamespace: podNamespace,
}
}
func (s *routeTableSelector) SelectOrBuildRouteTable(ctx context.Context, ref *core.ResourceRef) (*gatewayv1.RouteTable, error) {
// Read or build route table
// unlike virtual service, name must be provided as there is no "default" virtual service
name := ref.GetName()
if name == "" {
return nil, eris.New("must provide a name for the target route table")
}
ns := ref.GetNamespace()
if ns == "" {
ns = defaults.GlooSystem
}
found, err := s.client.Read(ref.GetNamespace(), ref.GetName(), clients.ReadOpts{Ctx: ctx})
if err != nil && !sk_errors.IsNotExist(err) {
return nil, err
}
if found != nil {
return found, nil
}
// Build a new default route table object
return s.build(ctx, ref)
}
func (s *routeTableSelector) build(ctx context.Context, ref *core.ResourceRef) (*gatewayv1.RouteTable, error) {
routeTable := &gatewayv1.RouteTable{
Metadata: &core.Metadata{
Namespace: ref.GetNamespace(),
Name: ref.GetName(),
},
}
if routeTable.GetMetadata().GetNamespace() == "" {
routeTable.GetMetadata().Namespace = s.podNamespace
}
if routeTable.GetMetadata().GetName() == "" {
routeTable.GetMetadata().Name = "default"
}
return routeTable, nil
}