mirror of
https://github.com/niusmallnan/steve.git
synced 2025-06-25 14:11:36 +00:00
Add virtual clusters resource
This commit is contained in:
parent
fbf6e18a30
commit
d3ce9c1739
101
pkg/server/resources/clusters/clusters.go
Normal file
101
pkg/server/resources/clusters/clusters.go
Normal file
@ -0,0 +1,101 @@
|
||||
package clusters
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/rancher/steve/pkg/schemaserver/store/empty"
|
||||
"github.com/rancher/steve/pkg/schemaserver/types"
|
||||
"github.com/rancher/wrangler/pkg/schemas/validation"
|
||||
)
|
||||
|
||||
const (
|
||||
rancherCluster = "management.cattle.io.cluster"
|
||||
localID = "local"
|
||||
)
|
||||
|
||||
var (
|
||||
local = types.APIObject{
|
||||
Type: "cluster",
|
||||
ID: localID,
|
||||
Object: &Cluster{},
|
||||
}
|
||||
localList = types.APIObjectList{
|
||||
Objects: []types.APIObject{
|
||||
local,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
type Cluster struct {
|
||||
}
|
||||
|
||||
func Register(schemas *types.APISchemas) {
|
||||
schemas.MustImportAndCustomize(Cluster{}, func(schema *types.APISchema) {
|
||||
schema.CollectionMethods = []string{http.MethodGet}
|
||||
schema.ResourceMethods = []string{http.MethodGet}
|
||||
schema.Store = &Store{}
|
||||
schema.Formatter = func(request *types.APIRequest, resource *types.RawResource) {
|
||||
resource.Links["api"] = request.URLBuilder.RelativeToRoot("/k8s/clusters/" + resource.ID)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
type Store struct {
|
||||
empty.Store
|
||||
}
|
||||
|
||||
func toClusterList(obj types.APIObjectList, err error) (types.APIObjectList, error) {
|
||||
for i := range obj.Objects {
|
||||
obj.Objects[i], _ = toCluster(obj.Objects[i], err)
|
||||
}
|
||||
return obj, err
|
||||
}
|
||||
|
||||
func toCluster(obj types.APIObject, err error) (types.APIObject, error) {
|
||||
return types.APIObject{
|
||||
Type: "cluster",
|
||||
ID: obj.ID,
|
||||
Object: &Cluster{},
|
||||
}, err
|
||||
}
|
||||
|
||||
func (s *Store) ByID(apiOp *types.APIRequest, schema *types.APISchema, id string) (types.APIObject, error) {
|
||||
clusters := apiOp.Schemas.LookupSchema(rancherCluster)
|
||||
if clusters == nil {
|
||||
if id == localID {
|
||||
return local, nil
|
||||
}
|
||||
return types.APIObject{}, validation.NotFound
|
||||
}
|
||||
return toCluster(clusters.Store.ByID(apiOp, clusters, id))
|
||||
}
|
||||
|
||||
func (s *Store) List(apiOp *types.APIRequest, schema *types.APISchema) (types.APIObjectList, error) {
|
||||
clusters := apiOp.Schemas.LookupSchema(rancherCluster)
|
||||
if clusters == nil {
|
||||
return localList, nil
|
||||
}
|
||||
return toClusterList(clusters.Store.List(apiOp, clusters))
|
||||
}
|
||||
|
||||
func (s *Store) Watch(apiOp *types.APIRequest, schema *types.APISchema, w types.WatchRequest) (chan types.APIEvent, error) {
|
||||
clusters := apiOp.Schemas.LookupSchema(rancherCluster)
|
||||
if clusters == nil {
|
||||
return nil, nil
|
||||
}
|
||||
target, err := clusters.Store.Watch(apiOp, clusters, w)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make(chan types.APIEvent)
|
||||
go func() {
|
||||
defer close(result)
|
||||
for event := range target {
|
||||
event.Object, _ = toCluster(event.Object, nil)
|
||||
result <- event
|
||||
}
|
||||
}()
|
||||
|
||||
return result, nil
|
||||
}
|
@ -9,6 +9,7 @@ import (
|
||||
"github.com/rancher/steve/pkg/schemaserver/subscribe"
|
||||
"github.com/rancher/steve/pkg/schemaserver/types"
|
||||
"github.com/rancher/steve/pkg/server/resources/apigroups"
|
||||
"github.com/rancher/steve/pkg/server/resources/clusters"
|
||||
"github.com/rancher/steve/pkg/server/resources/common"
|
||||
"github.com/rancher/steve/pkg/server/resources/counts"
|
||||
"github.com/rancher/steve/pkg/server/resources/userpreferences"
|
||||
@ -21,6 +22,7 @@ func DefaultSchemas(baseSchema *types.APISchemas, ccache clustercache.ClusterCac
|
||||
subscribe.Register(baseSchema)
|
||||
apiroot.Register(baseSchema, []string{"v1"}, []string{"proxy:/apis"})
|
||||
userpreferences.Register(baseSchema, cg)
|
||||
clusters.Register(baseSchema)
|
||||
return baseSchema
|
||||
}
|
||||
|
||||
|
@ -19,7 +19,6 @@ func Register(schemas *types.APISchemas, cg proxy.ClientGetter) {
|
||||
schemas.InternalSchemas.TypeName("userpreference", UserPreference{})
|
||||
schemas.MustImportAndCustomize(UserPreference{}, func(schema *types.APISchema) {
|
||||
schema.CollectionMethods = []string{http.MethodGet}
|
||||
schema.ResourceMethods = []string{http.MethodGet}
|
||||
schema.ResourceMethods = []string{http.MethodGet, http.MethodPut, http.MethodDelete}
|
||||
schema.Store = New(cg)
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user