mirror of
https://github.com/niusmallnan/steve.git
synced 2026-01-29 21:38:35 +00:00
Add column support
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
package server
|
||||
package publicapi
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/rancher/naok/pkg/accesscontrol"
|
||||
"github.com/rancher/naok/pkg/attributes"
|
||||
k8sproxy "github.com/rancher/naok/pkg/proxy"
|
||||
"github.com/rancher/naok/pkg/resources/schema"
|
||||
"github.com/rancher/naok/pkg/server/router"
|
||||
"github.com/rancher/norman/pkg/api"
|
||||
"github.com/rancher/norman/pkg/types"
|
||||
"github.com/rancher/norman/pkg/urlbuilder"
|
||||
@@ -16,29 +16,30 @@ import (
|
||||
"k8s.io/client-go/rest"
|
||||
)
|
||||
|
||||
func newAPIServer(cfg *rest.Config, sf schema.Factory) (http.Handler, error) {
|
||||
func NewHandler(cfg *rest.Config, sf schema.Factory) (http.Handler, error) {
|
||||
var (
|
||||
err error
|
||||
)
|
||||
|
||||
a := &apiServer{
|
||||
Router: mux.NewRouter(),
|
||||
sf: sf,
|
||||
server: api.NewAPIServer(),
|
||||
server: api.DefaultAPIServer(),
|
||||
}
|
||||
a.server.AccessControl = accesscontrol.NewAccessControl()
|
||||
|
||||
a.Router.NotFoundHandler, err = k8sproxy.Handler("/", cfg)
|
||||
proxy, err := k8sproxy.Handler("/", cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
a.Router.StrictSlash(true)
|
||||
a.server.AccessControl = accesscontrol.NewAccessControl()
|
||||
return a, a.routes()
|
||||
return router.Routes(router.Handlers{
|
||||
K8sResource: a.apiHandler(k8sAPI),
|
||||
GenericResource: a.apiHandler(nil),
|
||||
K8sProxy: proxy,
|
||||
}), nil
|
||||
}
|
||||
|
||||
type apiServer struct {
|
||||
*mux.Router
|
||||
sf schema.Factory
|
||||
server *api.Server
|
||||
}
|
||||
@@ -77,3 +78,21 @@ func (a *apiServer) Schema(base string, schema *types.Schema) string {
|
||||
}
|
||||
return urlbuilder.ConstructBasicURL(base, "v1", strings.ToLower(schema.ID))
|
||||
}
|
||||
|
||||
type APIFunc func(schema.Factory, *types.APIRequest)
|
||||
|
||||
func (a *apiServer) apiHandler(apiFunc APIFunc) http.Handler {
|
||||
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||
a.api(rw, req, apiFunc)
|
||||
})
|
||||
}
|
||||
|
||||
func (a *apiServer) api(rw http.ResponseWriter, req *http.Request, apiFunc APIFunc) {
|
||||
apiOp, ok := a.common(rw, req)
|
||||
if ok {
|
||||
if apiFunc != nil {
|
||||
apiFunc(a.sf, apiOp)
|
||||
}
|
||||
a.server.Handle(apiOp)
|
||||
}
|
||||
}
|
||||
38
pkg/server/publicapi/handlers.go
Normal file
38
pkg/server/publicapi/handlers.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package publicapi
|
||||
|
||||
import (
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/rancher/naok/pkg/attributes"
|
||||
"github.com/rancher/naok/pkg/resources/schema"
|
||||
"github.com/rancher/norman/pkg/types"
|
||||
runtimeschema "k8s.io/apimachinery/pkg/runtime/schema"
|
||||
)
|
||||
|
||||
func k8sAPI(sf schema.Factory, apiOp *types.APIRequest) {
|
||||
vars := mux.Vars(apiOp.Request)
|
||||
group := vars["group"]
|
||||
if group == "core" {
|
||||
group = ""
|
||||
}
|
||||
|
||||
apiOp.Name = vars["name"]
|
||||
apiOp.Type = sf.ByGVR(runtimeschema.GroupVersionResource{
|
||||
Version: vars["version"],
|
||||
Group: group,
|
||||
Resource: vars["resource"],
|
||||
})
|
||||
|
||||
nOrN := vars["nameorns"]
|
||||
if nOrN != "" {
|
||||
schema := apiOp.Schemas.Schema(apiOp.Type)
|
||||
if attributes.Namespaced(schema) {
|
||||
vars["namespace"] = nOrN
|
||||
} else {
|
||||
vars["name"] = nOrN
|
||||
}
|
||||
}
|
||||
|
||||
if namespace := vars["namespace"]; namespace != "" {
|
||||
apiOp.Namespaces = []string{namespace}
|
||||
}
|
||||
}
|
||||
29
pkg/server/router/router.go
Normal file
29
pkg/server/router/router.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
type Handlers struct {
|
||||
K8sResource http.Handler
|
||||
GenericResource http.Handler
|
||||
K8sProxy http.Handler
|
||||
}
|
||||
|
||||
func Routes(h Handlers) http.Handler {
|
||||
m := mux.NewRouter()
|
||||
m.UseEncodedPath()
|
||||
m.StrictSlash(true)
|
||||
m.NotFoundHandler = h.K8sProxy
|
||||
|
||||
m.Path("/v1/{type:schemas}/{name:.*}").Handler(h.GenericResource)
|
||||
m.Path("/v1/{group}.{version}.{resource}").Handler(h.K8sResource)
|
||||
m.Path("/v1/{group}.{version}.{resource}/{nameorns}").Handler(h.K8sResource)
|
||||
m.Path("/v1/{group}.{version}.{resource}/{namespace}/{name}").Handler(h.K8sResource)
|
||||
m.Path("/v1/{type}").Handler(h.GenericResource)
|
||||
m.Path("/v1/{type}/{name}").Handler(h.GenericResource)
|
||||
|
||||
return m
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/rancher/naok/pkg/attributes"
|
||||
"github.com/rancher/norman/pkg/types"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
)
|
||||
|
||||
type APIFunc func(*types.APIRequest)
|
||||
|
||||
func (a *apiServer) routes() error {
|
||||
a.Path("/v1/{type:schemas}/{name:.*}").Handler(a.handle(nil))
|
||||
a.Path("/v1/{group}.{version}.{resource}").Handler(a.handle(a.k8sAPI))
|
||||
a.Path("/v1/{group}.{version}.{resource}/{nameorns}").Handler(a.handle(a.k8sAPI))
|
||||
a.Path("/v1/{group}.{version}.{resource}/{namespace}/{name}").Handler(a.handle(a.k8sAPI))
|
||||
a.Path("/v1/{type}").Handler(a.handle(nil))
|
||||
a.Path("/v1/{type}/{name}").Handler(a.handle(nil))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *apiServer) handle(apiFunc APIFunc) http.Handler {
|
||||
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||
a.api(rw, req, apiFunc)
|
||||
})
|
||||
}
|
||||
|
||||
func (a *apiServer) api(rw http.ResponseWriter, req *http.Request, apiFunc APIFunc) {
|
||||
apiOp, ok := a.common(rw, req)
|
||||
if ok {
|
||||
if apiFunc != nil {
|
||||
apiFunc(apiOp)
|
||||
}
|
||||
a.server.Handle(apiOp)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *apiServer) k8sAPI(apiOp *types.APIRequest) {
|
||||
vars := mux.Vars(apiOp.Request)
|
||||
group := vars["group"]
|
||||
if group == "core" {
|
||||
group = ""
|
||||
}
|
||||
|
||||
apiOp.Name = vars["name"]
|
||||
apiOp.Type = a.sf.ByGVR(schema.GroupVersionResource{
|
||||
Version: vars["version"],
|
||||
Group: group,
|
||||
Resource: vars["resource"],
|
||||
})
|
||||
|
||||
nOrN := vars["nameorns"]
|
||||
if nOrN != "" {
|
||||
schema := apiOp.Schemas.Schema(apiOp.Type)
|
||||
if attributes.Namespaced(schema) {
|
||||
vars["namespace"] = nOrN
|
||||
} else {
|
||||
vars["name"] = nOrN
|
||||
}
|
||||
}
|
||||
|
||||
if namespace := vars["namespace"]; namespace != "" {
|
||||
apiOp.Namespaces = []string{namespace}
|
||||
}
|
||||
}
|
||||
@@ -4,12 +4,11 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/rancher/naok/pkg/resources"
|
||||
|
||||
"github.com/rancher/naok/pkg/controllers/schema"
|
||||
|
||||
"github.com/rancher/naok/pkg/accesscontrol"
|
||||
"github.com/rancher/naok/pkg/client"
|
||||
"github.com/rancher/naok/pkg/controllers/schema"
|
||||
"github.com/rancher/naok/pkg/resources"
|
||||
"github.com/rancher/naok/pkg/server/publicapi"
|
||||
"github.com/rancher/wrangler-api/pkg/generated/controllers/apiextensions.k8s.io"
|
||||
"github.com/rancher/wrangler-api/pkg/generated/controllers/apiregistration.k8s.io"
|
||||
rbaccontroller "github.com/rancher/wrangler-api/pkg/generated/controllers/rbac"
|
||||
@@ -17,7 +16,6 @@ import (
|
||||
"github.com/rancher/wrangler/pkg/start"
|
||||
"github.com/sirupsen/logrus"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/rest"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
@@ -52,7 +50,21 @@ func Run(ctx context.Context, cfg Config) error {
|
||||
return err
|
||||
}
|
||||
|
||||
starter, err := startAPI(ctx, cfg.ListenAddress, restConfig, k8s, crd, api, rbac)
|
||||
cf, err := client.NewFactory(restConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sf := resources.SchemaFactory(cf,
|
||||
accesscontrol.NewAccessStore(rbac.Rbac().V1()))
|
||||
|
||||
schema.Register(ctx,
|
||||
k8s.Discovery(),
|
||||
crd.Apiextensions().V1beta1().CustomResourceDefinition(),
|
||||
api.Apiregistration().V1().APIService(),
|
||||
sf)
|
||||
|
||||
handler, err := publicapi.NewHandler(restConfig, sf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -61,37 +73,6 @@ func Run(ctx context.Context, cfg Config) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := starter(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
<-ctx.Done()
|
||||
return nil
|
||||
}
|
||||
|
||||
func startAPI(ctx context.Context, listenAddress string, restConfig *rest.Config, k8s *kubernetes.Clientset, crd *apiextensions.Factory,
|
||||
api *apiregistration.Factory, rbac *rbaccontroller.Factory) (func() error, error) {
|
||||
|
||||
cf, err := client.NewFactory(restConfig)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
as := accesscontrol.NewAccessStore(rbac.Rbac().V1())
|
||||
sf := resources.SchemaFactory(cf, as)
|
||||
|
||||
schema.Register(ctx,
|
||||
k8s.Discovery(),
|
||||
crd.Apiextensions().V1beta1().CustomResourceDefinition(),
|
||||
api.Apiregistration().V1().APIService(),
|
||||
sf)
|
||||
|
||||
return func() error {
|
||||
handler, err := newAPIServer(restConfig, sf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logrus.Infof("listening on %s", listenAddress)
|
||||
return http.ListenAndServe(listenAddress, handler)
|
||||
}, nil
|
||||
logrus.Infof("listening on %s", cfg.ListenAddress)
|
||||
return http.ListenAndServe(cfg.ListenAddress, handler)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user