feat: kubesphere 4.0 (#6115)
* feat: kubesphere 4.0 Signed-off-by: ci-bot <ci-bot@kubesphere.io> * feat: kubesphere 4.0 Signed-off-by: ci-bot <ci-bot@kubesphere.io> --------- Signed-off-by: ci-bot <ci-bot@kubesphere.io> Co-authored-by: ks-ci-bot <ks-ci-bot@example.com> Co-authored-by: joyceliu <joyceliu@yunify.com>
This commit is contained in:
committed by
GitHub
parent
b5015ec7b9
commit
447a51f08b
@@ -1,72 +1,62 @@
|
||||
/*
|
||||
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.
|
||||
*/
|
||||
* Please refer to the LICENSE file in the root directory of the project.
|
||||
* https://github.com/kubesphere/kubesphere/blob/master/LICENSE
|
||||
*/
|
||||
|
||||
package cluster
|
||||
|
||||
import (
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"context"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
clusterv1alpha1 "kubesphere.io/api/cluster/v1alpha1"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/api"
|
||||
"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
|
||||
cache runtimeclient.Reader
|
||||
}
|
||||
|
||||
func New(informers externalversions.SharedInformerFactory) v1alpha3.Interface {
|
||||
func New(cache runtimeclient.Reader) v1alpha3.Interface {
|
||||
return &clustersGetter{
|
||||
informers: informers,
|
||||
cache: cache,
|
||||
}
|
||||
}
|
||||
|
||||
func (c clustersGetter) Get(_, name string) (runtime.Object, error) {
|
||||
cluster, err := c.informers.Cluster().V1alpha1().Clusters().Lister().Get(name)
|
||||
if err != nil {
|
||||
func (c *clustersGetter) Get(_, name string) (runtime.Object, error) {
|
||||
cluster := &clusterv1alpha1.Cluster{}
|
||||
if err := c.cache.Get(context.Background(), types.NamespacedName{Name: name}, cluster); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c.transform(cluster), nil
|
||||
}
|
||||
|
||||
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 {
|
||||
func (c *clustersGetter) List(_ string, query *query.Query) (*api.ListResult, error) {
|
||||
cluster := &clusterv1alpha1.ClusterList{}
|
||||
if err := c.cache.List(context.Background(), cluster, client.MatchingLabelsSelector{Selector: query.Selector()}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []runtime.Object
|
||||
for _, cluster := range clusters {
|
||||
result = append(result, cluster)
|
||||
for _, item := range cluster.Items {
|
||||
result = append(result, item.DeepCopy())
|
||||
}
|
||||
|
||||
return v1alpha3.DefaultList(result, query, c.compare, c.filter, c.transform), nil
|
||||
}
|
||||
|
||||
func (c clustersGetter) transform(obj runtime.Object) runtime.Object {
|
||||
func (c *clustersGetter) transform(obj runtime.Object) runtime.Object {
|
||||
in := obj.(*clusterv1alpha1.Cluster)
|
||||
out := in.DeepCopy()
|
||||
out.Spec.Connection.KubeConfig = nil
|
||||
return out
|
||||
}
|
||||
|
||||
func (c clustersGetter) compare(left runtime.Object, right runtime.Object, field query.Field) bool {
|
||||
func (c *clustersGetter) compare(left runtime.Object, right runtime.Object, field query.Field) bool {
|
||||
leftCluster, ok := left.(*clusterv1alpha1.Cluster)
|
||||
if !ok {
|
||||
return false
|
||||
@@ -80,7 +70,7 @@ func (c clustersGetter) compare(left runtime.Object, right runtime.Object, field
|
||||
return v1alpha3.DefaultObjectMetaCompare(leftCluster.ObjectMeta, rightCluster.ObjectMeta, field)
|
||||
}
|
||||
|
||||
func (c clustersGetter) filter(object runtime.Object, filter query.Filter) bool {
|
||||
func (c *clustersGetter) filter(object runtime.Object, filter query.Filter) bool {
|
||||
cluster, ok := object.(*clusterv1alpha1.Cluster)
|
||||
if !ok {
|
||||
return false
|
||||
|
||||
@@ -1,124 +0,0 @@
|
||||
/*
|
||||
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 cluster
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
|
||||
clusterv1alpha1 "kubesphere.io/api/cluster/v1alpha1"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/api"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/query"
|
||||
"kubesphere.io/kubesphere/pkg/client/clientset/versioned/fake"
|
||||
"kubesphere.io/kubesphere/pkg/client/informers/externalversions"
|
||||
)
|
||||
|
||||
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