From 16120b80ba1c86ee6982ec58272f9725fe85a0db Mon Sep 17 00:00:00 2001 From: Pavel Sviderski Date: Wed, 4 Dec 2024 12:15:55 +1000 Subject: [PATCH] add ListContainers and RemoveContainer methods to Docker service --- internal/machine/api/pb/docker.pb.go | 303 ++++++++++++++++++---- internal/machine/api/pb/docker.proto | 18 ++ internal/machine/api/pb/docker_grpc.pb.go | 76 ++++++ internal/machine/docker/client.go | 65 ++++- internal/machine/docker/server.go | 44 +++- 5 files changed, 446 insertions(+), 60 deletions(-) diff --git a/internal/machine/api/pb/docker.pb.go b/internal/machine/api/pb/docker.pb.go index e4590dcc..6af377c4 100644 --- a/internal/machine/api/pb/docker.pb.go +++ b/internal/machine/api/pb/docker.pb.go @@ -208,6 +208,158 @@ func (x *StartContainerRequest) GetOptions() []byte { return nil } +type ListContainersRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // JSON serialized container.ListOptions. + Options []byte `protobuf:"bytes,1,opt,name=options,proto3" json:"options,omitempty"` +} + +func (x *ListContainersRequest) Reset() { + *x = ListContainersRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_internal_machine_api_pb_docker_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ListContainersRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ListContainersRequest) ProtoMessage() {} + +func (x *ListContainersRequest) ProtoReflect() protoreflect.Message { + mi := &file_internal_machine_api_pb_docker_proto_msgTypes[3] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ListContainersRequest.ProtoReflect.Descriptor instead. +func (*ListContainersRequest) Descriptor() ([]byte, []int) { + return file_internal_machine_api_pb_docker_proto_rawDescGZIP(), []int{3} +} + +func (x *ListContainersRequest) GetOptions() []byte { + if x != nil { + return x.Options + } + return nil +} + +type ListContainersResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // JSON serialized []container.Summary. + Containers []byte `protobuf:"bytes,1,opt,name=containers,proto3" json:"containers,omitempty"` +} + +func (x *ListContainersResponse) Reset() { + *x = ListContainersResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_internal_machine_api_pb_docker_proto_msgTypes[4] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ListContainersResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ListContainersResponse) ProtoMessage() {} + +func (x *ListContainersResponse) ProtoReflect() protoreflect.Message { + mi := &file_internal_machine_api_pb_docker_proto_msgTypes[4] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ListContainersResponse.ProtoReflect.Descriptor instead. +func (*ListContainersResponse) Descriptor() ([]byte, []int) { + return file_internal_machine_api_pb_docker_proto_rawDescGZIP(), []int{4} +} + +func (x *ListContainersResponse) GetContainers() []byte { + if x != nil { + return x.Containers + } + return nil +} + +type RemoveContainerRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` + // JSON serialized container.RemoveOptions. + Options []byte `protobuf:"bytes,2,opt,name=options,proto3" json:"options,omitempty"` +} + +func (x *RemoveContainerRequest) Reset() { + *x = RemoveContainerRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_internal_machine_api_pb_docker_proto_msgTypes[5] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *RemoveContainerRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*RemoveContainerRequest) ProtoMessage() {} + +func (x *RemoveContainerRequest) ProtoReflect() protoreflect.Message { + mi := &file_internal_machine_api_pb_docker_proto_msgTypes[5] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use RemoveContainerRequest.ProtoReflect.Descriptor instead. +func (*RemoveContainerRequest) Descriptor() ([]byte, []int) { + return file_internal_machine_api_pb_docker_proto_rawDescGZIP(), []int{5} +} + +func (x *RemoveContainerRequest) GetId() string { + if x != nil { + return x.Id + } + return "" +} + +func (x *RemoveContainerRequest) GetOptions() []byte { + if x != nil { + return x.Options + } + return nil +} + type PullImageRequest struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -221,7 +373,7 @@ type PullImageRequest struct { func (x *PullImageRequest) Reset() { *x = PullImageRequest{} if protoimpl.UnsafeEnabled { - mi := &file_internal_machine_api_pb_docker_proto_msgTypes[3] + mi := &file_internal_machine_api_pb_docker_proto_msgTypes[6] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -234,7 +386,7 @@ func (x *PullImageRequest) String() string { func (*PullImageRequest) ProtoMessage() {} func (x *PullImageRequest) ProtoReflect() protoreflect.Message { - mi := &file_internal_machine_api_pb_docker_proto_msgTypes[3] + mi := &file_internal_machine_api_pb_docker_proto_msgTypes[6] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -247,7 +399,7 @@ func (x *PullImageRequest) ProtoReflect() protoreflect.Message { // Deprecated: Use PullImageRequest.ProtoReflect.Descriptor instead. func (*PullImageRequest) Descriptor() ([]byte, []int) { - return file_internal_machine_api_pb_docker_proto_rawDescGZIP(), []int{3} + return file_internal_machine_api_pb_docker_proto_rawDescGZIP(), []int{6} } func (x *PullImageRequest) GetImage() string { @@ -276,7 +428,7 @@ type JSONMessage struct { func (x *JSONMessage) Reset() { *x = JSONMessage{} if protoimpl.UnsafeEnabled { - mi := &file_internal_machine_api_pb_docker_proto_msgTypes[4] + mi := &file_internal_machine_api_pb_docker_proto_msgTypes[7] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -289,7 +441,7 @@ func (x *JSONMessage) String() string { func (*JSONMessage) ProtoMessage() {} func (x *JSONMessage) ProtoReflect() protoreflect.Message { - mi := &file_internal_machine_api_pb_docker_proto_msgTypes[4] + mi := &file_internal_machine_api_pb_docker_proto_msgTypes[7] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -302,7 +454,7 @@ func (x *JSONMessage) ProtoReflect() protoreflect.Message { // Deprecated: Use JSONMessage.ProtoReflect.Descriptor instead. func (*JSONMessage) Descriptor() ([]byte, []int) { - return file_internal_machine_api_pb_docker_proto_rawDescGZIP(), []int{4} + return file_internal_machine_api_pb_docker_proto_rawDescGZIP(), []int{7} } func (x *JSONMessage) GetMessage() []byte { @@ -337,32 +489,52 @@ var file_internal_machine_api_pb_docker_proto_rawDesc = []byte{ 0x61, 0x72, 0x74, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x02, - 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x22, 0x42, 0x0a, - 0x10, 0x50, 0x75, 0x6c, 0x6c, 0x49, 0x6d, 0x61, 0x67, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, - 0x74, 0x12, 0x14, 0x0a, 0x05, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, - 0x52, 0x05, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, - 0x6e, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, - 0x73, 0x22, 0x27, 0x0a, 0x0b, 0x4a, 0x53, 0x4f, 0x4e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, - 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, - 0x0c, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x32, 0xd4, 0x01, 0x0a, 0x06, 0x44, - 0x6f, 0x63, 0x6b, 0x65, 0x72, 0x12, 0x4c, 0x0a, 0x0f, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x43, - 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x12, 0x1b, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x43, - 0x72, 0x65, 0x61, 0x74, 0x65, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x52, 0x65, - 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1c, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x43, 0x72, 0x65, 0x61, - 0x74, 0x65, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, - 0x6e, 0x73, 0x65, 0x12, 0x44, 0x0a, 0x0e, 0x53, 0x74, 0x61, 0x72, 0x74, 0x43, 0x6f, 0x6e, 0x74, - 0x61, 0x69, 0x6e, 0x65, 0x72, 0x12, 0x1a, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x53, 0x74, 0x61, 0x72, - 0x74, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, - 0x74, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x12, 0x36, 0x0a, 0x09, 0x50, 0x75, 0x6c, - 0x6c, 0x49, 0x6d, 0x61, 0x67, 0x65, 0x12, 0x15, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x50, 0x75, 0x6c, - 0x6c, 0x49, 0x6d, 0x61, 0x67, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x10, 0x2e, - 0x61, 0x70, 0x69, 0x2e, 0x4a, 0x53, 0x4f, 0x4e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x30, - 0x01, 0x42, 0x37, 0x5a, 0x35, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, - 0x70, 0x73, 0x76, 0x69, 0x64, 0x65, 0x72, 0x73, 0x6b, 0x69, 0x2f, 0x75, 0x6e, 0x63, 0x6c, 0x6f, - 0x75, 0x64, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x6d, 0x61, 0x63, 0x68, - 0x69, 0x6e, 0x65, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x33, + 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x22, 0x31, 0x0a, + 0x15, 0x4c, 0x69, 0x73, 0x74, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x73, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x18, 0x0a, 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, + 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, + 0x22, 0x38, 0x0a, 0x16, 0x4c, 0x69, 0x73, 0x74, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, + 0x72, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x1e, 0x0a, 0x0a, 0x63, 0x6f, + 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x73, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0a, + 0x63, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x73, 0x22, 0x42, 0x0a, 0x16, 0x52, 0x65, + 0x6d, 0x6f, 0x76, 0x65, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x52, 0x65, 0x71, + 0x75, 0x65, 0x73, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x02, 0x69, 0x64, 0x12, 0x18, 0x0a, 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, + 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x22, 0x42, + 0x0a, 0x10, 0x50, 0x75, 0x6c, 0x6c, 0x49, 0x6d, 0x61, 0x67, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x05, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x6f, 0x70, 0x74, 0x69, + 0x6f, 0x6e, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, + 0x6e, 0x73, 0x22, 0x27, 0x0a, 0x0b, 0x4a, 0x53, 0x4f, 0x4e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, + 0x65, 0x12, 0x18, 0x0a, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x0c, 0x52, 0x07, 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x32, 0xe7, 0x02, 0x0a, 0x06, + 0x44, 0x6f, 0x63, 0x6b, 0x65, 0x72, 0x12, 0x4c, 0x0a, 0x0f, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, + 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x12, 0x1b, 0x2e, 0x61, 0x70, 0x69, 0x2e, + 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1c, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x43, 0x72, 0x65, + 0x61, 0x74, 0x65, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x44, 0x0a, 0x0e, 0x53, 0x74, 0x61, 0x72, 0x74, 0x43, 0x6f, 0x6e, + 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x12, 0x1a, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x53, 0x74, 0x61, + 0x72, 0x74, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x12, 0x49, 0x0a, 0x0e, 0x4c, 0x69, + 0x73, 0x74, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x73, 0x12, 0x1a, 0x2e, 0x61, + 0x70, 0x69, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, + 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x1b, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x4c, + 0x69, 0x73, 0x74, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x73, 0x52, 0x65, 0x73, + 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x46, 0x0a, 0x0f, 0x52, 0x65, 0x6d, 0x6f, 0x76, 0x65, 0x43, + 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x12, 0x1b, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x52, + 0x65, 0x6d, 0x6f, 0x76, 0x65, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x45, 0x6d, 0x70, 0x74, 0x79, 0x12, 0x36, 0x0a, + 0x09, 0x50, 0x75, 0x6c, 0x6c, 0x49, 0x6d, 0x61, 0x67, 0x65, 0x12, 0x15, 0x2e, 0x61, 0x70, 0x69, + 0x2e, 0x50, 0x75, 0x6c, 0x6c, 0x49, 0x6d, 0x61, 0x67, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, + 0x74, 0x1a, 0x10, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x4a, 0x53, 0x4f, 0x4e, 0x4d, 0x65, 0x73, 0x73, + 0x61, 0x67, 0x65, 0x30, 0x01, 0x42, 0x37, 0x5a, 0x35, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, + 0x63, 0x6f, 0x6d, 0x2f, 0x70, 0x73, 0x76, 0x69, 0x64, 0x65, 0x72, 0x73, 0x6b, 0x69, 0x2f, 0x75, + 0x6e, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, + 0x6d, 0x61, 0x63, 0x68, 0x69, 0x6e, 0x65, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x70, 0x62, 0x62, 0x06, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -377,24 +549,31 @@ func file_internal_machine_api_pb_docker_proto_rawDescGZIP() []byte { return file_internal_machine_api_pb_docker_proto_rawDescData } -var file_internal_machine_api_pb_docker_proto_msgTypes = make([]protoimpl.MessageInfo, 5) +var file_internal_machine_api_pb_docker_proto_msgTypes = make([]protoimpl.MessageInfo, 8) var file_internal_machine_api_pb_docker_proto_goTypes = []any{ (*CreateContainerRequest)(nil), // 0: api.CreateContainerRequest (*CreateContainerResponse)(nil), // 1: api.CreateContainerResponse (*StartContainerRequest)(nil), // 2: api.StartContainerRequest - (*PullImageRequest)(nil), // 3: api.PullImageRequest - (*JSONMessage)(nil), // 4: api.JSONMessage - (*emptypb.Empty)(nil), // 5: google.protobuf.Empty + (*ListContainersRequest)(nil), // 3: api.ListContainersRequest + (*ListContainersResponse)(nil), // 4: api.ListContainersResponse + (*RemoveContainerRequest)(nil), // 5: api.RemoveContainerRequest + (*PullImageRequest)(nil), // 6: api.PullImageRequest + (*JSONMessage)(nil), // 7: api.JSONMessage + (*emptypb.Empty)(nil), // 8: google.protobuf.Empty } var file_internal_machine_api_pb_docker_proto_depIdxs = []int32{ 0, // 0: api.Docker.CreateContainer:input_type -> api.CreateContainerRequest 2, // 1: api.Docker.StartContainer:input_type -> api.StartContainerRequest - 3, // 2: api.Docker.PullImage:input_type -> api.PullImageRequest - 1, // 3: api.Docker.CreateContainer:output_type -> api.CreateContainerResponse - 5, // 4: api.Docker.StartContainer:output_type -> google.protobuf.Empty - 4, // 5: api.Docker.PullImage:output_type -> api.JSONMessage - 3, // [3:6] is the sub-list for method output_type - 0, // [0:3] is the sub-list for method input_type + 3, // 2: api.Docker.ListContainers:input_type -> api.ListContainersRequest + 5, // 3: api.Docker.RemoveContainer:input_type -> api.RemoveContainerRequest + 6, // 4: api.Docker.PullImage:input_type -> api.PullImageRequest + 1, // 5: api.Docker.CreateContainer:output_type -> api.CreateContainerResponse + 8, // 6: api.Docker.StartContainer:output_type -> google.protobuf.Empty + 4, // 7: api.Docker.ListContainers:output_type -> api.ListContainersResponse + 8, // 8: api.Docker.RemoveContainer:output_type -> google.protobuf.Empty + 7, // 9: api.Docker.PullImage:output_type -> api.JSONMessage + 5, // [5:10] is the sub-list for method output_type + 0, // [0:5] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name 0, // [0:0] is the sub-list for extension extendee 0, // [0:0] is the sub-list for field type_name @@ -443,7 +622,7 @@ func file_internal_machine_api_pb_docker_proto_init() { } } file_internal_machine_api_pb_docker_proto_msgTypes[3].Exporter = func(v any, i int) any { - switch v := v.(*PullImageRequest); i { + switch v := v.(*ListContainersRequest); i { case 0: return &v.state case 1: @@ -455,6 +634,42 @@ func file_internal_machine_api_pb_docker_proto_init() { } } file_internal_machine_api_pb_docker_proto_msgTypes[4].Exporter = func(v any, i int) any { + switch v := v.(*ListContainersResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_internal_machine_api_pb_docker_proto_msgTypes[5].Exporter = func(v any, i int) any { + switch v := v.(*RemoveContainerRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_internal_machine_api_pb_docker_proto_msgTypes[6].Exporter = func(v any, i int) any { + switch v := v.(*PullImageRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_internal_machine_api_pb_docker_proto_msgTypes[7].Exporter = func(v any, i int) any { switch v := v.(*JSONMessage); i { case 0: return &v.state @@ -473,7 +688,7 @@ func file_internal_machine_api_pb_docker_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_internal_machine_api_pb_docker_proto_rawDesc, NumEnums: 0, - NumMessages: 5, + NumMessages: 8, NumExtensions: 0, NumServices: 1, }, diff --git a/internal/machine/api/pb/docker.proto b/internal/machine/api/pb/docker.proto index d64eede0..237a2f0b 100644 --- a/internal/machine/api/pb/docker.proto +++ b/internal/machine/api/pb/docker.proto @@ -9,6 +9,8 @@ import "google/protobuf/empty.proto"; service Docker { rpc CreateContainer(CreateContainerRequest) returns (CreateContainerResponse); rpc StartContainer(StartContainerRequest) returns (google.protobuf.Empty); + rpc ListContainers(ListContainersRequest) returns (ListContainersResponse); + rpc RemoveContainer(RemoveContainerRequest) returns (google.protobuf.Empty); rpc PullImage(PullImageRequest) returns (stream JSONMessage); } @@ -35,6 +37,22 @@ message StartContainerRequest { bytes options = 2; } +message ListContainersRequest { + // JSON serialized container.ListOptions. + bytes options = 1; +} + +message ListContainersResponse { + // JSON serialized []container.Summary. + bytes containers = 1; +} + +message RemoveContainerRequest { + string id = 1; + // JSON serialized container.RemoveOptions. + bytes options = 2; +} + message PullImageRequest { string image = 1; // JSON serialized image.PullOptions. diff --git a/internal/machine/api/pb/docker_grpc.pb.go b/internal/machine/api/pb/docker_grpc.pb.go index 0ab9557d..d1cc1c5a 100644 --- a/internal/machine/api/pb/docker_grpc.pb.go +++ b/internal/machine/api/pb/docker_grpc.pb.go @@ -22,6 +22,8 @@ const _ = grpc.SupportPackageIsVersion9 const ( Docker_CreateContainer_FullMethodName = "/api.Docker/CreateContainer" Docker_StartContainer_FullMethodName = "/api.Docker/StartContainer" + Docker_ListContainers_FullMethodName = "/api.Docker/ListContainers" + Docker_RemoveContainer_FullMethodName = "/api.Docker/RemoveContainer" Docker_PullImage_FullMethodName = "/api.Docker/PullImage" ) @@ -31,6 +33,8 @@ const ( type DockerClient interface { CreateContainer(ctx context.Context, in *CreateContainerRequest, opts ...grpc.CallOption) (*CreateContainerResponse, error) StartContainer(ctx context.Context, in *StartContainerRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) + ListContainers(ctx context.Context, in *ListContainersRequest, opts ...grpc.CallOption) (*ListContainersResponse, error) + RemoveContainer(ctx context.Context, in *RemoveContainerRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) PullImage(ctx context.Context, in *PullImageRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[JSONMessage], error) } @@ -62,6 +66,26 @@ func (c *dockerClient) StartContainer(ctx context.Context, in *StartContainerReq return out, nil } +func (c *dockerClient) ListContainers(ctx context.Context, in *ListContainersRequest, opts ...grpc.CallOption) (*ListContainersResponse, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(ListContainersResponse) + err := c.cc.Invoke(ctx, Docker_ListContainers_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *dockerClient) RemoveContainer(ctx context.Context, in *RemoveContainerRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(emptypb.Empty) + err := c.cc.Invoke(ctx, Docker_RemoveContainer_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + func (c *dockerClient) PullImage(ctx context.Context, in *PullImageRequest, opts ...grpc.CallOption) (grpc.ServerStreamingClient[JSONMessage], error) { cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) stream, err := c.cc.NewStream(ctx, &Docker_ServiceDesc.Streams[0], Docker_PullImage_FullMethodName, cOpts...) @@ -87,6 +111,8 @@ type Docker_PullImageClient = grpc.ServerStreamingClient[JSONMessage] type DockerServer interface { CreateContainer(context.Context, *CreateContainerRequest) (*CreateContainerResponse, error) StartContainer(context.Context, *StartContainerRequest) (*emptypb.Empty, error) + ListContainers(context.Context, *ListContainersRequest) (*ListContainersResponse, error) + RemoveContainer(context.Context, *RemoveContainerRequest) (*emptypb.Empty, error) PullImage(*PullImageRequest, grpc.ServerStreamingServer[JSONMessage]) error mustEmbedUnimplementedDockerServer() } @@ -104,6 +130,12 @@ func (UnimplementedDockerServer) CreateContainer(context.Context, *CreateContain func (UnimplementedDockerServer) StartContainer(context.Context, *StartContainerRequest) (*emptypb.Empty, error) { return nil, status.Errorf(codes.Unimplemented, "method StartContainer not implemented") } +func (UnimplementedDockerServer) ListContainers(context.Context, *ListContainersRequest) (*ListContainersResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method ListContainers not implemented") +} +func (UnimplementedDockerServer) RemoveContainer(context.Context, *RemoveContainerRequest) (*emptypb.Empty, error) { + return nil, status.Errorf(codes.Unimplemented, "method RemoveContainer not implemented") +} func (UnimplementedDockerServer) PullImage(*PullImageRequest, grpc.ServerStreamingServer[JSONMessage]) error { return status.Errorf(codes.Unimplemented, "method PullImage not implemented") } @@ -164,6 +196,42 @@ func _Docker_StartContainer_Handler(srv interface{}, ctx context.Context, dec fu return interceptor(ctx, in, info, handler) } +func _Docker_ListContainers_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(ListContainersRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(DockerServer).ListContainers(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: Docker_ListContainers_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(DockerServer).ListContainers(ctx, req.(*ListContainersRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _Docker_RemoveContainer_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(RemoveContainerRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(DockerServer).RemoveContainer(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: Docker_RemoveContainer_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(DockerServer).RemoveContainer(ctx, req.(*RemoveContainerRequest)) + } + return interceptor(ctx, in, info, handler) +} + func _Docker_PullImage_Handler(srv interface{}, stream grpc.ServerStream) error { m := new(PullImageRequest) if err := stream.RecvMsg(m); err != nil { @@ -190,6 +258,14 @@ var Docker_ServiceDesc = grpc.ServiceDesc{ MethodName: "StartContainer", Handler: _Docker_StartContainer_Handler, }, + { + MethodName: "ListContainers", + Handler: _Docker_ListContainers_Handler, + }, + { + MethodName: "RemoveContainer", + Handler: _Docker_RemoveContainer_Handler, + }, }, Streams: []grpc.StreamDesc{ { diff --git a/internal/machine/docker/client.go b/internal/machine/docker/client.go index 344df3ff..a4947f07 100644 --- a/internal/machine/docker/client.go +++ b/internal/machine/docker/client.go @@ -4,8 +4,8 @@ import ( "context" "encoding/json" "fmt" + "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/container" - "github.com/docker/docker/api/types/image" "github.com/docker/docker/api/types/network" "github.com/docker/docker/errdefs" "github.com/docker/docker/pkg/jsonmessage" @@ -90,13 +90,60 @@ func (c *Client) CreateContainer( func (c *Client) StartContainer(ctx context.Context, id string, opts container.StartOptions) error { optsBytes, err := json.Marshal(opts) if err != nil { - return fmt.Errorf("marshal start options: %w", err) + return fmt.Errorf("marshal options: %w", err) } _, err = c.grpcClient.StartContainer(ctx, &pb.StartContainerRequest{ Id: id, Options: optsBytes, }) + if err != nil { + if s, ok := status.FromError(err); ok { + if s.Code() == codes.NotFound { + return errdefs.NotFound(err) + } + } + } + return err +} + +func (c *Client) ListContainers(ctx context.Context, opts container.ListOptions) ([]types.Container, error) { + optsBytes, err := json.Marshal(opts) + if err != nil { + return nil, fmt.Errorf("marshal options: %w", err) + } + + resp, err := c.grpcClient.ListContainers(ctx, &pb.ListContainersRequest{Options: optsBytes}) + if err != nil { + return nil, err + } + + var containers []types.Container + if err = json.Unmarshal(resp.Containers, &containers); err != nil { + return nil, fmt.Errorf("unmarshal containers: %w", err) + } + + return containers, nil +} + +// RemoveContainer stops (kills after grace period) and removes a container with the given ID. +func (c *Client) RemoveContainer(ctx context.Context, id string, opts container.RemoveOptions) error { + optsBytes, err := json.Marshal(opts) + if err != nil { + return fmt.Errorf("marshal options: %w", err) + } + + _, err = c.grpcClient.RemoveContainer(ctx, &pb.RemoveContainerRequest{ + Id: id, + Options: optsBytes, + }) + if err != nil { + if s, ok := status.FromError(err); ok { + if s.Code() == codes.NotFound { + return errdefs.NotFound(err) + } + } + } return err } @@ -105,18 +152,8 @@ type PullImageMessage struct { Err error } -func (c *Client) PullImage( - ctx context.Context, image string, opts image.PullOptions, -) (<-chan PullImageMessage, error) { - optsBytes, err := json.Marshal(opts) - if err != nil { - return nil, fmt.Errorf("marshal pull options: %w", err) - } - - stream, err := c.grpcClient.PullImage(ctx, &pb.PullImageRequest{ - Image: image, - Options: optsBytes, - }) +func (c *Client) PullImage(ctx context.Context, image string) (<-chan PullImageMessage, error) { + stream, err := c.grpcClient.PullImage(ctx, &pb.PullImageRequest{Image: image}) if err != nil { return nil, err } diff --git a/internal/machine/docker/server.go b/internal/machine/docker/server.go index 6807b445..38fa5651 100644 --- a/internal/machine/docker/server.go +++ b/internal/machine/docker/server.go @@ -70,7 +70,7 @@ func (s *Server) StartContainer(ctx context.Context, req *pb.StartContainerReque var opts container.StartOptions if len(req.Options) > 0 { if err := json.Unmarshal(req.Options, &opts); err != nil { - return nil, status.Errorf(codes.InvalidArgument, "unmarshal start options: %v", err) + return nil, status.Errorf(codes.InvalidArgument, "unmarshal options: %v", err) } } @@ -81,13 +81,53 @@ func (s *Server) StartContainer(ctx context.Context, req *pb.StartContainerReque return &emptypb.Empty{}, nil } +func (s *Server) ListContainers(ctx context.Context, req *pb.ListContainersRequest) (*pb.ListContainersResponse, error) { + var opts container.ListOptions + if len(req.Options) > 0 { + if err := json.Unmarshal(req.Options, &opts); err != nil { + return nil, status.Errorf(codes.InvalidArgument, "unmarshal options: %v", err) + } + } + + containers, err := s.client.ContainerList(ctx, opts) + if err != nil { + return nil, status.Errorf(codes.Internal, "list container: %v", err) + } + + containersBytes, err := json.Marshal(containers) + if err != nil { + return nil, status.Errorf(codes.Internal, "marshal containers: %v", err) + } + + return &pb.ListContainersResponse{ + Containers: containersBytes, + }, nil +} + +// RemoveContainer stops (kills after grace period) and removes a container with the given ID. +func (s *Server) RemoveContainer(ctx context.Context, req *pb.RemoveContainerRequest) (*emptypb.Empty, error) { + var opts container.RemoveOptions + if len(req.Options) > 0 { + if err := json.Unmarshal(req.Options, &opts); err != nil { + return nil, status.Errorf(codes.InvalidArgument, "unmarshal options: %v", err) + } + } + + if err := s.client.ContainerRemove(ctx, req.Id, opts); err != nil { + return nil, status.Errorf(codes.Internal, "remove container: %v", err) + } + + return &emptypb.Empty{}, nil +} + func (s *Server) PullImage(req *pb.PullImageRequest, stream grpc.ServerStreamingServer[pb.JSONMessage]) error { ctx := stream.Context() + // TODO: replace with another JSON serializable type (PullOptions.PrivilegeFunc is not serializable). var opts image.PullOptions if len(req.Options) > 0 { if err := json.Unmarshal(req.Options, &opts); err != nil { - return status.Errorf(codes.InvalidArgument, "unmarshal pull options: %v", err) + return status.Errorf(codes.InvalidArgument, "unmarshal options: %v", err) } }