2020-05-16 12:13:51 +02:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-06-05 16:28:18 +02:00
|
|
|
"errors"
|
2020-05-16 12:13:51 +02:00
|
|
|
|
|
|
|
"github.com/docker/api/containers"
|
2020-06-05 16:28:18 +02:00
|
|
|
containersv1 "github.com/docker/api/protos/containers/v1"
|
2020-06-06 22:46:35 +02:00
|
|
|
"github.com/docker/api/server/proxy/streams"
|
2020-05-16 12:13:51 +02:00
|
|
|
)
|
|
|
|
|
2020-06-05 16:28:18 +02:00
|
|
|
func portsToGrpc(ports []containers.Port) []*containersv1.Port {
|
|
|
|
var result []*containersv1.Port
|
2020-05-16 12:13:51 +02:00
|
|
|
for _, port := range ports {
|
2020-06-05 16:28:18 +02:00
|
|
|
result = append(result, &containersv1.Port{
|
2020-05-16 12:13:51 +02:00
|
|
|
ContainerPort: port.ContainerPort,
|
|
|
|
HostPort: port.HostPort,
|
|
|
|
HostIp: port.HostIP,
|
|
|
|
Protocol: port.Protocol,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-06-05 16:28:18 +02:00
|
|
|
func (p *proxy) List(ctx context.Context, request *containersv1.ListRequest) (*containersv1.ListResponse, error) {
|
2020-06-08 18:34:32 +02:00
|
|
|
containerList, err := Client(ctx).ContainerService().List(ctx, request.GetAll())
|
2020-05-16 12:13:51 +02:00
|
|
|
if err != nil {
|
2020-06-05 16:28:18 +02:00
|
|
|
return &containersv1.ListResponse{}, err
|
2020-05-16 12:13:51 +02:00
|
|
|
}
|
|
|
|
|
2020-06-05 16:28:18 +02:00
|
|
|
response := &containersv1.ListResponse{
|
|
|
|
Containers: []*containersv1.Container{},
|
2020-05-16 12:13:51 +02:00
|
|
|
}
|
2020-06-08 18:34:32 +02:00
|
|
|
for _, container := range containerList {
|
2020-06-05 16:28:18 +02:00
|
|
|
response.Containers = append(response.Containers, &containersv1.Container{
|
2020-05-16 12:13:51 +02:00
|
|
|
Id: container.ID,
|
|
|
|
Image: container.Image,
|
|
|
|
Command: container.Command,
|
|
|
|
Status: container.Status,
|
|
|
|
CpuTime: container.CPUTime,
|
|
|
|
Labels: container.Labels,
|
|
|
|
MemoryLimit: container.MemoryLimit,
|
|
|
|
MemoryUsage: container.MemoryUsage,
|
|
|
|
PidsCurrent: container.PidsCurrent,
|
|
|
|
PidsLimit: container.PidsLimit,
|
|
|
|
Ports: portsToGrpc(container.Ports),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2020-06-05 16:28:18 +02:00
|
|
|
func (p *proxy) Stop(ctx context.Context, request *containersv1.StopRequest) (*containersv1.StopResponse, error) {
|
2020-05-18 12:02:04 +02:00
|
|
|
timeoutValue := request.GetTimeout()
|
2020-06-08 18:34:32 +02:00
|
|
|
return &containersv1.StopResponse{}, Client(ctx).ContainerService().Stop(ctx, request.Id, &timeoutValue)
|
2020-05-16 12:13:51 +02:00
|
|
|
}
|
|
|
|
|
2020-06-05 16:28:18 +02:00
|
|
|
func (p *proxy) Run(ctx context.Context, request *containersv1.RunRequest) (*containersv1.RunResponse, error) {
|
|
|
|
ports := []containers.Port{}
|
|
|
|
for _, p := range request.GetPorts() {
|
|
|
|
ports = append(ports, containers.Port{
|
|
|
|
ContainerPort: p.ContainerPort,
|
|
|
|
HostIP: p.HostIp,
|
|
|
|
HostPort: p.HostPort,
|
|
|
|
Protocol: p.Protocol,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-08 18:34:32 +02:00
|
|
|
return &containersv1.RunResponse{}, Client(ctx).ContainerService().Run(ctx, containers.ContainerConfig{
|
2020-06-05 16:28:18 +02:00
|
|
|
ID: request.GetId(),
|
|
|
|
Image: request.GetImage(),
|
|
|
|
Labels: request.GetLabels(),
|
|
|
|
Ports: ports,
|
|
|
|
Volumes: request.GetVolumes(),
|
|
|
|
})
|
2020-05-16 12:13:51 +02:00
|
|
|
}
|
|
|
|
|
2020-06-05 16:28:18 +02:00
|
|
|
func (p *proxy) Delete(ctx context.Context, request *containersv1.DeleteRequest) (*containersv1.DeleteResponse, error) {
|
2020-06-08 18:34:32 +02:00
|
|
|
return &containersv1.DeleteResponse{}, Client(ctx).ContainerService().Delete(ctx, request.Id, request.Force)
|
2020-05-16 12:13:51 +02:00
|
|
|
}
|
|
|
|
|
2020-06-05 16:28:18 +02:00
|
|
|
func (p *proxy) Exec(ctx context.Context, request *containersv1.ExecRequest) (*containersv1.ExecResponse, error) {
|
|
|
|
p.mu.Lock()
|
|
|
|
stream, ok := p.streams[request.StreamId]
|
|
|
|
p.mu.Unlock()
|
|
|
|
if !ok {
|
|
|
|
return &containersv1.ExecResponse{}, errors.New("unknown stream id")
|
|
|
|
}
|
|
|
|
|
2020-06-06 22:46:35 +02:00
|
|
|
io := &streams.IO{
|
|
|
|
Stream: stream,
|
|
|
|
}
|
2020-05-16 12:13:51 +02:00
|
|
|
|
2020-06-08 18:34:32 +02:00
|
|
|
return &containersv1.ExecResponse{}, Client(ctx).ContainerService().Exec(ctx, request.GetId(), request.GetCommand(), io, io)
|
2020-05-16 12:13:51 +02:00
|
|
|
}
|
2020-05-18 23:55:17 +02:00
|
|
|
|
2020-06-05 16:28:18 +02:00
|
|
|
func (p *proxy) Logs(request *containersv1.LogsRequest, stream containersv1.Containers_LogsServer) error {
|
2020-06-08 18:34:32 +02:00
|
|
|
return Client(stream.Context()).ContainerService().Logs(stream.Context(), request.GetContainerId(), containers.LogsRequest{
|
2020-05-18 23:55:17 +02:00
|
|
|
Follow: request.Follow,
|
2020-06-06 22:46:35 +02:00
|
|
|
Writer: &streams.Log{
|
|
|
|
Stream: stream,
|
|
|
|
},
|
2020-05-18 23:55:17 +02:00
|
|
|
})
|
|
|
|
}
|