1
0
mirror of https://github.com/rancher/steve.git synced 2025-07-06 03:26:35 +00:00
steve/pkg/schema/converter/k8stonorman.go

110 lines
3.7 KiB
Go
Raw Normal View History

// Package converter is responsible for converting the types registered with a k8s server to schemas which can be used
// by the UI (and other consumers) to discover the resources available and the current user's permissions.
2019-08-13 23:36:03 +00:00
package converter
import (
"fmt"
"strings"
2019-08-13 23:36:03 +00:00
"github.com/rancher/apiserver/pkg/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/steve/pkg/attributes"
"github.com/rancher/steve/pkg/resources/apigroups"
v1 "github.com/rancher/wrangler/v3/pkg/generated/controllers/apiextensions.k8s.io/v1"
2019-08-13 23:36:03 +00:00
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
"k8s.io/kube-openapi/pkg/util/proto"
)
const (
gvkExtensionName = "x-kubernetes-group-version-kind"
gvkExtensionGroup = "group"
gvkExtensionVersion = "version"
gvkExtensionKind = "kind"
2019-08-13 23:36:03 +00:00
)
func GVKToVersionedSchemaID(gvk schema.GroupVersionKind) string {
2019-08-13 23:36:03 +00:00
if gvk.Group == "" {
return strings.ToLower(fmt.Sprintf("core.%s.%s", gvk.Version, gvk.Kind))
2019-08-13 23:36:03 +00:00
}
return strings.ToLower(fmt.Sprintf("%s.%s.%s", gvk.Group, gvk.Version, gvk.Kind))
2019-08-13 23:36:03 +00:00
}
func gvrToPluralName(gvr schema.GroupVersionResource) string {
2019-08-14 18:08:34 +00:00
if gvr.Group == "" {
return fmt.Sprintf("core.%s.%s", gvr.Version, gvr.Resource)
2019-08-13 23:36:03 +00:00
}
2019-08-14 18:08:34 +00:00
return fmt.Sprintf("%s.%s.%s", gvr.Group, gvr.Version, gvr.Resource)
2019-08-13 23:36:03 +00:00
}
func GVKToSchemaID(gvk schema.GroupVersionKind) string {
if gvk.Group == "" {
return strings.ToLower(gvk.Kind)
}
return strings.ToLower(fmt.Sprintf("%s.%s", gvk.Group, gvk.Kind))
}
func GVRToPluralName(gvr schema.GroupVersionResource) string {
if gvr.Group == "" {
return gvr.Resource
}
return fmt.Sprintf("%s.%s", gvr.Group, gvr.Resource)
}
// GetGVKForProto attempts to retrieve a GVK for a given OpenAPI V2 schema
// object.
// The GVK is defined in an extension. It is possible that the protoSchema does
// not have the GVK extension set - in that case, we return nil.
func GetGVKForProtoSchema(protoSchema proto.Schema) *schema.GroupVersionKind {
extensions, ok := protoSchema.GetExtensions()[gvkExtensionName].([]any)
if !ok {
return nil
}
for _, extension := range extensions {
if gvkExtension, ok := extension.(map[any]any); ok {
gvk := schema.GroupVersionKind{
Group: convert.ToString(gvkExtension[gvkExtensionGroup]),
Version: convert.ToString(gvkExtension[gvkExtensionVersion]),
Kind: convert.ToString(gvkExtension[gvkExtensionKind]),
}
return &gvk
}
}
return nil
}
// GetGVKForKind attempts to retrieve a GVK for a given Kind. Not all kind represent top level resources,
// so this function may return nil if the kind did not have a gvk extension
func GetGVKForKind(kind *proto.Kind) *schema.GroupVersionKind {
return GetGVKForProtoSchema(kind)
}
// ToSchemas creates the schemas for a K8s server, using client to discover groups/resources, and crd to potentially
// add additional information about new fields/resources. Mostly ties together addDiscovery and addCustomResources.
2021-04-21 16:49:03 +00:00
func ToSchemas(crd v1.CustomResourceDefinitionClient, client discovery.DiscoveryInterface) (map[string]*types.APISchema, error) {
2020-01-31 05:37:59 +00:00
result := map[string]*types.APISchema{}
2019-08-13 23:36:03 +00:00
addTemplateBased(result)
if err := addDiscovery(client, result); err != nil {
2019-08-13 23:36:03 +00:00
return nil, err
}
if err := addCustomResources(crd, result); err != nil {
2019-08-13 23:36:03 +00:00
return nil, err
}
if err := addDescription(client, result); err != nil {
2019-08-14 18:08:34 +00:00
return nil, err
}
2019-08-13 23:36:03 +00:00
return result, nil
}
// some schemas are not based on real resources but are filled-in by a template later on. This function adds the base
// schema so that these endpoints are still recognizable in the api
func addTemplateBased(schemas map[string]*types.APISchema) {
apiGroupGVK := attributes.GVK(&apigroups.BaseSchema)
schemas[GVKToVersionedSchemaID(apiGroupGVK)] = &apigroups.BaseSchema
}