Skip to content

Commit 7f60dd0

Browse files
authored
initial protobufs && grpc (nanovms#1416)
1 parent 047db58 commit 7f60dd0

File tree

12 files changed

+667
-98
lines changed

12 files changed

+667
-98
lines changed

.circleci/config.yml

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ jobs:
1515
- run: sudo apt-get update
1616
- run: sudo apt-get install qemu-system-x86
1717
- run: go install github.com/jstemmer/go-junit-report@latest
18+
- run: wget https://github.com/bufbuild/buf/releases/download/v1.13.1/buf-Linux-x86_64 && sudo mv buf-Linux-x86_64 /usr/bin/buf && sudo chmod +x /usr/bin/buf
19+
- run: go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
20+
- run: go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
21+
- run: curl -L https://github.com/grpc-ecosystem/grpc-gateway/releases/download/v2.15.0/protoc-gen-openapiv2-v2.15.0-linux-x86_64 -o ~/go/bin/protoc-gen-openapiv2 && chmod +x ~/go/bin/protoc-gen-openapiv2
22+
- run: curl -L https://github.com/grpc-ecosystem/grpc-gateway/releases/download/v2.15.0/protoc-gen-grpc-gateway-v2.15.0-linux-x86_64 -o ~/go/bin/protoc-gen-grpc-gateway && chmod +x ~/go/bin/protoc-gen-grpc-gateway
1823
- run: make deps
24+
- run: make generate
1925
- run: ./hack/verify-gofmt.sh
2026
- run: ./hack/verify-govet.sh
2127
- run: ./hack/verify-golint.sh
@@ -34,9 +40,15 @@ jobs:
3440
steps:
3541
- checkout
3642
- run: /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
37-
- run: brew update && brew install go && brew tap nanovms/homebrew-qemu && brew install nanovms/homebrew-qemu/qemu
43+
- run: brew update && brew install go && brew tap nanovms/homebrew-qemu && brew install nanovms/homebrew-qemu/qemu && brew install wget && brew install protoc-gen-go-grpc
3844
- run: go install github.com/jstemmer/go-junit-report@latest
45+
- run: brew install bufbuild/buf/buf
46+
- run: go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
47+
- run: go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
48+
- run: curl -L https://github.com/grpc-ecosystem/grpc-gateway/releases/download/v2.15.0/protoc-gen-openapiv2-v2.15.0-darwin-x86_64 -o ~/go/bin/protoc-gen-openapiv2 && chmod +x ~/go/bin/protoc-gen-openapiv2
49+
- run: curl -L https://github.com/grpc-ecosystem/grpc-gateway/releases/download/v2.15.0/protoc-gen-grpc-gateway-v2.15.0-darwin-x86_64 -o ~/go/bin/protoc-gen-grpc-gateway && chmod +x ~/go/bin/protoc-gen-grpc-gateway
3950
- run: make deps
51+
- run: make generate PATH=$PATH:~/go/bin
4052
- run: mkdir -p $TEST_RESULTS
4153
- run: go run ops.go update
4254
- run:
@@ -49,7 +61,13 @@ jobs:
4961
- image: cimg/go:1.19.5
5062
steps:
5163
- checkout
64+
- run: wget https://github.com/bufbuild/buf/releases/download/v1.13.1/buf-Linux-x86_64 && sudo mv buf-Linux-x86_64 /usr/bin/buf && sudo chmod +x /usr/bin/buf
65+
- run: go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
66+
- run: go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
67+
- run: curl -L https://github.com/grpc-ecosystem/grpc-gateway/releases/download/v2.15.0/protoc-gen-openapiv2-v2.15.0-linux-x86_64 -o ~/go/bin/protoc-gen-openapiv2 && chmod +x ~/go/bin/protoc-gen-openapiv2
68+
- run: curl -L https://github.com/grpc-ecosystem/grpc-gateway/releases/download/v2.15.0/protoc-gen-grpc-gateway-v2.15.0-linux-x86_64 -o ~/go/bin/protoc-gen-grpc-gateway && chmod +x ~/go/bin/protoc-gen-grpc-gateway
5269
- run: go build
70+
- run: make generate
5371
- run: echo "deb http://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
5472
- run: curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
5573
- run: sudo apt-get update && sudo apt-get install google-cloud-sdk

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
*.pb.go
2+
*.pb.gw.go
3+
*.swagger.json
14
*.dmg
25
*.zip
36
*.md5

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ post-test: do-test
2727

2828
test: post-test
2929

30+
generate:
31+
buf generate --path ./protos/imageservice/imageservice.proto
32+
3033
clean:
3134
$(GOCLEAN)
3235
rm -f $(BINARY_NAME)
36+
rm -rf proto/imageservice/*.go
37+
rm -rf proto/imageservice/*.json
3338

3439
run:
3540
$(GOBUILD) -o $(BINARY_NAME) -v .

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ macOS notes:
7676
GO111MODULE=on go build -ldflags "-w"
7777
```
7878

79+
For protobufs/grpc we use https://buf.build/ .
80+
81+
To generate protobufs (but do not check in to vcs):
82+
83+
```
84+
make generate
85+
```
86+
7987
For [detailed instructions](https://nanovms.gitbook.io/ops/developer/prerequisites), please consult the documentation.
8088

8189
# Hello World

buf.gen.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version: v1beta1
2+
plugins:
3+
- name: go
4+
out: protos
5+
opt:
6+
- paths=source_relative
7+
- name: go-grpc
8+
out: protos
9+
opt:
10+
- paths=source_relative
11+
- require_unimplemented_servers=false
12+
- name: grpc-gateway
13+
out: protos
14+
opt:
15+
- paths=source_relative
16+
- name: openapiv2
17+
out: protos

buf.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: v1beta1
2+
build:
3+
roots:
4+
- protos
5+
lint:
6+
use:
7+
- DEFAULT

cmd/cmd_daemon.go

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,22 @@ package cmd
22

33
import (
44
"fmt"
5+
6+
"context"
7+
"log"
8+
"net"
9+
"net/http"
10+
"os"
11+
12+
api "github.com/nanovms/ops/lepton"
13+
"github.com/nanovms/ops/provider"
14+
"github.com/nanovms/ops/types"
15+
16+
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
17+
"github.com/nanovms/ops/protos/imageservice"
518
"github.com/spf13/cobra"
19+
"google.golang.org/grpc"
20+
"google.golang.org/grpc/credentials/insecure"
621
)
722

823
// DaemonizeCommand turns ops into a daemon
@@ -16,10 +31,97 @@ func DaemonizeCommand() *cobra.Command {
1631
return cmdDaemonize
1732
}
1833

34+
// prob belongs in a root grpc-server folder
35+
// not in the cmds folder
36+
type server struct{}
37+
38+
func (*server) GetImages(_ context.Context, in *imageservice.ImageListRequest) (*imageservice.ImagesResponse, error) {
39+
40+
// stubbed for now - could conceivablly store creds in server and
41+
// target any provider which would be nice
42+
c := &types.Config{}
43+
pc := &types.ProviderConfig{}
44+
45+
p, err := provider.CloudProvider("onprem", pc)
46+
if err != nil {
47+
fmt.Println(err)
48+
}
49+
50+
ctx := api.NewContext(c)
51+
images, err := p.GetImages(ctx)
52+
if err != nil {
53+
return nil, err
54+
}
55+
56+
pb := &imageservice.ImagesResponse{
57+
Count: int32(len(images)),
58+
}
59+
60+
for i := 0; i < len(images); i++ {
61+
img := &imageservice.Image{
62+
Name: images[i].Name,
63+
Path: images[i].Path,
64+
Size: images[i].Size,
65+
Created: images[i].Created.String(),
66+
}
67+
68+
pb.Images = append(pb.Images, img)
69+
}
70+
71+
return pb, nil
72+
}
73+
1974
func daemonizeCommandHandler(cmd *cobra.Command, args []string) {
2075
fmt.Println("Note: If on a mac this expects ops to have suid bit set for networking.")
2176
fmt.Println("if you used the installer you are set otherwise run the following command\n" +
2277
"\tsudo chown -R root /usr/local/bin/qemu-system-x86_64\n" +
2378
"\tsudo chmod u+s /usr/local/bin/qemu-system-x86_64")
24-
fmt.Println("daemonizing")
79+
80+
lis, err := net.Listen("tcp", ":8080")
81+
if err != nil {
82+
fmt.Println(err)
83+
os.Exit(1)
84+
}
85+
86+
s := grpc.NewServer()
87+
imageservice.RegisterImagesServer(s, &server{})
88+
log.Println("Serving gRPC on 0.0.0.0:8080")
89+
go func() {
90+
err := s.Serve(lis)
91+
if err != nil {
92+
fmt.Println(err)
93+
os.Exit(1)
94+
}
95+
}()
96+
97+
conn, err := grpc.DialContext(
98+
context.Background(),
99+
"0.0.0.0:8080",
100+
grpc.WithBlock(),
101+
grpc.WithTransportCredentials(insecure.NewCredentials()),
102+
)
103+
if err != nil {
104+
fmt.Println(err)
105+
os.Exit(1)
106+
}
107+
108+
gwmux := runtime.NewServeMux()
109+
err = imageservice.RegisterImagesHandler(context.Background(), gwmux, conn)
110+
if err != nil {
111+
fmt.Println(err)
112+
os.Exit(1)
113+
}
114+
115+
gwServer := &http.Server{
116+
Addr: ":8090",
117+
Handler: gwmux,
118+
}
119+
120+
log.Println("Serving json on http://0.0.0.0:8090")
121+
fmt.Println("try issuing a request:\tcurl -XGET -k http://localhost:8090/v1/images | jq")
122+
err = gwServer.ListenAndServe()
123+
if err != nil {
124+
fmt.Println(err)
125+
os.Exit(1)
126+
}
25127
}

go.mod

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/nanovms/ops
33
go 1.19
44

55
require (
6-
cloud.google.com/go/storage v1.10.0
6+
cloud.google.com/go/storage v1.27.0
77
github.com/Azure/azure-sdk-for-go v63.0.0+incompatible
88
github.com/Azure/azure-storage-blob-go v0.10.0
99
github.com/Azure/go-autorest/autorest v0.11.24
@@ -20,6 +20,7 @@ require (
2020
github.com/go-errors/errors v1.0.1
2121
github.com/golang/mock v1.6.0
2222
github.com/gophercloud/gophercloud v0.12.0
23+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.0
2324
github.com/minio/minio-go v6.0.14+incompatible
2425
github.com/moby/term v0.0.0-20210610120745-9d4ed1856297
2526
github.com/olekukonko/tablewriter v0.0.4
@@ -30,22 +31,29 @@ require (
3031
github.com/spf13/afero v1.6.0
3132
github.com/spf13/cobra v1.2.1
3233
github.com/spf13/pflag v1.0.5
33-
github.com/stretchr/testify v1.7.0
34+
github.com/stretchr/testify v1.8.1
3435
github.com/terra-farm/go-virtualbox v0.0.4
3536
github.com/tj/go-spin v1.1.0
3637
github.com/ttacon/chalk v0.0.0-20160626202418-22c06c80ed31
3738
github.com/vmware/govmomi v0.22.2
3839
github.com/vultr/govultr/v2 v2.10.0
3940
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064
40-
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f
41-
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f
42-
google.golang.org/api v0.56.0
41+
golang.org/x/oauth2 v0.3.0
42+
golang.org/x/sys v0.3.0
43+
google.golang.org/api v0.103.0
44+
google.golang.org/genproto v0.0.0-20221207170731-23e4bf6bdc37
45+
google.golang.org/grpc v1.51.0
46+
google.golang.org/protobuf v1.28.1
47+
gopkg.in/yaml.v2 v2.4.0
4348
gotest.tools v2.2.0+incompatible
4449
k8s.io/client-go v0.23.1
4550
)
4651

4752
require (
48-
cloud.google.com/go v0.93.3 // indirect
53+
cloud.google.com/go v0.105.0 // indirect
54+
cloud.google.com/go/compute v1.13.0 // indirect
55+
cloud.google.com/go/compute/metadata v0.2.1 // indirect
56+
cloud.google.com/go/iam v0.8.0 // indirect
4957
github.com/Azure/azure-pipeline-go v0.2.2 // indirect
5058
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
5159
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
@@ -68,11 +76,12 @@ require (
6876
github.com/golang-jwt/jwt/v4 v4.4.1 // indirect
6977
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
7078
github.com/golang/protobuf v1.5.2 // indirect
71-
github.com/google/go-cmp v0.5.6 // indirect
79+
github.com/google/go-cmp v0.5.9 // indirect
7280
github.com/google/go-querystring v1.1.0 // indirect
7381
github.com/google/gofuzz v1.2.0 // indirect
7482
github.com/google/uuid v1.3.0 // indirect
75-
github.com/googleapis/gax-go/v2 v2.1.0 // indirect
83+
github.com/googleapis/enterprise-certificate-proxy v0.2.0 // indirect
84+
github.com/googleapis/gax-go/v2 v2.7.0 // indirect
7685
github.com/googleapis/gnostic v0.5.5 // indirect
7786
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
7887
github.com/hashicorp/go-retryablehttp v0.7.0 // indirect
@@ -96,17 +105,14 @@ require (
96105
github.com/rivo/uniseg v0.2.0 // indirect
97106
github.com/rogpeppe/go-internal v1.6.1 // indirect
98107
github.com/sirupsen/logrus v1.8.1 // indirect
99-
go.opencensus.io v0.23.0 // indirect
100-
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
101-
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
102-
golang.org/x/text v0.3.7 // indirect
108+
go.opencensus.io v0.24.0 // indirect
109+
golang.org/x/net v0.3.0 // indirect
110+
golang.org/x/term v0.3.0 // indirect
111+
golang.org/x/text v0.5.0 // indirect
103112
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
113+
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
104114
google.golang.org/appengine v1.6.7 // indirect
105-
google.golang.org/genproto v0.0.0-20220614165028-45ed7f3ff16e // indirect
106-
google.golang.org/grpc v1.47.0 // indirect
107-
google.golang.org/protobuf v1.28.0 // indirect
108115
gopkg.in/inf.v0 v0.9.1 // indirect
109-
gopkg.in/yaml.v2 v2.4.0 // indirect
110116
gopkg.in/yaml.v3 v3.0.1 // indirect
111117
k8s.io/api v0.23.1 // indirect
112118
k8s.io/apimachinery v0.23.1 // indirect

0 commit comments

Comments
 (0)