Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
de5f4070
Commit
de5f4070
authored
Apr 17, 2016
by
Random-Liu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor CreateExec, StartExec and InspectExec.
parent
934d7ebb
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
43 additions
and
57 deletions
+43
-57
docker.go
pkg/kubelet/dockertools/docker.go
+3
-3
exec.go
pkg/kubelet/dockertools/exec.go
+5
-8
fake_docker_client.go
pkg/kubelet/dockertools/fake_docker_client.go
+5
-5
instrumented_docker.go
pkg/kubelet/dockertools/instrumented_docker.go
+5
-5
kube_docker_client.go
pkg/kubelet/dockertools/kube_docker_client.go
+18
-27
manager.go
pkg/kubelet/dockertools/manager.go
+6
-8
manager_test.go
pkg/kubelet/dockertools/manager_test.go
+1
-1
No files found.
pkg/kubelet/dockertools/docker.go
View file @
de5f4070
...
...
@@ -71,9 +71,9 @@ type DockerInterface interface {
Logs
(
opts
docker
.
LogsOptions
)
error
Version
()
(
*
docker
.
Env
,
error
)
Info
()
(
*
docker
.
Env
,
error
)
CreateExec
(
docker
.
CreateExecOptions
)
(
*
docker
.
Exec
,
error
)
StartExec
(
string
,
docker
.
StartExec
Options
)
error
InspectExec
(
id
string
)
(
*
docker
.
ExecInspect
,
error
)
CreateExec
(
string
,
dockertypes
.
ExecConfig
)
(
*
dockertypes
.
ContainerExecCreateResponse
,
error
)
StartExec
(
string
,
docker
types
.
ExecStartCheck
,
Stream
Options
)
error
InspectExec
(
id
string
)
(
*
docker
types
.
Container
ExecInspect
,
error
)
AttachToContainer
(
opts
docker
.
AttachToContainerOptions
)
error
}
...
...
pkg/kubelet/dockertools/exec.go
View file @
de5f4070
...
...
@@ -24,7 +24,6 @@ import (
"time"
dockertypes
"github.com/docker/engine-api/types"
docker
"github.com/fsouza/go-dockerclient"
"github.com/golang/glog"
kubecontainer
"k8s.io/kubernetes/pkg/kubelet/container"
)
...
...
@@ -100,27 +99,25 @@ func (*NsenterExecHandler) ExecInContainer(client DockerInterface, container *do
type
NativeExecHandler
struct
{}
func
(
*
NativeExecHandler
)
ExecInContainer
(
client
DockerInterface
,
container
*
dockertypes
.
ContainerJSON
,
cmd
[]
string
,
stdin
io
.
Reader
,
stdout
,
stderr
io
.
WriteCloser
,
tty
bool
)
error
{
createOpts
:=
docker
.
CreateExecOptions
{
Container
:
container
.
ID
,
createOpts
:=
dockertypes
.
ExecConfig
{
Cmd
:
cmd
,
AttachStdin
:
stdin
!=
nil
,
AttachStdout
:
stdout
!=
nil
,
AttachStderr
:
stderr
!=
nil
,
Tty
:
tty
,
}
execObj
,
err
:=
client
.
CreateExec
(
createOpts
)
execObj
,
err
:=
client
.
CreateExec
(
c
ontainer
.
ID
,
c
reateOpts
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to exec in container - Exec setup failed - %v"
,
err
)
}
startOpts
:=
docker
.
StartExecOptions
{
Detach
:
false
,
startOpts
:=
docker
types
.
ExecStartCheck
{
Detach
:
false
,
Tty
:
tty
}
streamOpts
:=
StreamOptions
{
InputStream
:
stdin
,
OutputStream
:
stdout
,
ErrorStream
:
stderr
,
Tty
:
tty
,
RawTerminal
:
tty
,
}
err
=
client
.
StartExec
(
execObj
.
ID
,
startOpts
)
err
=
client
.
StartExec
(
execObj
.
ID
,
startOpts
,
streamOpts
)
if
err
!=
nil
{
return
err
}
...
...
pkg/kubelet/dockertools/fake_docker_client.go
View file @
de5f4070
...
...
@@ -52,7 +52,7 @@ type FakeDockerClient struct {
RemovedImages
sets
.
String
VersionInfo
docker
.
Env
Information
docker
.
Env
ExecInspect
*
docker
.
ExecInspect
ExecInspect
*
docker
types
.
Container
ExecInspect
execCmd
[]
string
EnableSleep
bool
}
...
...
@@ -447,15 +447,15 @@ func (f *FakeDockerClient) Info() (*docker.Env, error) {
return
&
f
.
Information
,
nil
}
func
(
f
*
FakeDockerClient
)
CreateExec
(
opts
docker
.
CreateExecOptions
)
(
*
docker
.
Exec
,
error
)
{
func
(
f
*
FakeDockerClient
)
CreateExec
(
id
string
,
opts
dockertypes
.
ExecConfig
)
(
*
dockertypes
.
ContainerExecCreateResponse
,
error
)
{
f
.
Lock
()
defer
f
.
Unlock
()
f
.
execCmd
=
opts
.
Cmd
f
.
called
=
append
(
f
.
called
,
"create_exec"
)
return
&
docker
.
Exec
{
ID
:
"12345678"
},
nil
return
&
docker
types
.
ContainerExecCreateResponse
{
ID
:
"12345678"
},
nil
}
func
(
f
*
FakeDockerClient
)
StartExec
(
_
string
,
_
docker
.
StartExec
Options
)
error
{
func
(
f
*
FakeDockerClient
)
StartExec
(
startExec
string
,
opts
dockertypes
.
ExecStartCheck
,
sopts
Stream
Options
)
error
{
f
.
Lock
()
defer
f
.
Unlock
()
f
.
called
=
append
(
f
.
called
,
"start_exec"
)
...
...
@@ -469,7 +469,7 @@ func (f *FakeDockerClient) AttachToContainer(opts docker.AttachToContainerOption
return
nil
}
func
(
f
*
FakeDockerClient
)
InspectExec
(
id
string
)
(
*
docker
.
ExecInspect
,
error
)
{
func
(
f
*
FakeDockerClient
)
InspectExec
(
id
string
)
(
*
docker
types
.
Container
ExecInspect
,
error
)
{
return
f
.
ExecInspect
,
f
.
popError
(
"inspect_exec"
)
}
...
...
pkg/kubelet/dockertools/instrumented_docker.go
View file @
de5f4070
...
...
@@ -166,25 +166,25 @@ func (in instrumentedDockerInterface) Info() (*docker.Env, error) {
return
out
,
err
}
func
(
in
instrumentedDockerInterface
)
CreateExec
(
opts
docker
.
CreateExecOptions
)
(
*
docker
.
Exec
,
error
)
{
func
(
in
instrumentedDockerInterface
)
CreateExec
(
id
string
,
opts
dockertypes
.
ExecConfig
)
(
*
dockertypes
.
ContainerExecCreateResponse
,
error
)
{
const
operation
=
"create_exec"
defer
recordOperation
(
operation
,
time
.
Now
())
out
,
err
:=
in
.
client
.
CreateExec
(
opts
)
out
,
err
:=
in
.
client
.
CreateExec
(
id
,
opts
)
recordError
(
operation
,
err
)
return
out
,
err
}
func
(
in
instrumentedDockerInterface
)
StartExec
(
startExec
string
,
opts
docker
.
StartExec
Options
)
error
{
func
(
in
instrumentedDockerInterface
)
StartExec
(
startExec
string
,
opts
docker
types
.
ExecStartCheck
,
sopts
Stream
Options
)
error
{
const
operation
=
"start_exec"
defer
recordOperation
(
operation
,
time
.
Now
())
err
:=
in
.
client
.
StartExec
(
startExec
,
opts
)
err
:=
in
.
client
.
StartExec
(
startExec
,
opts
,
sopts
)
recordError
(
operation
,
err
)
return
err
}
func
(
in
instrumentedDockerInterface
)
InspectExec
(
id
string
)
(
*
docker
.
ExecInspect
,
error
)
{
func
(
in
instrumentedDockerInterface
)
InspectExec
(
id
string
)
(
*
docker
types
.
Container
ExecInspect
,
error
)
{
const
operation
=
"inspect_exec"
defer
recordOperation
(
operation
,
time
.
Now
())
...
...
pkg/kubelet/dockertools/kube_docker_client.go
View file @
de5f4070
...
...
@@ -243,28 +243,19 @@ func (d *kubeDockerClient) Info() (*docker.Env, error) {
return
convertEnv
(
resp
)
}
func
(
d
*
kubeDockerClient
)
CreateExec
(
opts
docker
.
CreateExecOptions
)
(
*
docker
.
Exec
,
error
)
{
cfg
:=
dockertypes
.
ExecConfig
{}
if
err
:=
convertType
(
&
opts
,
&
cfg
);
err
!=
nil
{
return
nil
,
err
}
resp
,
err
:=
d
.
client
.
ContainerExecCreate
(
getDefaultContext
(),
cfg
)
// 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
)
if
err
!=
nil
{
return
nil
,
err
}
exec
:=
&
docker
.
Exec
{}
if
err
:=
convertType
(
&
resp
,
exec
);
err
!=
nil
{
return
nil
,
err
}
return
exec
,
nil
return
&
resp
,
nil
}
func
(
d
*
kubeDockerClient
)
StartExec
(
startExec
string
,
opts
docker
.
StartExec
Options
)
error
{
func
(
d
*
kubeDockerClient
)
StartExec
(
startExec
string
,
opts
docker
types
.
ExecStartCheck
,
sopts
Stream
Options
)
error
{
if
opts
.
Detach
{
return
d
.
client
.
ContainerExecStart
(
getDefaultContext
(),
startExec
,
dockertypes
.
ExecStartCheck
{
Detach
:
opts
.
Detach
,
Tty
:
opts
.
Tty
,
})
return
d
.
client
.
ContainerExecStart
(
getDefaultContext
(),
startExec
,
opts
)
}
resp
,
err
:=
d
.
client
.
ContainerExecAttach
(
getDefaultContext
(),
startExec
,
dockertypes
.
ExecConfig
{
Detach
:
opts
.
Detach
,
...
...
@@ -274,23 +265,15 @@ func (d *kubeDockerClient) StartExec(startExec string, opts docker.StartExecOpti
return
err
}
defer
resp
.
Close
()
if
opts
.
Success
!=
nil
{
opts
.
Success
<-
struct
{}{}
<-
opts
.
Success
}
return
d
.
holdHijackedConnection
(
opts
.
RawTerminal
||
opts
.
Tty
,
opts
.
InputStream
,
opts
.
OutputStream
,
opts
.
ErrorStream
,
resp
)
return
d
.
holdHijackedConnection
(
sopts
.
RawTerminal
||
opts
.
Tty
,
sopts
.
InputStream
,
sopts
.
OutputStream
,
sopts
.
ErrorStream
,
resp
)
}
func
(
d
*
kubeDockerClient
)
InspectExec
(
id
string
)
(
*
docker
.
ExecInspect
,
error
)
{
func
(
d
*
kubeDockerClient
)
InspectExec
(
id
string
)
(
*
docker
types
.
Container
ExecInspect
,
error
)
{
resp
,
err
:=
d
.
client
.
ContainerExecInspect
(
getDefaultContext
(),
id
)
if
err
!=
nil
{
return
nil
,
err
}
exec
:=
&
docker
.
ExecInspect
{}
if
err
:=
convertType
(
&
resp
,
exec
);
err
!=
nil
{
return
nil
,
err
}
return
exec
,
nil
return
&
resp
,
nil
}
func
(
d
*
kubeDockerClient
)
AttachToContainer
(
opts
docker
.
AttachToContainerOptions
)
error
{
...
...
@@ -367,6 +350,14 @@ func parseDockerTimestamp(s string) (time.Time, error) {
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
}
// 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.
...
...
pkg/kubelet/dockertools/manager.go
View file @
de5f4070
...
...
@@ -1014,27 +1014,25 @@ func (dm *DockerManager) defaultSecurityOpt() ([]string, error) {
// RunInContainer run the command inside the container identified by containerID
func
(
dm
*
DockerManager
)
RunInContainer
(
containerID
kubecontainer
.
ContainerID
,
cmd
[]
string
)
([]
byte
,
error
)
{
glog
.
V
(
2
)
.
Infof
(
"Using docker native exec to run cmd %+v inside container %s"
,
cmd
,
containerID
)
createOpts
:=
docker
.
CreateExecOptions
{
Container
:
containerID
.
ID
,
createOpts
:=
dockertypes
.
ExecConfig
{
Cmd
:
cmd
,
AttachStdin
:
false
,
AttachStdout
:
true
,
AttachStderr
:
true
,
Tty
:
false
,
}
execObj
,
err
:=
dm
.
client
.
CreateExec
(
createOpts
)
execObj
,
err
:=
dm
.
client
.
CreateExec
(
c
ontainerID
.
ID
,
c
reateOpts
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to run in container - Exec setup failed - %v"
,
err
)
}
var
buf
bytes
.
Buffer
startOpts
:=
docker
.
StartExecOptions
{
Detach
:
false
,
Tty
:
false
,
startOpts
:=
dockertypes
.
ExecStartCheck
{
Detach
:
false
,
Tty
:
false
}
streamOpts
:=
StreamOptions
{
OutputStream
:
&
buf
,
ErrorStream
:
&
buf
,
RawTerminal
:
false
,
}
err
=
dm
.
client
.
StartExec
(
execObj
.
ID
,
startOpts
)
err
=
dm
.
client
.
StartExec
(
execObj
.
ID
,
startOpts
,
streamOpts
)
if
err
!=
nil
{
glog
.
V
(
2
)
.
Infof
(
"StartExec With error: %v"
,
err
)
return
nil
,
err
...
...
@@ -1061,7 +1059,7 @@ func (dm *DockerManager) RunInContainer(containerID kubecontainer.ContainerID, c
}
type
dockerExitError
struct
{
Inspect
*
docker
.
ExecInspect
Inspect
*
docker
types
.
Container
ExecInspect
}
func
(
d
*
dockerExitError
)
String
()
string
{
...
...
pkg/kubelet/dockertools/manager_test.go
View file @
de5f4070
...
...
@@ -438,7 +438,7 @@ func TestKillContainerInPod(t *testing.T) {
func
TestKillContainerInPodWithPreStop
(
t
*
testing
.
T
)
{
manager
,
fakeDocker
:=
newTestDockerManager
()
fakeDocker
.
ExecInspect
=
&
docker
.
ExecInspect
{
fakeDocker
.
ExecInspect
=
&
docker
types
.
Container
ExecInspect
{
Running
:
false
,
ExitCode
:
0
,
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment