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
6ddffdd7
Commit
6ddffdd7
authored
Jun 05, 2015
by
Yifan Gu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
kubelet: Move TestPortForwardNoSuchContainer() to dockertools package.
Also refactor TestPortForward() to be neutral to container runtime.
parent
f83d5356
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
47 additions
and
55 deletions
+47
-55
runtime.go
pkg/kubelet/container/runtime.go
+6
-0
manager.go
pkg/kubelet/dockertools/manager.go
+5
-1
manager_test.go
pkg/kubelet/dockertools/manager_test.go
+23
-0
kubelet.go
pkg/kubelet/kubelet.go
+3
-0
kubelet_test.go
pkg/kubelet/kubelet_test.go
+10
-54
No files found.
pkg/kubelet/container/runtime.go
View file @
6ddffdd7
...
@@ -19,6 +19,7 @@ package container
...
@@ -19,6 +19,7 @@ package container
import
(
import
(
"fmt"
"fmt"
"io"
"io"
"reflect"
"strings"
"strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
...
@@ -282,6 +283,11 @@ func (p *Pod) FindContainerByName(containerName string) *Container {
...
@@ -282,6 +283,11 @@ func (p *Pod) FindContainerByName(containerName string) *Container {
return
nil
return
nil
}
}
// IsEmpty returns true if the pod is empty.
func
(
p
*
Pod
)
IsEmpty
()
bool
{
return
reflect
.
DeepEqual
(
p
,
&
Pod
{})
}
// GetPodFullName returns a name that uniquely identifies a pod.
// GetPodFullName returns a name that uniquely identifies a pod.
func
GetPodFullName
(
pod
*
api
.
Pod
)
string
{
func
GetPodFullName
(
pod
*
api
.
Pod
)
string
{
// Use underscore as the delimiter because it is not allowed in pod name
// Use underscore as the delimiter because it is not allowed in pod name
...
...
pkg/kubelet/dockertools/manager.go
View file @
6ddffdd7
...
@@ -1006,6 +1006,10 @@ func (dm *DockerManager) ExecInContainer(containerId string, cmd []string, stdin
...
@@ -1006,6 +1006,10 @@ func (dm *DockerManager) ExecInContainer(containerId string, cmd []string, stdin
return
dm
.
execHandler
.
ExecInContainer
(
dm
.
client
,
container
,
cmd
,
stdin
,
stdout
,
stderr
,
tty
)
return
dm
.
execHandler
.
ExecInContainer
(
dm
.
client
,
container
,
cmd
,
stdin
,
stdout
,
stderr
,
tty
)
}
}
func
noPodInfraContainerError
(
podName
,
podNamespace
string
)
error
{
return
fmt
.
Errorf
(
"cannot find pod infra container in pod %q"
,
kubecontainer
.
BuildPodFullName
(
podName
,
podNamespace
))
}
// PortForward executes socat in the pod's network namespace and copies
// PortForward executes socat in the pod's network namespace and copies
// data between stream (representing the user's local connection on their
// data between stream (representing the user's local connection on their
// computer) and the specified port in the container.
// computer) and the specified port in the container.
...
@@ -1017,7 +1021,7 @@ func (dm *DockerManager) ExecInContainer(containerId string, cmd []string, stdin
...
@@ -1017,7 +1021,7 @@ func (dm *DockerManager) ExecInContainer(containerId string, cmd []string, stdin
func
(
dm
*
DockerManager
)
PortForward
(
pod
*
kubecontainer
.
Pod
,
port
uint16
,
stream
io
.
ReadWriteCloser
)
error
{
func
(
dm
*
DockerManager
)
PortForward
(
pod
*
kubecontainer
.
Pod
,
port
uint16
,
stream
io
.
ReadWriteCloser
)
error
{
podInfraContainer
:=
pod
.
FindContainerByName
(
PodInfraContainerName
)
podInfraContainer
:=
pod
.
FindContainerByName
(
PodInfraContainerName
)
if
podInfraContainer
==
nil
{
if
podInfraContainer
==
nil
{
return
fmt
.
Errorf
(
"cannot find pod infra container in pod %q"
,
kubecontainer
.
BuildPodFullName
(
pod
.
Name
,
pod
.
Namespace
)
)
return
noPodInfraContainerError
(
pod
.
Name
,
pod
.
Namespace
)
}
}
container
,
err
:=
dm
.
client
.
InspectContainer
(
string
(
podInfraContainer
.
ID
))
container
,
err
:=
dm
.
client
.
InspectContainer
(
string
(
podInfraContainer
.
ID
))
if
err
!=
nil
{
if
err
!=
nil
{
...
...
pkg/kubelet/dockertools/manager_test.go
View file @
6ddffdd7
...
@@ -1914,3 +1914,26 @@ func TestSyncPodEventHandlerFails(t *testing.T) {
...
@@ -1914,3 +1914,26 @@ func TestSyncPodEventHandlerFails(t *testing.T) {
t
.
Errorf
(
"Wrong stopped container, expected: bar, get: %q"
,
dockerName
.
ContainerName
)
t
.
Errorf
(
"Wrong stopped container, expected: bar, get: %q"
,
dockerName
.
ContainerName
)
}
}
}
}
func
TestPortForwardNoSuchContainer
(
t
*
testing
.
T
)
{
dm
,
_
:=
newTestDockerManager
()
podName
,
podNamespace
:=
"podName"
,
"podNamespace"
err
:=
dm
.
PortForward
(
&
kubecontainer
.
Pod
{
ID
:
"podID"
,
Name
:
podName
,
Namespace
:
podNamespace
,
Containers
:
nil
,
},
5000
,
nil
,
)
if
err
==
nil
{
t
.
Fatal
(
"unexpected non-error"
)
}
expectedErr
:=
noPodInfraContainerError
(
podName
,
podNamespace
)
if
!
reflect
.
DeepEqual
(
err
,
expectedErr
)
{
t
.
Fatalf
(
"expected %v, but saw %v"
,
expectedErr
,
err
)
}
}
pkg/kubelet/kubelet.go
View file @
6ddffdd7
...
@@ -2235,6 +2235,9 @@ func (kl *Kubelet) PortForward(podFullName string, podUID types.UID, port uint16
...
@@ -2235,6 +2235,9 @@ func (kl *Kubelet) PortForward(podFullName string, podUID types.UID, port uint16
return
err
return
err
}
}
pod
:=
kubecontainer
.
Pods
(
pods
)
.
FindPod
(
podFullName
,
podUID
)
pod
:=
kubecontainer
.
Pods
(
pods
)
.
FindPod
(
podFullName
,
podUID
)
if
pod
.
IsEmpty
()
{
return
fmt
.
Errorf
(
"pod not found (%q)"
,
podFullName
)
}
return
kl
.
runner
.
PortForward
(
&
pod
,
port
,
stream
)
return
kl
.
runner
.
PortForward
(
&
pod
,
port
,
stream
)
}
}
...
...
pkg/kubelet/kubelet_test.go
View file @
6ddffdd7
...
@@ -934,6 +934,7 @@ func TestGetContainerInfoWithNoMatchingContainers(t *testing.T) {
...
@@ -934,6 +934,7 @@ func TestGetContainerInfoWithNoMatchingContainers(t *testing.T) {
type
fakeContainerCommandRunner
struct
{
type
fakeContainerCommandRunner
struct
{
Cmd
[]
string
Cmd
[]
string
ID
string
ID
string
PodID
types
.
UID
E
error
E
error
Stdin
io
.
Reader
Stdin
io
.
Reader
Stdout
io
.
WriteCloser
Stdout
io
.
WriteCloser
...
@@ -960,11 +961,7 @@ func (f *fakeContainerCommandRunner) ExecInContainer(id string, cmd []string, in
...
@@ -960,11 +961,7 @@ func (f *fakeContainerCommandRunner) ExecInContainer(id string, cmd []string, in
}
}
func
(
f
*
fakeContainerCommandRunner
)
PortForward
(
pod
*
kubecontainer
.
Pod
,
port
uint16
,
stream
io
.
ReadWriteCloser
)
error
{
func
(
f
*
fakeContainerCommandRunner
)
PortForward
(
pod
*
kubecontainer
.
Pod
,
port
uint16
,
stream
io
.
ReadWriteCloser
)
error
{
podInfraContainer
:=
pod
.
FindContainerByName
(
dockertools
.
PodInfraContainerName
)
f
.
PodID
=
pod
.
ID
if
podInfraContainer
==
nil
{
return
fmt
.
Errorf
(
"cannot find pod infra container in pod %q"
,
kubecontainer
.
BuildPodFullName
(
pod
.
Name
,
pod
.
Namespace
))
}
f
.
ID
=
string
(
podInfraContainer
.
ID
)
f
.
Port
=
port
f
.
Port
=
port
f
.
Stream
=
stream
f
.
Stream
=
stream
return
nil
return
nil
...
@@ -2123,73 +2120,32 @@ func TestPortForwardNoSuchPod(t *testing.T) {
...
@@ -2123,73 +2120,32 @@ func TestPortForwardNoSuchPod(t *testing.T) {
}
}
}
}
func
TestPortForward
NoSuchContainer
(
t
*
testing
.
T
)
{
func
TestPortForward
(
t
*
testing
.
T
)
{
testKubelet
:=
newTestKubeletWithFakeRuntime
(
t
)
testKubelet
:=
newTestKubeletWithFakeRuntime
(
t
)
kubelet
:=
testKubelet
.
kubelet
kubelet
:=
testKubelet
.
kubelet
fakeRuntime
:=
testKubelet
.
fakeRuntime
fakeRuntime
:=
testKubelet
.
fakeRuntime
fakeCommandRunner
:=
fakeContainerCommandRunner
{}
kubelet
.
runner
=
&
fakeCommandRunner
podName
:=
"podFoo"
podName
:=
"podFoo"
podNamespace
:=
"nsFoo"
podNamespace
:=
"nsFoo"
var
port
uint16
=
5000
podID
:=
types
.
UID
(
"12345678"
)
fakeRuntime
.
PodList
=
[]
*
kubecontainer
.
Pod
{
fakeRuntime
.
PodList
=
[]
*
kubecontainer
.
Pod
{
{
{
ID
:
"12345678"
,
ID
:
podID
,
Name
:
podName
,
Name
:
podName
,
Namespace
:
podNamespace
,
Namespace
:
podNamespace
,
Containers
:
[]
*
kubecontainer
.
Container
{
Containers
:
[]
*
kubecontainer
.
Container
{
{
Name
:
"bar"
,
{
ID
:
"barID"
},
Name
:
"foo"
,
ID
:
"containerFoo"
,
},
},
},
},
},
}
}
err
:=
kubelet
.
PortForward
(
kubecontainer
.
GetPodFullName
(
&
api
.
Pod
{
ObjectMeta
:
api
.
ObjectMeta
{
UID
:
"12345678"
,
Name
:
podName
,
Namespace
:
podNamespace
,
}}),
""
,
port
,
nil
,
)
if
err
==
nil
{
t
.
Fatal
(
"unexpected non-error"
)
}
if
fakeCommandRunner
.
ID
!=
""
{
t
.
Fatal
(
"unexpected invocation of runner.PortForward"
)
}
}
func
TestPortForward
(
t
*
testing
.
T
)
{
fakeCommandRunner
:=
fakeContainerCommandRunner
{}
fakeCommandRunner
:=
fakeContainerCommandRunner
{}
testKubelet
:=
newTestKubelet
(
t
)
kubelet
:=
testKubelet
.
kubelet
fakeDocker
:=
testKubelet
.
fakeDocker
kubelet
.
runner
=
&
fakeCommandRunner
kubelet
.
runner
=
&
fakeCommandRunner
podName
:=
"podFoo"
podNamespace
:=
"nsFoo"
containerID
:=
"containerFoo"
var
port
uint16
=
5000
var
port
uint16
=
5000
stream
:=
&
fakeReadWriteCloser
{}
stream
:=
&
fakeReadWriteCloser
{}
infraContainerID
:=
"infra"
fakeDocker
.
ContainerList
=
[]
docker
.
APIContainers
{
{
ID
:
infraContainerID
,
Names
:
[]
string
{
"/k8s_POD"
+
"_"
+
podName
+
"_"
+
podNamespace
+
"_12345678_42"
},
},
{
ID
:
containerID
,
Names
:
[]
string
{
"/k8s_"
+
containerID
+
"_"
+
podName
+
"_"
+
podNamespace
+
"_12345678_42"
},
},
}
err
:=
kubelet
.
PortForward
(
err
:=
kubelet
.
PortForward
(
kubecontainer
.
GetPodFullName
(
&
api
.
Pod
{
ObjectMeta
:
api
.
ObjectMeta
{
kubecontainer
.
GetPodFullName
(
&
api
.
Pod
{
ObjectMeta
:
api
.
ObjectMeta
{
UID
:
"12345678"
,
UID
:
"12345678"
,
...
@@ -2203,7 +2159,7 @@ func TestPortForward(t *testing.T) {
...
@@ -2203,7 +2159,7 @@ func TestPortForward(t *testing.T) {
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %s"
,
err
)
t
.
Fatalf
(
"unexpected error: %s"
,
err
)
}
}
if
e
,
a
:=
infraContainerID
,
fakeCommandRunner
.
ID
;
e
!=
a
{
if
e
,
a
:=
podID
,
fakeCommandRunner
.
Pod
ID
;
e
!=
a
{
t
.
Fatalf
(
"container id: expected %q, got %q"
,
e
,
a
)
t
.
Fatalf
(
"container id: expected %q, got %q"
,
e
,
a
)
}
}
if
e
,
a
:=
port
,
fakeCommandRunner
.
Port
;
e
!=
a
{
if
e
,
a
:=
port
,
fakeCommandRunner
.
Port
;
e
!=
a
{
...
...
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