1
0
mirror of https://github.com/rancher/steve.git synced 2025-06-24 05:57:34 +00:00
steve/pkg/server/resources/apigroups/apigroup.go

65 lines
1.6 KiB
Go
Raw Normal View History

2019-08-14 20:13:26 +00:00
package apigroups
import (
"net/http"
2020-01-31 05:37:59 +00:00
"github.com/rancher/steve/pkg/schemaserver/store/empty"
"github.com/rancher/steve/pkg/schemaserver/types"
2019-08-14 20:13:26 +00:00
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/discovery"
)
2020-01-31 05:37:59 +00:00
func Register(schemas *types.APISchemas, discovery discovery.DiscoveryInterface) {
schemas.MustImportAndCustomize(v1.APIGroup{}, func(schema *types.APISchema) {
2019-08-14 20:13:26 +00:00
schema.CollectionMethods = []string{http.MethodGet}
schema.ResourceMethods = []string{http.MethodGet}
schema.Store = NewStore(discovery)
schema.Formatter = func(request *types.APIRequest, resource *types.RawResource) {
2020-01-31 05:37:59 +00:00
resource.ID = resource.APIObject.Data().String("name")
2019-08-14 20:13:26 +00:00
}
})
}
type Store struct {
empty.Store
discovery discovery.DiscoveryInterface
}
func NewStore(discovery discovery.DiscoveryInterface) types.Store {
return &Store{
Store: empty.Store{},
discovery: discovery,
}
}
2020-01-31 05:37:59 +00:00
func (e *Store) ByID(apiOp *types.APIRequest, schema *types.APISchema, id string) (types.APIObject, error) {
return types.DefaultByID(e, apiOp, schema, id)
}
2019-08-14 20:13:26 +00:00
2020-01-31 05:37:59 +00:00
func toAPIObject(schema *types.APISchema, group v1.APIGroup) types.APIObject {
if group.Name == "" {
group.Name = "core"
2019-08-14 20:13:26 +00:00
}
2020-01-31 05:37:59 +00:00
return types.APIObject{
Type: schema.ID,
ID: group.Name,
Object: group,
2019-08-14 20:13:26 +00:00
}
}
2020-01-31 05:37:59 +00:00
func (e *Store) List(apiOp *types.APIRequest, schema *types.APISchema) (types.APIObjectList, error) {
2019-08-14 20:13:26 +00:00
groupList, err := e.discovery.ServerGroups()
if err != nil {
2020-01-31 05:37:59 +00:00
return types.APIObjectList{}, err
2019-08-14 20:13:26 +00:00
}
2020-01-31 05:37:59 +00:00
var result types.APIObjectList
2019-08-14 20:13:26 +00:00
for _, item := range groupList.Groups {
2020-01-31 05:37:59 +00:00
result.Objects = append(result.Objects, toAPIObject(schema, item))
2019-08-14 20:13:26 +00:00
}
2020-01-31 05:37:59 +00:00
return result, nil
2019-08-14 20:13:26 +00:00
}