resource.go 11.6 KB
Newer Older
H
hongming 已提交
1
/*
H
hongming 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15
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.
*/
H
hongming 已提交
16

Z
zryfish 已提交
17 18 19 20
package resource

import (
	"errors"
21

Z
zhangmin 已提交
22
	snapshotv1beta1 "github.com/kubernetes-csi/external-snapshotter/v2/pkg/apis/volumesnapshot/v1beta1"
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
	devopsv1alpha3 "kubesphere.io/kubesphere/pkg/apis/devops/v1alpha3"
H
hongming 已提交
28 29
	iamv1alpha2 "kubesphere.io/kubesphere/pkg/apis/iam/v1alpha2"
	tenantv1alpha1 "kubesphere.io/kubesphere/pkg/apis/tenant/v1alpha1"
H
hongming 已提交
30
	tenantv1alpha2 "kubesphere.io/kubesphere/pkg/apis/tenant/v1alpha2"
H
hongming 已提交
31
	typesv1beta1 "kubesphere.io/kubesphere/pkg/apis/types/v1beta1"
Z
zryfish 已提交
32
	"kubesphere.io/kubesphere/pkg/apiserver/query"
Z
zryfish 已提交
33
	"kubesphere.io/kubesphere/pkg/informers"
Z
zryfish 已提交
34
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
H
hongming 已提交
35
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/application"
36
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/cluster"
H
hongming 已提交
37
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/clusterrole"
H
hongming 已提交
38
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/clusterrolebinding"
H
hongming 已提交
39
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/configmap"
40
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/customresourcedefinition"
41
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/daemonset"
Z
zryfish 已提交
42
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/deployment"
H
hongming 已提交
43
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/devops"
Z
zryfish 已提交
44 45 46
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/federatedapplication"
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/federatedconfigmap"
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/federateddeployment"
47
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/federatedingress"
H
hongming 已提交
48
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/federatednamespace"
Z
zryfish 已提交
49 50 51
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/federatedpersistentvolumeclaim"
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/federatedsecret"
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/federatedservice"
52
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/federatedstatefulset"
H
hongming 已提交
53
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/globalrole"
H
hongming 已提交
54
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/globalrolebinding"
55 56
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/group"
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/groupbinding"
57
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/ingress"
Z
zryfish 已提交
58
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/job"
Z
zryfish 已提交
59
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/loginrecord"
H
hongming 已提交
60
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/namespace"
D
Duan Jiong 已提交
61
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/networkpolicy"
Z
zryfish 已提交
62
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/node"
Z
zhangmin 已提交
63
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/persistentvolumeclaim"
H
hongming 已提交
64 65
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/pod"
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/role"
H
hongming 已提交
66
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/rolebinding"
67 68
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/service"
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/statefulset"
H
hongming 已提交
69
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/user"
Z
zhangmin 已提交
70
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/volumesnapshot"
H
hongming 已提交
71 72
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/workspace"
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/workspacerole"
H
hongming 已提交
73 74
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/workspacerolebinding"
	"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/workspacetemplate"
Z
zryfish 已提交
75 76 77 78
)

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

H
hongming 已提交
79
type ResourceGetter struct {
Z
zryfish 已提交
80 81 82
	getters map[schema.GroupVersionResource]v1alpha3.Interface
}

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

Z
zryfish 已提交
86
	getters[schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"}] = deployment.New(factory.KubernetesSharedInformerFactory())
87 88 89
	getters[schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "daemonsets"}] = daemonset.New(factory.KubernetesSharedInformerFactory())
	getters[schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "statefulsets"}] = statefulset.New(factory.KubernetesSharedInformerFactory())
	getters[schema.GroupVersionResource{Group: "", Version: "v1", Resource: "services"}] = service.New(factory.KubernetesSharedInformerFactory())
H
hongming 已提交
90
	getters[schema.GroupVersionResource{Group: "", Version: "v1", Resource: "namespaces"}] = namespace.New(factory.KubernetesSharedInformerFactory())
