From 9e4f93989f5590f4caaeef45bfe346ef2ac41690 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Wed, 11 Sep 2019 11:03:22 -0700 Subject: [PATCH] Switch schema ID to gvk and plural name to gvr --- pkg/resources/schema/collection.go | 9 +++++++-- pkg/resources/schema/converter/crd.go | 16 ++++++++-------- pkg/resources/schema/converter/discovery.go | 12 +++--------- pkg/resources/schema/converter/k8stonorman.go | 9 +++++---- pkg/resources/schema/converter/openapi.go | 2 +- pkg/server/publicapi/apiserver.go | 15 ++++----------- 6 files changed, 28 insertions(+), 35 deletions(-) diff --git a/pkg/resources/schema/collection.go b/pkg/resources/schema/collection.go index 9952e98..bfbc67b 100644 --- a/pkg/resources/schema/collection.go +++ b/pkg/resources/schema/collection.go @@ -16,6 +16,7 @@ import ( type Factory interface { Schemas(user user.Info) (*types.Schemas, error) ByGVR(gvr schema.GroupVersionResource) string + ByGVK(gvr schema.GroupVersionKind) string } type Collection struct { @@ -54,8 +55,8 @@ func NewCollection(baseSchema *types.Schemas, access *accesscontrol.AccessStore) } func (c *Collection) Reset(schemas map[string]*types.Schema) { - byGVR := map[schema.GroupVersionResource]string{} byGVK := map[schema.GroupVersionKind]string{} + byGVR := map[schema.GroupVersionResource]string{} for _, s := range schemas { gvr := attributes.GVR(s) @@ -95,7 +96,11 @@ func (c *Collection) ByGVR(gvr schema.GroupVersionResource) string { return id } gvr.Resource = name.GuessPluralName(strings.ToLower(gvr.Resource)) - return c.byGVR[gvr] + return c.byGVK[schema.GroupVersionKind{ + Group: gvr.Group, + Version: gvr.Version, + Kind: gvr.Resource, + }] } func (c *Collection) ByGVK(gvk schema.GroupVersionKind) string { diff --git a/pkg/resources/schema/converter/crd.go b/pkg/resources/schema/converter/crd.go index f095f97..3eacfc6 100644 --- a/pkg/resources/schema/converter/crd.go +++ b/pkg/resources/schema/converter/crd.go @@ -30,20 +30,20 @@ func AddCustomResources(crd v1beta1.CustomResourceDefinitionClient, schemas map[ }) } - group, resource := crd.Spec.Group, crd.Status.AcceptedNames.Plural + group, kind := crd.Spec.Group, crd.Status.AcceptedNames.Kind if crd.Spec.Version != "" { - forVersion(group, crd.Spec.Version, resource, schemas, crd.Spec.AdditionalPrinterColumns, columns) + forVersion(group, crd.Spec.Version, kind, schemas, crd.Spec.AdditionalPrinterColumns, columns) } for _, version := range crd.Spec.Versions { - forVersion(group, version.Name, resource, schemas, crd.Spec.AdditionalPrinterColumns, columns) + forVersion(group, version.Name, kind, schemas, crd.Spec.AdditionalPrinterColumns, columns) } } return nil } -func forVersion(group, version, resource string, schemas map[string]*types.Schema, columnDefs []beta1.CustomResourceColumnDefinition, columns []table.Column) { +func forVersion(group, version, kind string, schemas map[string]*types.Schema, columnDefs []beta1.CustomResourceColumnDefinition, columns []table.Column) { var versionColumns []table.Column for _, col := range columnDefs { versionColumns = append(versionColumns, table.Column{ @@ -57,10 +57,10 @@ func forVersion(group, version, resource string, schemas map[string]*types.Schem versionColumns = columns } - id := GVRToSchemaID(schema.GroupVersionResource{ - Group: group, - Version: version, - Resource: resource, + id := GVKToSchemaID(schema.GroupVersionKind{ + Group: group, + Version: version, + Kind: kind, }) schema := schemas[id] diff --git a/pkg/resources/schema/converter/discovery.go b/pkg/resources/schema/converter/discovery.go index 3068b30..c53ab55 100644 --- a/pkg/resources/schema/converter/discovery.go +++ b/pkg/resources/schema/converter/discovery.go @@ -62,21 +62,21 @@ func refresh(gv schema.GroupVersion, groupToPreferredVersion map[string]string, Version: gv.Version, Kind: resource.Kind, } - gvr := gvk.GroupVersion().WithResource(resource.Name) logrus.Infof("APIVersion %s/%s Kind %s", gvk.Group, gvk.Version, gvk.Kind) - schema := schemas[gvkToSchemaID(gvk)] + schema := schemas[GVKToSchemaID(gvk)] if schema == nil { schema = &types.Schema{ + ID: GVKToSchemaID(gvk), Type: "schema", Dynamic: true, } attributes.SetGVK(schema, gvk) } - schema.PluralName = resource.Name + schema.PluralName = GVRToPluralName(gvr) attributes.SetAPIResource(schema, resource) if preferredVersion := groupToPreferredVersion[gv.Group]; preferredVersion != "" && preferredVersion != gv.Version { attributes.SetPreferredVersion(schema, preferredVersion) @@ -85,12 +85,6 @@ func refresh(gv schema.GroupVersion, groupToPreferredVersion map[string]string, attributes.SetPreferredGroup(schema, group) } - // switch ID to be GVR, not GVK - if schema.ID != "" { - delete(schemas, schema.ID) - } - - schema.ID = GVRToSchemaID(gvr) schemas[schema.ID] = schema } diff --git a/pkg/resources/schema/converter/k8stonorman.go b/pkg/resources/schema/converter/k8stonorman.go index 49489ae..246010d 100644 --- a/pkg/resources/schema/converter/k8stonorman.go +++ b/pkg/resources/schema/converter/k8stonorman.go @@ -2,6 +2,7 @@ package converter import ( "fmt" + "strings" "github.com/rancher/norman/pkg/types" "github.com/rancher/wrangler-api/pkg/generated/controllers/apiextensions.k8s.io/v1beta1" @@ -9,14 +10,14 @@ import ( "k8s.io/client-go/discovery" ) -func gvkToSchemaID(gvk schema.GroupVersionKind) string { +func GVKToSchemaID(gvk schema.GroupVersionKind) string { if gvk.Group == "" { - return fmt.Sprintf("core.%s.%s", gvk.Version, gvk.Kind) + return strings.ToLower(fmt.Sprintf("core.%s.%s", gvk.Version, gvk.Kind)) } - return fmt.Sprintf("%s.%s.%s", gvk.Group, gvk.Version, gvk.Kind) + return strings.ToLower(fmt.Sprintf("%s.%s.%s", gvk.Group, gvk.Version, gvk.Kind)) } -func GVRToSchemaID(gvr schema.GroupVersionResource) string { +func GVRToPluralName(gvr schema.GroupVersionResource) string { if gvr.Group == "" { return fmt.Sprintf("core.%s.%s", gvr.Version, gvr.Resource) } diff --git a/pkg/resources/schema/converter/openapi.go b/pkg/resources/schema/converter/openapi.go index 7651546..02617cf 100644 --- a/pkg/resources/schema/converter/openapi.go +++ b/pkg/resources/schema/converter/openapi.go @@ -40,7 +40,7 @@ func modelToSchema(modelName string, k *proto.Kind) *types.Schema { Kind: convert.ToString(m["kind"]), } - s.ID = gvkToSchemaID(gvk) + s.ID = GVKToSchemaID(gvk) attributes.SetGVK(&s, gvk) } } diff --git a/pkg/server/publicapi/apiserver.go b/pkg/server/publicapi/apiserver.go index 4060f3f..5ecd637 100644 --- a/pkg/server/publicapi/apiserver.go +++ b/pkg/server/publicapi/apiserver.go @@ -2,10 +2,10 @@ package publicapi import ( "net/http" - "strings" + + "github.com/sirupsen/logrus" "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" @@ -53,11 +53,12 @@ func (a *apiServer) common(rw http.ResponseWriter, req *http.Request) (*types.AP schemas, err := a.sf.Schemas(user) if err != nil { + logrus.Errorf("HTTP request failed: %v", err) rw.Write([]byte(err.Error())) rw.WriteHeader(http.StatusInternalServerError) } - urlBuilder, err := urlbuilder.New(req, a, schemas) + urlBuilder, err := urlbuilder.NewPrefixed(req, schemas, "v1") if err != nil { rw.Write([]byte(err.Error())) rw.WriteHeader(http.StatusInternalServerError) @@ -72,14 +73,6 @@ func (a *apiServer) common(rw http.ResponseWriter, req *http.Request) (*types.AP }, true } -func (a *apiServer) Schema(base string, schema *types.Schema) string { - gvr := attributes.GVR(schema) - if gvr.Resource == "" { - return urlbuilder.ConstructBasicURL(base, "v1", schema.PluralName) - } - return urlbuilder.ConstructBasicURL(base, "v1", strings.ToLower(schema.ID)) -} - type APIFunc func(schema.Factory, *types.APIRequest) func (a *apiServer) apiHandler(apiFunc APIFunc) http.Handler {