v2.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package remotecommand
  14. import (
  15. "fmt"
  16. "io"
  17. "io/ioutil"
  18. "net/http"
  19. "sync"
  20. "k8s.io/kubernetes/pkg/api"
  21. "k8s.io/kubernetes/pkg/util/runtime"
  22. )
  23. // streamProtocolV2 implements version 2 of the streaming protocol for attach
  24. // and exec. The original streaming protocol was unversioned. As a result, this
  25. // version is referred to as version 2, even though it is the first actual
  26. // numbered version.
  27. type streamProtocolV2 struct {
  28. StreamOptions
  29. errorStream io.Reader
  30. remoteStdin io.ReadWriteCloser
  31. remoteStdout io.Reader
  32. remoteStderr io.Reader
  33. }
  34. var _ streamProtocolHandler = &streamProtocolV2{}
  35. func newStreamProtocolV2(options StreamOptions) streamProtocolHandler {
  36. return &streamProtocolV2{
  37. StreamOptions: options,
  38. }
  39. }
  40. func (p *streamProtocolV2) createStreams(conn streamCreator) error {
  41. var err error
  42. headers := http.Header{}
  43. // set up error stream
  44. headers.Set(api.StreamType, api.StreamTypeError)
  45. p.errorStream, err = conn.CreateStream(headers)
  46. if err != nil {
  47. return err
  48. }
  49. // set up stdin stream
  50. if p.Stdin != nil {
  51. headers.Set(api.StreamType, api.StreamTypeStdin)
  52. p.remoteStdin, err = conn.CreateStream(headers)
  53. if err != nil {
  54. return err
  55. }
  56. }
  57. // set up stdout stream
  58. if p.Stdout != nil {
  59. headers.Set(api.StreamType, api.StreamTypeStdout)
  60. p.remoteStdout, err = conn.CreateStream(headers)
  61. if err != nil {
  62. return err
  63. }
  64. }
  65. // set up stderr stream
  66. if p.Stderr != nil && !p.Tty {
  67. headers.Set(api.StreamType, api.StreamTypeStderr)
  68. p.remoteStderr, err = conn.CreateStream(headers)
  69. if err != nil {
  70. return err
  71. }
  72. }
  73. return nil
  74. }
  75. func (p *streamProtocolV2) copyStdin() {
  76. if p.Stdin != nil {
  77. var once sync.Once
  78. // copy from client's stdin to container's stdin
  79. go func() {
  80. defer runtime.HandleCrash()
  81. // if p.stdin is noninteractive, p.g. `echo abc | kubectl exec -i <pod> -- cat`, make sure
  82. // we close remoteStdin as soon as the copy from p.stdin to remoteStdin finishes. Otherwise
  83. // the executed command will remain running.
  84. defer once.Do(func() { p.remoteStdin.Close() })
  85. if _, err := io.Copy(p.remoteStdin, p.Stdin); err != nil {
  86. runtime.HandleError(err)
  87. }
  88. }()
  89. // read from remoteStdin until the stream is closed. this is essential to
  90. // be able to exit interactive sessions cleanly and not leak goroutines or
  91. // hang the client's terminal.
  92. //
  93. // TODO we aren't using go-dockerclient any more; revisit this to determine if it's still
  94. // required by engine-api.
  95. //
  96. // go-dockerclient's current hijack implementation
  97. // (https://github.com/fsouza/go-dockerclient/blob/89f3d56d93788dfe85f864a44f85d9738fca0670/client.go#L564)
  98. // waits for all three streams (stdin/stdout/stderr) to finish copying
  99. // before returning. When hijack finishes copying stdout/stderr, it calls
  100. // Close() on its side of remoteStdin, which allows this copy to complete.
  101. // When that happens, we must Close() on our side of remoteStdin, to
  102. // allow the copy in hijack to complete, and hijack to return.
  103. go func() {
  104. defer runtime.HandleCrash()
  105. defer once.Do(func() { p.remoteStdin.Close() })
  106. // this "copy" doesn't actually read anything - it's just here to wait for
  107. // the server to close remoteStdin.
  108. if _, err := io.Copy(ioutil.Discard, p.remoteStdin); err != nil {
  109. runtime.HandleError(err)
  110. }
  111. }()
  112. }
  113. }
  114. func (p *streamProtocolV2) copyStdout(wg *sync.WaitGroup) {
  115. if p.Stdout == nil {
  116. return
  117. }
  118. wg.Add(1)
  119. go func() {
  120. defer runtime.HandleCrash()
  121. defer wg.Done()
  122. if _, err := io.Copy(p.Stdout, p.remoteStdout); err != nil {
  123. runtime.HandleError(err)
  124. }
  125. }()
  126. }
  127. func (p *streamProtocolV2) copyStderr(wg *sync.WaitGroup) {
  128. if p.Stderr == nil || p.Tty {
  129. return
  130. }
  131. wg.Add(1)
  132. go func() {
  133. defer runtime.HandleCrash()
  134. defer wg.Done()
  135. if _, err := io.Copy(p.Stderr, p.remoteStderr); err != nil {
  136. runtime.HandleError(err)
  137. }
  138. }()
  139. }
  140. func (p *streamProtocolV2) stream(conn streamCreator) error {
  141. if err := p.createStreams(conn); err != nil {
  142. return err
  143. }
  144. // now that all the streams have been created, proceed with reading & copying
  145. errorChan := watchErrorStream(p.errorStream, &errorDecoderV2{})
  146. p.copyStdin()
  147. var wg sync.WaitGroup
  148. p.copyStdout(&wg)
  149. p.copyStderr(&wg)
  150. // we're waiting for stdout/stderr to finish copying
  151. wg.Wait()
  152. // waits for errorStream to finish reading with an error or nil
  153. return <-errorChan
  154. }
  155. // errorDecoderV2 interprets the error channel data as plain text.
  156. type errorDecoderV2 struct{}
  157. func (d *errorDecoderV2) decode(message []byte) error {
  158. return fmt.Errorf("error executing remote command: %s", message)
  159. }