Skip to content

Commit 72b7cb7

Browse files
committed
Support for GRPC based guestagent communication
Signed-off-by: Balaji Vijayakumar <[email protected]>
1 parent 37b74b6 commit 72b7cb7

File tree

19 files changed

+700
-243
lines changed

19 files changed

+700
-243
lines changed

.github/workflows/test.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ jobs:
2929
- uses: actions/setup-go@v5
3030
with:
3131
go-version: 1.22.x
32+
- name: Install protoc
33+
run: |
34+
sudo apt-get update
35+
sudo apt-get install -y protobuf-compiler
36+
- name: Verify generated files
37+
run: make install-tools generate check-generated
3238
- name: Run golangci-lint
3339
uses: golangci/[email protected]
3440
with:

Makefile

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,14 @@ uninstall:
231231
if [ "$$(readlink "$(DEST)/bin/nerdctl")" = "nerdctl.lima" ]; then rm "$(DEST)/bin/nerdctl"; fi
232232
if [ "$$(readlink "$(DEST)/bin/apptainer")" = "apptainer.lima" ]; then rm "$(DEST)/bin/apptainer"; fi
233233

234+
.PHONY: check-generated
235+
check-generated:
236+
@test -z "$$(git status --short | grep ".pb.desc" | tee /dev/stderr)" || \
237+
((git diff $$(find . -name '*.pb.desc') | cat) && \
238+
(echo "Please run 'make generate' when making changes to proto files and check-in the generated file changes" && false))
239+
234240
.PHONY: lint
235-
lint:
241+
lint: check-generated
236242
golangci-lint run ./...
237243
yamllint .
238244
find . -name '*.sh' | xargs shellcheck
@@ -242,6 +248,15 @@ lint:
242248
clean:
243249
rm -rf _output vendor
244250

251+
.PHONY: install-tools
252+
install-tools:
253+
go install google.golang.org/protobuf/cmd/[email protected]
254+
go install google.golang.org/grpc/cmd/[email protected]
255+
256+
.PHONY: generate
257+
generate:
258+
go generate ./...
259+
245260
.PHONY: artifacts-darwin
246261
artifacts-darwin:
247262
mkdir -p _artifacts

