forked from openet/gloo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route_table.go
81 lines (69 loc) · 2.37 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
73
74
75
76
77
78
79
80
81
package selectionutils
import (
"context"
"github.com/solo-io/gloo/projects/gloo/pkg/defaults"
"github.com/solo-io/go-utils/errors"
"github.com/solo-io/gloo/pkg/listers"
gatewayv1 "github.com/solo-io/gloo/projects/gateway/pkg/api/v1"
"github.com/solo-io/go-utils/contextutils"
"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"
"go.uber.org/zap"
)
type RouteTableSelector interface {
SelectOrCreateRouteTable(ctx context.Context, ref *core.ResourceRef) (*gatewayv1.RouteTable, error)
}
type routeTableSelector struct {
client gatewayv1.RouteTableClient
namespaceLister listers.NamespaceLister
podNamespace string
}
var _ RouteTableSelector = &routeTableSelector{}
func NewRouteTableSelector(client gatewayv1.RouteTableClient, namespaceLister listers.NamespaceLister, podNamespace string) *routeTableSelector {
return &routeTableSelector{
client: client,
namespaceLister: namespaceLister,
podNamespace: podNamespace,
}
}
func (s *routeTableSelector) SelectOrCreateRouteTable(ctx context.Context, ref *core.ResourceRef) (*gatewayv1.RouteTable, error) {
// Read or create route table
// unlike virtual service, name must be provided as there is no "default" virtual service
name := ref.GetName()
if name == "" {
return nil, errors.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
}
return s.create(ctx, ref)
}
func (s *routeTableSelector) create(ctx context.Context, ref *core.ResourceRef) (*gatewayv1.RouteTable, error) {
routeTable := &gatewayv1.RouteTable{
Metadata: core.Metadata{
Namespace: ref.GetNamespace(),
Name: ref.GetName(),
},
}
if routeTable.GetMetadata().Namespace == "" {
routeTable.Metadata.Namespace = s.podNamespace
}
if routeTable.GetMetadata().Name == "" {
routeTable.Metadata.Name = "default"
}
written, err := s.client.Write(routeTable, clients.WriteOpts{Ctx: ctx})
if err != nil {
return nil, err
}
contextutils.LoggerFrom(ctx).Infow("Created new default route table", zap.Any("routeTable", routeTable))
return written, nil
}