Skip to content

Commit 5f243ee

Browse files
committed
apiserver: add feature gate AllowProxyRequestToClusters
Signed-off-by: scydas <[email protected]>
1 parent 82604f7 commit 5f243ee

File tree

4 files changed

+65
-15
lines changed

4 files changed

+65
-15
lines changed

pkg/kubeapiserver/apiserver.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,12 @@ func NewDefaultConfig() *Config {
6666
}
6767

6868
type ExtraConfig struct {
69-
SecretNamespace string
70-
AllowPediaClusterConfigReuse bool
71-
ExtraProxyRequestHeaderPrefixes []string
72-
AllowedProxySubresources map[schema.GroupResource]sets.Set[string]
69+
SecretNamespace string
70+
AllowPediaClusterConfigReuse bool
71+
ExtraProxyRequestHeaderPrefixes []string
72+
AllowedProxySubresources map[schema.GroupResource]sets.Set[string]
73+
EnableProxyPathForForwardRequest bool
74+
AllowForwardUnsyncResourceRequest bool
7375
}
7476

7577
type Config struct {
@@ -171,16 +173,20 @@ func (c completedConfig) New(delegationTarget genericapiserver.DelegationTarget)
171173
}
172174
proxy := proxyrest.NewRemoteProxyREST(c.GenericConfig.Serializer, connector)
173175

174-
// forward request
175-
genericserver.Handler.NonGoRestfulMux.HandlePrefix("/proxy/", http.StripPrefix("/proxy", proxy))
176+
if c.ExtraConfig.EnableProxyPathForForwardRequest {
177+
// forward request
178+
genericserver.Handler.NonGoRestfulMux.HandlePrefix("/proxy/", http.StripPrefix("/proxy", proxy))
179+
methods = sortedMethods
180+
}
176181

177182
// handle root discovery request
178183
discoveryHandler := WrapForwardRequestHandler(discoveryManager, proxy)
179184
genericserver.Handler.NonGoRestfulMux.Handle("/api", discoveryHandler)
180185
genericserver.Handler.NonGoRestfulMux.Handle("/apis", discoveryHandler)
181186

182187
resourceHandler := &ResourceHandler{
183-
minRequestTimeout: time.Duration(c.GenericConfig.MinRequestTimeout) * time.Second,
188+
allowForwardUnsyncResourceRequest: c.ExtraConfig.AllowForwardUnsyncResourceRequest,
189+
minRequestTimeout: time.Duration(c.GenericConfig.MinRequestTimeout) * time.Second,
184190

185191
delegate: delegate,
186192
proxy: proxy,

pkg/kubeapiserver/features.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package kubeapiserver
2+
3+
import (
4+
"k8s.io/apimachinery/pkg/util/runtime"
5+
utilfeature "k8s.io/apiserver/pkg/util/feature"
6+
"k8s.io/component-base/featuregate"
7+
)
8+
9+
const (
10+
// AllowProxyRequestToClusters is a feature gate for the apiserver to handle proxy and forward requests.
11+
//
12+
// owner: @scydas
13+
// alpha: v0.9.0
14+
AllowProxyRequestToClusters featuregate.Feature = "AllowProxyRequestToClusters"
15+
)
16+
17+
func init() {
18+
runtime.Must(utilfeature.DefaultMutableFeatureGate.Add(defaultInternalStorageFeatureGates))
19+
}
20+
21+
// defaultInternalStorageFeatureGates consists of all known custom internalstorage feature keys.
22+
// To add a new feature, define a key for it above and add it here.
23+
var defaultInternalStorageFeatureGates = map[featuregate.Feature]featuregate.FeatureSpec{
24+
AllowProxyRequestToClusters: {Default: false, PreRelease: featuregate.Alpha},
25+
}

pkg/kubeapiserver/options.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/spf13/pflag"
88
"k8s.io/apimachinery/pkg/runtime/schema"
99
"k8s.io/apimachinery/pkg/util/sets"
10+
utilfeature "k8s.io/apiserver/pkg/util/feature"
1011

1112
proxyrest "github.com/clusterpedia-io/clusterpedia/pkg/kubeapiserver/resourcerest/proxy"
1213
)
@@ -18,6 +19,9 @@ type Options struct {
1819

1920
AllowedProxySubresources []string
2021
ExtraProxyRequestHeaderPrefixes []string
22+
23+
EnableProxyPathForForwardRequest bool
24+
AllowForwardUnsyncResourceRequest bool
2125
}
2226

