client.go 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package libdocker
  14. import (
  15. "time"
  16. dockertypes "github.com/docker/docker/api/types"
  17. dockercontainer "github.com/docker/docker/api/types/container"
  18. dockerimagetypes "github.com/docker/docker/api/types/image"
  19. dockerapi "github.com/docker/docker/client"
  20. "k8s.io/klog"
  21. )
  22. const (
  23. // https://docs.docker.com/engine/reference/api/docker_remote_api/
  24. // docker version should be at least 1.13.1
  25. MinimumDockerAPIVersion = "1.26.0"
  26. // Status of a container returned by ListContainers.
  27. StatusRunningPrefix = "Up"
  28. StatusCreatedPrefix = "Created"
  29. StatusExitedPrefix = "Exited"
  30. // Fake docker endpoint
  31. FakeDockerEndpoint = "fake://"
  32. )
  33. // Interface is an abstract interface for testability. It abstracts the interface of docker client.
  34. type Interface interface {
  35. ListContainers(options dockertypes.ContainerListOptions) ([]dockertypes.Container, error)
  36. InspectContainer(id string) (*dockertypes.ContainerJSON, error)
  37. InspectContainerWithSize(id string) (*dockertypes.ContainerJSON, error)
  38. CreateContainer(dockertypes.ContainerCreateConfig) (*dockercontainer.ContainerCreateCreatedBody, error)
  39. StartContainer(id string) error
  40. StopContainer(id string, timeout time.Duration) error
  41. UpdateContainerResources(id string, updateConfig dockercontainer.UpdateConfig) error
  42. RemoveContainer(id string, opts dockertypes.ContainerRemoveOptions) error
  43. InspectImageByRef(imageRef string) (*dockertypes.ImageInspect, error)
  44. InspectImageByID(imageID string) (*dockertypes.ImageInspect, error)
  45. ListImages(opts dockertypes.ImageListOptions) ([]dockertypes.ImageSummary, error)
  46. PullImage(image string, auth dockertypes.AuthConfig, opts dockertypes.ImagePullOptions) error
  47. RemoveImage(image string, opts dockertypes.ImageRemoveOptions) ([]dockertypes.ImageDeleteResponseItem, error)
  48. ImageHistory(id string) ([]dockerimagetypes.HistoryResponseItem, error)
  49. Logs(string, dockertypes.ContainerLogsOptions, StreamOptions) error
  50. Version() (*dockertypes.Version, error)
  51. Info() (*dockertypes.Info, error)
  52. CreateExec(string, dockertypes.ExecConfig) (*dockertypes.IDResponse, error)
  53. StartExec(string, dockertypes.ExecStartCheck, StreamOptions) error
  54. InspectExec(id string) (*dockertypes.ContainerExecInspect, error)
  55. AttachToContainer(string, dockertypes.ContainerAttachOptions, StreamOptions) error
  56. ResizeContainerTTY(id string, height, width uint) error
  57. ResizeExecTTY(id string, height, width uint) error
  58. GetContainerStats(id string) (*dockertypes.StatsJSON, error)
  59. }
  60. // Get a *dockerapi.Client, either using the endpoint passed in, or using
  61. // DOCKER_HOST, DOCKER_TLS_VERIFY, and DOCKER_CERT path per their spec
  62. func getDockerClient(dockerEndpoint string) (*dockerapi.Client, error) {
  63. if len(dockerEndpoint) > 0 {
  64. klog.Infof("Connecting to docker on %s", dockerEndpoint)
  65. return dockerapi.NewClient(dockerEndpoint, "", nil, nil)
  66. }
  67. return dockerapi.NewClientWithOpts(dockerapi.FromEnv)
  68. }
  69. // ConnectToDockerOrDie creates docker client connecting to docker daemon.
  70. // If the endpoint passed in is "fake://", a fake docker client
  71. // will be returned. The program exits if error occurs. The requestTimeout
  72. // is the timeout for docker requests. If timeout is exceeded, the request
  73. // will be cancelled and throw out an error. If requestTimeout is 0, a default
  74. // value will be applied.
  75. func ConnectToDockerOrDie(dockerEndpoint string, requestTimeout, imagePullProgressDeadline time.Duration) Interface {
  76. client, err := getDockerClient(dockerEndpoint)
  77. if err != nil {
  78. klog.Fatalf("Couldn't connect to docker: %v", err)
  79. }
  80. klog.Infof("Start docker client with request timeout=%v", requestTimeout)
  81. return newKubeDockerClient(client, requestTimeout, imagePullProgressDeadline)
  82. }