-
Notifications
You must be signed in to change notification settings - Fork 248
/
customresource.go
311 lines (296 loc) · 10.7 KB
/
customresource.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* Tencent is pleased to support the open source community by making Blueking Container Service available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package v4http
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"path/filepath"
"strconv"
"strings"
"github.com/Tencent/bk-bcs/bcs-common/common/blog"
simplejson "github.com/bitly/go-simplejson"
restful "github.com/emicklei/go-restful"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
const (
// default kube custom resource definition url
defaultCRDURL = "/apis/apiextensions.k8s.io/v1beta1/customresourcedefinitions"
// default custom resource definition apiVersion we use
defaultAPIVersion = "apiextensions.k8s.io/v1beta1"
defaultMesosVersion = "v4"
defaultNamespaceURL = "api/v1/namespaces"
)
// kubeProxy proxy for custom resource
type kubeProxy struct {
// kube config details
config *rest.Config
// client for namespace check
client *http.Client
// custom resource proxy
crsProxy *httputil.ReverseProxy
// custom resource definition proxy
crdsProxy *httputil.ReverseProxy
}
func (proxy *kubeProxy) init() error {
// create specified tranport from kube config
httpRoundTripper, err := rest.TransportFor(proxy.config)
if err != nil {
blog.Errorf("bcs-mesos-driver initialize kube proxy transport failed, message: %s, config object: %v", err.Error(),
proxy.config)
return fmt.Errorf("bcs-mesos-driver create CustomResource transport failed")
}
proxy.client = &http.Client{
Transport: httpRoundTripper,
}
proxy.crsProxy = &httputil.ReverseProxy{
Director: proxy.customResourceNamespaceValidate,
Transport: httpRoundTripper,
}
proxy.crdsProxy = &httputil.ReverseProxy{
Director: proxy.apiVersionReqConvert,
Transport: httpRoundTripper,
ModifyResponse: proxy.apiVersionResConvert,
}
blog.Infof("bcs-mesos-driver init local proxy for custom resource proxy successfully.")
return nil
}
func (proxy *kubeProxy) isNamespaceActive(namespace string) bool {
reqURL := fmt.Sprintf("%s/%s/%s", proxy.config.Host, defaultNamespaceURL, namespace)
nsReq, _ := http.NewRequest(http.MethodGet, reqURL, nil)
resp, err := proxy.client.Do(nsReq)
if err != nil {
blog.Errorf("check %s failed, %s", reqURL, err.Error())
return false
}
if resp.StatusCode == http.StatusNotFound {
blog.Infof("%s, namespace Not found", reqURL)
return false
}
if resp.StatusCode == http.StatusOK {
blog.Infof("%s, namespace %s is Found!", reqURL, namespace)
return true
}
// others
return false
}
func (proxy *kubeProxy) createNamespace(namespace string) error {
reqURL := fmt.Sprintf("%s/%s", proxy.config.Host, defaultNamespaceURL)
ns := &corev1.Namespace{
TypeMeta: metav1.TypeMeta{
Kind: "Namespace",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
},
}
nsBody, _ := json.Marshal(ns)
nsReq, _ := http.NewRequest(http.MethodPost, reqURL, bytes.NewBuffer(nsBody))
nsReq.Header.Add("Content-Type", "application/json")
resp, err := proxy.client.Do(nsReq)
if err != nil {
blog.Errorf("create %s namespace failed. request body: %s", reqURL, string(nsBody))
return err
}
if resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusCreated ||
resp.StatusCode == http.StatusAccepted {
blog.Infof("create namespace %s [%s] success.", reqURL, namespace)
return nil
}
// others
return fmt.Errorf("unknow reason for creation ns failed: %d", resp.StatusCode)
}
func (proxy *kubeProxy) customResourceNamespaceValidate(req *http.Request) {
if req.Method != http.MethodPost {
return
}
// validate namespace exist
allBytes, err := ioutil.ReadAll(req.Body)
if err != nil {
blog.Errorf("Reading custom resource Request for namespace validation failed, %s. URL: %s", err.Error(),
req.URL.String())
return
}
if len(allBytes) == 0 {
blog.Errorf("bcs-mesos-driver get empty body when in POST or PUT request, URL: %s, Method: %s", req.URL.String(),
req.Method)
return
}
buffer := bytes.NewBuffer(allBytes)
req.Body = ioutil.NopCloser(buffer)
req.ContentLength = int64(buffer.Len())
req.GetBody = func() (io.ReadCloser, error) {
r := bytes.NewReader(allBytes)
return ioutil.NopCloser(r), nil
}
jsonObj, err := simplejson.NewJson(allBytes)
if err != nil {
blog.Errorf("Custom Resource POST data is not expected json, %s. URL: %s. origin data: %s", err.Error(),
req.URL.String(), string(allBytes))
return
}
// fix when namespace is empty
meta, ok := jsonObj.CheckGet("metadata")
if !ok {
blog.Errorf("Custom Resource Post to %s lost meta data", req.URL.String())
return
}
ns, ok := meta.CheckGet("namespace")
if !ok {
blog.Errorf("Custom Resource Post to %s lost namespace data", req.URL.String())
return
}
namespace, _ := ns.String()
if len(namespace) == 0 {
blog.Errorf("Custom Resource POST to %s lost Namespace. %s", req.URL.String(), err.Error())
return
}
if proxy.isNamespaceActive(namespace) {
return
}
blog.Infof("namespace %s do not exist, create first...", namespace)
if err := proxy.createNamespace(namespace); err != nil {
blog.Errorf("create Namespace for %s failed, %s", req.URL.String(), err.Error())
return
}
blog.Infof("create Namespace for %s success.", req.URL.String())
}
// apiVersionReqConvert convert all request
// attention: all json key is
func (proxy *kubeProxy) apiVersionReqConvert(req *http.Request) {
if req.Method == http.MethodGet || req.Method == http.MethodDelete {
blog.V(3).Infof("bcs-mesos-driver skip %s convertion. Method: %s", req.URL.Path, req.Method)
return
}
allBytes, err := ioutil.ReadAll(req.Body)
if err != nil {
blog.Errorf("bcs-mesos-driver custom resource definition Request Convert failed, %s", err.Error())
return
}
if len(allBytes) == 0 {
blog.Errorf("bcs-mesos-driver get empty body when in POST or PUT request, URL: %s, Method: %s", req.URL.String(),
req.Method)
return
}
jsonObj, err := simplejson.NewJson(allBytes)
if err != nil {
blog.Errorf("bcs-mesos-driver decode json json failed, %s. details request body: %s", err.Error(), string(allBytes))
return
}
blog.V(3).Infof("forwarding URL %s old body: %s", req.URL.String(), string(allBytes))
jsonObj.Set("apiVersion", defaultAPIVersion)
newBody, err := jsonObj.MarshalJSON()
if err != nil {
blog.Errorf("bcs-mesos-driver new custom resource definition Request json Marshal failed, %s. URL: %s", err.Error(),
req.URL.Path)
return
}
blog.V(3).Infof("forwarding URL %s new body: %s", req.URL.String(), string(newBody))
buffer := bytes.NewBuffer(newBody)
req.Body = ioutil.NopCloser(buffer)
req.ContentLength = int64(buffer.Len())
req.GetBody = func() (io.ReadCloser, error) {
r := bytes.NewReader(newBody)
return ioutil.NopCloser(r), nil
}
blog.Infof("bcs-mesos-driver custom resource definition [%s] Request convert success.", req.URL.Path)
}
func (proxy *kubeProxy) apiVersionResConvert(resp *http.Response) error {
allBytes, _ := ioutil.ReadAll(resp.Body)
if len(allBytes) == 0 {
blog.Errorf("mesos-driver response for %s is Empty", resp.Request.URL.String())
return nil
}
tmpStr := string(allBytes)
newStr := strings.Replace(tmpStr, defaultAPIVersion, defaultMesosVersion, 1)
buffer := bytes.NewBuffer([]byte(newStr))
resp.Body = ioutil.NopCloser(buffer)
resp.ContentLength = int64(buffer.Len())
// setting header for contentLength
resp.Header.Set("Content-Length", strconv.Itoa(buffer.Len()))
blog.Infof("mesos-driver convert custom resource definition [%s] response success", resp.Request.URL.String())
return nil
}
func (s *Scheduler) initKube() error {
if s.config.KubeConfig == "" {
// incluster configuration for mesos-driver container
// blog.Infof("bcs-mesos-driver use in-cluster configuration...")
// config, err := rest.InClusterConfig()
// if err != nil {
// blog.Errorf("bcs-mesos-driver create in-cluster configuraiont failed, %s", err.Error())
// return nil, err
// }
// return config, nil
blog.Infof("bcs-mesos-driver no kubeconfig detected, skip all compatible custom resource initialization")
// compatible with history version, clean in-cluster code
return nil
}
// outcluster deployment configuration for mesos-driver process
blog.Infof("bcs-mesos-driver use process deployment with KubeConfig %s", s.config.KubeConfig)
config, err := clientcmd.BuildConfigFromFlags("", s.config.KubeConfig)
if err != nil {
blog.Errorf("bcs-mesos-driver build configuration with kubeConfig %s failed, %s", s.config.KubeConfig, err.Error())
return err
}
// create reversproxy for custom resource
s.localProxy = &kubeProxy{
config: config,
}
return s.localProxy.init()
}
// customResourceForwarding xxx
// transparent forwarding for custom resource
// * block all watch request
// * redirect mesos driver url to api custom resource url
func (s *Scheduler) customResourceForwarding(req *restful.Request, resp *restful.Response) {
if strings.Contains(req.Request.URL.Path, "watch") {
blog.Warnf("bcs-mesos-driver custom resource do not support watch request. URL: %s", req.Request.URL.Path)
resp.WriteErrorString(http.StatusNotFound, http.StatusText(http.StatusNotFound))
return
}
// change Path & Host
rawRequest := req.Request
original := rawRequest.URL.String()
mesosURL := req.PathParameter("uri")
kubeURL := filepath.Join("/apis", mesosURL)
tmpURL, _ := url.Parse(s.localProxy.config.Host)
rawRequest.URL.Scheme = tmpURL.Scheme
rawRequest.URL.Host = tmpURL.Host
rawRequest.URL.Path = kubeURL
blog.Infof("bcs-mesos-driver custom resource forwarding from %s to %s", original, rawRequest.URL.String())
s.localProxy.crsProxy.ServeHTTP(resp, rawRequest)
}
func (s *Scheduler) customResourceDefinitionForwarding(req *restful.Request, resp *restful.Response) {
name := req.PathParameter("name")
kubeURL := defaultCRDURL
if len(name) != 0 {
kubeURL = filepath.Join(defaultCRDURL, name)
}
rawRequest := req.Request
original := rawRequest.URL.String()
tmpURL, _ := url.Parse(s.localProxy.config.Host)
rawRequest.URL.Scheme = tmpURL.Scheme
rawRequest.URL.Host = tmpURL.Host
rawRequest.URL.Path = kubeURL
blog.Infof("bcs-mesos-driver CustomResourceDefinition forwarding from %s to %s", original, rawRequest.URL.String())
s.localProxy.crdsProxy.ServeHTTP(resp, rawRequest)
}