resource.go 1.7 KB
Newer Older
Z
zryfish 已提交
1 2 3 4 5 6 7
package resource

import (
	"errors"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"kubesphere.io/kubesphere/pkg/api"
	"kubesphere.io/kubesphere/pkg/apiserver/query"
Z
zryfish 已提交
8
	"kubesphere.io/kubesphere/pkg/informers"
Z
zryfish 已提交
9 10 11 12 13 14 15 16 17 18
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/deployment"
)

var ErrResourceNotSupported = errors.New("resource is not supported")

type NamespacedResourceGetter struct {
	getters map[schema.GroupVersionResource]v1alpha3.Interface
}

Z
zryfish 已提交
19
func New(factory informers.InformerFactory) *NamespacedResourceGetter {
Z
zryfish 已提交
20 21
	getters := make(map[schema.GroupVersionResource]v1alpha3.Interface)

Z
zryfish 已提交
22
	getters[schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"}] = deployment.New(factory.KubernetesSharedInformerFactory())
Z
zryfish 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

	return &NamespacedResourceGetter{
		getters: getters,
	}
}

// tryResource will retrieve a getter with resource name, it doesn't guarantee find resource with correct group version
// need to refactor this use schema.GroupVersionResource
func (r *NamespacedResourceGetter) tryResource(resource string) v1alpha3.Interface {
	for k, v := range r.getters {
		if k.Resource == resource {
			return v
		}
	}

	return nil
}

func (r *NamespacedResourceGetter) Get(resource, namespace, name string) (interface{}, error) {
	getter := r.tryResource(resource)
	if getter == nil {
		return nil, ErrResourceNotSupported
	}

	return getter.Get(namespace, name)
}

func (r *NamespacedResourceGetter) List(resource, namespace string, query *query.Query) (*api.ListResult, error) {
	getter := r.tryResource(resource)
	if getter == nil {
		return nil, ErrResourceNotSupported
	}

Z
zryfish 已提交
56
	return getter.List(namespace, query)
Z
zryfish 已提交
57 58

}