Switch schema ID to gvk and plural name to gvr

This commit is contained in:
Darren Shepherd 2019-09-11 11:03:22 -07:00
parent ef235af49c
commit 9e4f93989f
6 changed files with 28 additions and 35 deletions

View File

@ -16,6 +16,7 @@ import (
type Factory interface { type Factory interface {
Schemas(user user.Info) (*types.Schemas, error) Schemas(user user.Info) (*types.Schemas, error)
ByGVR(gvr schema.GroupVersionResource) string ByGVR(gvr schema.GroupVersionResource) string
ByGVK(gvr schema.GroupVersionKind) string
} }
type Collection struct { type Collection struct {
@ -54,8 +55,8 @@ func NewCollection(baseSchema *types.Schemas, access *accesscontrol.AccessStore)
} }
func (c *Collection) Reset(schemas map[string]*types.Schema) { func (c *Collection) Reset(schemas map[string]*types.Schema) {
byGVR := map[schema.GroupVersionResource]string{}
byGVK := map[schema.GroupVersionKind]string{} byGVK := map[schema.GroupVersionKind]string{}
byGVR := map[schema.GroupVersionResource]string{}
for _, s := range schemas { for _, s := range schemas {
gvr := attributes.GVR(s) gvr := attributes.GVR(s)
@ -95,7 +96,11 @@ func (c *Collection) ByGVR(gvr schema.GroupVersionResource) string {
return id return id
} }
gvr.Resource = name.GuessPluralName(strings.ToLower(gvr.Resource)) 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 { func (c *Collection) ByGVK(gvk schema.GroupVersionKind) string {

View File

@ -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 != "" { 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 { 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 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 var versionColumns []table.Column
for _, col := range columnDefs { for _, col := range columnDefs {
versionColumns = append(versionColumns, table.Column{ versionColumns = append(versionColumns, table.Column{
@ -57,10 +57,10 @@ func forVersion(group, version, resource string, schemas map[string]*types.Schem
versionColumns = columns versionColumns = columns
} }
id := GVRToSchemaID(schema.GroupVersionResource{ id := GVKToSchemaID(schema.GroupVersionKind{
Group: group, Group: group,
Version: version, Version: version,
Resource: resource, Kind: kind,
}) })
schema := schemas[id] schema := schemas[id]

View File

@ -62,21 +62,21 @@ func refresh(gv schema.GroupVersion, groupToPreferredVersion map[string]string,
Version: gv.Version, Version: gv.Version,
Kind: resource.Kind, Kind: resource.Kind,
} }
gvr := gvk.GroupVersion().WithResource(resource.Name) gvr := gvk.GroupVersion().WithResource(resource.Name)
logrus.Infof("APIVersion %s/%s Kind %s", gvk.Group, gvk.Version, gvk.Kind) logrus.Infof("APIVersion %s/%s Kind %s", gvk.Group, gvk.Version, gvk.Kind)
schema := schemas[gvkToSchemaID(gvk)] schema := schemas[GVKToSchemaID(gvk)]
if schema == nil { if schema == nil {
schema = &types.Schema{ schema = &types.Schema{
ID: GVKToSchemaID(gvk),
Type: "schema", Type: "schema",
Dynamic: true, Dynamic: true,
} }
attributes.SetGVK(schema, gvk) attributes.SetGVK(schema, gvk)
} }
schema.PluralName = resource.Name schema.PluralName = GVRToPluralName(gvr)
attributes.SetAPIResource(schema, resource) attributes.SetAPIResource(schema, resource)
if preferredVersion := groupToPreferredVersion[gv.Group]; preferredVersion != "" && preferredVersion != gv.Version { if preferredVersion := groupToPreferredVersion[gv.Group]; preferredVersion != "" && preferredVersion != gv.Version {
attributes.SetPreferredVersion(schema, preferredVersion) attributes.SetPreferredVersion(schema, preferredVersion)
@ -85,12 +85,6 @@ func refresh(gv schema.GroupVersion, groupToPreferredVersion map[string]string,
attributes.SetPreferredGroup(schema, group) 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 schemas[schema.ID] = schema
} }

View File

@ -2,6 +2,7 @@ package converter
import ( import (
"fmt" "fmt"
"strings"
"github.com/rancher/norman/pkg/types" "github.com/rancher/norman/pkg/types"
"github.com/rancher/wrangler-api/pkg/generated/controllers/apiextensions.k8s.io/v1beta1" "github.com/rancher/wrangler-api/pkg/generated/controllers/apiextensions.k8s.io/v1beta1"
@ -9,14 +10,14 @@ import (
"k8s.io/client-go/discovery" "k8s.io/client-go/discovery"
) )
func gvkToSchemaID(gvk schema.GroupVersionKind) string { func GVKToSchemaID(gvk schema.GroupVersionKind) string {
if gvk.Group == "" { 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 == "" { if gvr.Group == "" {
return fmt.Sprintf("core.%s.%s", gvr.Version, gvr.Resource) return fmt.Sprintf("core.%s.%s", gvr.Version, gvr.Resource)
} }

View File

@ -40,7 +40,7 @@ func modelToSchema(modelName string, k *proto.Kind) *types.Schema {
Kind: convert.ToString(m["kind"]), Kind: convert.ToString(m["kind"]),
} }
s.ID = gvkToSchemaID(gvk) s.ID = GVKToSchemaID(gvk)
attributes.SetGVK(&s, gvk) attributes.SetGVK(&s, gvk)
} }
} }

View File

@ -2,10 +2,10 @@ package publicapi
import ( import (
"net/http" "net/http"
"strings"
"github.com/sirupsen/logrus"
"github.com/rancher/naok/pkg/accesscontrol" "github.com/rancher/naok/pkg/accesscontrol"
"github.com/rancher/naok/pkg/attributes"
k8sproxy "github.com/rancher/naok/pkg/proxy" k8sproxy "github.com/rancher/naok/pkg/proxy"
"github.com/rancher/naok/pkg/resources/schema" "github.com/rancher/naok/pkg/resources/schema"
"github.com/rancher/naok/pkg/server/router" "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) schemas, err := a.sf.Schemas(user)
if err != nil { if err != nil {
logrus.Errorf("HTTP request failed: %v", err)
rw.Write([]byte(err.Error())) rw.Write([]byte(err.Error()))
rw.WriteHeader(http.StatusInternalServerError) rw.WriteHeader(http.StatusInternalServerError)
} }
urlBuilder, err := urlbuilder.New(req, a, schemas) urlBuilder, err := urlbuilder.NewPrefixed(req, schemas, "v1")
if err != nil { if err != nil {
rw.Write([]byte(err.Error())) rw.Write([]byte(err.Error()))
rw.WriteHeader(http.StatusInternalServerError) rw.WriteHeader(http.StatusInternalServerError)
@ -72,14 +73,6 @@ func (a *apiServer) common(rw http.ResponseWriter, req *http.Request) (*types.AP
}, true }, 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) type APIFunc func(schema.Factory, *types.APIRequest)
func (a *apiServer) apiHandler(apiFunc APIFunc) http.Handler { func (a *apiServer) apiHandler(apiFunc APIFunc) http.Handler {