Skip to content

Commit

Permalink
Merge pull request #719 from scydas/covert-connect
Browse files Browse the repository at this point in the history
apiserver: fix covert connect methods to kube verbs
  • Loading branch information
Iceber authored Jan 6, 2025
2 parents 4b74b9e + cf78b54 commit c77bd26
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
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

0 comments on commit c77bd26

Please sign in to comment.