v3.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2019 Yunion
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*
  15. Copyright 2016 The Kubernetes Authors.
  16. Licensed under the Apache License, Version 2.0 (the "License");
  17. you may not use this file except in compliance with the License.
  18. You may obtain a copy of the License at
  19. http://www.apache.org/licenses/LICENSE-2.0
  20. Unless required by applicable law or agreed to in writing, software
  21. distributed under the License is distributed on an "AS IS" BASIS,
  22. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. See the License for the specific language governing permissions and
  24. limitations under the License.
  25. */
  26. package remotecommand
  27. import (
  28. "encoding/json"
  29. "io"
  30. "net/http"
  31. "sync"
  32. v1 "k8s.io/api/core/v1"
  33. "yunion.io/x/pkg/util/runtime"
  34. )
  35. // streamProtocolV3 implements version 3 of the streaming protocol for attach
  36. // and exec. This version adds support for resizing the container's terminal.
  37. type streamProtocolV3 struct {
  38. *streamProtocolV2
  39. resizeStream io.Writer
  40. }
  41. var _ streamProtocolHandler = &streamProtocolV3{}
  42. func newStreamProtocolV3(options StreamOptions) streamProtocolHandler {
  43. return &streamProtocolV3{
  44. streamProtocolV2: newStreamProtocolV2(options).(*streamProtocolV2),
  45. }
  46. }
  47. func (p *streamProtocolV3) createStreams(conn streamCreator) error {
  48. // set up the streams from v2
  49. if err := p.streamProtocolV2.createStreams(conn); err != nil {
  50. return err
  51. }
  52. // set up resize stream
  53. if p.Tty {
  54. headers := http.Header{}
  55. headers.Set(v1.StreamType, v1.StreamTypeResize)
  56. var err error
  57. p.resizeStream, err = conn.CreateStream(headers)
  58. if err != nil {
  59. return err
  60. }
  61. }
  62. return nil
  63. }
  64. func (p *streamProtocolV3) handleResizes() {
  65. if p.resizeStream == nil || p.TerminalSizeQueue == nil {
  66. return
  67. }
  68. go func() {
  69. defer runtime.HandleCrash()
  70. encoder := json.NewEncoder(p.resizeStream)
  71. for {
  72. size := p.TerminalSizeQueue.Next()
  73. if size == nil {
  74. return
  75. }
  76. if err := encoder.Encode(&size); err != nil {
  77. runtime.HandleError(err)
  78. }
  79. }
  80. }()
  81. }
  82. func (p *streamProtocolV3) stream(conn streamCreator) error {
  83. if err := p.createStreams(conn); err != nil {
  84. return err
  85. }
  86. // now that all the streams have been created, proceed with reading & copying
  87. errorChan := watchErrorStream(p.errorStream, &errorDecoderV3{})
  88. p.handleResizes()
  89. p.copyStdin()
  90. var wg sync.WaitGroup
  91. p.copyStdout(&wg)
  92. p.copyStderr(&wg)
  93. // we're waiting for stdout/stderr to finish copying
  94. wg.Wait()
  95. // waits for errorStream to finish reading with an error or nil
  96. return <-errorChan
  97. }
  98. type errorDecoderV3 struct {
  99. errorDecoderV2
  100. }