-
Notifications
You must be signed in to change notification settings - Fork 328
/
filter.go
150 lines (132 loc) · 4.74 KB
/
filter.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
/*
* 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 filter
import (
"bytes"
"context"
"io/ioutil"
"net/http"
genericrequest "k8s.io/apiserver/pkg/endpoints/request"
)
const clusterContextKey = "clusterName"
const requestBodyKey = "requestBody"
const fuzzyResourceContextKey = "fuzzyResourceName"
const namespaceContextKey = "namespace"
const namespaceParamKey = "namespace"
// ClusterNameHeaderKey is the header name of cluster.
const ClusterNameHeaderKey = "X-TKE-ClusterName"
// ProjectNameHeaderKey is the header name of project.
const ProjectNameHeaderKey = "X-TKE-ProjectName"
// FuzzyResourceNameHeaderKey is the header name of fuzzy resource query name.
const FuzzyResourceNameHeaderKey = "X-TKE-FuzzyResourceName"
// RequestBody represents the body of HTTP request.
type RequestBody struct {
Data []byte
ContentType string
}
// ClusterFrom get the cluster name from request context.
func ClusterFrom(ctx context.Context) string {
clusterName, ok := ctx.Value(clusterContextKey).(string)
if !ok {
return ""
}
return clusterName
}
// FuzzyResourceFrom get the fuzzy resource name from request context.
func FuzzyResourceFrom(ctx context.Context) string {
fuzzyResourceName, ok := ctx.Value(fuzzyResourceContextKey).(string)
if !ok {
return ""
}
return fuzzyResourceName
}
// RequestBodyFrom returns the RequestBody object.
func RequestBodyFrom(ctx context.Context) (*RequestBody, bool) {
val := ctx.Value(requestBodyKey)
if val == nil {
return nil, false
}
obj, ok := val.(*RequestBody)
return obj, ok
}
// ClusterFrom get the cluster name from request context.
func NamespaceFrom(ctx context.Context) string {
namespace, ok := ctx.Value(namespaceContextKey).(string)
if !ok {
return ""
}
return namespace
}
// WithCluster creates an http handler that tries to get the cluster name from
// the given request, and then stores any such cluster name found onto the
// provided context for the request.
func WithCluster(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
clusterName := req.Header.Get(ClusterNameHeaderKey)
if clusterName != "" {
req = req.WithContext(genericrequest.WithValue(req.Context(), clusterContextKey, clusterName))
}
handler.ServeHTTP(w, req)
})
}
// WithFuzzyResource adds the fuzzy resource name to the context of the http
// access chain.
func WithFuzzyResource(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
fuzzyResourceName := req.Header.Get(FuzzyResourceNameHeaderKey)
if fuzzyResourceName != "" {
req = req.WithContext(genericrequest.WithValue(req.Context(), fuzzyResourceContextKey, fuzzyResourceName))
}
handler.ServeHTTP(w, req)
})
}
// WithRequestBody adds the request body to the context of the http access chain.
func WithRequestBody(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
method := req.Method
if method == http.MethodPut || method == http.MethodPatch || method == http.MethodPost || method == http.MethodDelete {
if req.Body != nil {
var bodyBytes []byte
bodyBytes, _ = ioutil.ReadAll(req.Body)
req.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
contentType := req.Header.Get("Content-Type")
if len(contentType) == 0 {
contentType = "application/json"
}
requestBody := &RequestBody{
Data: bodyBytes,
ContentType: contentType,
}
req = req.WithContext(genericrequest.WithValue(req.Context(), requestBodyKey, requestBody))
}
}
handler.ServeHTTP(w, req)
})
}
// WithNamespace creates an http handler that tries to get the namespace id from
// the given request query params, and then stores any such namespace found onto the
// provided context for the request.
func WithNamespace(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
keys, ok := req.URL.Query()[namespaceParamKey]
if ok && len(keys[0]) > 0 {
req = req.WithContext(genericrequest.WithValue(req.Context(), namespaceContextKey, keys[0]))
}
handler.ServeHTTP(w, req)
})
}