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

cdh:golang support to dynamic generate go code with proto file #605

Merged
merged 1 commit into from
Jul 8, 2024
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
23 changes: 19 additions & 4 deletions confidential-data-hub/golang/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,36 @@ endif

RPC ?= grpc
DESTDIR ?= /usr/local/bin
PROTODIR = $(CURDIR)/../hub/protos
BIN_NAME := cdh-go-client

.PHONY: build test install clean
.PHONY: generate build test install clean

build:
generate:
@echo "Generating Go code..."
ifeq ($(RPC), $(filter $(RPC), grpc ttrpc))
@cp ../hub/protos/api.proto ./pkg/api/api.proto
@sed -i 's/generators = \["go", ".*"\]/generators = ["go", "go-$(RPC)"]/' ./pkg/api/Protobuild.toml
@cd pkg/api/ && protobuild github.com/confidential-containers/guest-components/confidential-data-hub/golang/pkg/api && cd -
else
$(error ERROR: Unsupported RPC type $(RPC)!)
endif

build: generate
@echo "Building Go binaries..."
ifeq ($(RPC), $(filter $(RPC), grpc ttrpc))
GOARCH=$(ARCH) go build -ldflags "-X main.clientType=$(RPC)" -o bin/$(BIN_NAME) ./cmd/$(RPC)-client
else
$(error ERROR: Unsupported RPC type $(RPC)!)
endif

test:
test: generate
@echo "Running unit tests..."
go test ./...
ifeq ($(RPC), $(filter $(RPC), grpc ttrpc))
go test ./pkg/$(RPC)
else
$(error ERROR: Unsupported RPC type $(RPC)!)
endif

install:
@echo "Installing binaries..."
Expand Down
25 changes: 18 additions & 7 deletions confidential-data-hub/golang/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,37 @@ This offers a streamlined client interface for engaging with Confidential Data H

## Getting Started

### Install dependencies

```bash
$ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
$ go install github.com/containerd/ttrpc/cmd/protoc-gen-go-ttrpc@latest
$ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
$ go install github.com/containerd/protobuild@latest
```

### Usage as library

Import the package into your Go project:

```go
//common interface
import common "github.com/confidential-containers/guest-components/confidential-data-hub/golang/pkg/core"
import common "github.com/confidential-containers/guest-components/confidential-data-hub/golang/pkg/core"

//grpc package
import cdhgrpcapi "github.com/confidential-containers/guest-components/confidential-data-hub/golang/pkg/grpc"
import cdhgrpc "github.com/confidential-containers/guest-components/confidential-data-hub/golang/pkg/api/grpc"
//ttrpc package
import cdhttrpcapi "github.com/confidential-containers/guest-components/confidential-data-hub/golang/pkg/ttrpc"
import cdhttrpc "github.com/confidential-containers/guest-components/confidential-data-hub/golang/pkg/ttrpc"
```

Create a new client instance:

```go
//cdh grpc client
c, err := cdhgrpcapi.CreateCDHGrpcClient("127.0.0.1:8043")
c, err := cdhgrpc.CreateCDHGrpcClient("127.0.0.1:8043")

//cdh ttrpc client
c, err := cdhttrpcapi.CreateCDHTtrpcClient("/run/confidential-containers/cdh.sock")
c, err := cdhttrpc.CreateCDHTtrpcClient("/run/confidential-containers/cdh.sock")
```

Interact with `CDH` using the client, for example :
Expand All @@ -38,9 +47,11 @@ unsealedValue, err := common.UnsealEnv(ctx, c, sealedSecret)

Build and Install the binary, such as:
```bash
$ make RPC=grpc
$ make build RPC=grpc
Generating Go code...
protoc -I.:/root/go/src:/usr/local/include:/usr/include --go_out=/root/go/src --go_opt=Mgithub.com/confidential-containers/guest-components/confidential-data-hub/golang/pkg/api/api.proto=github.com/confidential-containers/guest-components/confidential-data-hub/golang/pkg/api/cdhapi --go-grpc_out=/root/go/src /root/go/src/github.com/confidential-containers/guest-components/confidential-data-hub/golang/pkg/api/api.proto
/root/go/src/github.com/confidential-containers/guest-components/confidential-data-hub/golang
Building Go binaries...
GOARCH=amd64 go build -o bin/cdh-go-client ./cmd/grpc-client
$ sudo make install
Installing binaries...
install -D -m0755 bin/cdh-go-client /usr/local/bin
Expand Down
4 changes: 2 additions & 2 deletions confidential-data-hub/golang/cmd/grpc-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"os"

common "github.com/confidential-containers/guest-components/confidential-data-hub/golang/pkg/core"
cdhgrpcapi "github.com/confidential-containers/guest-components/confidential-data-hub/golang/pkg/grpc"
cdhgrpc "github.com/confidential-containers/guest-components/confidential-data-hub/golang/pkg/grpc"
)

var (
Expand All @@ -23,7 +23,7 @@ func main() {
common.Init()
flag.Parse()

c, err := cdhgrpcapi.CreateCDHGrpcClient(common.Socket)
c, err := cdhgrpc.CreateCDHGrpcClient(common.Socket)
if err != nil {
fmt.Printf("failed to create cdh client %v", err)
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions confidential-data-hub/golang/cmd/ttrpc-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"os"

common "github.com/confidential-containers/guest-components/confidential-data-hub/golang/pkg/core"
cdhttrpcapi "github.com/confidential-containers/guest-components/confidential-data-hub/golang/pkg/ttrpc"
cdhttrpc "github.com/confidential-containers/guest-components/confidential-data-hub/golang/pkg/ttrpc"
)

var (
Expand All @@ -24,7 +24,7 @@ func main() {
common.Init()
flag.Parse()

c, err := cdhttrpcapi.CreateCDHTtrpcClient(common.Socket)
c, err := cdhttrpc.CreateCDHTtrpcClient(common.Socket)
if err != nil {
fmt.Printf("failed to create cdh client %v", err)
os.Exit(1)
Expand Down
19 changes: 19 additions & 0 deletions confidential-data-hub/golang/pkg/api/Protobuild.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version = "2"
generators = ["go", "go-ttrpc"]

# Control protoc include paths. Below are usually some good defaults, but feel
# free to try it without them if it works for your project.
[includes]
# Include paths that will be added before all others. Typically, you want to
# treat the root of the project as an include, but this may not be necessary.
# before = ["./protobuf"]

# Paths that will be added untouched to the end of the includes. We use
# `/usr/local/include` to pickup the common install location of protobuf.
# This is the default.
# after = ["/usr/local/include"]

# This section maps protobuf imports to Go packages. These will become
# `-M` directives in the call to the go protobuf generator.
[packages]
"github.com/confidential-containers/guest-components/confidential-data-hub/golang/pkg/api/api.proto" = "github.com/confidential-containers/guest-components/confidential-data-hub/golang/pkg/api/cdhapi"
Loading
Loading