2327
func NewOptions() *Options {
@@ -40,12 +44,20 @@ func (o *Options) AddFlags(fs *pflag.FlagSet) {
4044
// If you have a better solution, please submit an issue!
4145
fs.StringSliceVar(&o.AllowedProxySubresources, "allowed-proxy-subresources", o.AllowedProxySubresources, ""+
4246
"List of subresources that support proxying requests to the specified cluster, formatted as '[resource/subresource],[subresource],...'. "+
43-
fmt.Sprintf("Supported proxy subresources include %q", strings.Join(resources, ",")),
47+
fmt.Sprintf("Supported proxy subresources include %q.", strings.Join(resources, ",")),
4448
)
4549

4650
fs.BoolVar(&o.AllowPediaClusterConfigForProxyRequest, "allow-pediacluster-config-for-proxy-request", o.AllowPediaClusterConfigForProxyRequest, ""+
4751
"Allow proxy requests to use the cluster configuration from PediaCluster when authentication information cannot be got from the header.",
4852
)
53+
54+
fs.BoolVar(&o.EnableProxyPathForForwardRequest, "enable-proxy-path-for-forward-request", o.EnableProxyPathForForwardRequest, ""+
55+
"Add a '/proxy' path in the API to proxy any request.",
56+
)
57+
fs.BoolVar(&o.AllowForwardUnsyncResourceRequest, "allow-forward-unsync-resource-request", o.AllowForwardUnsyncResourceRequest, ""+
58+
"Allow forwarding requests for unsynchronized resource types."+
59+
"By default, only requests for resource types configured in PediaCluster can be forwarded.",
60+
)
4961
}
5062

5163
var supportedProxyCoreSubresources = map[string][]string{
@@ -55,6 +67,11 @@ var supportedProxyCoreSubresources = map[string][]string{
5567
}
5668

5769
func (o *Options) Config() (*ExtraConfig, error) {
70+
if !utilfeature.DefaultFeatureGate.Enabled(AllowProxyRequestToClusters) && (len(o.AllowedProxySubresources) != 0 ||
71+
o.EnableProxyPathForForwardRequest || o.AllowForwardUnsyncResourceRequest) {
72+
return nil, fmt.Errorf("please enable feature gate %s to allow apiserver to handle the proxy and forward requests", AllowProxyRequestToClusters)
73+
}
74+
5875
subresources := make(map[schema.GroupResource]sets.Set[string])
5976

6077
for _, subresource := range o.AllowedProxySubresources {
@@ -89,7 +106,9 @@ func (o *Options) Config() (*ExtraConfig, error) {
89106
}
90107
}
91108
return &ExtraConfig{
92-
AllowPediaClusterConfigReuse: o.AllowPediaClusterConfigForProxyRequest,
93-
AllowedProxySubresources: subresources,
109+
AllowPediaClusterConfigReuse: o.AllowPediaClusterConfigForProxyRequest,
110+
AllowedProxySubresources: subresources,
111+
EnableProxyPathForForwardRequest: o.EnableProxyPathForForwardRequest,
112+
AllowForwardUnsyncResourceRequest: o.AllowForwardUnsyncResourceRequest,
94113
}, nil
95114
}

pkg/kubeapiserver/resource_handler.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ import (
2727
)
2828

2929
type ResourceHandler struct {
30-
couldForwardAnyRequest bool
31-
minRequestTimeout time.Duration
32-
delegate http.Handler
33-
proxy http.Handler
30+
allowForwardUnsyncResourceRequest bool
31+
minRequestTimeout time.Duration
32+
delegate http.Handler
33+
proxy http.Handler
3434

3535
rest *RESTManager
3636
discovery *discovery.DiscoveryManager
@@ -62,7 +62,7 @@ func (r *ResourceHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
6262
shouldForwardRequest = true
6363
}
6464

65-
if shouldForwardRequest && r.couldForwardAnyRequest {
65+
if shouldForwardRequest && r.allowForwardUnsyncResourceRequest {
6666
r.proxy.ServeHTTP(w, req)
6767
return
6868
}

0 commit comments

Comments
 (0)