-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cluster up: use docker engine-api client #14729
Merged
openshift-bot
merged 1 commit into
openshift:master
from
csrwng:clusterup_docker_engine
Jun 22, 2017
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,280 @@ | ||
package dockerhelper | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"time" | ||
|
||
"golang.org/x/net/context" | ||
|
||
"github.com/docker/docker/pkg/stdcopy" | ||
"github.com/docker/engine-api/client" | ||
"github.com/docker/engine-api/types" | ||
"github.com/docker/engine-api/types/container" | ||
"github.com/docker/engine-api/types/network" | ||
) | ||
|
||
const ( | ||
// defaultDockerOpTimeout is the default timeout of short running docker operations. | ||
defaultDockerOpTimeout = 10 * time.Minute | ||
) | ||
|
||
// NewClient creates an instance of the client Interface, given a docker engine | ||
// client | ||
func NewClient(endpoint string, client *client.Client) Interface { | ||
return &dockerClient{ | ||
endpoint: endpoint, | ||
client: client, | ||
} | ||
} | ||
|
||
type dockerClient struct { | ||
endpoint string | ||
client *client.Client | ||
} | ||
|
||
type operationTimeout struct { | ||
err error | ||
} | ||
|
||
func (e operationTimeout) Error() string { | ||
return fmt.Sprintf("operation timeout: %v", e.err) | ||
} | ||
|
||
func (c *dockerClient) Endpoint() string { | ||
return c.endpoint | ||
} | ||
|
||
func (c *dockerClient) ServerVersion() (*types.Version, error) { | ||
ctx, cancel := defaultContext() | ||
defer cancel() | ||
version, err := c.client.ServerVersion(ctx) | ||
if ctxErr := contextError(ctx); ctxErr != nil { | ||
return nil, ctxErr | ||
} | ||
return &version, err | ||
} | ||
|
||
func (c *dockerClient) Info() (*types.Info, error) { | ||
ctx, cancel := defaultContext() | ||
defer cancel() | ||
info, err := c.client.Info(ctx) | ||
if ctxErr := contextError(ctx); ctxErr != nil { | ||
return nil, ctxErr | ||
} | ||
return &info, err | ||
} | ||
|
||
func (c *dockerClient) ContainerList(options types.ContainerListOptions) ([]types.Container, error) { | ||
ctx, cancel := defaultContext() | ||
defer cancel() | ||
containers, err := c.client.ContainerList(ctx, options) | ||
if ctxErr := contextError(ctx); ctxErr != nil { | ||
return nil, ctxErr | ||
} | ||
return containers, err | ||
} | ||
|
||
func (c *dockerClient) ContainerCreate(config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, name string) (*types.ContainerCreateResponse, error) { | ||
ctx, cancel := defaultContext() | ||
defer cancel() | ||
response, err := c.client.ContainerCreate(ctx, config, hostConfig, networkingConfig, name) | ||
if ctxErr := contextError(ctx); ctxErr != nil { | ||
return nil, ctxErr | ||
} | ||
return &response, err | ||
} | ||
|
||
func (c *dockerClient) ContainerInspect(container string) (*types.ContainerJSON, error) { | ||
ctx, cancel := defaultContext() | ||
defer cancel() | ||
response, err := c.client.ContainerInspect(ctx, container) | ||
if ctxErr := contextError(ctx); ctxErr != nil { | ||
return nil, ctxErr | ||
} | ||
return &response, err | ||
} | ||
|
||
func (c *dockerClient) ContainerRemove(container string, options types.ContainerRemoveOptions) error { | ||
ctx, cancel := defaultContext() | ||
defer cancel() | ||
err := c.client.ContainerRemove(ctx, container, options) | ||
if ctxErr := contextError(ctx); ctxErr != nil { | ||
return ctxErr | ||
} | ||
return err | ||
} | ||
|
||
func (c *dockerClient) ContainerLogs(container string, options types.ContainerLogsOptions, stdOut, stdErr io.Writer) error { | ||
ctx, cancel := defaultContext() | ||
defer cancel() | ||
response, err := c.client.ContainerLogs(ctx, container, options) | ||
if ctxErr := contextError(ctx); ctxErr != nil { | ||
return ctxErr | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
defer response.Close() | ||
return redirectResponseToOutputStream(stdOut, stdErr, response) | ||
} | ||
|
||
func (c *dockerClient) ContainerStart(container string) error { | ||
ctx, cancel := defaultContext() | ||
defer cancel() | ||
err := c.client.ContainerStart(ctx, container) | ||
if ctxErr := contextError(ctx); ctxErr != nil { | ||
return ctxErr | ||
} | ||
return err | ||
} | ||
|
||
func (c *dockerClient) ContainerStop(container string, timeout int) error { | ||
ctx, cancel := defaultContext() | ||
defer cancel() | ||
err := c.client.ContainerStop(ctx, container, timeout) | ||
if ctxErr := contextError(ctx); ctxErr != nil { | ||
return ctxErr | ||
} | ||
return err | ||
} | ||
|
||
func (c *dockerClient) ContainerWait(container string) (int, error) { | ||
ctx, cancel := defaultContext() | ||
defer cancel() | ||
rc, err := c.client.ContainerWait(ctx, container) | ||
if ctxErr := contextError(ctx); ctxErr != nil { | ||
return 0, ctxErr | ||
} | ||
return rc, err | ||
} | ||
|
||
func (c *dockerClient) CopyToContainer(container string, dest string, src io.Reader, options types.CopyToContainerOptions) error { | ||
return c.client.CopyToContainer(context.Background(), container, dest, src, options) | ||
} | ||
|
||
func (c *dockerClient) CopyFromContainer(container string, src string) (io.ReadCloser, error) { | ||
response, _, err := c.client.CopyFromContainer(context.Background(), container, src) | ||
return response, err | ||
} | ||
|
||
func (c *dockerClient) ContainerExecCreate(container string, config types.ExecConfig) (*types.ContainerExecCreateResponse, error) { | ||
ctx, cancel := defaultContext() | ||
defer cancel() | ||
response, err := c.client.ContainerExecCreate(ctx, container, config) | ||
if ctxErr := contextError(ctx); ctxErr != nil { | ||
return nil, ctxErr | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &response, err | ||
} | ||
|
||
func (c *dockerClient) ContainerExecAttach(execID string, stdIn io.Reader, stdOut, stdErr io.Writer) error { | ||
ctx, cancel := defaultContext() | ||
defer cancel() | ||
response, err := c.client.ContainerExecAttach(ctx, execID, types.ExecConfig{ | ||
AttachStdin: stdIn != nil, | ||
AttachStdout: true, | ||
AttachStderr: true, | ||
}) | ||
if ctxErr := contextError(ctx); ctxErr != nil { | ||
return ctxErr | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
defer response.Close() | ||
return holdHijackedConnection(stdIn, stdOut, stdErr, response) | ||
} | ||
|
||
func (c *dockerClient) ContainerExecInspect(execID string) (*types.ContainerExecInspect, error) { | ||
ctx, cancel := defaultContext() | ||
defer cancel() | ||
response, err := c.client.ContainerExecInspect(ctx, execID) | ||
if ctxErr := contextError(ctx); ctxErr != nil { | ||
return nil, ctxErr | ||
} | ||
return &response, err | ||
} | ||
|
||
func (c *dockerClient) ImageInspectWithRaw(imageID string, getSize bool) (*types.ImageInspect, []byte, error) { | ||
ctx, cancel := defaultContext() | ||
defer cancel() | ||
image, raw, err := c.client.ImageInspectWithRaw(ctx, imageID, getSize) | ||
if ctxErr := contextError(ctx); ctxErr != nil { | ||
return nil, nil, ctxErr | ||
} | ||
return &image, raw, err | ||
} | ||
|
||
func (c *dockerClient) ImagePull(ref string, options types.ImagePullOptions, writer io.Writer) error { | ||
ctx := context.Background() | ||
response, err := c.client.ImagePull(ctx, ref, options) | ||
if err != nil { | ||
return err | ||
} | ||
defer response.Close() | ||
_, err = io.Copy(writer, response) | ||
return err | ||
} | ||
|
||
func defaultContext() (context.Context, context.CancelFunc) { | ||
return context.WithTimeout(context.Background(), defaultDockerOpTimeout) | ||
} | ||
|
||
func contextError(ctx context.Context) error { | ||
if ctx.Err() == context.DeadlineExceeded { | ||
return operationTimeout{err: ctx.Err()} | ||
} | ||
return ctx.Err() | ||
} | ||
|
||
// redirectResponseToOutputStream redirect the response stream to stdout and stderr. | ||
func redirectResponseToOutputStream(outputStream, errorStream io.Writer, resp io.Reader) error { | ||
if outputStream == nil { | ||
outputStream = ioutil.Discard | ||
} | ||
if errorStream == nil { | ||
errorStream = ioutil.Discard | ||
} | ||
_, err := stdcopy.StdCopy(outputStream, errorStream, resp) | ||
return err | ||
} | ||
|
||
// holdHijackedConnection holds the HijackedResponse, redirects the inputStream to the connection, and redirects the response | ||
// stream to stdout and stderr. | ||
func holdHijackedConnection(inputStream io.Reader, outputStream, errorStream io.Writer, resp types.HijackedResponse) error { | ||
receiveStdout := make(chan error) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. must be buffered ( |
||
if outputStream != nil || errorStream != nil { | ||
go func() { | ||
receiveStdout <- redirectResponseToOutputStream(outputStream, errorStream, resp.Reader) | ||
}() | ||
} | ||
|
||
sendStdin := make(chan error, 1) | ||
go func() { | ||
defer resp.CloseWrite() | ||
if inputStream != nil { | ||
_, err := io.Copy(resp.Conn, inputStream) | ||
sendStdin <- err | ||
return | ||
} | ||
sendStdin <- nil | ||
}() | ||
|
||
select { | ||
case err := <-receiveStdout: | ||
return err | ||
case sendErr := <-sendStdin: | ||
if sendErr != nil { | ||
return sendErr | ||
} | ||
if outputStream != nil || errorStream != nil { | ||
return <-receiveStdout | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
currently appears to be unused - are you intending to use it in the future?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's used here https://github.com/openshift/origin/pull/14729/files#diff-11bba6ee4c8e50647af674cb95779e73R202
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the link didn't work for me - I can see it's used in Endpoint(), but I can't see Endpoint() being used anywhere...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line 202 of dockerhelper/helper.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, thanks.