Skip to content

Commit

Permalink
feat: add grpc gw http listener
Browse files Browse the repository at this point in the history
Signed-off-by: Boris Glimcher <[email protected]>
  • Loading branch information
glimchb authored and artek-koltun committed Sep 19, 2023
1 parent 4e3600c commit 1019377
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ RUN go build -v -o /opi-spdk-bridge ./cmd/...
FROM alpine:3.18
COPY --from=builder /opi-spdk-bridge /
COPY --from=docker.io/fullstorydev/grpcurl:v1.8.7-alpine /bin/grpcurl /usr/local/bin/
EXPOSE 50051
CMD [ "/opi-spdk-bridge", "-port=50051" ]
EXPOSE 50051 8082
CMD [ "/opi-spdk-bridge", "-grpc_port=50051", "-http_port=8082" ]
HEALTHCHECK CMD grpcurl -plaintext localhost:50051 list || exit 1
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ It is just an example and implies SPDK just as example, not mandated by OPI.
on DPU/IPU (i.e. with IP=10.10.10.10) run

```bash
$ docker run --rm -it -v /var/tmp/:/var/tmp/ -p 50051:50051 ghcr.io/opiproject/opi-spdk-bridge:main
2022/09/21 21:39:49 server listening at [::]:50051
$ docker run --rm -it -v /var/tmp/:/var/tmp/ -p 50051:50051 -p 8082:8082 ghcr.io/opiproject/opi-spdk-bridge:main
2023/09/12 20:29:05 Connection to SPDK will be via: unix detected from /var/tmp/spdk.sock
2023/09/12 20:29:05 gRPC server listening at [::]:50051
2023/09/12 20:29:05 HTTP Server listening at 8082
```

on X86 management VM run
Expand Down Expand Up @@ -208,6 +210,12 @@ opi-spdk-server_1 | 2022/08/05 14:39:40 Received from SPDK: false
opi-spdk-server_1 | 2022/08/05 14:39:40 Could not delete: id:8
```

In addition HTTP is supported via grpc gateway, for example:

```bash
curl -kL http://10.10.10.10:8082/v1/inventory/1/inventory/2
```

Another remote call example

```bash
Expand Down
52 changes: 48 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
package main

import (
"context"
"flag"
"fmt"
"log"
"net"
"net/http"
"strings"
"time"

"github.com/opiproject/gospdk/spdk"

Expand All @@ -18,9 +21,14 @@ import (
"github.com/opiproject/opi-spdk-bridge/pkg/middleend"
"github.com/opiproject/opi-spdk-bridge/pkg/server"

pc "github.com/opiproject/opi-api/common/v1/gen/go"
pb "github.com/opiproject/opi-api/storage/v1alpha1/gen/go"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/reflection"

"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
)

func splitBusesBySeparator(str string) []string {
Expand All @@ -31,8 +39,11 @@ func splitBusesBySeparator(str string) []string {
}

func main() {
var port int
flag.IntVar(&port, "port", 50051, "The Server port")
var grpcPort int
flag.IntVar(&grpcPort, "grpc_port", 50051, "The gRPC server port")

var httpPort int
flag.IntVar(&httpPort, "http_port", 8082, "The HTTP server port")

var spdkAddress string
flag.StringVar(&spdkAddress, "spdk_addr", "/var/tmp/spdk.sock", "Points to SPDK unix socket/tcp socket to interact with")
Expand All @@ -56,9 +67,14 @@ func main() {
var tlsFiles string
flag.StringVar(&tlsFiles, "tls", "", "TLS files in server_cert:server_key:ca_cert format.")

go runGatewayServer(grpcPort, httpPort)
runGrpcServer(grpcPort, useKvm, spdkAddress, qmpAddress, ctrlrDir, busesStr, tcpTransportListenAddr, tlsFiles)
}

func runGrpcServer(grpcPort int, useKvm bool, spdkAddress, qmpAddress, ctrlrDir, busesStr, tcpTransportListenAddr, tlsFiles string) {
buses := splitBusesBySeparator(busesStr)

lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", grpcPort))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
Expand Down Expand Up @@ -114,8 +130,36 @@ func main() {

reflection.Register(s)

log.Printf("Server listening at %v", lis.Addr())
log.Printf("gRPC server listening at %v", lis.Addr())
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}

func runGatewayServer(grpcPort int, httpPort int) {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()

// Register gRPC server endpoint
// Note: Make sure the gRPC server is running properly and accessible
mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
err := pc.RegisterInventorySvcHandlerFromEndpoint(ctx, mux, fmt.Sprintf(":%d", grpcPort), opts)
if err != nil {
log.Panic("cannot register handler server")
}

// Start HTTP server (and proxy calls to gRPC server endpoint)
log.Printf("HTTP Server listening at %v", httpPort)
server := &http.Server{
Addr: fmt.Sprintf(":%d", httpPort),
Handler: mux,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
}
err = server.ListenAndServe()
if err != nil {
log.Panic("cannot start HTTP gateway server")
}
}
12 changes: 11 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ services:
volumes_from:
- spdk
ports:
- "8082:8082"
- "50051:50051"
networks:
- opi
Expand All @@ -65,7 +66,7 @@ services:
condition: service_healthy
redis:
condition: service_healthy
command: sh -c "/opi-spdk-bridge -port=50051 -spdk_addr=/var/tmp/spdk.sock -tcp_trid=$$(getent hosts spdk | awk '{ print $$1 }'):7777"
command: sh -c "/opi-spdk-bridge -grpc_port=50051 -http_port=8082 -spdk_addr=/var/tmp/spdk.sock -tcp_trid=$$(getent hosts spdk | awk '{ print $$1 }'):7777"
healthcheck:
test: grpcurl -plaintext localhost:50051 list || exit 1

Expand All @@ -76,6 +77,15 @@ services:
healthcheck:
test: ["CMD", "redis-cli", "--raw", "incr", "ping"]

opi-gw-test:
image: curlimages/curl:8.2.1
networks:
- opi
depends_on:
opi-spdk-server:
condition: service_healthy
command: curl -qkL http://opi-spdk-server:8082/v1/inventory/1/inventory/2

opi-spdk-client:
image: ghcr.io/opiproject/godpu:main@sha256:a6fd61121fc87d887bd58264606a55c36ae0dcea0da9e76856499e2f84a961b0
networks:
Expand Down

0 comments on commit 1019377

Please sign in to comment.