Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

apiserver: fix covert connect methods to kube verbs #719

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/apiserver/registry/clusterpedia/resources/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var _ genericrest.SingularNameProvider = &REST{}
// NewREST returns a RESTStorage object that will work against API services
func NewREST(resourceHandler http.Handler, methods []string) *REST {
if len(methods) == 0 {
methods = []string{"Get"}
methods = []string{"GET"}
}
return &REST{
methods: methods,
Expand Down
8 changes: 5 additions & 3 deletions pkg/kubeapiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,18 @@ func (c completedConfig) New(delegationTarget genericapiserver.DelegationTarget)

controller := NewClusterResourceController(restManager, discoveryManager, c.ExtraConfig.InformerFactory.Cluster().V1alpha2().PediaClusters())

methodSet := sets.Set[string]{}
methodSet := sets.New("GET")
for _, rest := range proxyrest.GetSubresourceRESTs(controller) {
restManager.preRegisterSubresource(subresource{
if err := restManager.preRegisterSubresource(subresource{
gr: rest.ParentGroupResource(),
kind: rest.ParentKind(),
namespaced: rest.Namespaced(),

name: rest.Subresource(),
connecter: rest,
})
}); err != nil {
return nil, nil, err
}
methodSet.Insert(rest.ConnectMethods()...)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/kubeapiserver/resourcerest/proxy/subresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func GetSubresourceRESTs(connGetter ClusterConnectionGetter) []*PodSubresourceRe
parent: schema.GroupResource{Group: "", Resource: "pods"},
parentKind: "Pod",
namespaced: true,
methods: []string{"GET"},
methods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"},
upgradeRequired: false,
options: &api.PodProxyOptions{},
connGetter: connGetter,
Expand Down
31 changes: 29 additions & 2 deletions pkg/kubeapiserver/restmanager.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kubeapiserver

import (
"fmt"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -34,6 +35,21 @@ import (
"github.com/clusterpedia-io/clusterpedia/pkg/storage"
)

// toDiscoveryKubeVerb maps an action.Verb to the logical kube verb, used for discovery
var toDiscoveryKubeVerb = map[string]string{
"CONNECT": "", // do not list in discovery.
"DELETE": "delete",
"DELETECOLLECTION": "deletecollection",
"GET": "get",
"LIST": "list",
"PATCH": "patch",
"POST": "create",
"PROXY": "proxy",
"PUT": "update",
"WATCH": "watch",
"WATCHLIST": "watch",
}

type RESTManager struct {
serializer runtime.NegotiatedSerializer
storageFactory storage.StorageFactory
Expand Down Expand Up @@ -116,12 +132,22 @@ type subresource struct {
}

// preRegisterSubresource is non-concurrently safe and only called at initialization time
func (m *RESTManager) preRegisterSubresource(subresource subresource) {
func (m *RESTManager) preRegisterSubresource(subresource subresource) error {
var verbs []string
for _, method := range subresource.connecter.ConnectMethods() {
if verb := toDiscoveryKubeVerb[method]; verb != "" {
verbs = append(verbs, verb)
}
}
if len(verbs) == 0 {
return fmt.Errorf("resource['%s/%s'] available verbs are empty", subresource.gr.String(), subresource.name)
}

objGVK := subresource.connecter.(rest.Storage).New().GetObjectKind().GroupVersionKind()
apiResource := metav1.APIResource{
Name: subresource.gr.Resource + "/" + subresource.name,
Namespaced: subresource.namespaced,
Verbs: subresource.connecter.ConnectMethods(),
Verbs: verbs,
Kind: objGVK.Kind,
}

Expand All @@ -143,6 +169,7 @@ func (m *RESTManager) preRegisterSubresource(subresource subresource) {
RequestScope: requestScope,
Storage: subresource.connecter,
}
return nil
}

func (m *RESTManager) GetAPIGroups() map[string]metav1.APIGroup {
Expand Down
Loading