1
0
mirror of https://github.com/rancher/steve.git synced 2025-07-06 19:38:56 +00:00
steve/pkg/resources/apigroups/apigroup.go
Michael Bolot e7a76d14f0 Adding APIGroups back to the schema
Prior schema calculations started with openapiv2 models which included a
model for APIGroups. However, new schema calculations use
ServerGroupsAndResources first, which omitted these values. This
re-adds this type using a static schema.
2024-07-02 13:27:04 -05:00

82 lines
1.9 KiB
Go

package apigroups
import (
"net/http"
"github.com/rancher/steve/pkg/schema"
"github.com/rancher/apiserver/pkg/store/empty"
"github.com/rancher/apiserver/pkg/types"
wschemas "github.com/rancher/wrangler/v3/pkg/schemas"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/discovery"
)
var BaseSchema = types.APISchema{
Schema: &wschemas.Schema{
ID: "apigroup",
Attributes: map[string]interface{}{
"group": "",
"kind": "APIGroup",
"version": "v1",
},
},
}
func Template(discovery discovery.DiscoveryInterface) schema.Template {
return schema.Template{
ID: "apigroup",
Customize: func(apiSchema *types.APISchema) {
apiSchema.CollectionMethods = []string{http.MethodGet}
apiSchema.ResourceMethods = []string{http.MethodGet}
},
Formatter: func(request *types.APIRequest, resource *types.RawResource) {
resource.ID = resource.APIObject.Data().String("name")
},
Store: NewStore(discovery),
}
}
type Store struct {
empty.Store
discovery discovery.DiscoveryInterface
}
func NewStore(discovery discovery.DiscoveryInterface) types.Store {
return &Store{
Store: empty.Store{},
discovery: discovery,
}
}
func (e *Store) ByID(apiOp *types.APIRequest, schema *types.APISchema, id string) (types.APIObject, error) {
return types.DefaultByID(e, apiOp, schema, id)
}
func toAPIObject(schema *types.APISchema, group v1.APIGroup) types.APIObject {
if group.Name == "" {
group.Name = "core"
}
return types.APIObject{
Type: schema.ID,
ID: group.Name,
Object: group,
}
}
func (e *Store) List(apiOp *types.APIRequest, schema *types.APISchema) (types.APIObjectList, error) {
groupList, err := e.discovery.ServerGroups()
if err != nil {
return types.APIObjectList{}, err
}
var result types.APIObjectList
for _, item := range groupList.Groups {
result.Objects = append(result.Objects, toAPIObject(schema, item))
}
return result, nil
}