Skip to content

Commit 638889e

Browse files
authored
feat: supports container actions for agents (#3164)
1 parent f09e294 commit 638889e

File tree

9 files changed

+410
-120
lines changed

9 files changed

+410
-120
lines changed

internal/agent/client.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,25 @@ func (c *Client) Host() (docker.Host, error) {
368368
}, nil
369369
}
370370

371+
func (c *Client) ContainerAction(containerId string, action docker.ContainerAction) error {
372+
var containerAction pb.ContainerAction
373+
switch action {
374+
case docker.Start:
375+
containerAction = pb.ContainerAction_Start
376+
377+
case docker.Stop:
378+
containerAction = pb.ContainerAction_Stop
379+
380+
case docker.Restart:
381+
containerAction = pb.ContainerAction_Restart
382+
383+
}
384+
385+
_, err := c.client.ContainerAction(context.Background(), &pb.ContainerActionRequest{ContainerId: containerId, Action: containerAction})
386+
387+
return err
388+
}
389+
371390
func (c *Client) Close() error {
372391
return c.conn.Close()
373392
}

internal/agent/pb/rpc.pb.go

Lines changed: 232 additions & 95 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/agent/pb/rpc_grpc.pb.go

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/agent/pb/types.pb.go

Lines changed: 77 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/agent/server.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,31 @@ func (s *server) StreamContainerStarted(in *pb.StreamContainerStartedRequest, ou
285285
}
286286
}
287287

288+
func (s *server) ContainerAction(ctx context.Context, in *pb.ContainerActionRequest) (*pb.ContainerActionResponse, error) {
289+
var action docker.ContainerAction
290+
switch in.Action {
291+
case pb.ContainerAction_Start:
292+
action = docker.Start
293+
294+
case pb.ContainerAction_Stop:
295+
action = docker.Stop
296+
297+
case pb.ContainerAction_Restart:
298+
action = docker.Restart
299+
300+
default:
301+
return nil, status.Error(codes.InvalidArgument, "invalid action")
302+
}
303+
304+
err := s.client.ContainerActions(action, in.ContainerId)
305+
306+
if err != nil {
307+
return nil, status.Error(codes.Internal, err.Error())
308+
}
309+
310+
return &pb.ContainerActionResponse{}, nil
311+
}
312+
288313
func NewServer(client docker.Client, certificates tls.Certificate, dozzleVersion string) *grpc.Server {
289314
caCertPool := x509.NewCertPool()
290315
c, err := x509.ParseCertificate(certificates.Certificate[0])

internal/support/docker/agent_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,5 @@ func (d *agentService) SubscribeContainersStarted(ctx context.Context, container
6565
}
6666

6767
func (a *agentService) ContainerAction(container docker.Container, action docker.ContainerAction) error {
68-
panic("not implemented")
68+
return a.client.ContainerAction(container.ID, action)
6969
}

internal/support/docker/retriable_client_manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ func (m *RetriableClientManager) RetryAndList() ([]ClientService, []error) {
107107
host.Available = true
108108

109109
// We don't want to block the subscribers in event.go
110-
go func() {
110+
go func(host docker.Host) {
111111
select {
112112
case channel <- host:
113113
case <-ctx.Done():
114114
}
115-
}()
115+
}(host)
116116

117117
return true
118118
})

protos/rpc.proto

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ service AgentService {
1919
rpc StreamContainerStarted(StreamContainerStartedRequest)
2020
returns (stream StreamContainerStartedResponse) {}
2121
rpc HostInfo(HostInfoRequest) returns (HostInfoResponse) {}
22+
rpc ContainerAction(ContainerActionRequest)
23+
returns (ContainerActionResponse) {}
2224
}
2325

2426
message ListContainersRequest {}
@@ -64,3 +66,11 @@ message HostInfoResponse { Host host = 1; }
6466

6567
message StreamContainerStartedRequest {}
6668
message StreamContainerStartedResponse { Container container = 1; }
69+
70+
message ContainerActionRequest {
71+
string containerId = 1;
72+
ContainerAction action = 2;
73+
}
74+
75+
message ContainerActionResponse {
76+
}

protos/types.proto

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,9 @@ message Host {
6565
string agentVersion = 11;
6666
string dockerVersion = 12;
6767
}
68+
69+
enum ContainerAction {
70+
Start = 0;
71+
Stop = 1;
72+
Restart = 2;
73+
}

0 commit comments

Comments
 (0)