2019-08-12 22:15:19 +00:00
|
|
|
package counts
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2019-09-09 21:28:55 +00:00
|
|
|
"strconv"
|
2019-08-12 22:15:19 +00:00
|
|
|
"sync"
|
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
schema2 "k8s.io/apimachinery/pkg/runtime/schema"
|
2019-08-16 18:40:42 +00:00
|
|
|
|
2019-08-12 23:47:23 +00:00
|
|
|
"github.com/rancher/naok/pkg/accesscontrol"
|
2019-09-09 21:28:55 +00:00
|
|
|
"github.com/rancher/naok/pkg/attributes"
|
|
|
|
"github.com/rancher/naok/pkg/clustercache"
|
2019-08-12 22:15:19 +00:00
|
|
|
"github.com/rancher/norman/pkg/store/empty"
|
|
|
|
"github.com/rancher/norman/pkg/types"
|
2019-09-09 21:28:55 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2019-08-12 22:15:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ignore = map[string]bool{
|
2019-08-13 04:32:57 +00:00
|
|
|
"count": true,
|
|
|
|
"schema": true,
|
|
|
|
"apiRoot": true,
|
2019-08-12 22:15:19 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
func Register(schemas *types.Schemas, ccache clustercache.ClusterCache) {
|
2019-08-12 22:15:19 +00:00
|
|
|
schemas.MustImportAndCustomize(Count{}, func(schema *types.Schema) {
|
|
|
|
schema.CollectionMethods = []string{http.MethodGet}
|
2019-08-13 04:32:57 +00:00
|
|
|
schema.ResourceMethods = []string{http.MethodGet}
|
2019-08-12 23:47:23 +00:00
|
|
|
schema.Attributes["access"] = accesscontrol.AccessListMap{
|
|
|
|
"watch": accesscontrol.AccessList{
|
|
|
|
{
|
|
|
|
Namespace: "*",
|
|
|
|
ResourceName: "*",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2019-09-09 21:28:55 +00:00
|
|
|
schema.Store = &Store{
|
|
|
|
ccache: ccache,
|
|
|
|
}
|
2019-08-12 22:15:19 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type Count struct {
|
2019-08-12 23:47:23 +00:00
|
|
|
ID string `json:"id,omitempty"`
|
2019-09-09 21:28:55 +00:00
|
|
|
Counts map[string]ItemCount `json:"counts"`
|
2019-08-12 22:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ItemCount struct {
|
2019-08-13 04:32:57 +00:00
|
|
|
Count int `json:"count,omitempty"`
|
|
|
|
Namespaces map[string]int `json:"namespaces,omitempty"`
|
2019-09-09 21:28:55 +00:00
|
|
|
Revision int `json:"revision,omitempty"`
|
2019-08-12 22:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Store struct {
|
|
|
|
empty.Store
|
2019-09-09 21:28:55 +00:00
|
|
|
ccache clustercache.ClusterCache
|
2019-08-12 22:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) ByID(apiOp *types.APIRequest, schema *types.Schema, id string) (types.APIObject, error) {
|
2019-09-09 21:28:55 +00:00
|
|
|
c := s.getCount(apiOp)
|
|
|
|
return types.ToAPI(c), nil
|
2019-08-12 22:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) List(apiOp *types.APIRequest, schema *types.Schema, opt *types.QueryOptions) (types.APIObject, error) {
|
2019-09-09 21:28:55 +00:00
|
|
|
c := s.getCount(apiOp)
|
|
|
|
return types.ToAPI([]interface{}{c}), nil
|
2019-08-12 22:15:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) Watch(apiOp *types.APIRequest, schema *types.Schema, w types.WatchRequest) (chan types.APIEvent, error) {
|
2019-09-09 21:28:55 +00:00
|
|
|
var (
|
|
|
|
result = make(chan types.APIEvent, 100)
|
|
|
|
counts map[string]ItemCount
|
|
|
|
gvrToSchema = map[schema2.GroupVersionResource]*types.Schema{}
|
|
|
|
countLock sync.Mutex
|
|
|
|
)
|
|
|
|
|
|
|
|
counts = s.getCount(apiOp).Counts
|
|
|
|
for id := range counts {
|
|
|
|
schema := apiOp.Schemas.Schema(id)
|
|
|
|
if schema == nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-08-12 23:47:23 +00:00
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
gvrToSchema[attributes.GVR(schema)] = schema
|
2019-08-12 23:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2019-09-09 21:28:55 +00:00
|
|
|
<-apiOp.Context().Done()
|
|
|
|
close(result)
|
2019-08-12 23:47:23 +00:00
|
|
|
}()
|
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
onChange := func(add bool, gvr schema2.GroupVersionResource, _ string, obj runtime.Object) error {
|
|
|
|
countLock.Lock()
|
|
|
|
defer countLock.Unlock()
|
2019-08-12 23:47:23 +00:00
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
schema := gvrToSchema[gvr]
|
|
|
|
if schema == nil {
|
|
|
|
return nil
|
2019-08-12 23:47:23 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
apiObj := apiOp.Filter(nil, schema, types.ToAPI(obj))
|
|
|
|
if apiObj.IsNil() {
|
|
|
|
return nil
|
2019-08-12 23:47:23 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
_, namespace, revision, ok := getInfo(obj)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-12 23:47:23 +00:00
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
itemCount := counts[schema.ID]
|
|
|
|
if revision <= itemCount.Revision {
|
|
|
|
return nil
|
2019-08-12 23:47:23 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
if add {
|
|
|
|
itemCount.Count++
|
|
|
|
if namespace != "" {
|
|
|
|
itemCount.Namespaces[namespace]++
|
2019-08-13 04:32:57 +00:00
|
|
|
}
|
2019-09-09 21:28:55 +00:00
|
|
|
} else {
|
|
|
|
itemCount.Count--
|
|
|
|
if namespace != "" {
|
|
|
|
itemCount.Namespaces[namespace]--
|
2019-08-13 04:32:57 +00:00
|
|
|
}
|
2019-08-12 23:47:23 +00:00
|
|
|
}
|
2019-09-09 21:28:55 +00:00
|
|
|
|
|
|
|
counts[schema.ID] = itemCount
|
|
|
|
countsCopy := map[string]ItemCount{}
|
|
|
|
for k, v := range counts {
|
|
|
|
countsCopy[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
result <- types.APIEvent{
|
|
|
|
Name: "resource.change",
|
|
|
|
ResourceType: "counts",
|
|
|
|
Object: types.ToAPI(Count{
|
|
|
|
ID: "count",
|
|
|
|
Counts: countsCopy,
|
|
|
|
}),
|
2019-08-12 23:47:23 +00:00
|
|
|
}
|
2019-09-09 21:28:55 +00:00
|
|
|
|
|
|
|
return nil
|
2019-08-12 23:47:23 +00:00
|
|
|
}
|
2019-08-12 22:15:19 +00:00
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
s.ccache.OnAdd(apiOp.Context(), func(gvr schema2.GroupVersionResource, key string, obj runtime.Object) error {
|
|
|
|
return onChange(true, gvr, key, obj)
|
|
|
|
})
|
|
|
|
s.ccache.OnRemove(apiOp.Context(), func(gvr schema2.GroupVersionResource, key string, obj runtime.Object) error {
|
|
|
|
return onChange(false, gvr, key, nil)
|
|
|
|
})
|
2019-08-12 22:15:19 +00:00
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
return result, nil
|
|
|
|
}
|
2019-08-12 22:15:19 +00:00
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
func (s *Store) schemasToWatch(apiOp *types.APIRequest) (result []*types.Schema) {
|
2019-08-12 22:15:19 +00:00
|
|
|
for _, schema := range apiOp.Schemas.Schemas() {
|
|
|
|
if ignore[schema.ID] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-16 18:40:42 +00:00
|
|
|
if attributes.PreferredVersion(schema) != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if attributes.PreferredGroup(schema) != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-12 22:15:19 +00:00
|
|
|
if schema.Store == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if apiOp.AccessControl.CanList(apiOp, schema) != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
if apiOp.AccessControl.CanWatch(apiOp, schema) != nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-08-12 22:15:19 +00:00
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
result = append(result, schema)
|
2019-08-12 22:15:19 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
return
|
|
|
|
}
|
2019-08-12 23:47:23 +00:00
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
func getInfo(obj interface{}) (name string, namespace string, revision int, ok bool) {
|
|
|
|
r, ok := obj.(runtime.Object)
|
|
|
|
if !ok {
|
|
|
|
return "", "", 0, false
|
2019-08-12 23:47:23 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
meta, err := meta.Accessor(r)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", 0, false
|
2019-08-12 22:15:19 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
revision, err = strconv.Atoi(meta.GetResourceVersion())
|
|
|
|
if err != nil {
|
|
|
|
return "", "", 0, false
|
2019-08-12 22:15:19 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
return meta.GetName(), meta.GetNamespace(), revision, true
|
2019-08-12 22:15:19 +00:00
|
|
|
}
|
2019-08-12 23:47:23 +00:00
|
|
|
|
2019-09-09 21:28:55 +00:00
|
|
|
func (s *Store) getCount(apiOp *types.APIRequest) Count {
|
|
|
|
counts := map[string]ItemCount{}
|
|
|
|
|
|
|
|
for _, schema := range s.schemasToWatch(apiOp) {
|
|
|
|
gvr := attributes.GVR(schema)
|
|
|
|
|
|
|
|
rev := 0
|
|
|
|
itemCount := ItemCount{
|
|
|
|
Namespaces: map[string]int{},
|
2019-08-12 23:47:23 +00:00
|
|
|
}
|
2019-09-09 21:28:55 +00:00
|
|
|
|
|
|
|
for _, obj := range s.ccache.List(gvr) {
|
|
|
|
_, ns, revision, ok := getInfo(obj)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if revision > rev {
|
|
|
|
rev = revision
|
|
|
|
}
|
|
|
|
|
|
|
|
itemCount.Count++
|
|
|
|
if ns != "" {
|
|
|
|
itemCount.Namespaces[ns]++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
itemCount.Revision = rev
|
|
|
|
counts[schema.ID] = itemCount
|
|
|
|
}
|
|
|
|
|
|
|
|
return Count{
|
|
|
|
ID: "count",
|
|
|
|
Counts: counts,
|
|
|
|
}
|
2019-08-12 23:47:23 +00:00
|
|
|
}
|