1
0
mirror of https://github.com/rancher/steve.git synced 2025-07-31 06:23:11 +00:00
steve/pkg/schema/converter/discovery.go

93 lines
2.3 KiB
Go
Raw Normal View History

2019-08-13 23:36:03 +00:00
package converter
import (
"strings"
2020-01-31 05:01:21 +00:00
"github.com/rancher/norman/v2/pkg/types"
2019-09-11 21:05:00 +00:00
"github.com/rancher/steve/pkg/attributes"
2019-08-13 23:36:03 +00:00
"github.com/rancher/wrangler/pkg/merr"
"github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/discovery"
)
2019-08-16 18:40:42 +00:00
var (
preferredGroups = map[string]string{
"extensions": "apps",
}
)
2019-08-13 23:36:03 +00:00
func AddDiscovery(client discovery.DiscoveryInterface, schemas map[string]*types.Schema) error {
logrus.Info("Refreshing all schemas")
2019-08-16 18:40:42 +00:00
groups, resourceLists, err := client.ServerGroupsAndResources()
2019-08-13 23:36:03 +00:00
if err != nil {
return err
}
2019-08-16 18:40:42 +00:00
versions := indexVersions(groups)
2019-08-13 23:36:03 +00:00
var errs []error
for _, resourceList := range resourceLists {
gv, err := schema.ParseGroupVersion(resourceList.GroupVersion)
if err != nil {
errs = append(errs, err)
}
2019-08-16 18:40:42 +00:00
if err := refresh(gv, versions, resourceList, schemas); err != nil {
2019-08-13 23:36:03 +00:00
errs = append(errs, err)
}
}
return merr.NewErrors(errs...)
}
2019-08-16 18:40:42 +00:00
func indexVersions(groups []*metav1.APIGroup) map[string]string {
result := map[string]string{}
for _, group := range groups {
result[group.Name] = group.PreferredVersion.Version
}
return result
}
func refresh(gv schema.GroupVersion, groupToPreferredVersion map[string]string, resources *metav1.APIResourceList, schemas map[string]*types.Schema) error {
2019-08-13 23:36:03 +00:00
for _, resource := range resources.APIResources {
if strings.Contains(resource.Name, "/") {
continue
}
gvk := schema.GroupVersionKind{
Group: gv.Group,
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)]
2019-08-13 23:36:03 +00:00
if schema == nil {
schema = &types.Schema{
ID: GVKToSchemaID(gvk),
2019-08-13 23:36:03 +00:00
Type: "schema",
Dynamic: true,
}
attributes.SetGVK(schema, gvk)
}
schema.PluralName = GVRToPluralName(gvr)
2019-08-13 23:36:03 +00:00
attributes.SetAPIResource(schema, resource)
2019-08-16 18:40:42 +00:00
if preferredVersion := groupToPreferredVersion[gv.Group]; preferredVersion != "" && preferredVersion != gv.Version {
attributes.SetPreferredVersion(schema, preferredVersion)
}
if group := preferredGroups[gv.Group]; group != "" {
attributes.SetPreferredGroup(schema, group)
}
2019-08-13 23:36:03 +00:00
schemas[schema.ID] = schema
}
return nil
}