resource.go 4.7 KB
Newer Older
H
hongming 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *
 * Copyright 2020 The KubeSphere Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * /
 */

Z
zryfish 已提交
19 20 21 22
package resource

import (
	"errors"
H
hongming 已提交
23 24
	rbacv1 "k8s.io/api/rbac/v1"
	"k8s.io/apimachinery/pkg/runtime"
Z
zryfish 已提交
25 26
	"k8s.io/apimachinery/pkg/runtime/schema"
	"kubesphere.io/kubesphere/pkg/api"
H
hongming 已提交
27 28
	iamv1alpha2 "kubesphere.io/kubesphere/pkg/apis/iam/v1alpha2"
	tenantv1alpha1 "kubesphere.io/kubesphere/pkg/apis/tenant/v1alpha1"
Z
zryfish 已提交
29
	"kubesphere.io/kubesphere/pkg/apiserver/query"
Z
zryfish 已提交
30
	"kubesphere.io/kubesphere/pkg/informers"
Z
zryfish 已提交
31
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
H
hongming 已提交
32
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/application"
H
hongming 已提交
33 34
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/clusterrole"
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/configmap"
Z
zryfish 已提交
35
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/deployment"
H
hongming 已提交
36
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/globalrole"
H
hongming 已提交
37
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/namespace"
D
Duan Jiong 已提交
38
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/networkpolicy"
H
hongming 已提交
39 40 41 42 43
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/pod"
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/role"
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/user"
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/workspace"
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/workspacerole"
Z
zryfish 已提交
44 45 46 47
)

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

H
hongming 已提交
48
type ResourceGetter struct {
Z
zryfish 已提交
49 50 51
	getters map[schema.GroupVersionResource]v1alpha3.Interface
}

H
hongming 已提交
52
func NewResourceGetter(factory informers.InformerFactory) *ResourceGetter {
Z
zryfish 已提交
53 54
	getters := make(map[schema.GroupVersionResource]v1alpha3.Interface)

Z
zryfish 已提交
55
	getters[schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"}] = deployment.New(factory.KubernetesSharedInformerFactory())
H
hongming 已提交
56
	getters[schema.GroupVersionResource{Group: "", Version: "v1", Resource: "namespaces"}] = namespace.New(factory.KubernetesSharedInformerFactory())
H
hongming 已提交
57 58
	getters[schema.GroupVersionResource{Group: "", Version: "v1", Resource: "configmaps"}] = configmap.New(factory.KubernetesSharedInformerFactory())
	getters[schema.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"}] = pod.New(factory.KubernetesSharedInformerFactory())
H
hongming 已提交
59
	getters[schema.GroupVersionResource{Group: "app.k8s.io", Version: "v1beta1", Resource: "applications"}] = application.New(factory.ApplicationSharedInformerFactory())
D
Duan Jiong 已提交
60
	getters[schema.GroupVersionResource{Group: "networking.k8s.io", Version: "v1", Resource: "networkpolicies"}] = networkpolicy.New(factory.KubernetesSharedInformerFactory())
H
hongming 已提交
61 62 63 64 65 66
	getters[tenantv1alpha1.SchemeGroupVersion.WithResource(tenantv1alpha1.ResourcePluralWorkspace)] = workspace.New(factory.KubeSphereSharedInformerFactory())
	getters[iamv1alpha2.SchemeGroupVersion.WithResource(iamv1alpha2.ResourcesPluralGlobalRole)] = globalrole.New(factory.KubeSphereSharedInformerFactory())
	getters[iamv1alpha2.SchemeGroupVersion.WithResource(iamv1alpha2.ResourcesPluralWorkspaceRole)] = workspacerole.New(factory.KubeSphereSharedInformerFactory())
	getters[iamv1alpha2.SchemeGroupVersion.WithResource(iamv1alpha2.ResourcesPluralUser)] = user.New(factory.KubeSphereSharedInformerFactory())
	getters[rbacv1.SchemeGroupVersion.WithResource("roles")] = role.New(factory.KubernetesSharedInformerFactory())
	getters[rbacv1.SchemeGroupVersion.WithResource("clusterroles")] = clusterrole.New(factory.KubernetesSharedInformerFactory())
Z
zryfish 已提交
67

H
hongming 已提交
68
	return &ResourceGetter{
Z
zryfish 已提交
69 70 71 72 73 74
		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
H
hongming 已提交
75
func (r *ResourceGetter) tryResource(resource string) v1alpha3.Interface {
Z
zryfish 已提交
76 77 78 79 80 81 82 83
	for k, v := range r.getters {
		if k.Resource == resource {
			return v
		}
	}
	return nil
}

H
hongming 已提交
84
func (r *ResourceGetter) Get(resource, namespace, name string) (runtime.Object, error) {
Z
zryfish 已提交
85 86 87 88 89 90 91
	getter := r.tryResource(resource)
	if getter == nil {
		return nil, ErrResourceNotSupported
	}
	return getter.Get(namespace, name)
}

H
hongming 已提交
92
func (r *ResourceGetter) List(resource, namespace string, query *query.Query) (*api.ListResult, error) {
Z
zryfish 已提交
93 94 95 96
	getter := r.tryResource(resource)
	if getter == nil {
		return nil, ErrResourceNotSupported
	}
Z
zryfish 已提交
97
	return getter.List(namespace, query)
Z
zryfish 已提交
98
}