Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go: don't use plugins anymore #70

Merged
merged 1 commit into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ RUN go mod download
# build an app
COPY cmd/ cmd/
COPY pkg/ pkg/
RUN go build -v -buildmode=plugin -o /opi-smbios-bridge.so /app/pkg/... \
&& go build -v -buildmode=default -o /opi-smbios-bridge /app/cmd/...
RUN go build -v -o /opi-smbios-bridge ./cmd/...

# second stage to reduce image size
FROM alpine:3.17
RUN apk add --no-cache libc6-compat hwdata
RUN apk add --no-cache libc6-compat hwdata && rm -rf /var/cache/apk/*
COPY --from=builder /opi-smbios-bridge /
COPY --from=builder /opi-smbios-bridge.so /
COPY --from=docker.io/fullstorydev/grpcurl:v1.8.7-alpine /bin/grpcurl /usr/local/bin/
EXPOSE 50051
CMD [ "/opi-smbios-bridge", "-port=50051" ]
Expand Down
21 changes: 5 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,16 @@ See [CONTRIBUTING](https://github.com/opiproject/opi/blob/main/CONTRIBUTING.md)

## Getting started

build like this:

```bash
go build -v -buildmode=plugin -o /opi-smbios-bridge.so ./...
go build -v -o /opi-smbios-bridge ./cmd/...
```

in main app:
import like this:

```go
package main
import (
"plugin"
pc "github.com/opiproject/opi-api/common/v1/gen/go"
)
func main() {
plug, err := plugin.Open("/opi-smbios-bridge.so")
inventorySymbol, err := plug.Lookup("PluginInventory")
var inventory pc.InventorySvcServer
inventory, ok := inventorySymbol.(pc.InventorySvcServer)
s := grpc.NewServer()
pc.RegisterInventorySvcServer(s, inventory)
reflection.Register(s)
}
import "github.com/opiproject/opi-smbios-bridge/pkg/inventory"
```

## Using docker
Expand Down
25 changes: 4 additions & 21 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
"fmt"
"log"
"net"
"plugin"

pc "github.com/opiproject/opi-api/common/v1/gen/go"
"github.com/opiproject/opi-smbios-bridge/pkg/inventory"

"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
Expand All @@ -22,34 +23,16 @@ var (

func main() {
flag.Parse()
// Load the plugin
plug, err := plugin.Open("/opi-smbios-bridge.so")
if err != nil {
log.Fatal(err)
}
// 2. Look for an exported symbol such as a function or variable
inventorySymbol, err := plug.Lookup("PluginInventory")
if err != nil {
log.Fatal(err)
}
// 3. Attempt to cast the symbol to the Shipper
var inventory pc.InventorySvcServer
inventory, ok := inventorySymbol.(pc.InventorySvcServer)
if !ok {
log.Fatal("Invalid inventory type")
}
log.Printf("plugin serevr is %v", inventory)
// 4. If everything is ok from the previous assertions, then we can proceed
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()

pc.RegisterInventorySvcServer(s, inventory)
pc.RegisterInventorySvcServer(s, &inventory.Server{})
reflection.Register(s)

log.Printf("server listening at %v", lis.Addr())
log.Printf("Server listening at %v", lis.Addr())
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
Expand Down
13 changes: 6 additions & 7 deletions pkg/inventory/inventory.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2022 Dell Inc, or its subsidiaries.

// main is the main package of the application
package main
// Package inventory is the main package of the application
package inventory

import (
"context"
Expand All @@ -13,14 +13,13 @@ import (
pc "github.com/opiproject/opi-api/common/v1/gen/go"
)

type server struct {
// Server contains inventory related OPI services
type Server struct {
pc.UnimplementedInventorySvcServer
}

// PluginInventory is the server that we export to load dynamically at runtime
var PluginInventory server

func (s *server) InventoryGet(ctx context.Context, in *pc.InventoryGetRequest) (*pc.InventoryGetResponse, error) {
// InventoryGet returns inventory information
func (s *Server) InventoryGet(ctx context.Context, in *pc.InventoryGetRequest) (*pc.InventoryGetResponse, error) {
log.Printf("InventoryGet: Received from client: %v", in)

cpu, err := ghw.CPU()
Expand Down
5 changes: 3 additions & 2 deletions pkg/inventory/inventory_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2022 Dell Inc, or its subsidiaries.

package main
// Package inventory is the main package of the application
package inventory

import (
"context"
Expand All @@ -23,7 +24,7 @@ import (
func dialer() func(context.Context, string) (net.Conn, error) {
listener := bufconn.Listen(1024 * 1024)
server := grpc.NewServer()
pc.RegisterInventorySvcServer(server, &PluginInventory)
pc.RegisterInventorySvcServer(server, &Server{})

go func() {
if err := server.Serve(listener); err != nil {
Expand Down