@@ -19,16 +19,18 @@ import (
19
19
"k8s.io/klog/v2"
20
20
21
21
clusterv1alpha2 "github.com/clusterpedia-io/api/cluster/v1alpha2"
22
+ clusterpedia "github.com/clusterpedia-io/api/clusterpedia"
22
23
clusterlister "github.com/clusterpedia-io/clusterpedia/pkg/generated/listers/cluster/v1alpha2"
23
24
"github.com/clusterpedia-io/clusterpedia/pkg/kubeapiserver/discovery"
24
25
"github.com/clusterpedia-io/clusterpedia/pkg/kubeapiserver/resourcerest"
25
26
"github.com/clusterpedia-io/clusterpedia/pkg/utils/request"
26
27
)
27
28
28
29
type ResourceHandler struct {
29
- minRequestTimeout time.Duration
30
- delegate http.Handler
31
- proxy http.Handler
30
+ couldForwardAnyRequest bool
31
+ minRequestTimeout time.Duration
32
+ delegate http.Handler
33
+ proxy http.Handler
32
34
33
35
rest * RESTManager
34
36
discovery * discovery.DiscoveryManager
@@ -45,7 +47,26 @@ func (r *ResourceHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
45
47
return
46
48
}
47
49
48
- shouldForwardRequest := HasForwardRequestHeader (req )
50
+ var shouldForwardRequest bool
51
+ switch requestInfo .Verb {
52
+ case "list" , "watch" :
53
+ query := request .RequestQueryFrom (req .Context ())
54
+ if labelSelector := query .Get ("labelSelector" ); labelSelector != "" {
55
+ if newLabelSelector , trimed := trimForwardLabelForLabelSelectorQuery (labelSelector ); trimed {
56
+ query .Set ("labelSelector" , newLabelSelector )
57
+ shouldForwardRequest = true
58
+ }
59
+ }
60
+ }
61
+ if ! shouldForwardRequest && HasForwardRequestHeader (req ) {
62
+ shouldForwardRequest = true
63
+ }
64
+
65
+ if shouldForwardRequest && r .couldForwardAnyRequest {
66
+ r .proxy .ServeHTTP (w , req )
67
+ return
68
+ }
69
+
49
70
// handle discovery request
50
71
if ! requestInfo .IsResourceRequest {
51
72
if shouldForwardRequest {
@@ -180,6 +201,19 @@ func checkClusterAndWarning(ctx context.Context, cluster *clusterv1alpha2.PediaC
180
201
}
181
202
}
182
203
204
+ var forwardLabelLength = len (clusterpedia .SearchLabelForwardRequest )
205
+
206
+ func trimForwardLabelForLabelSelectorQuery (selector string ) (string , bool ) {
207
+ if i := strings .Index (selector , clusterpedia .SearchLabelForwardRequest ); i != - 1 {
208
+ l , other := selector [:i ], selector [i + forwardLabelLength :]
209
+ if i := strings .Index (other , "," ); i != - 1 {
210
+ l += other [i :]
211
+ }
212
+ return strings .TrimPrefix (strings .TrimSuffix (l , "," ), "," ), true
213
+ }
214
+ return selector , false
215
+ }
216
+
183
217
func HasForwardRequestHeader (req * http.Request ) bool {
184
218
value := req .Header .Get ("x-clusterpedia-forward" )
185
219
return strings .ToLower (value ) == "true"
0 commit comments