Refactor schema IDs and paths

This commit is contained in:
Darren Shepherd
2019-08-13 16:36:03 -07:00
parent 0baa096865
commit ad67c46055
15 changed files with 595 additions and 427 deletions

View File

@@ -2,40 +2,31 @@ package server
import (
"net/http"
"github.com/rancher/naok/pkg/counts"
"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/schemas"
"github.com/rancher/naok/pkg/resources/schema"
"github.com/rancher/norman/pkg/api"
"github.com/rancher/norman/pkg/store/proxy"
"github.com/rancher/norman/pkg/subscribe"
"github.com/rancher/norman/pkg/types"
"github.com/rancher/norman/pkg/urlbuilder"
"k8s.io/apiserver/pkg/authentication/user"
"k8s.io/client-go/rest"
)
func newAPIServer(cfg *rest.Config, cf proxy.ClientGetter, as *accesscontrol.AccessStore, sf schemas.SchemaFactory) (http.Handler, error) {
func newAPIServer(cfg *rest.Config, sf schema.Factory) (http.Handler, error) {
var (
err error
)
a := &apiServer{
Router: mux.NewRouter(),
cf: cf,
as: as,
sf: sf,
server: api.NewAPIServer(),
baseSchemas: types.EmptySchemas(),
Router: mux.NewRouter(),
sf: sf,
server: api.NewAPIServer(),
}
counts.Register(a.baseSchemas)
subscribe.Register(a.baseSchemas)
a.Router.NotFoundHandler, err = k8sproxy.Handler("/", cfg)
if err != nil {
return nil, err
@@ -48,22 +39,8 @@ func newAPIServer(cfg *rest.Config, cf proxy.ClientGetter, as *accesscontrol.Acc
type apiServer struct {
*mux.Router
cf proxy.ClientGetter
as *accesscontrol.AccessStore
sf schemas.SchemaFactory
server *api.Server
baseSchemas *types.Schemas
}
func (a *apiServer) newSchemas() (*types.Schemas, error) {
schemas, err := schemas.DefaultSchemaFactory()
if err != nil {
return nil, err
}
schemas.DefaultMapper = newDefaultMapper
schemas.AddSchemas(a.baseSchemas)
return schemas, nil
sf schema.Factory
server *api.Server
}
func (a *apiServer) common(rw http.ResponseWriter, req *http.Request) (*types.APIRequest, bool) {
@@ -72,8 +49,7 @@ func (a *apiServer) common(rw http.ResponseWriter, req *http.Request) (*types.AP
Groups: []string{"system:masters"},
}
accessSet := a.as.AccessFor(user)
schemas, err := a.sf.Schemas("", accessSet, a.newSchemas)
schemas, err := a.sf.Schemas(user)
if err != nil {
rw.Write([]byte(err.Error()))
rw.WriteHeader(http.StatusInternalServerError)
@@ -96,14 +72,8 @@ func (a *apiServer) common(rw http.ResponseWriter, req *http.Request) (*types.AP
func (a *apiServer) Schema(base string, schema *types.Schema) string {
gvr := attributes.GVR(schema)
if gvr.Group == "" && gvr.Version != "" && gvr.Resource != "" {
return urlbuilder.ConstructBasicURL(base, gvr.Version, gvr.Resource)
if gvr.Resource == "" {
return urlbuilder.ConstructBasicURL(base, "v1", schema.PluralName)
}
if gvr.Resource != "" {
return urlbuilder.ConstructBasicURL(base, "v1", "apis", gvr.Group, gvr.Version, gvr.Resource)
}
return urlbuilder.ConstructBasicURL(base, "v1", schema.PluralName)
return urlbuilder.ConstructBasicURL(base, "v1", strings.ToLower(schema.ID))
}

View File

@@ -1,36 +0,0 @@
package server
import (
"fmt"
"github.com/rancher/norman/pkg/types"
"github.com/rancher/norman/pkg/types/convert"
"github.com/rancher/norman/pkg/types/values"
)
func newDefaultMapper() types.Mapper {
return &defaultMapper{}
}
type defaultMapper struct {
types.EmptyMapper
}
func (d *defaultMapper) FromInternal(data map[string]interface{}) {
if t, ok := data["type"]; ok {
data["_type"] = t
}
if _, ok := data["id"]; ok || data == nil {
return
}
name := convert.ToString(values.GetValueN(data, "metadata", "name"))
namespace := convert.ToString(values.GetValueN(data, "metadata", "namespace"))
if namespace == "" {
data["id"] = name
} else {
data["id"] = fmt.Sprintf("%s/%s", namespace, name)
}
}

View File

@@ -12,18 +12,12 @@ import (
type APIFunc func(*types.APIRequest)
func (a *apiServer) routes() error {
a.Path("/v1/{type:schemas}").Handler(a.handle(nil))
a.Path("/v1/{type:schemas}/{name}").Handler(a.handle(nil))
a.Path("/v1/{type:subscribe}").Handler(a.handle(nil))
a.Path("/v1/{type:counts}").Handler(a.handle(nil))
a.Path("/v1/{type:counts}/{name}").Handler(a.handle(nil))
a.Path("/{version:v1}/{resource}").Handler(a.handle(a.k8sAPI))
a.Path("/{version:v1}/{resource}/{nameorns}").Handler(a.handle(a.k8sAPI))
a.Path("/{version:v1}/{resource}/{namespace}/{name}").Handler(a.handle(a.k8sAPI))
a.Path("/v1/{type}").Handler(a.handle(nil))
a.Path("/v1/{type:schemas}/{name:.*}").Handler(a.handle(nil))
a.Path("/v1/{type}/{name}").Handler(a.handle(nil))
a.Path("/v1/apis/{group}/{version}/{resource}").Handler(a.handle(a.k8sAPI))
a.Path("/v1/apis/{group}/{version}/{resource}/{nameorns}").Handler(a.handle(a.k8sAPI))
a.Path("/v1/apis/{group}/{version}/{resource}/{namespace}/{name}").Handler(a.handle(a.k8sAPI))
return nil
}
@@ -46,10 +40,15 @@ func (a *apiServer) api(rw http.ResponseWriter, req *http.Request, apiFunc APIFu
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: vars["group"],
Group: group,
Resource: vars["resource"],
})

View File

@@ -4,9 +4,12 @@ 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/schemas"
"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"
@@ -74,17 +77,17 @@ func startAPI(ctx context.Context, listenAddress string, restConfig *rest.Config
return nil, err
}
sf := schemas.Register(ctx,
cf,
as := accesscontrol.NewAccessStore(rbac.Rbac().V1())
sf := resources.SchemaFactory(cf, as)
schema.Register(ctx,
k8s.Discovery(),
crd.Apiextensions().V1beta1().CustomResourceDefinition(),
api.Apiregistration().V1().APIService(),
)
as := accesscontrol.NewAccessStore(rbac.Rbac().V1())
sf)
return func() error {
handler, err := newAPIServer(restConfig, cf, as, sf)
handler, err := newAPIServer(restConfig, sf)
if err != nil {
return err
}