Merge pull request #3462 from junotx/cmfix
add monitoring dashboard query api
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
Copyright 2021 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 clusterdashboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/klog"
|
||||
"kubesphere.io/kubesphere/pkg/api"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/query"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
|
||||
monitoringdashboardv1alpha1 "kubesphere.io/monitoring-dashboard/api/v1alpha1"
|
||||
"sigs.k8s.io/controller-runtime/pkg/cache"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
)
|
||||
|
||||
type dashboardGetter struct {
|
||||
c cache.Cache
|
||||
}
|
||||
|
||||
func New(c cache.Cache) v1alpha3.Interface {
|
||||
return &dashboardGetter{c}
|
||||
}
|
||||
|
||||
func (d *dashboardGetter) Get(_, name string) (runtime.Object, error) {
|
||||
dashboard := monitoringdashboardv1alpha1.ClusterDashboard{}
|
||||
err := d.c.Get(context.Background(), types.NamespacedName{Name: name}, &dashboard)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
return &dashboard, nil
|
||||
}
|
||||
|
||||
func (d *dashboardGetter) List(_ string, query *query.Query) (*api.ListResult, error) {
|
||||
dashboards := monitoringdashboardv1alpha1.ClusterDashboardList{}
|
||||
err := d.c.List(context.Background(), &dashboards, &client.ListOptions{LabelSelector: query.Selector()})
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
var result []runtime.Object
|
||||
for i := range dashboards.Items {
|
||||
result = append(result, &dashboards.Items[i])
|
||||
}
|
||||
|
||||
return v1alpha3.DefaultList(result, query, d.compare, d.filter), nil
|
||||
}
|
||||
|
||||
func (d *dashboardGetter) compare(left runtime.Object, right runtime.Object, field query.Field) bool {
|
||||
|
||||
leftClusterDashboard, ok := left.(*monitoringdashboardv1alpha1.ClusterDashboard)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
rightClusterDashboard, ok := right.(*monitoringdashboardv1alpha1.ClusterDashboard)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
return v1alpha3.DefaultObjectMetaCompare(leftClusterDashboard.ObjectMeta, rightClusterDashboard.ObjectMeta, field)
|
||||
}
|
||||
|
||||
func (d *dashboardGetter) filter(object runtime.Object, filter query.Filter) bool {
|
||||
dashboard, ok := object.(*monitoringdashboardv1alpha1.ClusterDashboard)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
return v1alpha3.DefaultObjectMetaFilter(dashboard.ObjectMeta, filter)
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
Copyright 2021 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 clusterdashboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes/scheme"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/query"
|
||||
monitoringdashboardv1alpha1 "kubesphere.io/monitoring-dashboard/api/v1alpha1"
|
||||
"sigs.k8s.io/controller-runtime/pkg/cache"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/envtest"
|
||||
)
|
||||
|
||||
var c client.Client
|
||||
|
||||
func compare(actual *monitoringdashboardv1alpha1.ClusterDashboard,
|
||||
expects ...*monitoringdashboardv1alpha1.ClusterDashboard) bool {
|
||||
for _, app := range expects {
|
||||
if actual.Name == app.Name && reflect.DeepEqual(actual.Labels, app.Labels) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func TestGetListClusterDashboards(t *testing.T) {
|
||||
e := &envtest.Environment{CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "..", "..", "config", "crds")}}
|
||||
cfg, err := e.Start()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
sch := scheme.Scheme
|
||||
if err := monitoringdashboardv1alpha1.AddToScheme(sch); err != nil {
|
||||
t.Fatalf("unable add APIs to scheme: %v", err)
|
||||
}
|
||||
|
||||
stopCh := make(chan struct{})
|
||||
|
||||
ce, _ := cache.New(cfg, cache.Options{Scheme: sch})
|
||||
go ce.Start(stopCh)
|
||||
ce.WaitForCacheSync(stopCh)
|
||||
|
||||
c, _ = client.New(cfg, client.Options{Scheme: sch})
|
||||
|
||||
var labelSet1 = map[string]string{"foo-1": "bar-1"}
|
||||
var labelSet2 = map[string]string{"foo-2": "bar-2"}
|
||||
|
||||
testCases := []*monitoringdashboardv1alpha1.ClusterDashboard{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "clusterdashboard-1",
|
||||
Labels: labelSet1,
|
||||
},
|
||||
},
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "clusterdashboard-2",
|
||||
Labels: labelSet2,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
ctx := context.TODO()
|
||||
|
||||
for _, board := range testCases {
|
||||
if err = c.Create(ctx, board); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
getter := New(ce)
|
||||
|
||||
results, err := getter.List("", &query.Query{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if results.TotalItems != len(testCases) {
|
||||
t.Fatal("TotalItems is not match")
|
||||
}
|
||||
|
||||
if len(results.Items) != len(testCases) {
|
||||
t.Fatal("Items numbers is not match mock data")
|
||||
}
|
||||
|
||||
for _, dashboard := range results.Items {
|
||||
dashboard, err := dashboard.(*monitoringdashboardv1alpha1.ClusterDashboard)
|
||||
if !err {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !compare(dashboard, testCases...) {
|
||||
t.Errorf("The results %v not match testcases %v", results.Items, testCases)
|
||||
}
|
||||
}
|
||||
|
||||
result, err := getter.Get("", "clusterdashboard-1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
dashboard := result.(*monitoringdashboardv1alpha1.ClusterDashboard)
|
||||
if !compare(dashboard, testCases...) {
|
||||
t.Errorf("The results %v not match testcases %v", result, testCases)
|
||||
}
|
||||
}
|
||||
88
pkg/models/resources/v1alpha3/dashboard/dashboard.go
Normal file
88
pkg/models/resources/v1alpha3/dashboard/dashboard.go
Normal file
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
Copyright 2021 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 dashboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/klog"
|
||||
"kubesphere.io/kubesphere/pkg/api"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/query"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
|
||||
monitoringdashboardv1alpha1 "kubesphere.io/monitoring-dashboard/api/v1alpha1"
|
||||
"sigs.k8s.io/controller-runtime/pkg/cache"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
)
|
||||
|
||||
type dashboardGetter struct {
|
||||
c cache.Cache
|
||||
}
|
||||
|
||||
func New(c cache.Cache) v1alpha3.Interface {
|
||||
return &dashboardGetter{c}
|
||||
}
|
||||
|
||||
func (d *dashboardGetter) Get(namespace, name string) (runtime.Object, error) {
|
||||
dashboard := monitoringdashboardv1alpha1.Dashboard{}
|
||||
err := d.c.Get(context.Background(), types.NamespacedName{Namespace: namespace, Name: name}, &dashboard)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
return &dashboard, nil
|
||||
}
|
||||
|
||||
func (d *dashboardGetter) List(namespace string, query *query.Query) (*api.ListResult, error) {
|
||||
dashboards := monitoringdashboardv1alpha1.DashboardList{}
|
||||
err := d.c.List(context.Background(), &dashboards, &client.ListOptions{Namespace: namespace, LabelSelector: query.Selector()})
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
var result []runtime.Object
|
||||
for i := range dashboards.Items {
|
||||
result = append(result, &dashboards.Items[i])
|
||||
}
|
||||
|
||||
return v1alpha3.DefaultList(result, query, d.compare, d.filter), nil
|
||||
}
|
||||
|
||||
func (d *dashboardGetter) compare(left runtime.Object, right runtime.Object, field query.Field) bool {
|
||||
|
||||
leftDashboard, ok := left.(*monitoringdashboardv1alpha1.Dashboard)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
rightDashboard, ok := right.(*monitoringdashboardv1alpha1.Dashboard)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
return v1alpha3.DefaultObjectMetaCompare(leftDashboard.ObjectMeta, rightDashboard.ObjectMeta, field)
|
||||
}
|
||||
|
||||
func (d *dashboardGetter) filter(object runtime.Object, filter query.Filter) bool {
|
||||
dashboard, ok := object.(*monitoringdashboardv1alpha1.Dashboard)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
return v1alpha3.DefaultObjectMetaFilter(dashboard.ObjectMeta, filter)
|
||||
}
|
||||
143
pkg/models/resources/v1alpha3/dashboard/dashboard_test.go
Normal file
143
pkg/models/resources/v1alpha3/dashboard/dashboard_test.go
Normal file
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
Copyright 2021 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 dashboard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
core "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/client-go/kubernetes/scheme"
|
||||
"k8s.io/klog/v2"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/query"
|
||||
monitoringdashboardv1alpha1 "kubesphere.io/monitoring-dashboard/api/v1alpha1"
|
||||
"sigs.k8s.io/controller-runtime/pkg/cache"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/envtest"
|
||||
)
|
||||
|
||||
var c client.Client
|
||||
|
||||
func createNamespace(name string, ctx context.Context) {
|
||||
namespace := &core.Namespace{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
},
|
||||
}
|
||||
err := c.Create(ctx, namespace)
|
||||
if err != nil {
|
||||
klog.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func compare(actual *monitoringdashboardv1alpha1.Dashboard, expects ...*monitoringdashboardv1alpha1.Dashboard) bool {
|
||||
for _, app := range expects {
|
||||
if actual.Name == app.Name && actual.Namespace == app.Namespace && reflect.DeepEqual(actual.Labels, app.Labels) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func TestGetListDashboards(t *testing.T) {
|
||||
e := &envtest.Environment{CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "..", "..", "config", "crds")}}
|
||||
cfg, err := e.Start()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
sch := scheme.Scheme
|
||||
if err := monitoringdashboardv1alpha1.AddToScheme(sch); err != nil {
|
||||
t.Fatalf("unable add APIs to scheme: %v", err)
|
||||
}
|
||||
|
||||
stopCh := make(chan struct{})
|
||||
|
||||
ce, _ := cache.New(cfg, cache.Options{Scheme: sch})
|
||||
go ce.Start(stopCh)
|
||||
ce.WaitForCacheSync(stopCh)
|
||||
|
||||
c, _ = client.New(cfg, client.Options{Scheme: sch})
|
||||
|
||||
var labelSet1 = map[string]string{"foo-1": "bar-1"}
|
||||
var labelSet2 = map[string]string{"foo-2": "bar-2"}
|
||||
|
||||
var ns = "ns-1"
|
||||
testCases := []*monitoringdashboardv1alpha1.Dashboard{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "dashboard-1",
|
||||
Namespace: ns,
|
||||
Labels: labelSet1,
|
||||
},
|
||||
},
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "dashboard-2",
|
||||
Namespace: ns,
|
||||
Labels: labelSet2,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
ctx := context.TODO()
|
||||
createNamespace(ns, ctx)
|
||||
|
||||
for _, board := range testCases {
|
||||
if err = c.Create(ctx, board); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
getter := New(ce)
|
||||
|
||||
results, err := getter.List(ns, &query.Query{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if results.TotalItems != len(testCases) {
|
||||
t.Fatal("TotalItems is not match")
|
||||
}
|
||||
|
||||
if len(results.Items) != len(testCases) {
|
||||
t.Fatal("Items numbers is not match mock data")
|
||||
}
|
||||
|
||||
for _, dashboard := range results.Items {
|
||||
dashboard, err := dashboard.(*monitoringdashboardv1alpha1.Dashboard)
|
||||
if !err {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !compare(dashboard, testCases...) {
|
||||
t.Errorf("The results %v not match testcases %v", results.Items, testCases)
|
||||
}
|
||||
}
|
||||
|
||||
result, err := getter.Get(ns, "dashboard-1")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
dashboard := result.(*monitoringdashboardv1alpha1.Dashboard)
|
||||
if !compare(dashboard, testCases...) {
|
||||
t.Errorf("The results %v not match testcases %v", result, testCases)
|
||||
}
|
||||
}
|
||||
@@ -36,11 +36,13 @@ import (
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/application"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/cluster"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/clusterdashboard"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/clusterrole"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/clusterrolebinding"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/configmap"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/customresourcedefinition"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/daemonset"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/dashboard"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/deployment"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/devops"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/federatedapplication"
|
||||
@@ -78,6 +80,7 @@ import (
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/workspacerole"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/workspacerolebinding"
|
||||
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3/workspacetemplate"
|
||||
monitoringdashboardv1alpha1 "kubesphere.io/monitoring-dashboard/api/v1alpha1"
|
||||
"sigs.k8s.io/controller-runtime/pkg/cache"
|
||||
)
|
||||
|
||||
@@ -130,6 +133,7 @@ func NewResourceGetter(factory informers.InformerFactory, cache cache.Cache) *Re
|
||||
clusterResourceGetters[clusterv1alpha1.SchemeGroupVersion.WithResource(clusterv1alpha1.ResourcesPluralCluster)] = cluster.New(factory.KubeSphereSharedInformerFactory())
|
||||
clusterResourceGetters[notificationv2beta1.SchemeGroupVersion.WithResource(notificationv2beta1.ResourcesPluralConfig)] = notification.NewNotificationConfigGetter(factory.KubeSphereSharedInformerFactory())
|
||||
clusterResourceGetters[notificationv2beta1.SchemeGroupVersion.WithResource(notificationv2beta1.ResourcesPluralReceiver)] = notification.NewNotificationReceiverGetter(factory.KubeSphereSharedInformerFactory())
|
||||
clusterResourceGetters[monitoringdashboardv1alpha1.GroupVersion.WithResource("clusterdashboards")] = clusterdashboard.New(cache)
|
||||
|
||||
// federated resources
|
||||
namespacedResourceGetters[typesv1beta1.SchemeGroupVersion.WithResource(typesv1beta1.ResourcePluralFederatedNamespace)] = federatednamespace.New(factory.KubeSphereSharedInformerFactory())
|
||||
@@ -141,6 +145,7 @@ func NewResourceGetter(factory informers.InformerFactory, cache cache.Cache) *Re
|
||||
namespacedResourceGetters[typesv1beta1.SchemeGroupVersion.WithResource(typesv1beta1.ResourcePluralFederatedPersistentVolumeClaim)] = federatedpersistentvolumeclaim.New(factory.KubeSphereSharedInformerFactory())
|
||||
namespacedResourceGetters[typesv1beta1.SchemeGroupVersion.WithResource(typesv1beta1.ResourcePluralFederatedStatefulSet)] = federatedstatefulset.New(factory.KubeSphereSharedInformerFactory())
|
||||
namespacedResourceGetters[typesv1beta1.SchemeGroupVersion.WithResource(typesv1beta1.ResourcePluralFederatedIngress)] = federatedingress.New(factory.KubeSphereSharedInformerFactory())
|
||||
namespacedResourceGetters[monitoringdashboardv1alpha1.GroupVersion.WithResource("dashboards")] = dashboard.New(cache)
|
||||
|
||||
return &ResourceGetter{
|
||||
namespacedResourceGetters: namespacedResourceGetters,
|
||||
|
||||
Reference in New Issue
Block a user