Skip to content
This repository was archived by the owner on Nov 16, 2020. It is now read-only.

Commit 0b32b82

Browse files
berndtjkars7e
authored andcommitted
Rebuild API cache from entitystore (#692)
* API cache now works across restarts of dispatch server * Ensure all HTTP methods are stored as uppercase Signed-off-by: Berndt Jung <[email protected]>
1 parent cd61b73 commit 0b32b82

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

pkg/api-manager/gateway/local/gateway.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import (
1212

1313
log "github.com/sirupsen/logrus"
1414

15+
apimanager "github.com/vmware/dispatch/pkg/api-manager"
1516
"github.com/vmware/dispatch/pkg/api-manager/gateway"
1617
"github.com/vmware/dispatch/pkg/client"
18+
entitystore "github.com/vmware/dispatch/pkg/entity-store"
1719
"github.com/vmware/dispatch/pkg/errors"
1820
"github.com/vmware/dispatch/pkg/http"
1921
"github.com/vmware/dispatch/pkg/trace"
@@ -23,6 +25,7 @@ import (
2325
type Gateway struct {
2426
Server *http.Server
2527

28+
store entitystore.EntityStore
2629
fnClient client.FunctionsClient
2730

2831
sync.RWMutex
@@ -33,15 +36,17 @@ type Gateway struct {
3336
}
3437

3538
// NewGateway creates a new local API gateway
36-
func NewGateway(functionsClient client.FunctionsClient) (*Gateway, error) {
39+
func NewGateway(store entitystore.EntityStore, functionsClient client.FunctionsClient) (*Gateway, error) {
3740
c := &Gateway{
3841
fnClient: functionsClient,
3942
Server: http.NewServer(nil),
43+
store: store,
4044
apis: make(map[string]*gateway.API),
4145
pathLookup: make(map[string][]*gateway.API),
4246
hostLookup: make(map[string][]*gateway.API),
4347
methodLookup: make(map[string][]*gateway.API),
4448
}
49+
c.rebuildCache()
4550
return c, nil
4651
}
4752

@@ -106,6 +111,18 @@ func (g *Gateway) DeleteAPI(ctx context.Context, api *gateway.API) error {
106111
// rebuildCache iterates over all configured APIs and populates lookup caches. Could optimized
107112
// to only add changes.
108113
func (g *Gateway) rebuildCache() {
114+
// Store should only be nil for tests
115+
if g.store != nil {
116+
var apis []*apimanager.API
117+
err := g.store.ListGlobal(context.TODO(), entitystore.Options{}, &apis)
118+
if err != nil {
119+
log.Errorf("error syncing APIs: %v", err)
120+
}
121+
g.apis = make(map[string]*gateway.API)
122+
for _, api := range apis {
123+
g.apis[api.Name] = &api.API
124+
}
125+
}
109126
g.hostLookup = make(map[string][]*gateway.API)
110127
g.methodLookup = make(map[string][]*gateway.API)
111128
g.pathLookup = make(map[string][]*gateway.API)

pkg/api-manager/gateway/local/gateway_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestGatewayGetRequest(t *testing.T) {
2828
fnClient.On("RunFunction", mock.Anything, mock.Anything, mock.Anything).Return(
2929
&v1.Run{}, nil,
3030
)
31-
gw, err := NewGateway(fnClient)
31+
gw, err := NewGateway(nil, fnClient)
3232
assert.NoError(t, err)
3333

3434
api1 := &gateway.API{
@@ -71,7 +71,7 @@ func TestGatewayPostRequest(t *testing.T) {
7171
fnClient.On("RunFunction", mock.Anything, mock.Anything, mock.Anything).Return(
7272
&v1.Run{}, nil,
7373
)
74-
gw, err := NewGateway(fnClient)
74+
gw, err := NewGateway(nil, fnClient)
7575
assert.NoError(t, err)
7676

7777
api1 := &gateway.API{

pkg/api-manager/handlers.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ func apiModelOntoEntity(organizationID string, m *v1.API) *API {
5656
} else {
5757
uris = m.Uris
5858
}
59+
var methods []string
60+
for _, method := range m.Methods {
61+
methods = append(methods, strings.ToUpper(method))
62+
}
5963
e := API{
6064
BaseEntity: entitystore.BaseEntity{
6165
Name: *m.Name,
@@ -70,7 +74,7 @@ func apiModelOntoEntity(organizationID string, m *v1.API) *API {
7074
Enabled: m.Enabled,
7175
TLS: m.TLS,
7276
Hosts: m.Hosts,
73-
Methods: m.Methods,
77+
Methods: methods,
7478
Protocols: m.Protocols,
7579
URIs: uris,
7680
CORS: m.Cors,

pkg/dispatchserver/local.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func runLocal(config *serverConfig) {
7575
functionsHandler, functionsShutdown := initFunctions(config, functionsDeps)
7676
defer functionsShutdown()
7777

78-
gw, err := local.NewGateway(functions)
78+
gw, err := local.NewGateway(store, functions)
7979
if err != nil {
8080
log.Fatalf("Error creating API Gateway: %v", err)
8181
}

0 commit comments

Comments
 (0)