k3s/pkg/kubelet/dockertools/kube_docker_client.go

355 lines
11 KiB
Go
Raw Normal View History

2016-03-25 18:57:14 +00:00
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package dockertools
import (
"bytes"
"encoding/base64"
"encoding/json"
2016-04-04 22:27:20 +00:00
"fmt"
2016-03-25 18:57:14 +00:00
"io"
"io/ioutil"
2016-04-04 22:27:20 +00:00
"time"
2016-03-25 18:57:14 +00:00
dockermessage "github.com/docker/docker/pkg/jsonmessage"
dockerstdcopy "github.com/docker/docker/pkg/stdcopy"
2016-03-25 18:57:14 +00:00
dockerapi "github.com/docker/engine-api/client"
dockertypes "github.com/docker/engine-api/types"
dockerfilters "github.com/docker/engine-api/types/filters"
"golang.org/x/net/context"
)
// kubeDockerClient is a wrapped layer of docker client for kubelet internal use. This layer is added to:
// 1) Redirect stream for exec and attach operations.
// 2) Wrap the context in this layer to make the DockerInterface cleaner.
// 3) Stabilize the DockerInterface. The engine-api is still under active development, the interface
// is not stabilized yet. However, the DockerInterface is used in many files in Kubernetes, we may
// not want to change the interface frequently. With this layer, we can port the engine api to the
// DockerInterface to avoid changing DockerInterface as much as possible.
// (See
// * https://github.com/docker/engine-api/issues/89
// * https://github.com/docker/engine-api/issues/137
// * https://github.com/docker/engine-api/pull/140)
// TODO(random-liu): Swith to new docker interface by refactoring the functions in the old DockerInterface
// one by one.
type kubeDockerClient struct {
client *dockerapi.Client
}
// Make sure that kubeDockerClient implemented the DockerInterface.
var _ DockerInterface = &kubeDockerClient{}
2016-04-21 06:41:23 +00:00
// the default ShmSize to use (in bytes) if not specified.
const defaultShmSize = int64(1024 * 1024 * 64)
2016-03-25 18:57:14 +00:00
// newKubeDockerClient creates an kubeDockerClient from an existing docker client.
func newKubeDockerClient(dockerClient *dockerapi.Client) DockerInterface {
return &kubeDockerClient{
client: dockerClient,
}
}
// getDefaultContext returns the default context, now the default context is
// context.Background()
// TODO(random-liu): Add timeout and timeout handling mechanism.
func getDefaultContext() context.Context {
return context.Background()
}
// convertType converts between different types with the same json format.
func convertType(src interface{}, dst interface{}) error {
data, err := json.Marshal(src)
if err != nil {
return err
}
return json.Unmarshal(data, dst)
}
// convertFilters converts filters to the filter type in engine-api.
func convertFilters(filters map[string][]string) dockerfilters.Args {
args := dockerfilters.NewArgs()
for name, fields := range filters {
for _, field := range fields {
args.Add(name, field)
}
}
return args
}
2016-04-04 08:56:49 +00:00
func (k *kubeDockerClient) ListContainers(options dockertypes.ContainerListOptions) ([]dockertypes.Container, error) {
containers, err := k.client.ContainerList(getDefaultContext(), options)
2016-03-25 18:57:14 +00:00
if err != nil {
return nil, err
}
2016-04-04 08:56:49 +00:00
apiContainers := []dockertypes.Container{}
for _, c := range containers {
apiContainers = append(apiContainers, dockertypes.Container(c))
2016-03-25 18:57:14 +00:00
}
return apiContainers, nil
}
2016-04-04 22:27:20 +00:00
func (d *kubeDockerClient) InspectContainer(id string) (*dockertypes.ContainerJSON, error) {
2016-03-25 18:57:14 +00:00
containerJSON, err := d.client.ContainerInspect(getDefaultContext(), id)
if err != nil {
if dockerapi.IsErrContainerNotFound(err) {
2016-04-04 22:27:20 +00:00
return nil, containerNotFoundError{ID: id}
2016-03-25 18:57:14 +00:00
}
return nil, err
}
2016-04-04 22:27:20 +00:00
return &containerJSON, nil
2016-03-25 18:57:14 +00:00
}
2016-04-14 17:36:13 +00:00
func (d *kubeDockerClient) CreateContainer(opts dockertypes.ContainerCreateConfig) (*dockertypes.ContainerCreateResponse, error) {
2016-04-21 06:41:23 +00:00
// we provide an explicit default shm size as to not depend on docker daemon.
// TODO: evaluate exposing this as a knob in the API
if opts.HostConfig != nil && opts.HostConfig.ShmSize <= 0 {
opts.HostConfig.ShmSize = defaultShmSize
}
2016-04-14 17:36:13 +00:00
createResp, err := d.client.ContainerCreate(getDefaultContext(), opts.Config, opts.HostConfig, opts.NetworkingConfig, opts.Name)
2016-03-25 18:57:14 +00:00
if err != nil {
return nil, err
}
2016-04-14 17:36:13 +00:00
return &createResp, nil
2016-03-25 18:57:14 +00:00
}
func (d *kubeDockerClient) StartContainer(id string) error {
2016-03-25 18:57:14 +00:00
return d.client.ContainerStart(getDefaultContext(), id)
}
// Stopping an already stopped container will not cause an error in engine-api.
func (d *kubeDockerClient) StopContainer(id string, timeout int) error {
return d.client.ContainerStop(getDefaultContext(), id, timeout)
2016-03-25 18:57:14 +00:00
}
func (d *kubeDockerClient) RemoveContainer(id string, opts dockertypes.ContainerRemoveOptions) error {
opts.ContainerID = id
return d.client.ContainerRemove(getDefaultContext(), opts)
2016-03-25 18:57:14 +00:00
}
2016-04-06 14:15:38 +00:00
func (d *kubeDockerClient) InspectImage(image string) (*dockertypes.ImageInspect, error) {
2016-03-25 18:57:14 +00:00
resp, _, err := d.client.ImageInspectWithRaw(getDefaultContext(), image, true)
if err != nil {
if dockerapi.IsErrImageNotFound(err) {
err = imageNotFoundError{ID: image}
2016-03-25 18:57:14 +00:00
}
return nil, err
}
return &resp, nil
2016-03-25 18:57:14 +00:00
}
2016-04-03 15:40:22 +00:00
func (d *kubeDockerClient) ListImages(opts dockertypes.ImageListOptions) ([]dockertypes.Image, error) {
images, err := d.client.ImageList(getDefaultContext(), opts)
2016-03-25 18:57:14 +00:00
if err != nil {
return nil, err
}
return images, nil
2016-03-25 18:57:14 +00:00
}
func base64EncodeAuth(auth dockertypes.AuthConfig) (string, error) {
2016-03-25 18:57:14 +00:00
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(auth); err != nil {
return "", err
}
return base64.URLEncoding.EncodeToString(buf.Bytes()), nil
}
func (d *kubeDockerClient) PullImage(image string, auth dockertypes.AuthConfig, opts dockertypes.ImagePullOptions) error {
// RegistryAuth is the base64 encoded credentials for the registry
2016-03-25 18:57:14 +00:00
base64Auth, err := base64EncodeAuth(auth)
if err != nil {
return err
}
opts.ImageID = image
opts.RegistryAuth = base64Auth
resp, err := d.client.ImagePull(getDefaultContext(), opts, nil)
2016-03-25 18:57:14 +00:00
if err != nil {
return err
}
defer resp.Close()
// TODO(random-liu): Use the image pulling progress information.
decoder := json.NewDecoder(resp)
for {
var msg dockermessage.JSONMessage
err := decoder.Decode(&msg)
if err == io.EOF {
break
}
if err != nil {
return err
}
if msg.Error != nil {
return msg.Error
}
}
return nil
2016-03-25 18:57:14 +00:00
}
func (d *kubeDockerClient) RemoveImage(image string, opts dockertypes.ImageRemoveOptions) ([]dockertypes.ImageDelete, error) {
return d.client.ImageRemove(getDefaultContext(), dockertypes.ImageRemoveOptions{ImageID: image})
2016-03-25 18:57:14 +00:00
}
2016-04-17 20:00:52 +00:00
func (d *kubeDockerClient) Logs(id string, opts dockertypes.ContainerLogsOptions, sopts StreamOptions) error {
opts.ContainerID = id
resp, err := d.client.ContainerLogs(getDefaultContext(), opts)
2016-03-25 18:57:14 +00:00
if err != nil {
return err
}
defer resp.Close()
2016-04-17 20:00:52 +00:00
return d.redirectResponseToOutputStream(sopts.RawTerminal, sopts.OutputStream, sopts.ErrorStream, resp)
2016-03-25 18:57:14 +00:00
}
2016-04-18 04:27:39 +00:00
func (d *kubeDockerClient) Version() (*dockertypes.Version, error) {
2016-03-25 18:57:14 +00:00
resp, err := d.client.ServerVersion(getDefaultContext())
if err != nil {
return nil, err
}
2016-04-18 04:27:39 +00:00
return &resp, nil
2016-03-25 18:57:14 +00:00
}
2016-04-18 04:27:39 +00:00
func (d *kubeDockerClient) Info() (*dockertypes.Info, error) {
2016-03-25 18:57:14 +00:00
resp, err := d.client.Info(getDefaultContext())
if err != nil {
return nil, err
}
2016-04-18 04:27:39 +00:00
return &resp, nil
2016-03-25 18:57:14 +00:00
}
// TODO(random-liu): Add unit test for exec and attach functions, just like what go-dockerclient did.
func (d *kubeDockerClient) CreateExec(id string, opts dockertypes.ExecConfig) (*dockertypes.ContainerExecCreateResponse, error) {
opts.Container = id
resp, err := d.client.ContainerExecCreate(getDefaultContext(), opts)
2016-03-25 18:57:14 +00:00
if err != nil {
return nil, err
}
return &resp, nil
2016-03-25 18:57:14 +00:00
}
func (d *kubeDockerClient) StartExec(startExec string, opts dockertypes.ExecStartCheck, sopts StreamOptions) error {
2016-03-25 18:57:14 +00:00
if opts.Detach {
return d.client.ContainerExecStart(getDefaultContext(), startExec, opts)
2016-03-25 18:57:14 +00:00
}
resp, err := d.client.ContainerExecAttach(getDefaultContext(), startExec, dockertypes.ExecConfig{
Detach: opts.Detach,
Tty: opts.Tty,
})
if err != nil {
return err
}
defer resp.Close()
return d.holdHijackedConnection(sopts.RawTerminal || opts.Tty, sopts.InputStream, sopts.OutputStream, sopts.ErrorStream, resp)
2016-03-25 18:57:14 +00:00
}
func (d *kubeDockerClient) InspectExec(id string) (*dockertypes.ContainerExecInspect, error) {
2016-03-25 18:57:14 +00:00
resp, err := d.client.ContainerExecInspect(getDefaultContext(), id)
if err != nil {
return nil, err
}
return &resp, nil
2016-03-25 18:57:14 +00:00
}
2016-04-17 20:00:52 +00:00
func (d *kubeDockerClient) AttachToContainer(id string, opts dockertypes.ContainerAttachOptions, sopts StreamOptions) error {
opts.ContainerID = id
resp, err := d.client.ContainerAttach(getDefaultContext(), opts)
2016-03-25 18:57:14 +00:00
if err != nil {
return err
}
defer resp.Close()
2016-04-17 20:00:52 +00:00
return d.holdHijackedConnection(sopts.RawTerminal, sopts.InputStream, sopts.OutputStream, sopts.ErrorStream, resp)
2016-03-25 18:57:14 +00:00
}
// redirectResponseToOutputStream redirect the response stream to stdout and stderr. When tty is true, all stream will
// only be redirected to stdout.
func (d *kubeDockerClient) redirectResponseToOutputStream(tty bool, outputStream, errorStream io.Writer, resp io.Reader) error {
if outputStream == nil {
outputStream = ioutil.Discard
}
if errorStream == nil {
errorStream = ioutil.Discard
}
var err error
if tty {
_, err = io.Copy(outputStream, resp)
} else {
_, err = dockerstdcopy.StdCopy(outputStream, errorStream, resp)
2016-03-25 18:57:14 +00:00
}
return err
}
// holdHijackedConnection hold the HijackedResponse, redirect the inputStream to the connection, and redirect the response
// stream to stdout and stderr. NOTE: If needed, we could also add context in this function.
func (d *kubeDockerClient) holdHijackedConnection(tty bool, inputStream io.Reader, outputStream, errorStream io.Writer, resp dockertypes.HijackedResponse) error {
receiveStdout := make(chan error)
if outputStream != nil || errorStream != nil {
go func() {
receiveStdout <- d.redirectResponseToOutputStream(tty, outputStream, errorStream, resp.Reader)
}()
}
stdinDone := make(chan struct{})
go func() {
if inputStream != nil {
io.Copy(resp.Conn, inputStream)
}
resp.CloseWrite()
close(stdinDone)
}()
select {
case err := <-receiveStdout:
return err
case <-stdinDone:
if outputStream != nil || errorStream != nil {
return <-receiveStdout
}
}
return nil
}
2016-04-04 22:27:20 +00:00
// parseDockerTimestamp parses the timestamp returned by DockerInterface from string to time.Time
func parseDockerTimestamp(s string) (time.Time, error) {
// Timestamp returned by Docker is in time.RFC3339Nano format.
return time.Parse(time.RFC3339Nano, s)
}
// StreamOptions are the options used to configure the stream redirection
type StreamOptions struct {
RawTerminal bool
InputStream io.Reader
OutputStream io.Writer
ErrorStream io.Writer
}
2016-04-04 22:27:20 +00:00
// containerNotFoundError is the error returned by InspectContainer when container not found. We
// add this error type for testability. We don't use the original error returned by engine-api
// because dockertypes.containerNotFoundError is private, we can't create and inject it in our test.
type containerNotFoundError struct {
ID string
}
func (e containerNotFoundError) Error() string {
return fmt.Sprintf("Error: No such container: %s", e.ID)
}
// imageNotFoundError is the error returned by InspectImage when image not found.
type imageNotFoundError struct {
ID string
}
func (e imageNotFoundError) Error() string {
return fmt.Sprintf("Error: No such image: %s", e.ID)
}