add cluster and crd to resources api group (#2053)
This commit is contained in:
61
pkg/models/resources/v1alpha3/cluster/clusters.go
Normal file
61
pkg/models/resources/v1alpha3/cluster/clusters.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"kubesphere.io/kubesphere/pkg/api"
|
||||
clusterv1alpha1 "kubesphere.io/kubesphere/pkg/apis/cluster/v1alpha1"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/query"
|
||||
"kubesphere.io/kubesphere/pkg/client/informers/externalversions"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
|
||||
)
|
||||
|
||||
type clustersGetter struct {
|
||||
informers externalversions.SharedInformerFactory
|
||||
}
|
||||
|
||||
func New(informers externalversions.SharedInformerFactory) v1alpha3.Interface {
|
||||
return &clustersGetter{
|
||||
informers: informers,
|
||||
}
|
||||
}
|
||||
|
||||
func (c clustersGetter) Get(_, name string) (runtime.Object, error) {
|
||||
return c.informers.Cluster().V1alpha1().Clusters().Lister().Get(name)
|
||||
}
|
||||
|
||||
func (c clustersGetter) List(_ string, query *query.Query) (*api.ListResult, error) {
|
||||
clusters, err := c.informers.Cluster().V1alpha1().Clusters().Lister().List(query.Selector())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []runtime.Object
|
||||
for _, deploy := range clusters {
|
||||
result = append(result, deploy)
|
||||
}
|
||||
|
||||
return v1alpha3.DefaultList(result, query, c.compare, c.filter), nil
|
||||
}
|
||||
|
||||
func (c clustersGetter) compare(left runtime.Object, right runtime.Object, field query.Field) bool {
|
||||
leftCluster, ok := left.(*clusterv1alpha1.Cluster)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
rightCluster, ok := right.(*clusterv1alpha1.Cluster)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
return v1alpha3.DefaultObjectMetaCompare(leftCluster.ObjectMeta, rightCluster.ObjectMeta, field)
|
||||
}
|
||||
|
||||
func (c clustersGetter) filter(object runtime.Object, filter query.Filter) bool {
|
||||
cluster, ok := object.(*clusterv1alpha1.Cluster)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
return v1alpha3.DefaultObjectMetaFilter(cluster.ObjectMeta, filter)
|
||||
}
|
||||
105
pkg/models/resources/v1alpha3/cluster/clusters_test.go
Normal file
105
pkg/models/resources/v1alpha3/cluster/clusters_test.go
Normal file
@@ -0,0 +1,105 @@
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"github.com/google/go-cmp/cmp"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"kubesphere.io/kubesphere/pkg/api"
|
||||
clusterv1alpha1 "kubesphere.io/kubesphere/pkg/apis/cluster/v1alpha1"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/query"
|
||||
"kubesphere.io/kubesphere/pkg/client/clientset/versioned/fake"
|
||||
"kubesphere.io/kubesphere/pkg/client/informers/externalversions"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var clusters = []*clusterv1alpha1.Cluster{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "foo",
|
||||
Labels: map[string]string{
|
||||
"cluster.kubesphere.io/region": "beijing",
|
||||
"cluster.kubesphere.io/group": "development",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "bar",
|
||||
Labels: map[string]string{
|
||||
"cluster.kubesphere.io/region": "beijing",
|
||||
"cluster.kubesphere.io/group": "production",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "whatever",
|
||||
Labels: map[string]string{
|
||||
"cluster.kubesphere.io/region": "shanghai",
|
||||
"cluster.kubesphere.io/group": "testing",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func clustersToInterface(clusters ...*clusterv1alpha1.Cluster) []interface{} {
|
||||
items := make([]interface{}, 0)
|
||||
|
||||
for _, cluster := range clusters {
|
||||
items = append(items, cluster)
|
||||
}
|
||||
|
||||
return items
|
||||
}
|
||||
|
||||
func clustersToRuntimeObject(clusters ...*clusterv1alpha1.Cluster) []runtime.Object {
|
||||
items := make([]runtime.Object, 0)
|
||||
|
||||
for _, cluster := range clusters {
|
||||
items = append(items, cluster)
|
||||
}
|
||||
|
||||
return items
|
||||
}
|
||||
|
||||
func TestClustersGetter(t *testing.T) {
|
||||
var testCases = []struct {
|
||||
description string
|
||||
query *query.Query
|
||||
expected *api.ListResult
|
||||
}{
|
||||
{
|
||||
description: "Test normal case",
|
||||
query: &query.Query{
|
||||
LabelSelector: "cluster.kubesphere.io/region=beijing",
|
||||
Ascending: false,
|
||||
},
|
||||
expected: &api.ListResult{
|
||||
TotalItems: 2,
|
||||
Items: clustersToInterface(clusters[0], clusters[1]),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
client := fake.NewSimpleClientset(clustersToRuntimeObject(clusters...)...)
|
||||
informer := externalversions.NewSharedInformerFactory(client, 0)
|
||||
|
||||
for _, cluster := range clusters {
|
||||
informer.Cluster().V1alpha1().Clusters().Informer().GetIndexer().Add(cluster)
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
|
||||
clusterGetter := New(informer)
|
||||
t.Run(testCase.description, func(t *testing.T) {
|
||||
result, err := clusterGetter.List("", testCase.query)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if diff := cmp.Diff(result, testCase.expected); len(diff) != 0 {
|
||||
t.Errorf("%T, got+ expected-, %s", testCase.expected, diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user