/
exec.go
128 lines (112 loc) · 3.8 KB
/
exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Tencent is pleased to support the open source community by making TKEStack
* available.
*
* Copyright (C) 2012-2019 Tencent. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of the
* License at
*
* https://opensource.org/licenses/Apache-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package storage
import (
"context"
"fmt"
"net/http"
"net/url"
corev1api "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
utilnet "k8s.io/apimachinery/pkg/util/net"
"k8s.io/apimachinery/pkg/util/proxy"
"k8s.io/apiserver/pkg/registry/rest"
platforminternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/platform/internalversion"
"tkestack.io/tke/pkg/platform/util"
)
// Support both GET and POST methods. We must support GET for browsers that want
// to use WebSockets.
var upgradeableMethods = []string{"GET", "POST"}
// ExecREST implements the exec endpoint for a Pod
type ExecREST struct {
platformClient platforminternalclient.PlatformInterface
}
// New creates a new Pod log options object
func (r *ExecREST) New() runtime.Object {
return &corev1api.PodExecOptions{}
}
// NewConnectOptions returns versioned resource that represents proxy parameters
func (r *ExecREST) NewConnectOptions() (runtime.Object, bool, string) {
return &corev1api.PodExecOptions{}, false, ""
}
// ConnectMethods returns the methods supported by exec
func (r *ExecREST) ConnectMethods() []string {
return upgradeableMethods
}
// Connect returns a handler for the pod exec proxy
func (r *ExecREST) Connect(ctx context.Context, name string, opts runtime.Object, responder rest.Responder) (http.Handler, error) {
execOpts, ok := opts.(*corev1api.PodExecOptions)
if !ok {
return nil, fmt.Errorf("invalid options object: %#v", opts)
}
location, transport, token, err := util.APIServerLocation(ctx, r.platformClient)
if err != nil {
return nil, err
}
params := url.Values{}
if execOpts.Container != "" {
params.Add("container", execOpts.Container)
}
if execOpts.Command != nil {
params["command"] = execOpts.Command
}
if execOpts.Stdin {
params.Add("stdin", "true")
} else {
params.Add("stdin", "false")
}
if execOpts.Stdout {
params.Add("stdout", "true")
} else {
params.Add("stdout", "false")
}
if execOpts.Stderr {
params.Add("stderr", "true")
} else {
params.Add("stderr", "false")
}
if execOpts.TTY {
params.Add("tty", "true")
} else {
params.Add("tty", "false")
}
location.RawQuery = params.Encode()
return &execHandler{
upgradeAwareHandler: newThrottledUpgradeAwareProxyHandler(location, transport, false, true, responder),
token: token,
}, nil
}
type execHandler struct {
upgradeAwareHandler *proxy.UpgradeAwareHandler
token string
}
func (h *execHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
newReq := req.WithContext(req.Context())
newReq.Header = utilnet.CloneHeader(req.Header)
if h.token != "" {
newReq.Header.Add("Authorization", fmt.Sprintf("Bearer %s", h.token))
}
h.upgradeAwareHandler.ServeHTTP(w, newReq)
}
func newThrottledUpgradeAwareProxyHandler(location *url.URL, transport http.RoundTripper, wrapTransport, upgradeRequired bool, responder rest.Responder) *proxy.UpgradeAwareHandler {
handler := proxy.NewUpgradeAwareHandler(location, transport, wrapTransport, upgradeRequired, proxy.NewErrorResponder(responder))
handler.InterceptRedirects = true
handler.RequireSameHostRedirects = true
handler.MaxBytesPerSec = 0
return handler
}