H
hongming 已提交
91 92
	getters[schema.GroupVersionResource{Group: "", Version: "v1", Resource: "configmaps"}] = configmap.New(factory.KubernetesSharedInformerFactory())
	getters[schema.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"}] = pod.New(factory.KubernetesSharedInformerFactory())
Z
zryfish 已提交
93
	getters[schema.GroupVersionResource{Group: "", Version: "v1", Resource: "nodes"}] = node.New(factory.KubernetesSharedInformerFactory())
94
	getters[schema.GroupVersionResource{Group: "extensions", Version: "v1beta1", Resource: "ingresses"}] = ingress.New(factory.KubernetesSharedInformerFactory())
H
hongming 已提交
95
	getters[schema.GroupVersionResource{Group: "app.k8s.io", Version: "v1beta1", Resource: "applications"}] = application.New(factory.ApplicationSharedInformerFactory())
D
Duan Jiong 已提交
96
	getters[schema.GroupVersionResource{Group: "networking.k8s.io", Version: "v1", Resource: "networkpolicies"}] = networkpolicy.New(factory.KubernetesSharedInformerFactory())
Z
zryfish 已提交
97 98 99
	getters[schema.GroupVersionResource{Group: "batch", Version: "v1", Resource: "jobs"}] = job.New(factory.KubernetesSharedInformerFactory())

	// kubesphere resources
H
hongming 已提交
100
	getters[devopsv1alpha3.SchemeGroupVersion.WithResource(devopsv1alpha3.ResourcePluralDevOpsProject)] = devops.New(factory.KubeSphereSharedInformerFactory())
H
hongming 已提交
101
	getters[tenantv1alpha1.SchemeGroupVersion.WithResource(tenantv1alpha1.ResourcePluralWorkspace)] = workspace.New(factory.KubeSphereSharedInformerFactory())
H
hongming 已提交
102
	getters[tenantv1alpha1.SchemeGroupVersion.WithResource(tenantv1alpha2.ResourcePluralWorkspaceTemplate)] = workspacetemplate.New(factory.KubeSphereSharedInformerFactory())
H
hongming 已提交
103 104
	getters[iamv1alpha2.SchemeGroupVersion.WithResource(iamv1alpha2.ResourcesPluralGlobalRole)] = globalrole.New(factory.KubeSphereSharedInformerFactory())
	getters[iamv1alpha2.SchemeGroupVersion.WithResource(iamv1alpha2.ResourcesPluralWorkspaceRole)] = workspacerole.New(factory.KubeSphereSharedInformerFactory())
H
hongming 已提交
105 106 107
	getters[iamv1alpha2.SchemeGroupVersion.WithResource(iamv1alpha2.ResourcesPluralUser)] = user.New(factory.KubeSphereSharedInformerFactory(), factory.KubernetesSharedInformerFactory())
	getters[iamv1alpha2.SchemeGroupVersion.WithResource(iamv1alpha2.ResourcesPluralGlobalRoleBinding)] = globalrolebinding.New(factory.KubeSphereSharedInformerFactory())
	getters[iamv1alpha2.SchemeGroupVersion.WithResource(iamv1alpha2.ResourcesPluralWorkspaceRoleBinding)] = workspacerolebinding.New(factory.KubeSphereSharedInformerFactory())
Z
zryfish 已提交
108
	getters[iamv1alpha2.SchemeGroupVersion.WithResource(iamv1alpha2.ResourcesPluralLoginRecord)] = loginrecord.New(factory.KubeSphereSharedInformerFactory())
109 110
	getters[iamv1alpha2.SchemeGroupVersion.WithResource(iamv1alpha2.ResourcePluralGroup)] = group.New(factory.KubeSphereSharedInformerFactory())
	getters[iamv1alpha2.SchemeGroupVersion.WithResource(iamv1alpha2.ResourcePluralGroupBinding)] = groupbinding.New(factory.KubeSphereSharedInformerFactory())
H
hongming 已提交
111 112 113 114
	getters[rbacv1.SchemeGroupVersion.WithResource(iamv1alpha2.ResourcesPluralRole)] = role.New(factory.KubernetesSharedInformerFactory())
	getters[rbacv1.SchemeGroupVersion.WithResource(iamv1alpha2.ResourcesPluralClusterRole)] = clusterrole.New(factory.KubernetesSharedInformerFactory())
	getters[rbacv1.SchemeGroupVersion.WithResource(iamv1alpha2.ResourcesPluralRoleBinding)] = rolebinding.New(factory.KubernetesSharedInformerFactory())
	getters[rbacv1.SchemeGroupVersion.WithResource(iamv1alpha2.ResourcesPluralClusterRoleBinding)] = clusterrolebinding.New(factory.KubernetesSharedInformerFactory())
Z
zhangmin 已提交
115
	getters[schema.GroupVersionResource{Group: "", Version: "v1", Resource: "persistentvolumeclaims"}] = persistentvolumeclaim.New(factory.KubernetesSharedInformerFactory(), factory.SnapshotSharedInformerFactory())
Z
zhangmin 已提交
116
	getters[snapshotv1beta1.SchemeGroupVersion.WithResource("volumesnapshots")] = volumesnapshot.New(factory.SnapshotSharedInformerFactory())
117 118
	getters[schema.GroupVersionResource{Group: "cluster.kubesphere.io", Version: "v1alpha1", Resource: "clusters"}] = cluster.New(factory.KubeSphereSharedInformerFactory())
	getters[schema.GroupVersionResource{Group: "apiextensions.k8s.io", Version: "v1", Resource: "customresourcedefinitions"}] = customresourcedefinition.New(factory.ApiExtensionSharedInformerFactory())
Z
zryfish 已提交
119 120 121 122 123 124 125 126 127

	// federated resources
	getters[typesv1beta1.SchemeGroupVersion.WithResource(typesv1beta1.ResourcePluralFederatedNamespace)] = federatednamespace.New(factory.KubeSphereSharedInformerFactory())
	getters[typesv1beta1.SchemeGroupVersion.WithResource(typesv1beta1.ResourcePluralFederatedDeployment)] = federateddeployment.New(factory.KubeSphereSharedInformerFactory())
	getters[typesv1beta1.SchemeGroupVersion.WithResource(typesv1beta1.ResourcePluralFederatedSecret)] = federatedsecret.New(factory.KubeSphereSharedInformerFactory())
	getters[typesv1beta1.SchemeGroupVersion.WithResource(typesv1beta1.ResourcePluralFederatedConfigmap)] = federatedconfigmap.New(factory.KubeSphereSharedInformerFactory())
	getters[typesv1beta1.SchemeGroupVersion.WithResource(typesv1beta1.ResourcePluralFederatedService)] = federatedservice.New(factory.KubeSphereSharedInformerFactory())
	getters[typesv1beta1.SchemeGroupVersion.WithResource(typesv1beta1.ResourcePluralFederatedApplication)] = federatedapplication.New(factory.KubeSphereSharedInformerFactory())
	getters[typesv1beta1.SchemeGroupVersion.WithResource(typesv1beta1.ResourcePluralFederatedPersistentVolumeClaim)] = federatedpersistentvolumeclaim.New(factory.KubeSphereSharedInformerFactory())
128 129
	getters[typesv1beta1.SchemeGroupVersion.WithResource(typesv1beta1.ResourcePluralFederatedStatefulSet)] = federatedstatefulset.New(factory.KubeSphereSharedInformerFactory())
	getters[typesv1beta1.SchemeGroupVersion.WithResource(typesv1beta1.ResourcePluralFederatedIngress)] = federatedingress.New(factory.KubeSphereSharedInformerFactory())
H
hongming 已提交
130

H
hongming 已提交
131
	return &ResourceGetter{
Z
zryfish 已提交
132 133 134 135
		getters: getters,
	}
}

