78
pkg/models/resources/v1alpha3/application/applications.go
Normal file
78
pkg/models/resources/v1alpha3/application/applications.go
Normal file
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
|
||||
Copyright 2019 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 application
|
||||
|
||||
import (
|
||||
appv1beta1 "github.com/kubernetes-sigs/application/pkg/apis/app/v1beta1"
|
||||
"github.com/kubernetes-sigs/application/pkg/client/informers/externalversions"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"kubesphere.io/kubesphere/pkg/api"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/query"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
|
||||
)
|
||||
|
||||
type applicationsGetter struct {
|
||||
informer externalversions.SharedInformerFactory
|
||||
}
|
||||
|
||||
func New(sharedInformers externalversions.SharedInformerFactory) v1alpha3.Interface {
|
||||
return &applicationsGetter{informer: sharedInformers}
|
||||
}
|
||||
|
||||
func (d *applicationsGetter) Get(namespace, name string) (runtime.Object, error) {
|
||||
return d.informer.App().V1beta1().Applications().Lister().Applications(namespace).Get(name)
|
||||
}
|
||||
|
||||
func (d *applicationsGetter) List(namespace string, query *query.Query) (*api.ListResult, error) {
|
||||
all, err := d.informer.App().V1beta1().Applications().Lister().Applications(namespace).List(labels.Everything())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []runtime.Object
|
||||
for _, app := range all {
|
||||
result = append(result, app)
|
||||
}
|
||||
|
||||
return v1alpha3.DefaultList(result, query, d.compare, d.filter), nil
|
||||
}
|
||||
|
||||
func (d *applicationsGetter) compare(left runtime.Object, right runtime.Object, field query.Field) bool {
|
||||
|
||||
leftApplication, ok := left.(*appv1beta1.Application)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
rightApplication, ok := right.(*appv1beta1.Application)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
return v1alpha3.DefaultObjectMetaCompare(leftApplication.ObjectMeta, rightApplication.ObjectMeta, field)
|
||||
}
|
||||
|
||||
func (d *applicationsGetter) filter(object runtime.Object, filter query.Filter) bool {
|
||||
application, ok := object.(*appv1beta1.Application)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
return v1alpha3.DefaultObjectMetaFilter(application.ObjectMeta, filter)
|
||||
}
|
||||
109
pkg/models/resources/v1alpha3/application/applications_test.go
Normal file
109
pkg/models/resources/v1alpha3/application/applications_test.go
Normal file
@@ -0,0 +1,109 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"github.com/google/go-cmp/cmp"
|
||||
appv1beta1 "github.com/kubernetes-sigs/application/pkg/apis/app/v1beta1"
|
||||
"github.com/kubernetes-sigs/application/pkg/client/clientset/versioned/fake"
|
||||
"github.com/kubernetes-sigs/application/pkg/client/informers/externalversions"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"kubesphere.io/kubesphere/pkg/api"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/query"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func applicationsToRuntimeObjects(applications ...*appv1beta1.Application) []runtime.Object {
|
||||
var objs []runtime.Object
|
||||
for _, app := range applications {
|
||||
objs = append(objs, app)
|
||||
}
|
||||
return objs
|
||||
}
|
||||
|
||||
func TestListApplications(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
namespace string
|
||||
deployments []*appv1beta1.Application
|
||||
query *query.Query
|
||||
expected api.ListResult
|
||||
expectedErr error
|
||||
}{
|
||||
{
|
||||
"test name filter",
|
||||
"bar2",
|
||||
[]*appv1beta1.Application{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "foo-1",
|
||||
Namespace: "bar",
|
||||
},
|
||||
},
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "foo-2",
|
||||
Namespace: "bar",
|
||||
},
|
||||
},
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "bar-2",
|
||||
Namespace: "bar2",
|
||||
},
|
||||
},
|
||||
},
|
||||
&query.Query{
|
||||
Pagination: &query.Pagination{
|
||||
Limit: 10,
|
||||
Offset: 0,
|
||||
},
|
||||
SortBy: query.FieldName,
|
||||
Ascending: false,
|
||||
Filters: []query.Filter{
|
||||
{
|
||||
Field: query.FieldNamespace,
|
||||
Value: query.Value("bar2"),
|
||||
},
|
||||
},
|
||||
},
|
||||
api.ListResult{
|
||||
Items: []interface{}{
|
||||
&appv1beta1.Application{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "bar-2",
|
||||
Namespace: "bar2",
|
||||
},
|
||||
},
|
||||
},
|
||||
TotalItems: 2,
|
||||
},
|
||||
nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.description, func(t *testing.T) {
|
||||
objs := applicationsToRuntimeObjects(test.deployments...)
|
||||
client := fake.NewSimpleClientset(objs...)
|
||||
|
||||
informer := externalversions.NewSharedInformerFactory(client, 0)
|
||||
|
||||
for _, deployment := range test.deployments {
|
||||
informer.App().V1beta1().Applications().Informer().GetIndexer().Add(deployment)
|
||||
}
|
||||
|
||||
getter := New(informer)
|
||||
|
||||
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.Items, test.expected.Items); diff != "" {
|
||||
t.Errorf("%T differ (-got, +want): %s", test.expected, diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
78
pkg/models/resources/v1alpha3/configmap/configmaps.go
Normal file
78
pkg/models/resources/v1alpha3/configmap/configmaps.go
Normal file
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
|
||||
Copyright 2019 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 configmap
|
||||
|
||||
import (
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"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 configmapsGetter struct {
|
||||
informer informers.SharedInformerFactory
|
||||
}
|
||||
|
||||
func New(sharedInformers informers.SharedInformerFactory) v1alpha3.Interface {
|
||||
return &configmapsGetter{informer: sharedInformers}
|
||||
}
|
||||
|
||||
func (d *configmapsGetter) Get(namespace, name string) (runtime.Object, error) {
|
||||
return d.informer.Core().V1().ConfigMaps().Lister().ConfigMaps(namespace).Get(name)
|
||||
}
|
||||
|
||||
func (d *configmapsGetter) List(namespace string, query *query.Query) (*api.ListResult, error) {
|
||||
all, err := d.informer.Core().V1().ConfigMaps().Lister().ConfigMaps(namespace).List(labels.Everything())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []runtime.Object
|
||||
for _, app := range all {
|
||||
result = append(result, app)
|
||||
}
|
||||
|
||||
return v1alpha3.DefaultList(result, query, d.compare, d.filter), nil
|
||||
}
|
||||
|
||||
func (d *configmapsGetter) compare(left runtime.Object, right runtime.Object, field query.Field) bool {
|
||||
|
||||
leftConfigMap, ok := left.(*corev1.ConfigMap)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
rightConfigMap, ok := right.(*corev1.ConfigMap)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
return v1alpha3.DefaultObjectMetaCompare(leftConfigMap.ObjectMeta, rightConfigMap.ObjectMeta, field)
|
||||
}
|
||||
|
||||
func (d *configmapsGetter) filter(object runtime.Object, filter query.Filter) bool {
|
||||
configMap, ok := object.(*corev1.ConfigMap)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
return v1alpha3.DefaultObjectMetaFilter(configMap.ObjectMeta, filter)
|
||||
}
|
||||
96
pkg/models/resources/v1alpha3/configmap/configmaps_test.go
Normal file
96
pkg/models/resources/v1alpha3/configmap/configmaps_test.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package configmap
|
||||
|
||||
import (
|
||||
"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"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestListConfigMaps(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: []query.Filter{
|
||||
{
|
||||
Field: query.FieldNamespace,
|
||||
Value: query.Value("default"),
|
||||
},
|
||||
},
|
||||
},
|
||||
&api.ListResult{
|
||||
Items: []interface{}{foo3, foo2, foo1},
|
||||
TotalItems: len(configmaps),
|
||||
},
|
||||
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.ConfigMap{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "foo1",
|
||||
Namespace: "default",
|
||||
},
|
||||
}
|
||||
foo2 = &corev1.ConfigMap{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "foo2",
|
||||
Namespace: "default",
|
||||
},
|
||||
}
|
||||
foo3 = &corev1.ConfigMap{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "foo3",
|
||||
Namespace: "default",
|
||||
},
|
||||
}
|
||||
configmaps = []interface{}{foo1, foo2, foo3}
|
||||
)
|
||||
|
||||
func prepare() v1alpha3.Interface {
|
||||
|
||||
client := fake.NewSimpleClientset()
|
||||
informer := informers.NewSharedInformerFactory(client, 0)
|
||||
|
||||
for _, configmap := range configmaps {
|
||||
informer.Core().V1().ConfigMaps().Informer().GetIndexer().Add(configmap)
|
||||
}
|
||||
|
||||
return New(informer)
|
||||
}
|
||||
@@ -1,149 +1,103 @@
|
||||
package deployment
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
v1 "k8s.io/api/apps/v1"
|
||||
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
appsv1 "k8s.io/api/apps/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"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func newDeployments(total int, name, namespace, application string) []*v1.Deployment {
|
||||
var deployments []*v1.Deployment
|
||||
|
||||
for i := 0; i < total; i++ {
|
||||
deploy := &v1.Deployment{
|
||||
TypeMeta: metaV1.TypeMeta{
|
||||
Kind: "Deployment",
|
||||
APIVersion: "v1",
|
||||
},
|
||||
ObjectMeta: metaV1.ObjectMeta{
|
||||
Name: fmt.Sprintf("%s-%d", name, i),
|
||||
Namespace: namespace,
|
||||
Labels: map[string]string{
|
||||
"seq": fmt.Sprintf("seq-%d", i),
|
||||
},
|
||||
Annotations: map[string]string{},
|
||||
CreationTimestamp: metaV1.Time{Time: time.Now().Add(time.Duration(i*5) * time.Second)},
|
||||
},
|
||||
Status: v1.DeploymentStatus{
|
||||
ReadyReplicas: int32(i + 1),
|
||||
Replicas: int32(i + 1),
|
||||
AvailableReplicas: int32(i + 1),
|
||||
Conditions: []v1.DeploymentCondition{
|
||||
{
|
||||
Type: v1.DeploymentAvailable,
|
||||
LastUpdateTime: metaV1.Time{Time: time.Now().Add(time.Duration(i*5) * time.Second)},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
deployments = append(deployments, deploy)
|
||||
}
|
||||
|
||||
return deployments
|
||||
}
|
||||
|
||||
func deploymentsToRuntimeObjects(deployments ...*v1.Deployment) []runtime.Object {
|
||||
var objs []runtime.Object
|
||||
for _, deploy := range deployments {
|
||||
objs = append(objs, deploy)
|
||||
}
|
||||
|
||||
return objs
|
||||
}
|
||||
|
||||
func TestListDeployments(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
namespace string
|
||||
deployments []*v1.Deployment
|
||||
query *query.Query
|
||||
expected api.ListResult
|
||||
expected *api.ListResult
|
||||
expectedErr error
|
||||
}{
|
||||
{
|
||||
"test name filter",
|
||||
"bar",
|
||||
[]*v1.Deployment{
|
||||
{
|
||||
ObjectMeta: metaV1.ObjectMeta{
|
||||
Name: "foo-1",
|
||||
Namespace: "bar",
|
||||
},
|
||||
},
|
||||
{
|
||||
ObjectMeta: metaV1.ObjectMeta{
|
||||
Name: "foo-2",
|
||||
Namespace: "bar",
|
||||
},
|
||||
},
|
||||
{
|
||||
ObjectMeta: metaV1.ObjectMeta{
|
||||
Name: "bar-1",
|
||||
Namespace: "bar",
|
||||
},
|
||||
},
|
||||
},
|
||||
&query.Query{
|
||||
Pagination: &query.Pagination{
|
||||
Limit: 1,
|
||||
Offset: 1,
|
||||
Offset: 0,
|
||||
},
|
||||
SortBy: query.FieldName,
|
||||
Ascending: false,
|
||||
Filters: []query.Filter{
|
||||
{
|
||||
Field: query.FieldName,
|
||||
Value: query.Value("foo"),
|
||||
Value: query.Value("foo2"),
|
||||
},
|
||||
},
|
||||
},
|
||||
api.ListResult{
|
||||
&api.ListResult{
|
||||
Items: []interface{}{
|
||||
&v1.Deployment{
|
||||
ObjectMeta: metaV1.ObjectMeta{
|
||||
Name: "foo-2",
|
||||
Namespace: "bar",
|
||||
},
|
||||
},
|
||||
foo2,
|
||||
},
|
||||
TotalItems: 2,
|
||||
TotalItems: 1,
|
||||
},
|
||||
nil,
|
||||
},
|
||||
}
|
||||
|
||||
getter := prepare()
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.description, func(t *testing.T) {
|
||||
objs := deploymentsToRuntimeObjects(test.deployments...)
|
||||
client := fake.NewSimpleClientset(objs...)
|
||||
|
||||
informer := informers.NewSharedInformerFactory(client, 0)
|
||||
|
||||
for _, deployment := range test.deployments {
|
||||
informer.Apps().V1().Deployments().Informer().GetIndexer().Add(deployment)
|
||||
}
|
||||
|
||||
getter := New(informer)
|
||||
|
||||
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.Items, test.expected.Items); diff != "" {
|
||||
if diff := cmp.Diff(got, test.expected); diff != "" {
|
||||
t.Errorf("%T differ (-got, +want): %s", test.expected, diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
foo1 = &appsv1.Deployment{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "foo1",
|
||||
Namespace: "bar",
|
||||
},
|
||||
}
|
||||
|
||||
foo2 = &appsv1.Deployment{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "foo2",
|
||||
Namespace: "bar",
|
||||
},
|
||||
}
|
||||
bar1 = &appsv1.Deployment{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "bar1",
|
||||
Namespace: "bar",
|
||||
},
|
||||
}
|
||||
|
||||
deployments = []interface{}{foo1, foo2, bar1}
|
||||
)
|
||||
|
||||
func prepare() v1alpha3.Interface {
|
||||
client := fake.NewSimpleClientset()
|
||||
informer := informers.NewSharedInformerFactory(client, 0)
|
||||
|
||||
for _, deployment := range deployments {
|
||||
informer.Apps().V1().Deployments().Informer().GetIndexer().Add(deployment)
|
||||
}
|
||||
|
||||
return New(informer)
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ type Interface interface {
|
||||
List(namespace string, query *query.Query) (*api.ListResult, error)
|
||||
}
|
||||
|
||||
// CompareFunc return true is left great than right
|
||||
type CompareFunc func(runtime.Object, runtime.Object, query.Field) bool
|
||||
|
||||
type FilterFunc func(runtime.Object, query.Filter) bool
|
||||
@@ -41,9 +42,9 @@ func DefaultList(objects []runtime.Object, query *query.Query, compareFunc Compa
|
||||
// sort by sortBy field
|
||||
sort.Slice(filtered, func(i, j int) bool {
|
||||
if !query.Ascending {
|
||||
return !compareFunc(filtered[i], filtered[j], query.SortBy)
|
||||
return compareFunc(filtered[i], filtered[j], query.SortBy)
|
||||
}
|
||||
return compareFunc(filtered[i], filtered[j], query.SortBy)
|
||||
return !compareFunc(filtered[i], filtered[j], query.SortBy)
|
||||
})
|
||||
|
||||
total := len(filtered)
|
||||
@@ -55,6 +56,7 @@ func DefaultList(objects []runtime.Object, query *query.Query, compareFunc Compa
|
||||
}
|
||||
}
|
||||
|
||||
// DefaultObjectMetaCompare return true is left great than right
|
||||
func DefaultObjectMetaCompare(left, right metav1.ObjectMeta, sortBy query.Field) bool {
|
||||
switch sortBy {
|
||||
// ?sortBy=name
|
||||
@@ -62,6 +64,10 @@ func DefaultObjectMetaCompare(left, right metav1.ObjectMeta, sortBy query.Field)
|
||||
return strings.Compare(left.Name, right.Name) > 0
|
||||
// ?sortBy=creationTimestamp
|
||||
case query.FieldCreationTimeStamp:
|
||||
// compare by name if creation timestamp is equal
|
||||
if left.CreationTimestamp.Equal(&right.CreationTimestamp) {
|
||||
return strings.Compare(left.Name, right.Name) > 0
|
||||
}
|
||||
return left.CreationTimestamp.After(right.CreationTimestamp.Time)
|
||||
default:
|
||||
return false
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/query"
|
||||
"kubesphere.io/kubesphere/pkg/informers"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/application"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/deployment"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/namespace"
|
||||
)
|
||||
@@ -17,11 +18,12 @@ type ResourceGetter struct {
|
||||
getters map[schema.GroupVersionResource]v1alpha3.Interface
|
||||
}
|
||||
|
||||
func New(factory informers.InformerFactory) *ResourceGetter {
|
||||
func NewResourceGetter(factory informers.InformerFactory) *ResourceGetter {
|
||||
getters := make(map[schema.GroupVersionResource]v1alpha3.Interface)
|
||||
|
||||
getters[schema.GroupVersionResource{Group: "apps", Version: "v1", Resource: "deployments"}] = deployment.New(factory.KubernetesSharedInformerFactory())
|
||||
getters[schema.GroupVersionResource{Group: "", Version: "v1", Resource: "namespaces"}] = namespace.New(factory.KubernetesSharedInformerFactory())
|
||||
getters[schema.GroupVersionResource{Group: "app.k8s.io", Version: "v1beta1", Resource: "applications"}] = application.New(factory.ApplicationSharedInformerFactory())
|
||||
|
||||
return &ResourceGetter{
|
||||
getters: getters,
|
||||
|
||||
@@ -22,8 +22,8 @@ import (
|
||||
"github.com/google/go-cmp/cmp"
|
||||
fakeapp "github.com/kubernetes-sigs/application/pkg/client/clientset/versioned/fake"
|
||||
fakeistio "istio.io/client-go/pkg/clientset/versioned/fake"
|
||||
v1 "k8s.io/api/core/v1"
|
||||
corev1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
fakek8s "k8s.io/client-go/kubernetes/fake"
|
||||
"kubesphere.io/kubesphere/pkg/api"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/query"
|
||||
@@ -34,37 +34,7 @@ import (
|
||||
|
||||
func TestResourceGetter(t *testing.T) {
|
||||
|
||||
namespaces := make([]interface{}, 0)
|
||||
defaultNamespace := &v1.Namespace{
|
||||
ObjectMeta: corev1.ObjectMeta{
|
||||
Name: "default",
|
||||
Labels: map[string]string{"kubesphere.io/workspace": "system-workspace"},
|
||||
},
|
||||
}
|
||||
kubesphereNamespace := &v1.Namespace{
|
||||
ObjectMeta: corev1.ObjectMeta{
|
||||
Name: "kubesphere-system",
|
||||
Labels: map[string]string{"kubesphere.io/workspace": "system-workspace"},
|
||||
},
|
||||
}
|
||||
|
||||
namespaces = append(namespaces, defaultNamespace, kubesphereNamespace)
|
||||
|
||||
ksClient := fakeks.NewSimpleClientset()
|
||||
k8sClient := fakek8s.NewSimpleClientset(defaultNamespace, kubesphereNamespace)
|
||||
istioClient := fakeistio.NewSimpleClientset()
|
||||
appClient := fakeapp.NewSimpleClientset()
|
||||
fakeInformerFactory := informers.NewInformerFactories(k8sClient, ksClient, istioClient, appClient)
|
||||
|
||||
k8sInformerFactory := fakeInformerFactory.KubernetesSharedInformerFactory()
|
||||
for _, namespace := range namespaces {
|
||||
err := k8sInformerFactory.Core().V1().Namespaces().Informer().GetIndexer().Add(namespace)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
resource := New(fakeInformerFactory)
|
||||
resource := prepare()
|
||||
|
||||
tests := []struct {
|
||||
Name string
|
||||
@@ -89,8 +59,8 @@ func TestResourceGetter(t *testing.T) {
|
||||
},
|
||||
ExpectError: nil,
|
||||
ExpectResponse: &api.ListResult{
|
||||
Items: namespaces,
|
||||
TotalItems: 2,
|
||||
Items: []interface{}{foo2, foo1, bar1},
|
||||
TotalItems: 3,
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -99,7 +69,6 @@ func TestResourceGetter(t *testing.T) {
|
||||
|
||||
result, err := resource.List(test.Resource, test.Namespace, test.Query)
|
||||
|
||||
t.Logf("%+v", result)
|
||||
if err != test.ExpectError {
|
||||
t.Errorf("expected error: %s, got: %s", test.ExpectError, err)
|
||||
}
|
||||
@@ -107,5 +76,44 @@ func TestResourceGetter(t *testing.T) {
|
||||
t.Errorf(diff)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var (
|
||||
foo1 = &corev1.Namespace{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "foo1",
|
||||
Namespace: "bar",
|
||||
},
|
||||
}
|
||||
|
||||
foo2 = &corev1.Namespace{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "foo2",
|
||||
Namespace: "bar",
|
||||
},
|
||||
}
|
||||
bar1 = &corev1.Namespace{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "bar1",
|
||||
Namespace: "bar",
|
||||
},
|
||||
}
|
||||
|
||||
namespaces = []interface{}{foo1, foo2, bar1}
|
||||
)
|
||||
|
||||
func prepare() *ResourceGetter {
|
||||
|
||||
ksClient := fakeks.NewSimpleClientset()
|
||||
k8sClient := fakek8s.NewSimpleClientset()
|
||||
istioClient := fakeistio.NewSimpleClientset()
|
||||
appClient := fakeapp.NewSimpleClientset()
|
||||
fakeInformerFactory := informers.NewInformerFactories(k8sClient, ksClient, istioClient, appClient)
|
||||
|
||||
for _, namespace := range namespaces {
|
||||
fakeInformerFactory.KubernetesSharedInformerFactory().Core().V1().
|
||||
Namespaces().Informer().GetIndexer().Add(namespace)
|
||||
}
|
||||
|
||||
return NewResourceGetter(fakeInformerFactory)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user