From 5cb765d28665940fb3052ee9224f62989a71db8b Mon Sep 17 00:00:00 2001 From: Pavel Sviderski Date: Fri, 1 Nov 2024 15:04:06 +1000 Subject: [PATCH] add Docker gRPC service to create and start a Docker container on machine --- internal/machine/api/pb/docker.pb.go | 345 ++++++++++++++++++++++ internal/machine/api/pb/docker.proto | 35 +++ internal/machine/api/pb/docker_grpc.pb.go | 160 ++++++++++ internal/machine/docker/client.go | 90 ++++++ internal/machine/docker/server.go | 75 +++++ 5 files changed, 705 insertions(+) create mode 100644 internal/machine/api/pb/docker.pb.go create mode 100644 internal/machine/api/pb/docker.proto create mode 100644 internal/machine/api/pb/docker_grpc.pb.go create mode 100644 internal/machine/docker/client.go create mode 100644 internal/machine/docker/server.go diff --git a/internal/machine/api/pb/docker.pb.go b/internal/machine/api/pb/docker.pb.go new file mode 100644 index 00000000..f996cec5 --- /dev/null +++ b/internal/machine/api/pb/docker.pb.go @@ -0,0 +1,345 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.34.2 +// protoc v5.27.3 +// source: internal/machine/api/pb/docker.proto + +package pb + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + emptypb "google.golang.org/protobuf/types/known/emptypb" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type CreateContainerRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // JSON serialized container.Config. + Config []byte `protobuf:"bytes,1,opt,name=config,proto3" json:"config,omitempty"` + // JSON serialized container.HostConfig. + HostConfig []byte `protobuf:"bytes,2,opt,name=host_config,json=hostConfig,proto3" json:"host_config,omitempty"` + // JSON serialized network.NetworkingConfig. + NetworkConfig []byte `protobuf:"bytes,3,opt,name=network_config,json=networkConfig,proto3" json:"network_config,omitempty"` + // JSON serialized ocispec.Platform. + Platform []byte `protobuf:"bytes,4,opt,name=platform,proto3" json:"platform,omitempty"` + Name string `protobuf:"bytes,5,opt,name=name,proto3" json:"name,omitempty"` +} + +func (x *CreateContainerRequest) Reset() { + *x = CreateContainerRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_internal_machine_api_pb_docker_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *CreateContainerRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*CreateContainerRequest) ProtoMessage() {} + +func (x *CreateContainerRequest) ProtoReflect() protoreflect.Message { + mi := &file_internal_machine_api_pb_docker_proto_msgTypes[0] + 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 CreateContainerRequest.ProtoReflect.Descriptor instead. +func (*CreateContainerRequest) Descriptor() ([]byte, []int) { + return file_internal_machine_api_pb_docker_proto_rawDescGZIP(), []int{0} +} + +func (x *CreateContainerRequest) GetConfig() []byte { + if x != nil { + return x.Config + } + return nil +} + +func (x *CreateContainerRequest) GetHostConfig() []byte { + if x != nil { + return x.HostConfig + } + return nil +} + +func (x *CreateContainerRequest) GetNetworkConfig() []byte { + if x != nil { + return x.NetworkConfig + } + return nil +} + +func (x *CreateContainerRequest) GetPlatform() []byte { + if x != nil { + return x.Platform + } + return nil +} + +func (x *CreateContainerRequest) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +type CreateContainerResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // JSON serialized container.CreateResponse. + Response []byte `protobuf:"bytes,1,opt,name=response,proto3" json:"response,omitempty"` +} + +func (x *CreateContainerResponse) Reset() { + *x = CreateContainerResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_internal_machine_api_pb_docker_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *CreateContainerResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*CreateContainerResponse) ProtoMessage() {} + +func (x *CreateContainerResponse) ProtoReflect() protoreflect.Message { + mi := &file_internal_machine_api_pb_docker_proto_msgTypes[1] + 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 CreateContainerResponse.ProtoReflect.Descriptor instead. +func (*CreateContainerResponse) Descriptor() ([]byte, []int) { + return file_internal_machine_api_pb_docker_proto_rawDescGZIP(), []int{1} +} + +func (x *CreateContainerResponse) GetResponse() []byte { + if x != nil { + return x.Response + } + return nil +} + +type StartContainerRequest 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.StartOptions. + Options []byte `protobuf:"bytes,2,opt,name=options,proto3" json:"options,omitempty"` +} + +func (x *StartContainerRequest) Reset() { + *x = StartContainerRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_internal_machine_api_pb_docker_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *StartContainerRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*StartContainerRequest) ProtoMessage() {} + +func (x *StartContainerRequest) ProtoReflect() protoreflect.Message { + mi := &file_internal_machine_api_pb_docker_proto_msgTypes[2] + 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 StartContainerRequest.ProtoReflect.Descriptor instead. +func (*StartContainerRequest) Descriptor() ([]byte, []int) { + return file_internal_machine_api_pb_docker_proto_rawDescGZIP(), []int{2} +} + +func (x *StartContainerRequest) GetId() string { + if x != nil { + return x.Id + } + return "" +} + +func (x *StartContainerRequest) GetOptions() []byte { + if x != nil { + return x.Options + } + return nil +} + +var File_internal_machine_api_pb_docker_proto protoreflect.FileDescriptor + +var file_internal_machine_api_pb_docker_proto_rawDesc = []byte{ + 0x0a, 0x24, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x6c, 0x2f, 0x6d, 0x61, 0x63, 0x68, 0x69, + 0x6e, 0x65, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x70, 0x62, 0x2f, 0x64, 0x6f, 0x63, 0x6b, 0x65, 0x72, + 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x03, 0x61, 0x70, 0x69, 0x1a, 0x1b, 0x67, 0x6f, 0x6f, + 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x65, 0x6d, 0x70, + 0x74, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xa8, 0x01, 0x0a, 0x16, 0x43, 0x72, 0x65, + 0x61, 0x74, 0x65, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x0c, 0x52, 0x06, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x1f, 0x0a, 0x0b, 0x68, + 0x6f, 0x73, 0x74, 0x5f, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, + 0x52, 0x0a, 0x68, 0x6f, 0x73, 0x74, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x12, 0x25, 0x0a, 0x0e, + 0x6e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x5f, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x18, 0x03, + 0x20, 0x01, 0x28, 0x0c, 0x52, 0x0d, 0x6e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x43, 0x6f, 0x6e, + 0x66, 0x69, 0x67, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d, 0x18, + 0x04, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x08, 0x70, 0x6c, 0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d, 0x12, + 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, + 0x61, 0x6d, 0x65, 0x22, 0x35, 0x0a, 0x17, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x43, 0x6f, 0x6e, + 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x1a, + 0x0a, 0x08, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, + 0x52, 0x08, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x41, 0x0a, 0x15, 0x53, 0x74, + 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, 0x32, 0x9c, 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, 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 ( + file_internal_machine_api_pb_docker_proto_rawDescOnce sync.Once + file_internal_machine_api_pb_docker_proto_rawDescData = file_internal_machine_api_pb_docker_proto_rawDesc +) + +func file_internal_machine_api_pb_docker_proto_rawDescGZIP() []byte { + file_internal_machine_api_pb_docker_proto_rawDescOnce.Do(func() { + file_internal_machine_api_pb_docker_proto_rawDescData = protoimpl.X.CompressGZIP(file_internal_machine_api_pb_docker_proto_rawDescData) + }) + return file_internal_machine_api_pb_docker_proto_rawDescData +} + +var file_internal_machine_api_pb_docker_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +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 + (*emptypb.Empty)(nil), // 3: 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 + 1, // 2: api.Docker.CreateContainer:output_type -> api.CreateContainerResponse + 3, // 3: api.Docker.StartContainer:output_type -> google.protobuf.Empty + 2, // [2:4] is the sub-list for method output_type + 0, // [0:2] 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 +} + +func init() { file_internal_machine_api_pb_docker_proto_init() } +func file_internal_machine_api_pb_docker_proto_init() { + if File_internal_machine_api_pb_docker_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_internal_machine_api_pb_docker_proto_msgTypes[0].Exporter = func(v any, i int) any { + switch v := v.(*CreateContainerRequest); 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[1].Exporter = func(v any, i int) any { + switch v := v.(*CreateContainerResponse); 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[2].Exporter = func(v any, i int) any { + switch v := v.(*StartContainerRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_internal_machine_api_pb_docker_proto_rawDesc, + NumEnums: 0, + NumMessages: 3, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_internal_machine_api_pb_docker_proto_goTypes, + DependencyIndexes: file_internal_machine_api_pb_docker_proto_depIdxs, + MessageInfos: file_internal_machine_api_pb_docker_proto_msgTypes, + }.Build() + File_internal_machine_api_pb_docker_proto = out.File + file_internal_machine_api_pb_docker_proto_rawDesc = nil + file_internal_machine_api_pb_docker_proto_goTypes = nil + file_internal_machine_api_pb_docker_proto_depIdxs = nil +} diff --git a/internal/machine/api/pb/docker.proto b/internal/machine/api/pb/docker.proto new file mode 100644 index 00000000..ad185d82 --- /dev/null +++ b/internal/machine/api/pb/docker.proto @@ -0,0 +1,35 @@ +syntax = "proto3"; + +package api; + +option go_package = "github.com/psviderski/uncloud/internal/machine/api/pb"; + +import "google/protobuf/empty.proto"; + +service Docker { + rpc CreateContainer(CreateContainerRequest) returns (CreateContainerResponse); + rpc StartContainer(StartContainerRequest) returns (google.protobuf.Empty); +} + +message CreateContainerRequest { + // JSON serialized container.Config. + bytes config = 1; + // JSON serialized container.HostConfig. + bytes host_config = 2; + // JSON serialized network.NetworkingConfig. + bytes network_config = 3; + // JSON serialized ocispec.Platform. + bytes platform = 4; + string name = 5; +} + +message CreateContainerResponse { + // JSON serialized container.CreateResponse. + bytes response = 1; +} + +message StartContainerRequest { + string id = 1; + // JSON serialized container.StartOptions. + bytes options = 2; +} diff --git a/internal/machine/api/pb/docker_grpc.pb.go b/internal/machine/api/pb/docker_grpc.pb.go new file mode 100644 index 00000000..181ee500 --- /dev/null +++ b/internal/machine/api/pb/docker_grpc.pb.go @@ -0,0 +1,160 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.5.1 +// - protoc v5.27.3 +// source: internal/machine/api/pb/docker.proto + +package pb + +import ( + context "context" + grpc "google.golang.org/grpc" + codes "google.golang.org/grpc/codes" + status "google.golang.org/grpc/status" + emptypb "google.golang.org/protobuf/types/known/emptypb" +) + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +// Requires gRPC-Go v1.64.0 or later. +const _ = grpc.SupportPackageIsVersion9 + +const ( + Docker_CreateContainer_FullMethodName = "/api.Docker/CreateContainer" + Docker_StartContainer_FullMethodName = "/api.Docker/StartContainer" +) + +// DockerClient is the client API for Docker service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. +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) +} + +type dockerClient struct { + cc grpc.ClientConnInterface +} + +func NewDockerClient(cc grpc.ClientConnInterface) DockerClient { + return &dockerClient{cc} +} + +func (c *dockerClient) CreateContainer(ctx context.Context, in *CreateContainerRequest, opts ...grpc.CallOption) (*CreateContainerResponse, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(CreateContainerResponse) + err := c.cc.Invoke(ctx, Docker_CreateContainer_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *dockerClient) StartContainer(ctx context.Context, in *StartContainerRequest, opts ...grpc.CallOption) (*emptypb.Empty, error) { + cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...) + out := new(emptypb.Empty) + err := c.cc.Invoke(ctx, Docker_StartContainer_FullMethodName, in, out, cOpts...) + if err != nil { + return nil, err + } + return out, nil +} + +// DockerServer is the server API for Docker service. +// All implementations must embed UnimplementedDockerServer +// for forward compatibility. +type DockerServer interface { + CreateContainer(context.Context, *CreateContainerRequest) (*CreateContainerResponse, error) + StartContainer(context.Context, *StartContainerRequest) (*emptypb.Empty, error) + mustEmbedUnimplementedDockerServer() +} + +// UnimplementedDockerServer must be embedded to have +// forward compatible implementations. +// +// NOTE: this should be embedded by value instead of pointer to avoid a nil +// pointer dereference when methods are called. +type UnimplementedDockerServer struct{} + +func (UnimplementedDockerServer) CreateContainer(context.Context, *CreateContainerRequest) (*CreateContainerResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method CreateContainer not implemented") +} +func (UnimplementedDockerServer) StartContainer(context.Context, *StartContainerRequest) (*emptypb.Empty, error) { + return nil, status.Errorf(codes.Unimplemented, "method StartContainer not implemented") +} +func (UnimplementedDockerServer) mustEmbedUnimplementedDockerServer() {} +func (UnimplementedDockerServer) testEmbeddedByValue() {} + +// UnsafeDockerServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to DockerServer will +// result in compilation errors. +type UnsafeDockerServer interface { + mustEmbedUnimplementedDockerServer() +} + +func RegisterDockerServer(s grpc.ServiceRegistrar, srv DockerServer) { + // If the following call pancis, it indicates UnimplementedDockerServer was + // embedded by pointer and is nil. This will cause panics if an + // unimplemented method is ever invoked, so we test this at initialization + // time to prevent it from happening at runtime later due to I/O. + if t, ok := srv.(interface{ testEmbeddedByValue() }); ok { + t.testEmbeddedByValue() + } + s.RegisterService(&Docker_ServiceDesc, srv) +} + +func _Docker_CreateContainer_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(CreateContainerRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(DockerServer).CreateContainer(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: Docker_CreateContainer_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(DockerServer).CreateContainer(ctx, req.(*CreateContainerRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _Docker_StartContainer_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(StartContainerRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(DockerServer).StartContainer(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: Docker_StartContainer_FullMethodName, + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(DockerServer).StartContainer(ctx, req.(*StartContainerRequest)) + } + return interceptor(ctx, in, info, handler) +} + +// Docker_ServiceDesc is the grpc.ServiceDesc for Docker service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var Docker_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "api.Docker", + HandlerType: (*DockerServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "CreateContainer", + Handler: _Docker_CreateContainer_Handler, + }, + { + MethodName: "StartContainer", + Handler: _Docker_StartContainer_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "internal/machine/api/pb/docker.proto", +} diff --git a/internal/machine/docker/client.go b/internal/machine/docker/client.go new file mode 100644 index 00000000..090b2c5d --- /dev/null +++ b/internal/machine/docker/client.go @@ -0,0 +1,90 @@ +package docker + +import ( + "context" + "encoding/json" + "fmt" + "github.com/docker/docker/api/types/container" + "github.com/docker/docker/api/types/network" + ocispec "github.com/opencontainers/image-spec/specs-go/v1" + "google.golang.org/grpc" + "uncloud/internal/machine/api/pb" +) + +// Client is a gRPC client for the Docker service that provides a similar interface to the Docker HTTP client. +type Client struct { + conn *grpc.ClientConn + grpcClient pb.DockerClient +} + +// NewClient creates a new Docker gRPC client with the provided gRPC connection. +func NewClient(conn *grpc.ClientConn) *Client { + return &Client{ + conn: conn, + grpcClient: pb.NewDockerClient(conn), + } +} + +// Close closes the gRPC connection. +func (c *Client) Close() error { + return c.conn.Close() +} + +// CreateContainer creates a new container based on the given configuration. +func (c *Client) CreateContainer( + ctx context.Context, + config *container.Config, + hostConfig *container.HostConfig, + networkingConfig *network.NetworkingConfig, + platform *ocispec.Platform, + name string, +) (container.CreateResponse, error) { + var resp container.CreateResponse + // Serialize configs to JSON. + configBytes, err := json.Marshal(config) + if err != nil { + return resp, fmt.Errorf("marshal container config: %w", err) + } + hostConfigBytes, err := json.Marshal(hostConfig) + if err != nil { + return resp, fmt.Errorf("marshal host config: %w", err) + } + networkingConfigBytes, err := json.Marshal(networkingConfig) + if err != nil { + return resp, fmt.Errorf("marshal networking config: %w", err) + } + platformBytes, err := json.Marshal(platform) + if err != nil { + return resp, fmt.Errorf("marshal platform: %w", err) + } + + grpcResp, err := c.grpcClient.CreateContainer(ctx, &pb.CreateContainerRequest{ + Config: configBytes, + HostConfig: hostConfigBytes, + NetworkConfig: networkingConfigBytes, + Platform: platformBytes, + Name: name, + }) + if err != nil { + return resp, err + } + + if err = json.Unmarshal(grpcResp.Response, &resp); err != nil { + return resp, fmt.Errorf("unmarshal gRPC response: %w", err) + } + return resp, nil +} + +// StartContainer starts a container with the given ID and options. +func (c *Client) StartContainer(ctx context.Context, id string, options container.StartOptions) error { + optionsBytes, err := json.Marshal(options) + if err != nil { + return fmt.Errorf("marshal start options: %w", err) + } + + _, err = c.grpcClient.StartContainer(ctx, &pb.StartContainerRequest{ + Id: id, + Options: optionsBytes, + }) + return err +} diff --git a/internal/machine/docker/server.go b/internal/machine/docker/server.go new file mode 100644 index 00000000..8dc9b517 --- /dev/null +++ b/internal/machine/docker/server.go @@ -0,0 +1,75 @@ +package docker + +import ( + "context" + "encoding/json" + "github.com/docker/docker/api/types/container" + "github.com/docker/docker/api/types/network" + "github.com/docker/docker/client" + ocispec "github.com/opencontainers/image-spec/specs-go/v1" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + "google.golang.org/protobuf/types/known/emptypb" + "uncloud/internal/machine/api/pb" +) + +// Server implements the gRPC Docker service that proxies requests to the Docker daemon. +type Server struct { + pb.UnimplementedDockerServer + client *client.Client +} + +// NewServer creates a new Docker gRPC server with the provided Docker client. +func NewServer(cli *client.Client) *Server { + return &Server{client: cli} +} + +// CreateContainer creates a new container based on the given configuration. +func (s *Server) CreateContainer(ctx context.Context, req *pb.CreateContainerRequest) (*pb.CreateContainerResponse, error) { + var config container.Config + var hostConfig container.HostConfig + var networkConfig network.NetworkingConfig + var platform ocispec.Platform + + // Unmarshal configurations from the request. + if err := json.Unmarshal(req.Config, &config); err != nil { + return nil, status.Errorf(codes.InvalidArgument, "unmarshal container config: %v", err) + } + if err := json.Unmarshal(req.HostConfig, &hostConfig); err != nil { + return nil, status.Errorf(codes.InvalidArgument, "unmarshal host config: %v", err) + } + if err := json.Unmarshal(req.NetworkConfig, &networkConfig); err != nil { + return nil, status.Errorf(codes.InvalidArgument, "unmarshal network config: %v", err) + } + if err := json.Unmarshal(req.Platform, &platform); err != nil { + return nil, status.Errorf(codes.InvalidArgument, "unmarshal platform: %v", err) + } + + resp, err := s.client.ContainerCreate(ctx, &config, &hostConfig, &networkConfig, &platform, req.Name) + if err != nil { + return nil, status.Errorf(codes.Internal, "create container: %v", err) + } + + respBytes, err := json.Marshal(resp) + if err != nil { + return nil, status.Errorf(codes.Internal, "marshal response: %v", err) + } + + return &pb.CreateContainerResponse{Response: respBytes}, nil +} + +// StartContainer starts a container with the given ID and options. +func (s *Server) StartContainer(ctx context.Context, req *pb.StartContainerRequest) (*emptypb.Empty, error) { + var options container.StartOptions + if len(req.Options) > 0 { + if err := json.Unmarshal(req.Options, &options); err != nil { + return nil, status.Errorf(codes.InvalidArgument, "unmarshal start options: %v", err) + } + } + + if err := s.client.ContainerStart(ctx, req.Id, options); err != nil { + return nil, status.Errorf(codes.Internal, "start container: %v", err) + } + + return &emptypb.Empty{}, nil +}