H
hongming 已提交
136
// TryResource will retrieve a getter with resource name, it doesn't guarantee find resource with correct group version
Z
zryfish 已提交
137
// need to refactor this use schema.GroupVersionResource
H
hongming 已提交
138
func (r *ResourceGetter) TryResource(resource string) v1alpha3.Interface {
Z
zryfish 已提交
139 140 141 142 143 144 145 146
	for k, v := range r.getters {
		if k.Resource == resource {
			return v
		}
	}
	return nil
}

H
hongming 已提交
147
func (r *ResourceGetter) Get(resource, namespace, name string) (runtime.Object, error) {
H
hongming 已提交
148
	getter := r.TryResource(resource)
Z
zryfish 已提交
149 150 151 152 153 154
	if getter == nil {
		return nil, ErrResourceNotSupported
	}
	return getter.Get(namespace, name)
}

H
hongming 已提交
155
func (r *ResourceGetter) List(resource, namespace string, query *query.Query) (*api.ListResult, error) {
H
hongming 已提交
156
	getter := r.TryResource(resource)
Z
zryfish 已提交
157 158 159
	if getter == nil {
		return nil, ErrResourceNotSupported
	}
Z
zryfish 已提交
160
	return getter.List(namespace, query)
Z
zryfish 已提交
161
}