Debian Codegraph @

pkg/util/proxy/streamtranslator.go

raw
1 /*
2 Copyright 2023 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package proxy
18
19 import (
20 "fmt"
21 "net/http"
22 "net/url"
23 "strconv"
24 "time"
25
26 "github.com/mxk/go-flowrate/flowrate"
27 apierrors "k8s.io/apimachinery/pkg/api/errors"
28 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29 "k8s.io/apimachinery/pkg/util/httpstream/spdy"
30 constants "k8s.io/apimachinery/pkg/util/remotecommand"
31 "k8s.io/apiserver/pkg/util/proxy/metrics"
32 "k8s.io/client-go/tools/remotecommand"
33 "k8s.io/client-go/util/exec"
34 )
35
36 // StreamTranslatorHandler is a handler which translates WebSocket stream data
37 // to SPDY to proxy to kubelet (and ContainerRuntime).
38 type StreamTranslatorHandler struct {
39 // Location is the location of the upstream proxy. It is used as the location to Dial on the upstream server
40 // for upgrade requests.
41 Location *url.URL
42 // Transport provides an optional round tripper to use to proxy. If nil, the default proxy transport is used
43 Transport http.RoundTripper
44 // MaxBytesPerSec throttles stream Reader/Writer if necessary
45 MaxBytesPerSec int64
46 // Options define the requested streams (e.g. stdin, stdout).
47 Options Options
48 }
49
50 // NewStreamTranslatorHandler creates a new proxy handler. Responder is required for returning
51 // errors to the caller.
52 func NewStreamTranslatorHandler(location *url.URL, transport http.RoundTripper, maxBytesPerSec int64, opts Options) *StreamTranslatorHandler {
53 return &StreamTranslatorHandler{
54 Location: location,
55 Transport: transport,
56 MaxBytesPerSec: maxBytesPerSec,
57 Options: opts,
58 }
59 }
60
61 func (h *StreamTranslatorHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
62 // Create WebSocket server, including particular streams requested. If this websocket
63 // endpoint is not able to be upgraded, the websocket library will return errors
64 // to the client.
65 websocketStreams, err := webSocketServerStreams(req, w, h.Options)
66 if err != nil {
67 // Client error increments bad request status code.
68 metrics.IncStreamTranslatorRequest(req.Context(), strconv.Itoa(http.StatusBadRequest))
69 return
70 }
71 defer websocketStreams.conn.Close()
72
73 // Creating SPDY executor, ensuring redirects are not followed.
74 spdyRoundTripper, err := spdy.NewRoundTripperWithConfig(spdy.RoundTripperConfig{UpgradeTransport: h.Transport, PingPeriod: 5 * time.Second})
75 if err != nil {
76 websocketStreams.writeStatus(apierrors.NewInternalError(err)) //nolint:errcheck
77 metrics.IncStreamTranslatorRequest(req.Context(), strconv.Itoa(http.StatusInternalServerError))
78 return
79 }
80 spdyExecutor, err := remotecommand.NewSPDYExecutorRejectRedirects(spdyRoundTripper, spdyRoundTripper, "POST", h.Location)
81 if err != nil {
82 websocketStreams.writeStatus(apierrors.NewInternalError(err)) //nolint:errcheck
83 metrics.IncStreamTranslatorRequest(req.Context(), strconv.Itoa(http.StatusInternalServerError))
84 return
85 }
86
87 // Wire the WebSocket server streams output to the SPDY client input. The stdin/stdout/stderr streams
88 // can be throttled if the transfer rate exceeds the "MaxBytesPerSec" (zero means unset). Throttling
89 // the streams instead of the underlying connection *may* not perform the same if two streams
90 // traveling the same direction (e.g. stdout, stderr) are being maxed out.
91 opts := remotecommand.StreamOptions{}
92 if h.Options.Stdin {
93 stdin := websocketStreams.stdinStream
94 if h.MaxBytesPerSec > 0 {
95 stdin = flowrate.NewReader(stdin, h.MaxBytesPerSec)
96 }
97 opts.Stdin = stdin
98 }
99 if h.Options.Stdout {
100 stdout := websocketStreams.stdoutStream
101 if h.MaxBytesPerSec > 0 {
102 stdout = flowrate.NewWriter(stdout, h.MaxBytesPerSec)
103 }
104 opts.Stdout = stdout
105 }
106 if h.Options.Stderr {
107 stderr := websocketStreams.stderrStream
108 if h.MaxBytesPerSec > 0 {
109 stderr = flowrate.NewWriter(stderr, h.MaxBytesPerSec)
110 }
111 opts.Stderr = stderr
112 }
113 if h.Options.Tty {
114 opts.Tty = true
115 opts.TerminalSizeQueue = &translatorSizeQueue{resizeChan: websocketStreams.resizeChan}
116 }
117 // Start the SPDY client with connected streams. Output from the WebSocket server
118 // streams will be forwarded into the SPDY client. Report SPDY execution errors
119 // through the websocket error stream.
120 err = spdyExecutor.StreamWithContext(req.Context(), opts)
121 if err != nil {
122 //nolint:errcheck // Ignore writeStatus returned error
123 if statusErr, ok := err.(*apierrors.StatusError); ok {
124 websocketStreams.writeStatus(statusErr)
125 // Increment status code returned within status error.
126 metrics.IncStreamTranslatorRequest(req.Context(), strconv.Itoa(int(statusErr.Status().Code)))
127 } else if exitErr, ok := err.(exec.CodeExitError); ok && exitErr.Exited() {
128 websocketStreams.writeStatus(codeExitToStatusError(exitErr))
129 // Returned an exit code from the container, so not an error in
130 // stream translator--add StatusOK to metrics.
131 metrics.IncStreamTranslatorRequest(req.Context(), strconv.Itoa(http.StatusOK))
132 } else {
133 websocketStreams.writeStatus(apierrors.NewInternalError(err))
134 metrics.IncStreamTranslatorRequest(req.Context(), strconv.Itoa(http.StatusInternalServerError))
135 }
136 return
137 }
138
139 // Write the success status back to the WebSocket client.
140 //nolint:errcheck
141 websocketStreams.writeStatus(&apierrors.StatusError{ErrStatus: metav1.Status{
142 Status: metav1.StatusSuccess,
143 }})
144 metrics.IncStreamTranslatorRequest(req.Context(), strconv.Itoa(http.StatusOK))
145 }
146
147 // translatorSizeQueue feeds the size events from the WebSocket
148 // resizeChan into the SPDY client input. Implements TerminalSizeQueue
149 // interface.
150 type translatorSizeQueue struct {
151 resizeChan chan remotecommand.TerminalSize
152 }
153
154 func (t *translatorSizeQueue) Next() *remotecommand.TerminalSize {
155 size, ok := <-t.resizeChan
156 if !ok {
157 return nil
158 }
159 return &size
160 }
161
162 // codeExitToStatusError converts a passed CodeExitError to the type necessary
163 // to send through an error stream using "writeStatus".
164 func codeExitToStatusError(exitErr exec.CodeExitError) *apierrors.StatusError {
165 rc := exitErr.ExitStatus()
166 return &apierrors.StatusError{
167 ErrStatus: metav1.Status{
168 Status: metav1.StatusFailure,
169 Reason: constants.NonZeroExitCodeReason,
170 Details: &metav1.StatusDetails{
171 Causes: []metav1.StatusCause{
172 {
173 Type: constants.ExitCodeCauseType,
174 Message: fmt.Sprintf("%d", rc),
175 },
176 },
177 },
178 Message: fmt.Sprintf("command terminated with non-zero exit code: %v", exitErr),
179 },
180 }
181 }