v4.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. Copyright 2016 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. "encoding/json"
  16. "errors"
  17. "fmt"
  18. "strconv"
  19. "sync"
  20. "k8s.io/kubernetes/pkg/api/unversioned"
  21. "k8s.io/kubernetes/pkg/kubelet/server/remotecommand"
  22. "k8s.io/kubernetes/pkg/util/exec"
  23. )
  24. // streamProtocolV4 implements version 4 of the streaming protocol for attach
  25. // and exec. This version adds support for exit codes on the error stream through
  26. // the use of unversioned.Status instead of plain text messages.
  27. type streamProtocolV4 struct {
  28. *streamProtocolV3
  29. }
  30. var _ streamProtocolHandler = &streamProtocolV4{}
  31. func newStreamProtocolV4(options StreamOptions) streamProtocolHandler {
  32. return &streamProtocolV4{
  33. streamProtocolV3: newStreamProtocolV3(options).(*streamProtocolV3),
  34. }
  35. }
  36. func (p *streamProtocolV4) createStreams(conn streamCreator) error {
  37. return p.streamProtocolV3.createStreams(conn)
  38. }
  39. func (p *streamProtocolV4) handleResizes() {
  40. p.streamProtocolV3.handleResizes()
  41. }
  42. func (p *streamProtocolV4) stream(conn streamCreator) error {
  43. if err := p.createStreams(conn); err != nil {
  44. return err
  45. }
  46. // now that all the streams have been created, proceed with reading & copying
  47. errorChan := watchErrorStream(p.errorStream, &errorDecoderV4{})
  48. p.handleResizes()
  49. p.copyStdin()
  50. var wg sync.WaitGroup
  51. p.copyStdout(&wg)
  52. p.copyStderr(&wg)
  53. // we're waiting for stdout/stderr to finish copying
  54. wg.Wait()
  55. // waits for errorStream to finish reading with an error or nil
  56. return <-errorChan
  57. }
  58. // errorDecoderV4 interprets the json-marshaled unversioned.Status on the error channel
  59. // and creates an exec.ExitError from it.
  60. type errorDecoderV4 struct{}
  61. func (d *errorDecoderV4) decode(message []byte) error {
  62. status := unversioned.Status{}
  63. err := json.Unmarshal(message, &status)
  64. if err != nil {
  65. return fmt.Errorf("error stream protocol error: %v in %q", err, string(message))
  66. }
  67. switch status.Status {
  68. case unversioned.StatusSuccess:
  69. return nil
  70. case unversioned.StatusFailure:
  71. if status.Reason == remotecommand.NonZeroExitCodeReason {
  72. if status.Details == nil {
  73. return errors.New("error stream protocol error: details must be set")
  74. }
  75. for i := range status.Details.Causes {
  76. c := &status.Details.Causes[i]
  77. if c.Type != remotecommand.ExitCodeCauseType {
  78. continue
  79. }
  80. rc, err := strconv.ParseUint(c.Message, 10, 8)
  81. if err != nil {
  82. return fmt.Errorf("error stream protocol error: invalid exit code value %q", c.Message)
  83. }
  84. return exec.CodeExitError{
  85. Err: fmt.Errorf("command terminated with exit code %d", rc),
  86. Code: int(rc),
  87. }
  88. }
  89. return fmt.Errorf("error stream protocol error: no %s cause given", remotecommand.ExitCodeCauseType)
  90. }
  91. default:
  92. return errors.New("error stream protocol error: unknown error")
  93. }
  94. return fmt.Errorf(status.Message)
  95. }