mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 19:13:34 +00:00
add Docker gRPC service to create and start a Docker container on machine
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user