proxy.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package proxy
  2. import (
  3. "context"
  4. "github.com/docker/api/client"
  5. "github.com/docker/api/containers"
  6. v1 "github.com/docker/api/containers/v1"
  7. )
  8. type clientKey struct{}
  9. // WithClient adds the client to the context
  10. func WithClient(ctx context.Context, c *client.Client) (context.Context, error) {
  11. return context.WithValue(ctx, clientKey{}, c), nil
  12. }
  13. // Client returns the client from the context
  14. func Client(ctx context.Context) *client.Client {
  15. c, _ := ctx.Value(clientKey{}).(*client.Client)
  16. return c
  17. }
  18. // NewContainerAPI creates a proxy container server
  19. func NewContainerAPI() v1.ContainersServer {
  20. return &proxyContainerAPI{}
  21. }
  22. type proxyContainerAPI struct{}
  23. func (p *proxyContainerAPI) List(ctx context.Context, _ *v1.ListRequest) (*v1.ListResponse, error) {
  24. client := Client(ctx)
  25. c, err := client.ContainerService().List(ctx)
  26. if err != nil {
  27. return &v1.ListResponse{}, nil
  28. }
  29. response := &v1.ListResponse{
  30. Containers: []*v1.Container{},
  31. }
  32. for _, container := range c {
  33. response.Containers = append(response.Containers, &v1.Container{
  34. Id: container.ID,
  35. Image: container.Image,
  36. })
  37. }
  38. return response, nil
  39. }
  40. func (p *proxyContainerAPI) Create(ctx context.Context, request *v1.CreateRequest) (*v1.CreateResponse, error) {
  41. client := Client(ctx)
  42. err := client.ContainerService().Run(ctx, containers.ContainerConfig{
  43. ID: request.Id,
  44. Image: request.Image,
  45. })
  46. return &v1.CreateResponse{}, err
  47. }
  48. func (p *proxyContainerAPI) Start(_ context.Context, _ *v1.StartRequest) (*v1.StartResponse, error) {
  49. panic("not implemented") // TODO: Implement
  50. }
  51. func (p *proxyContainerAPI) Stop(_ context.Context, _ *v1.StopRequest) (*v1.StopResponse, error) {
  52. panic("not implemented") // TODO: Implement
  53. }
  54. func (p *proxyContainerAPI) Kill(_ context.Context, _ *v1.KillRequest) (*v1.KillResponse, error) {
  55. panic("not implemented") // TODO: Implement
  56. }
  57. func (p *proxyContainerAPI) Delete(_ context.Context, _ *v1.DeleteRequest) (*v1.DeleteResponse, error) {
  58. panic("not implemented") // TODO: Implement
  59. }
  60. func (p *proxyContainerAPI) Update(_ context.Context, _ *v1.UpdateRequest) (*v1.UpdateResponse, error) {
  61. panic("not implemented") // TODO: Implement
  62. }
  63. func (p *proxyContainerAPI) Exec(_ context.Context, _ *v1.ExecRequest) (*v1.ExecResponse, error) {
  64. panic("not implemented") // TODO: Implement
  65. }