diff --git a/pkg/api/types.go b/pkg/api/types.go index 5e10391e0..544901ccb 100644 --- a/pkg/api/types.go +++ b/pkg/api/types.go @@ -88,6 +88,7 @@ var SupportedGroupVersionResources = map[ClientType][]schema.GroupVersionResourc {Group: "", Version: "v1", Resource: "persistentvolumeclaims"}, {Group: "", Version: "v1", Resource: "secrets"}, {Group: "", Version: "v1", Resource: "configmaps"}, + {Group: "", Version: "v1", Resource: "serviceaccounts"}, {Group: "rbac.authorization.k8s.io", Version: "v1", Resource: "roles"}, {Group: "rbac.authorization.k8s.io", Version: "v1", Resource: "rolebindings"}, diff --git a/pkg/apiserver/apiserver.go b/pkg/apiserver/apiserver.go index 440cacee5..a7a4301bb 100644 --- a/pkg/apiserver/apiserver.go +++ b/pkg/apiserver/apiserver.go @@ -20,6 +20,12 @@ import ( "bytes" "context" "fmt" + "net/http" + rt "runtime" + "time" + + "strconv" + "github.com/emicklei/go-restful" "k8s.io/apimachinery/pkg/runtime/schema" urlruntime "k8s.io/apimachinery/pkg/util/runtime" @@ -84,11 +90,7 @@ import ( "kubesphere.io/kubesphere/pkg/simple/client/sonarqube" "kubesphere.io/kubesphere/pkg/utils/metrics" utilnet "kubesphere.io/kubesphere/pkg/utils/net" - "net/http" - rt "runtime" runtimecache "sigs.k8s.io/controller-runtime/pkg/cache" - "strconv" - "time" ) const ( @@ -377,6 +379,8 @@ func (s *APIServer) waitForResourceSync(stopCh <-chan struct{}) error { {Group: "", Version: "v1", Resource: "persistentvolumeclaims"}, {Group: "", Version: "v1", Resource: "secrets"}, {Group: "", Version: "v1", Resource: "configmaps"}, + {Group: "", Version: "v1", Resource: "serviceaccounts"}, + {Group: "rbac.authorization.k8s.io", Version: "v1", Resource: "roles"}, {Group: "rbac.authorization.k8s.io", Version: "v1", Resource: "rolebindings"}, {Group: "rbac.authorization.k8s.io", Version: "v1", Resource: "clusterroles"}, diff --git a/pkg/models/resources/v1alpha3/resource/resource.go b/pkg/models/resources/v1alpha3/resource/resource.go index df09fe0dd..14bb881ca 100644 --- a/pkg/models/resources/v1alpha3/resource/resource.go +++ b/pkg/models/resources/v1alpha3/resource/resource.go @@ -18,6 +18,7 @@ package resource import ( "errors" + snapshotv1beta1 "github.com/kubernetes-csi/external-snapshotter/client/v3/apis/volumesnapshot/v1beta1" rbacv1 "k8s.io/api/rbac/v1" "k8s.io/apimachinery/pkg/runtime" @@ -66,6 +67,7 @@ import ( "kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/role" "kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/rolebinding" "kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/service" + "kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/serviceaccount" "kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/statefulset" "kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/user" "kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/volumesnapshot" @@ -93,6 +95,7 @@ func NewResourceGetter(factory informers.InformerFactory, cache cache.Cache) *Re getters[schema.GroupVersionResource{Group: "", Version: "v1", Resource: "configmaps"}] = configmap.New(factory.KubernetesSharedInformerFactory()) getters[schema.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"}] = pod.New(factory.KubernetesSharedInformerFactory()) getters[schema.GroupVersionResource{Group: "", Version: "v1", Resource: "nodes"}] = node.New(factory.KubernetesSharedInformerFactory()) + getters[schema.GroupVersionResource{Group: "", Version: "v1", Resource: "serviceaccounts"}] = serviceaccount.New(factory.KubernetesSharedInformerFactory()) getters[schema.GroupVersionResource{Group: "extensions", Version: "v1beta1", Resource: "ingresses"}] = ingress.New(factory.KubernetesSharedInformerFactory()) getters[schema.GroupVersionResource{Group: "networking.k8s.io", Version: "v1", Resource: "networkpolicies"}] = networkpolicy.New(factory.KubernetesSharedInformerFactory()) getters[schema.GroupVersionResource{Group: "batch", Version: "v1", Resource: "jobs"}] = job.New(factory.KubernetesSharedInformerFactory()) diff --git a/pkg/models/resources/v1alpha3/serviceaccount/serviceaccount.go b/pkg/models/resources/v1alpha3/serviceaccount/serviceaccount.go new file mode 100644 index 000000000..f310c6655 --- /dev/null +++ b/pkg/models/resources/v1alpha3/serviceaccount/serviceaccount.go @@ -0,0 +1,76 @@ +/* +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. +*/ + +package serviceaccount + +import ( + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/informers" + "kubesphere.io/kubesphere/pkg/api" + "kubesphere.io/kubesphere/pkg/apiserver/query" + "kubesphere.io/kubesphere/pkg/models/resources/v1alpha3" +) + +type serviceaccountsGetter struct { + informer informers.SharedInformerFactory +} + +func New(sharedInformers informers.SharedInformerFactory) v1alpha3.Interface { + return &serviceaccountsGetter{informer: sharedInformers} +} + +func (d *serviceaccountsGetter) Get(namespace, name string) (runtime.Object, error) { + return d.informer.Core().V1().ServiceAccounts().Lister().ServiceAccounts(namespace).Get(name) +} + +func (d *serviceaccountsGetter) List(namespace string, query *query.Query) (*api.ListResult, error) { + serviceaccounts, err := d.informer.Core().V1().ServiceAccounts().Lister().ServiceAccounts(namespace).List(query.Selector()) + if err != nil { + return nil, err + } + + var result []runtime.Object + for _, serviceaccount := range serviceaccounts { + result = append(result, serviceaccount) + } + + return v1alpha3.DefaultList(result, query, d.compare, d.filter), nil +} + +func (d *serviceaccountsGetter) compare(left runtime.Object, right runtime.Object, field query.Field) bool { + + leftCM, ok := left.(*corev1.ServiceAccount) + if !ok { + return false + } + + rightCM, ok := right.(*corev1.ServiceAccount) + if !ok { + return false + } + + return v1alpha3.DefaultObjectMetaCompare(leftCM.ObjectMeta, rightCM.ObjectMeta, field) +} + +func (d *serviceaccountsGetter) filter(object runtime.Object, filter query.Filter) bool { + serviceAccount, ok := object.(*corev1.ServiceAccount) + if !ok { + return false + } + + return v1alpha3.DefaultObjectMetaFilter(serviceAccount.ObjectMeta, filter) +} diff --git a/pkg/models/resources/v1alpha3/serviceaccount/serviceaccount_test.go b/pkg/models/resources/v1alpha3/serviceaccount/serviceaccount_test.go new file mode 100644 index 000000000..b1e10f549 --- /dev/null +++ b/pkg/models/resources/v1alpha3/serviceaccount/serviceaccount_test.go @@ -0,0 +1,108 @@ +/* +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. +*/ + +package serviceaccount + +import ( + "testing" + + "github.com/google/go-cmp/cmp" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/informers" + "k8s.io/client-go/kubernetes/fake" + "kubesphere.io/kubesphere/pkg/api" + "kubesphere.io/kubesphere/pkg/apiserver/query" + "kubesphere.io/kubesphere/pkg/models/resources/v1alpha3" +) + +func TestListServiceAccounts(t *testing.T) { + tests := []struct { + description string + namespace string + query *query.Query + expected *api.ListResult + expectedErr error + }{ + { + "test name filter", + "default", + &query.Query{ + Pagination: &query.Pagination{ + Limit: 10, + Offset: 0, + }, + SortBy: query.FieldName, + Ascending: false, + Filters: map[query.Field]query.Value{query.FieldNamespace: query.Value("default")}, + }, + &api.ListResult{ + Items: []interface{}{foo3, foo2, foo1}, + TotalItems: len(serviceAccounts), + }, + nil, + }, + } + + getter := prepare() + + for _, test := range tests { + got, err := getter.List(test.namespace, test.query) + if test.expectedErr != nil && err != test.expectedErr { + t.Errorf("expected error, got nothing") + } else if err != nil { + t.Fatal(err) + } + + if diff := cmp.Diff(got, test.expected); diff != "" { + t.Errorf("%T differ (-got, +want): %s", test.expected, diff) + } + } +} + +var ( + foo1 = &corev1.ServiceAccount{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo1", + Namespace: "default", + }, + } + foo2 = &corev1.ServiceAccount{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo2", + Namespace: "default", + }, + } + foo3 = &corev1.ServiceAccount{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo3", + Namespace: "default", + }, + } + serviceAccounts = []interface{}{foo1, foo2, foo3} +) + +func prepare() v1alpha3.Interface { + + client := fake.NewSimpleClientset() + informer := informers.NewSharedInformerFactory(client, 0) + + for _, serviceAccount := range serviceAccounts { + informer.Core().V1().ServiceAccounts().Informer().GetIndexer().Add(serviceAccount) + } + + return New(informer) +}