@@ -13,6 +13,7 @@ import (
13
13
"k8s.io/apimachinery/pkg/util/wait"
14
14
"k8s.io/client-go/kubernetes/scheme"
15
15
tcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
16
+ "k8s.io/kubernetes/pkg/kubectl/util/term"
16
17
17
18
corev1 "k8s.io/api/core/v1"
18
19
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -83,9 +84,12 @@ func (a *Attacher) Attach(selector, namespace string) {
83
84
restClient := a .CoreV1Client .RESTClient ().(* restclient.RESTClient )
84
85
containerName := pod .Spec .Containers [0 ].Name
85
86
86
- attfn := defaultAttachFunc (restClient , pod .Name , containerName , pod .Namespace , a .Config )
87
+ t , err := setupTTY (os .Stdout , os .Stdin )
88
+ if err != nil {
89
+ return false , err
90
+ }
91
+ err = t .Safe (defaultAttachFunc (restClient , pod .Name , containerName , pod .Namespace , a .Config , t ))
87
92
88
- err = attfn ()
89
93
if err != nil {
90
94
a .logger .Warn ("attach retry" , zap .Error (err ))
91
95
return false , nil
@@ -96,8 +100,7 @@ func (a *Attacher) Attach(selector, namespace string) {
96
100
<- a .ctx .Done ()
97
101
}
98
102
99
- func defaultAttachFunc (restClient * restclient.RESTClient , podName string , containerName string , namespace string , config * restclient.Config ) func () error {
100
- raw := false
103
+ func defaultAttachFunc (restClient * restclient.RESTClient , podName string , containerName string , namespace string , config * restclient.Config , t term.TTY ) func () error {
101
104
return func () error {
102
105
req := restClient .Post ().
103
106
Resource ("pods" ).
@@ -109,11 +112,11 @@ func defaultAttachFunc(restClient *restclient.RESTClient, podName string, contai
109
112
Stdin : true ,
110
113
Stdout : true ,
111
114
Stderr : true ,
112
- TTY : raw ,
115
+ TTY : t . Raw ,
113
116
}, scheme .ParameterCodec )
114
117
115
118
att := & defaultRemoteAttach {}
116
- return att .Attach ("POST" , req .URL (), config , os . Stdin , os . Stdout , os .Stderr , raw , nil )
119
+ return att .Attach ("POST" , req .URL (), config , t . In , t . Out , os .Stderr , t . Raw , t . MonitorSize ( t . GetSize ()) )
117
120
}
118
121
}
119
122
@@ -132,3 +135,17 @@ func (*defaultRemoteAttach) Attach(method string, url *url.URL, config *restclie
132
135
TerminalSizeQueue : terminalSizeQueue ,
133
136
})
134
137
}
138
+
139
+ func setupTTY (out io.Writer , in io.Reader ) (term.TTY , error ) {
140
+ t := term.TTY {
141
+ Out : out ,
142
+ In : in ,
143
+ Raw : true ,
144
+ }
145
+
146
+ if ! t .IsTerminalIn () {
147
+ return t , fmt .Errorf ("unable to use a TTY if the input is not a terminal" )
148
+ }
149
+
150
+ return t , nil
151
+ }
0 commit comments