/
customresource_proxy.go
166 lines (151 loc) · 5.52 KB
/
customresource_proxy.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Tencent is pleased to support the open source community by making TKEStack
* available.
*
* Copyright (C) 2012-2021 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 (
"crypto/tls"
"fmt"
"net"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"time"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apiserver/pkg/endpoints/handlers/responsewriters"
"k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/client-go/rest"
platforminternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/platform/internalversion"
"tkestack.io/tke/pkg/apiserver/authentication"
"tkestack.io/tke/pkg/platform/apiserver/filter"
"tkestack.io/tke/pkg/platform/proxy"
)
type CustomResourceHandler struct {
LoopbackClientConfig *rest.Config
}
var (
// Scheme is the default instance of runtime.Scheme to which types in the TKE API are already registered.
Scheme = runtime.NewScheme()
// Codecs provides access to encoding and decoding for the scheme
Codecs = serializer.NewCodecFactory(Scheme)
unversionedVersion = schema.GroupVersion{Group: "", Version: "v1"}
unversionedTypes = []runtime.Object{
&metav1.Status{},
}
)
func init() {
Scheme.AddUnversionedTypes(unversionedVersion, unversionedTypes...)
}
// ServeHTTP is a proxy for unregister custom resource
func (n *CustomResourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
requestInfo, ok := request.RequestInfoFrom(r.Context())
if !ok {
responsewriters.ErrorNegotiated(
apierrors.NewInternalError(fmt.Errorf("no RequestInfo found in the context")),
Codecs, schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}, w, r,
)
return
}
// crd resource start with /apis
if requestInfo.APIPrefix != "apis" {
responsewriters.ErrorNegotiated(
apierrors.NewInternalError(fmt.Errorf("request crd is validate")),
Codecs, schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}, w, r,
)
return
}
platformClient := platforminternalclient.NewForConfigOrDie(n.LoopbackClientConfig)
config, err := proxy.GetConfig(r.Context(), platformClient)
if err != nil {
responsewriters.ErrorNegotiated(
apierrors.NewInternalError(err),
Codecs, schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}, w, r,
)
return
}
TLSClientConfig := &tls.Config{}
TLSClientConfig.InsecureSkipVerify = true
if config.TLSClientConfig.CertData != nil && config.TLSClientConfig.KeyData != nil {
cert, err := tls.X509KeyPair(config.TLSClientConfig.CertData, config.TLSClientConfig.KeyData)
if err != nil {
responsewriters.ErrorNegotiated(
apierrors.NewGenericServerResponse(http.StatusUnauthorized, requestInfo.Verb, schema.GroupResource{
Group: requestInfo.APIGroup,
Resource: requestInfo.Resource,
}, requestInfo.Name,
err.Error(), 0, true),
Codecs, schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}, w, r,
)
return
}
TLSClientConfig.Certificates = []tls.Certificate{cert}
} else if config.BearerToken == "" {
responsewriters.ErrorNegotiated(
apierrors.NewGenericServerResponse(http.StatusUnauthorized, requestInfo.Verb, schema.GroupResource{
Group: requestInfo.APIGroup,
Resource: requestInfo.Resource,
}, requestInfo.Name,
fmt.Sprintf("%s has NO BearerToken", filter.ClusterFrom(r.Context())),
0, true),
Codecs, schema.GroupVersion{Group: requestInfo.APIGroup, Version: requestInfo.APIVersion}, w, r,
)
return
}
reserveProxy := httputil.NewSingleHostReverseProxy(&url.URL{
Scheme: "https",
// TODO: support apiserver with path
Host: strings.TrimPrefix(config.Host, "https://"),
})
reserveProxy.Transport = &http.Transport{
DisableKeepAlives: true,
DialContext: (&net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 1000,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: TLSClientConfig,
}
reserveProxy.Director = buildDirector(r, config)
reserveProxy.FlushInterval = 100 * time.Millisecond
reserveProxy.ServeHTTP(w, r)
}
func buildDirector(r *http.Request, config *rest.Config) func(req *http.Request) {
clusterName := filter.ClusterFrom(r.Context())
userName, tenantID := authentication.UsernameAndTenantID(r.Context())
return func(req *http.Request) {
req.Header.Set(filter.ClusterNameHeaderKey, clusterName)
req.Header.Set("X-Remote-User", userName)
req.Header.Set("X-Remote-Extra-TenantID", tenantID)
if config.BearerToken != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", config.BearerToken))
}
req.URL = &url.URL{
Scheme: "https",
// TODO: support apiserver with path
Host: strings.TrimPrefix(config.Host, "https://"),
Path: r.RequestURI,
}
}
}