-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cluser api: json rpc reverse proxy for grpc apis
fixes: #252
refs: #254
Replace HTTP API with auto-generated API from GRPC API
- Loading branch information
Showing
21 changed files
with
3,385 additions
and
473 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package grpc | ||
|
||
import ( | ||
"bytes" | ||
|
||
"github.com/gogo/protobuf/jsonpb" | ||
"github.com/gogo/protobuf/proto" | ||
) | ||
|
||
func Marshal(obj proto.Message) ([]byte, error) { | ||
buf := bytes.Buffer{} | ||
marshaler := jsonpb.Marshaler{} | ||
if err := marshaler.Marshal(&buf, obj); err != nil { | ||
return nil, err | ||
} | ||
return buf.Bytes(), nil | ||
} | ||
|
||
// func VerifySignature(request *types.GRPCRequest) (crypto.Address, error) { | ||
// buf := bytes.Buffer{} | ||
// marshaler := jsonpb.Marshaler{} | ||
// // switch v := request.Payload.(type) { | ||
// // case *types.GRPCRequest_ManifestRequest: | ||
// // if err := marshaler.Marshal(&buf, v.ManifestRequest); err != nil { | ||
// // return nil, err | ||
// // } | ||
// // default: | ||
// // return nil, types.ErrInvalidPayload{Message: "invalid payload"} | ||
// // } | ||
|
||
// if err := marshaler.Marshal(&buf, request.ManifestRequest); err != nil { | ||
// return nil, err | ||
// } | ||
|
||
// key, err := crypto.PubKeyFromBytes(request.Key) | ||
// if err != nil { | ||
// return nil, err | ||
// } | ||
|
||
// sig, err := crypto.SignatureFromBytes(request.Signature) | ||
// if err != nil { | ||
// return nil, err | ||
// } | ||
|
||
// if !key.VerifyBytes(buf.Bytes(), sig) { | ||
// return nil, types.ErrInvalidSignature{"invalud signature"} | ||
// } | ||
// return key.Address(), err | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package grpc | ||
|
||
import ( | ||
"github.com/ovrclk/akash/types" | ||
"golang.org/x/net/context" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
type client struct { | ||
target string | ||
} | ||
|
||
type Client interface { | ||
Deploy(context.Context, *types.ManifestRequest) (*types.DeployRespone, error) | ||
} | ||
|
||
func NewClient(target string) (Client, error) { | ||
return &client{target: target}, nil | ||
} | ||
|
||
func (c *client) Deploy(ctx context.Context, manifestRequest *types.ManifestRequest) (*types.DeployRespone, error) { | ||
conn, err := grpc.Dial(c.target, grpc.WithInsecure()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer conn.Close() | ||
client := types.NewClusterClient(conn) | ||
resp, err := client.Deploy(ctx, manifestRequest) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return resp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package grpc | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/ovrclk/akash/manifest" | ||
kmocks "github.com/ovrclk/akash/provider/cluster/kube/mocks" | ||
"github.com/ovrclk/akash/provider/manifest/mocks" | ||
"github.com/ovrclk/akash/sdl" | ||
"github.com/ovrclk/akash/testutil" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
"github.com/tendermint/tmlibs/log" | ||
) | ||
|
||
func TestSendManifest(t *testing.T) { | ||
c, err := NewClient("localhost:3001") | ||
assert.NoError(t, err) | ||
|
||
sdl, err := sdl.ReadFile("../../_docs/deployment.yml") | ||
require.NoError(t, err) | ||
|
||
mani, err := sdl.Manifest() | ||
require.NoError(t, err) | ||
|
||
_, kmgr := testutil.NewNamedKey(t) | ||
signer := testutil.Signer(t, kmgr) | ||
|
||
deployment := testutil.DeploymentAddress(t) | ||
|
||
req, _, err := manifest.SignManifest(mani, signer, deployment) | ||
assert.NoError(t, err) | ||
|
||
handler := &mocks.Handler{} | ||
handler.On("HandleManifest", mock.Anything).Return(nil) | ||
|
||
client := &kmocks.Client{} | ||
|
||
server := newServer(log.NewTMLogger(os.Stdout), "tcp", ":3001", handler, client) | ||
go func() { | ||
err := server.listenAndServe() | ||
require.NoError(t, err) | ||
}() | ||
|
||
time.Sleep(1 * time.Second) | ||
|
||
_, err = c.Deploy(context.TODO(), req) | ||
assert.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package json | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/grpc-ecosystem/grpc-gateway/runtime" | ||
"github.com/ovrclk/akash/types" | ||
"github.com/tendermint/tmlibs/log" | ||
"golang.org/x/net/context" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
type proxy struct { | ||
endpoint string | ||
addr string | ||
log log.Logger | ||
mux *runtime.ServeMux | ||
} | ||
|
||
func new(ctx context.Context, log log.Logger, addr, endpoint string) (*proxy, error) { | ||
mux := runtime.NewServeMux() | ||
opts := []grpc.DialOption{grpc.WithInsecure()} | ||
err := types.RegisterClusterHandlerFromEndpoint(ctx, mux, endpoint, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &proxy{ | ||
endpoint: endpoint, | ||
addr: addr, | ||
log: log, | ||
mux: mux, | ||
}, nil | ||
} | ||
|
||
func (p *proxy) listenAndServe() error { | ||
ctx := context.Background() | ||
ctx, cancel := context.WithCancel(ctx) | ||
defer cancel() | ||
|
||
mux := runtime.NewServeMux() | ||
opts := []grpc.DialOption{grpc.WithInsecure()} | ||
err := types.RegisterClusterHandlerFromEndpoint(ctx, mux, p.endpoint, opts) | ||
if err != nil { | ||
return err | ||
} | ||
return http.ListenAndServe(p.addr, mux) | ||
} | ||
|
||
func Run(ctx context.Context, log log.Logger, address, endpoint string) error { | ||
ctx, cancel := context.WithCancel(ctx) | ||
proxy, err := new(ctx, log, address, endpoint) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Info("Starting GRPC JSON proxy server", "address", proxy.addr, "endpoint", proxy.endpoint) | ||
err = proxy.listenAndServe() | ||
cancel() | ||
|
||
log.Info("GRPC server shutdown") | ||
|
||
return err | ||
} |
Oops, something went wrong.