v4.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. "fmt"
  30. "strconv"
  31. "sync"
  32. "github.com/pkg/errors"
  33. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  34. )
  35. // streamProtocolV4 implements version 4 of the streaming protocol for attach
  36. // and exec. This version adds support for exit codes on the error stream through
  37. // the use of metav1.Status instead of plain text messages.
  38. type streamProtocolV4 struct {
  39. *streamProtocolV3
  40. }
  41. var _ streamProtocolHandler = &streamProtocolV4{}
  42. func newStreamProtocolV4(options StreamOptions) streamProtocolHandler {
  43. return &streamProtocolV4{
  44. streamProtocolV3: newStreamProtocolV3(options).(*streamProtocolV3),
  45. }
  46. }
  47. func (p *streamProtocolV4) createStreams(conn streamCreator) error {
  48. return p.streamProtocolV3.createStreams(conn)
  49. }
  50. func (p *streamProtocolV4) handleResizes() {
  51. p.streamProtocolV3.handleResizes()
  52. }
  53. func (p *streamProtocolV4) stream(conn streamCreator) error {
  54. if err := p.createStreams(conn); err != nil {
  55. return err
  56. }
  57. // now that all the streams have been created, proceed with reading & copying
  58. errorChan := watchErrorStream(p.errorStream, &errorDecoderV4{})
  59. p.handleResizes()
  60. p.copyStdin()
  61. var wg sync.WaitGroup
  62. p.copyStdout(&wg)
  63. p.copyStderr(&wg)
  64. // we're waiting for stdout/stderr to finish copying
  65. wg.Wait()
  66. // waits for errorStream to finish reading with an error or nil
  67. return <-errorChan
  68. }
  69. // errorDecoderV4 interprets the json-marshaled metav1.Status on the error channel
  70. // and creates an exec.ExitError from it.
  71. type errorDecoderV4 struct{}
  72. func (d *errorDecoderV4) decode(message []byte) error {
  73. status := metav1.Status{}
  74. err := json.Unmarshal(message, &status)
  75. if err != nil {
  76. return fmt.Errorf("error stream protocol error: %v in %q", err, string(message))
  77. }
  78. switch status.Status {
  79. case metav1.StatusSuccess:
  80. return nil
  81. case metav1.StatusFailure:
  82. if status.Reason == NonZeroExitCodeReason {
  83. if status.Details == nil {
  84. return errors.New("error stream protocol error: details must be set")
  85. }
  86. for i := range status.Details.Causes {
  87. c := &status.Details.Causes[i]
  88. if c.Type != ExitCodeCauseType {
  89. continue
  90. }
  91. rc, err := strconv.ParseUint(c.Message, 10, 8)
  92. if err != nil {
  93. return fmt.Errorf("error stream protocol error: invalid exit code value %q", c.Message)
  94. }
  95. return CodeExitError{
  96. Err: fmt.Errorf("command terminated with exit code %d", rc),
  97. Code: int(rc),
  98. }
  99. }
  100. return fmt.Errorf("error stream protocol error: no %s cause given", ExitCodeCauseType)
  101. }
  102. default:
  103. return errors.New("error stream protocol error: unknown error")
  104. }
  105. return fmt.Errorf("%s", status.Message)
  106. }
  107. // ExitError is an interface that presents an API similar to os.ProcessState, which is
  108. // what ExitError from os/exec is. This is designed to make testing a bit easier and
  109. // probably loses some of the cross-platform properties of the underlying library.
  110. type ExitError interface {
  111. String() string
  112. Error() string
  113. Exited() bool
  114. ExitStatus() int
  115. }
  116. // CodeExitError is an implementation of ExitError consisting of an error object
  117. // and an exit code (the upper bits of os.exec.ExitStatus).
  118. type CodeExitError struct {
  119. Err error
  120. Code int
  121. }
  122. var _ ExitError = CodeExitError{}
  123. func (e CodeExitError) Error() string {
  124. return e.Err.Error()
  125. }
  126. func (e CodeExitError) String() string {
  127. return e.Err.Error()
  128. }
  129. func (e CodeExitError) Exited() bool {
  130. return true
  131. }
  132. func (e CodeExitError) ExitStatus() int {
  133. return e.Code
  134. }