cmd/lima-guestagent/daemon_linux.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ package main
33
import (
44
"errors"
55
"net"
6-
"net/http"
76
"os"
87
"time"
98

10-
"github.com/gorilla/mux"
119
"github.com/lima-vm/lima/pkg/guestagent"
1210
"github.com/lima-vm/lima/pkg/guestagent/api/server"
1311
"github.com/lima-vm/lima/pkg/guestagent/serialport"
@@ -62,12 +60,6 @@ func daemonAction(cmd *cobra.Command, _ []string) error {
6260
if err != nil {
6361
return err
6462
}
65-
backend := &server.Backend{
66-
Agent: agent,
67-
}
68-
r := mux.NewRouter()
69-
server.AddRoutes(r, backend)
70-
srv := &http.Server{Handler: r}
7163
err = os.RemoveAll(socket)
7264
if err != nil {
7365
return err
@@ -99,5 +91,5 @@ func daemonAction(cmd *cobra.Command, _ []string) error {
9991
l = socketL
10092
logrus.Infof("serving the guest agent on %q", socket)
10193
}
102-
return srv.Serve(l)
94+
return server.StartServer(l, &server.GuestServer{Agent: agent})
10395
}

pkg/guestagent/api/api.go

Lines changed: 0 additions & 34 deletions
This file was deleted.

pkg/guestagent/api/client/client.go

Lines changed: 27 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,53 @@
11
package client
22

3-
// Forked from https://github.com/rootless-containers/rootlesskit/blob/v0.14.2/pkg/api/client/client.go
4-
// Apache License 2.0
5-
63
import (
74
"context"
8-
"encoding/json"
9-
"fmt"
105
"net"
11-
"net/http"
6+
7+
"google.golang.org/grpc/credentials/insecure"
128

139
"github.com/lima-vm/lima/pkg/guestagent/api"
14-
"github.com/lima-vm/lima/pkg/httpclientutil"
10+
"google.golang.org/grpc"
11+
"google.golang.org/protobuf/types/known/emptypb"
1512
)
1613

17-
type GuestAgentClient interface {
18-
HTTPClient() *http.Client
19-
Info(context.Context) (*api.Info, error)
20-
Events(context.Context, func(api.Event)) error
14+
type GuestAgentClient struct {
15+
cli api.GuestServiceClient
2116
}
2217

23-
// NewGuestAgentClient creates a client.
24-
// remote is a path to the UNIX socket, without unix:// prefix or a remote hostname/IP address.
25-
func NewGuestAgentClient(conn net.Conn) (GuestAgentClient, error) {
26-
hc, err := httpclientutil.NewHTTPClientWithConn(conn)
27-
if err != nil {
28-
return nil, err
18+
func NewGuestAgentClient(dialFn func(ctx context.Context) (net.Conn, error)) (*GuestAgentClient, error) {
19+
opts := []grpc.DialOption{
20+
grpc.WithContextDialer(func(ctx context.Context, target string) (net.Conn, error) {
21+
return dialFn(ctx)
22+
}),
23+
grpc.WithTransportCredentials(insecure.NewCredentials()),
2924
}
30-
return NewGuestAgentClientWithHTTPClient(hc), nil
31-
}
3225

33-
func NewGuestAgentClientWithHTTPClient(hc *http.Client) GuestAgentClient {
34-
return &client{
35-
Client: hc,
36-
version: "v1",
37-
dummyHost: "lima-guestagent",
38-
}
39-
}
40-
41-
type client struct {
42-
*http.Client
43-
// version is always "v1"
44-
// TODO(AkihiroSuda): negotiate the version
45-
version string
46-
dummyHost string
47-
}
48-
49-
func (c *client) HTTPClient() *http.Client {
50-
return c.Client
51-
}
52-
53-
func (c *client) Info(ctx context.Context) (*api.Info, error) {
54-
u := fmt.Sprintf("http://%s/%s/info", c.dummyHost, c.version)
55-
resp, err := httpclientutil.Get(ctx, c.HTTPClient(), u)
26+
clientConn, err := grpc.Dial("", opts...)
5627
if err != nil {
5728
return nil, err
5829
}
59-
defer resp.Body.Close()
60-
var info api.Info
61-
dec := json.NewDecoder(resp.Body)
62-
if err := dec.Decode(&info); err != nil {
63-
return nil, err
64-
}
65-
return &info, nil
30+
client := api.NewGuestServiceClient(clientConn)
31+
return &GuestAgentClient{
32+
cli: client,
33+
}, nil
34+
}
35+
36+
func (c *GuestAgentClient) Info(ctx context.Context) (*api.Info, error) {
37+
return c.cli.GetInfo(ctx, &emptypb.Empty{})
6638
}
6739

68-
func (c *client) Events(ctx context.Context, onEvent func(api.Event)) error {
69-
u := fmt.Sprintf("http://%s/%s/events", c.dummyHost, c.version)
70-
resp, err := httpclientutil.Get(ctx, c.HTTPClient(), u)
40+
func (c *GuestAgentClient) Events(ctx context.Context, eventCb func(response *api.Event)) error {
41+
events, err := c.cli.GetEvents(ctx, &emptypb.Empty{})
7142
if err != nil {
7243
return err
7344
}
74-
defer resp.Body.Close()
75-
dec := json.NewDecoder(resp.Body)
45+
7646
for {
77-
var ev api.Event
78-
if err := dec.Decode(&ev); err != nil {
47+
recv, err := events.Recv()
48+
if err != nil {
7949
return err
8050
}
81-
onEvent(ev)
51+
eventCb(recv)
8252
}
8353
}

pkg/guestagent/api/gen.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
//go:generate protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative guestservice.proto --descriptor_set_out=guestservice.pb.desc
2+
package api
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
�
3+
guestservice.protogoogle/protobuf/empty.protogoogle/protobuf/timestamp.proto"0
4+
Info(
5+
local_ports ( 2.IPPortR
6+
localPorts"�
7+
Event.
8+
time ( 2.google.protobuf.TimestampRtime3
9+
local_ports_added ( 2.IPPortRlocalPortsAdded7
10+
local_ports_removed ( 2.IPPortRlocalPortsRemoved
11+
errors ( Rerrors",
12+
IPPort
13+
ip ( Rip
14+
port (Rport2g
15+
GuestService(
16+
GetInfo.google.protobuf.Empty.Info-
17+
GetEvents.google.protobuf.Empty.Event0B!Zgithub.com/lima-vm/lima/pkg/apibproto3

0 commit comments

Comments
 (0)