From 6d2b0f54caa1ca6ff3bc16147617901499f4f06d Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Wed, 14 Aug 2019 13:39:08 -0700 Subject: [PATCH] Make / and /v1 work --- pkg/resources/schema.go | 2 ++ pkg/server/publicapi/apiserver.go | 1 + pkg/server/publicapi/handlers.go | 4 +++ pkg/server/router/router.go | 4 +++ .../rancher/norman/pkg/api/builtin/schema.go | 29 ++++++----------- .../api_root.go => store/apiroot/apiroot.go} | 31 ++++++++++++++++++- .../rancher/norman/pkg/urlbuilder/url.go | 3 ++ vendor/modules.txt | 1 + 8 files changed, 55 insertions(+), 20 deletions(-) rename vendor/github.com/rancher/norman/pkg/{api/builtin/api_root.go => store/apiroot/apiroot.go} (75%) diff --git a/pkg/resources/schema.go b/pkg/resources/schema.go index 9f93905..0a1c8f9 100644 --- a/pkg/resources/schema.go +++ b/pkg/resources/schema.go @@ -6,6 +6,7 @@ import ( "github.com/rancher/naok/pkg/resources/common" "github.com/rancher/naok/pkg/resources/counts" "github.com/rancher/naok/pkg/resources/schema" + "github.com/rancher/norman/pkg/store/apiroot" "github.com/rancher/norman/pkg/store/proxy" "github.com/rancher/norman/pkg/subscribe" "github.com/rancher/norman/pkg/types" @@ -19,6 +20,7 @@ func SchemaFactory(getter proxy.ClientGetter, as *accesscontrol.AccessStore, k8s counts.Register(baseSchema) subscribe.Register(baseSchema) apigroups.Register(baseSchema, k8s.Discovery()) + apiroot.Register(baseSchema, []string{"v1"}, nil) common.Register(collection, getter) diff --git a/pkg/server/publicapi/apiserver.go b/pkg/server/publicapi/apiserver.go index b20e693..4060f3f 100644 --- a/pkg/server/publicapi/apiserver.go +++ b/pkg/server/publicapi/apiserver.go @@ -36,6 +36,7 @@ func NewHandler(cfg *rest.Config, sf schema.Factory) (http.Handler, error) { K8sResource: a.apiHandler(k8sAPI), GenericResource: a.apiHandler(nil), K8sProxy: proxy, + APIRoot: a.apiHandler(apiRoot), }), nil } diff --git a/pkg/server/publicapi/handlers.go b/pkg/server/publicapi/handlers.go index 510f185..a8e8b7f 100644 --- a/pkg/server/publicapi/handlers.go +++ b/pkg/server/publicapi/handlers.go @@ -36,3 +36,7 @@ func k8sAPI(sf schema.Factory, apiOp *types.APIRequest) { apiOp.Namespaces = []string{namespace} } } + +func apiRoot(sf schema.Factory, apiOp *types.APIRequest) { + apiOp.Type = "apiRoot" +} diff --git a/pkg/server/router/router.go b/pkg/server/router/router.go index 32257b6..caa7dd6 100644 --- a/pkg/server/router/router.go +++ b/pkg/server/router/router.go @@ -9,6 +9,7 @@ import ( type Handlers struct { K8sResource http.Handler GenericResource http.Handler + APIRoot http.Handler K8sProxy http.Handler } @@ -18,6 +19,9 @@ func Routes(h Handlers) http.Handler { m.StrictSlash(true) m.NotFoundHandler = h.K8sProxy + m.Path("/").Handler(h.APIRoot) + m.Path("/{name:v1}").Handler(h.APIRoot) + 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) diff --git a/vendor/github.com/rancher/norman/pkg/api/builtin/schema.go b/vendor/github.com/rancher/norman/pkg/api/builtin/schema.go index c862efe..c857351 100644 --- a/vendor/github.com/rancher/norman/pkg/api/builtin/schema.go +++ b/vendor/github.com/rancher/norman/pkg/api/builtin/schema.go @@ -1,6 +1,8 @@ package builtin import ( + "net/http" + "github.com/rancher/norman/pkg/store/schema" "github.com/rancher/norman/pkg/types" ) @@ -52,30 +54,12 @@ var ( }, } - APIRoot = types.Schema{ - ID: "apiRoot", - CollectionMethods: []string{"GET"}, - ResourceMethods: []string{"GET"}, - ResourceFields: map[string]types.Field{ - "apiVersion": {Type: "map[json]"}, - "path": {Type: "string"}, - }, - Formatter: APIRootFormatter, - Store: NewAPIRootStore(nil, nil), - } - Schemas = types.EmptySchemas(). MustAddSchema(Schema). MustAddSchema(Error). - MustAddSchema(Collection). - MustAddSchema(APIRoot) + MustAddSchema(Collection) ) -func apiVersionFromMap(schemas *types.Schemas, apiVersion map[string]interface{}) string { - version, _ := apiVersion["version"].(string) - return version -} - func SchemaFormatter(apiOp *types.APIRequest, resource *types.RawResource) { schema := apiOp.Schemas.Schema(resource.ID) if schema == nil { @@ -88,6 +72,13 @@ func SchemaFormatter(apiOp *types.APIRequest, resource *types.RawResource) { } } +func getSchemaCollectionLink(apiOp *types.APIRequest, schema *types.Schema) string { + if schema != nil && contains(schema.CollectionMethods, http.MethodGet) { + return apiOp.URLBuilder.Collection(schema) + } + return "" +} + func contains(list []string, needle string) bool { for _, v := range list { if v == needle { diff --git a/vendor/github.com/rancher/norman/pkg/api/builtin/api_root.go b/vendor/github.com/rancher/norman/pkg/store/apiroot/apiroot.go similarity index 75% rename from vendor/github.com/rancher/norman/pkg/api/builtin/api_root.go rename to vendor/github.com/rancher/norman/pkg/store/apiroot/apiroot.go index a46b2ea..d39da6c 100644 --- a/vendor/github.com/rancher/norman/pkg/api/builtin/api_root.go +++ b/vendor/github.com/rancher/norman/pkg/store/apiroot/apiroot.go @@ -1,4 +1,4 @@ -package builtin +package apiroot import ( "net/http" @@ -7,6 +7,20 @@ import ( "github.com/rancher/norman/pkg/types" ) +func Register(schemas *types.Schemas, versions, roots []string) { + schemas.MustAddSchema(types.Schema{ + ID: "apiRoot", + CollectionMethods: []string{"GET"}, + ResourceMethods: []string{"GET"}, + ResourceFields: map[string]types.Field{ + "apiVersion": {Type: "map[json]"}, + "path": {Type: "string"}, + }, + Formatter: APIRootFormatter, + Store: NewAPIRootStore(versions, roots), + }) +} + func APIRootFormatter(apiOp *types.APIRequest, resource *types.RawResource) { path, _ := resource.Values["path"].(string) if path == "" { @@ -86,6 +100,7 @@ func (a *APIRootStore) List(apiOp *types.APIRequest, schema *types.Schema, opt * func apiVersionToAPIRootMap(version string) map[string]interface{} { return map[string]interface{}{ + "id": version, "type": "apiRoot", "apiVersion": map[string]interface{}{ "version": version, @@ -93,3 +108,17 @@ func apiVersionToAPIRootMap(version string) map[string]interface{} { "path": "/" + version, } } + +func apiVersionFromMap(schemas *types.Schemas, apiVersion map[string]interface{}) string { + version, _ := apiVersion["version"].(string) + return version +} + +func contains(list []string, needle string) bool { + for _, v := range list { + if v == needle { + return true + } + } + return false +} diff --git a/vendor/github.com/rancher/norman/pkg/urlbuilder/url.go b/vendor/github.com/rancher/norman/pkg/urlbuilder/url.go index fc635c6..d71828f 100644 --- a/vendor/github.com/rancher/norman/pkg/urlbuilder/url.go +++ b/vendor/github.com/rancher/norman/pkg/urlbuilder/url.go @@ -99,6 +99,9 @@ func (u *DefaultURLBuilder) Current() string { } func (u *DefaultURLBuilder) RelativeToRoot(path string) string { + if len(path) > 0 && path[0] != '/' { + return u.responseURLBase + "/" + path + } return u.responseURLBase + path } diff --git a/vendor/modules.txt b/vendor/modules.txt index a7c5a42..60409da 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -42,6 +42,7 @@ github.com/pkg/errors github.com/rancher/norman/pkg/authorization github.com/rancher/norman/pkg/types github.com/rancher/norman/pkg/types/convert +github.com/rancher/norman/pkg/store/apiroot github.com/rancher/norman/pkg/store/proxy github.com/rancher/norman/pkg/subscribe github.com/rancher/norman/pkg/data