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
c080c8a1
Commit
c080c8a1
authored
Nov 03, 2015
by
k8s-merge-robot
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #16537 from fabianofranz/attach_handles_tty_support
Auto commit by PR queue bot
parents
c1d380db
c60f19a1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
89 additions
and
6 deletions
+89
-6
attach.go
pkg/kubectl/cmd/attach.go
+22
-6
attach_test.go
pkg/kubectl/cmd/attach_test.go
+67
-0
No files found.
pkg/kubectl/cmd/attach.go
View file @
c080c8a1
...
@@ -160,9 +160,16 @@ func (p *AttachOptions) Run() error {
...
@@ -160,9 +160,16 @@ func (p *AttachOptions) Run() error {
return
fmt
.
Errorf
(
"pod %s is not running and cannot be attached to; current phase is %s"
,
p
.
PodName
,
pod
.
Status
.
Phase
)
return
fmt
.
Errorf
(
"pod %s is not running and cannot be attached to; current phase is %s"
,
p
.
PodName
,
pod
.
Status
.
Phase
)
}
}
// TODO: refactor with terminal helpers from the edit utility once that is merged
var
stdin
io
.
Reader
var
stdin
io
.
Reader
tty
:=
p
.
TTY
tty
:=
p
.
TTY
containerToAttach
:=
p
.
GetContainer
(
pod
)
if
tty
&&
!
containerToAttach
.
TTY
{
tty
=
false
fmt
.
Fprintf
(
p
.
Err
,
"Unable to use a TTY - container %s doesn't allocate one
\n
"
,
containerToAttach
.
Name
)
}
// TODO: refactor with terminal helpers from the edit utility once that is merged
if
p
.
Stdin
{
if
p
.
Stdin
{
stdin
=
p
.
In
stdin
=
p
.
In
if
tty
{
if
tty
{
...
@@ -204,7 +211,7 @@ func (p *AttachOptions) Run() error {
...
@@ -204,7 +211,7 @@ func (p *AttachOptions) Run() error {
Namespace
(
pod
.
Namespace
)
.
Namespace
(
pod
.
Namespace
)
.
SubResource
(
"attach"
)
SubResource
(
"attach"
)
req
.
VersionedParams
(
&
api
.
PodAttachOptions
{
req
.
VersionedParams
(
&
api
.
PodAttachOptions
{
Container
:
p
.
GetContainerName
(
pod
)
,
Container
:
containerToAttach
.
Name
,
Stdin
:
stdin
!=
nil
,
Stdin
:
stdin
!=
nil
,
Stdout
:
p
.
Out
!=
nil
,
Stdout
:
p
.
Out
!=
nil
,
Stderr
:
p
.
Err
!=
nil
,
Stderr
:
p
.
Err
!=
nil
,
...
@@ -214,12 +221,21 @@ func (p *AttachOptions) Run() error {
...
@@ -214,12 +221,21 @@ func (p *AttachOptions) Run() error {
return
p
.
Attach
.
Attach
(
"POST"
,
req
.
URL
(),
p
.
Config
,
stdin
,
p
.
Out
,
p
.
Err
,
tty
)
return
p
.
Attach
.
Attach
(
"POST"
,
req
.
URL
(),
p
.
Config
,
stdin
,
p
.
Out
,
p
.
Err
,
tty
)
}
}
// GetContainer
Name returns the name of
the container to attach to, with a fallback.
// GetContainer
returns
the container to attach to, with a fallback.
func
(
p
*
AttachOptions
)
GetContainer
Name
(
pod
*
api
.
Pod
)
string
{
func
(
p
*
AttachOptions
)
GetContainer
(
pod
*
api
.
Pod
)
api
.
Container
{
if
len
(
p
.
ContainerName
)
>
0
{
if
len
(
p
.
ContainerName
)
>
0
{
return
p
.
ContainerName
for
_
,
container
:=
range
pod
.
Spec
.
Containers
{
if
container
.
Name
==
p
.
ContainerName
{
return
container
}
}
}
}
glog
.
V
(
4
)
.
Infof
(
"defaulting container name to %s"
,
pod
.
Spec
.
Containers
[
0
]
.
Name
)
glog
.
V
(
4
)
.
Infof
(
"defaulting container name to %s"
,
pod
.
Spec
.
Containers
[
0
]
.
Name
)
return
pod
.
Spec
.
Containers
[
0
]
.
Name
return
pod
.
Spec
.
Containers
[
0
]
}
// GetContainerName returns the name of the container to attach to, with a fallback.
func
(
p
*
AttachOptions
)
GetContainerName
(
pod
*
api
.
Pod
)
string
{
return
p
.
GetContainer
(
pod
)
.
Name
}
}
pkg/kubectl/cmd/attach_test.go
View file @
c080c8a1
...
@@ -22,6 +22,7 @@ import (
...
@@ -22,6 +22,7 @@ import (
"io"
"io"
"net/http"
"net/http"
"net/url"
"net/url"
"strings"
"testing"
"testing"
"github.com/spf13/cobra"
"github.com/spf13/cobra"
...
@@ -192,6 +193,72 @@ func TestAttach(t *testing.T) {
...
@@ -192,6 +193,72 @@ func TestAttach(t *testing.T) {
}
}
}
}
func
TestAttachWarnings
(
t
*
testing
.
T
)
{
version
:=
testapi
.
Default
.
Version
()
tests
:=
[]
struct
{
name
,
container
,
version
,
podPath
,
expectedErr
,
expectedOut
string
pod
*
api
.
Pod
stdin
,
tty
bool
}{
{
name
:
"fallback tty if not supported"
,
version
:
version
,
podPath
:
"/api/"
+
version
+
"/namespaces/test/pods/foo"
,
pod
:
attachPod
(),
stdin
:
true
,
tty
:
true
,
expectedErr
:
"Unable to use a TTY - container bar doesn't allocate one"
,
},
}
for
_
,
test
:=
range
tests
{
f
,
tf
,
codec
:=
NewAPIFactory
()
tf
.
Client
=
&
fake
.
RESTClient
{
Codec
:
codec
,
Client
:
fake
.
HTTPClientFunc
(
func
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
switch
p
,
m
:=
req
.
URL
.
Path
,
req
.
Method
;
{
case
p
==
test
.
podPath
&&
m
==
"GET"
:
body
:=
objBody
(
codec
,
test
.
pod
)
return
&
http
.
Response
{
StatusCode
:
200
,
Body
:
body
},
nil
default
:
t
.
Errorf
(
"%s: unexpected request: %s %#v
\n
%#v"
,
test
.
name
,
req
.
Method
,
req
.
URL
,
req
)
return
nil
,
fmt
.
Errorf
(
"unexpected request"
)
}
}),
}
tf
.
Namespace
=
"test"
tf
.
ClientConfig
=
&
client
.
Config
{
Version
:
test
.
version
}
bufOut
:=
bytes
.
NewBuffer
([]
byte
{})
bufErr
:=
bytes
.
NewBuffer
([]
byte
{})
bufIn
:=
bytes
.
NewBuffer
([]
byte
{})
ex
:=
&
fakeRemoteAttach
{}
params
:=
&
AttachOptions
{
ContainerName
:
test
.
container
,
In
:
bufIn
,
Out
:
bufOut
,
Err
:
bufErr
,
Stdin
:
test
.
stdin
,
TTY
:
test
.
tty
,
Attach
:
ex
,
}
cmd
:=
&
cobra
.
Command
{}
if
err
:=
params
.
Complete
(
f
,
cmd
,
[]
string
{
"foo"
});
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
err
:=
params
.
Run
();
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
test
.
stdin
&&
test
.
tty
{
if
!
test
.
pod
.
Spec
.
Containers
[
0
]
.
TTY
{
if
!
strings
.
Contains
(
bufErr
.
String
(),
test
.
expectedErr
)
{
t
.
Errorf
(
"%s: Expected TTY fallback warning for attach request: %s"
,
test
.
name
,
bufErr
.
String
())
continue
}
}
}
}
}
func
attachPod
()
*
api
.
Pod
{
func
attachPod
()
*
api
.
Pod
{
return
&
api
.
Pod
{
return
&
api
.
Pod
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"foo"
,
Namespace
:
"test"
,
ResourceVersion
:
"10"
},
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"foo"
,
Namespace
:
"test"
,
ResourceVersion
:
"10"
},
...
...
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