Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
水淹萌龙
kubesphere
提交
4f5c1378
K
kubesphere
项目概览
水淹萌龙
/
kubesphere
与 Fork 源项目一致
Fork自
KubeSphere / kubesphere
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
K
kubesphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
4f5c1378
编写于
3月 12, 2021
作者:
J
junotx
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add monitoring dashboard query api
Signed-off-by:
N
junotx
<
junotx@126.com
>
上级
b573d7ec
变更
10
展开全部
隐藏空白更改
内联
并排
Showing
10 changed file
with
879 addition
and
35 deletion
+879
-35
config/crds/monitoring.kubesphere.io_clusterdashboards.yaml
config/crds/monitoring.kubesphere.io_clusterdashboards.yaml
+184
-0
config/crds/monitoring.kubesphere.io_dashboards.yaml
config/crds/monitoring.kubesphere.io_dashboards.yaml
+184
-0
go.mod
go.mod
+4
-4
go.sum
go.sum
+51
-31
pkg/apis/addtoscheme_dashboard_v1alpha1.go
pkg/apis/addtoscheme_dashboard_v1alpha1.go
+7
-0
pkg/models/resources/v1alpha3/clusterdashboard/clusterdashboard.go
...s/resources/v1alpha3/clusterdashboard/clusterdashboard.go
+88
-0
pkg/models/resources/v1alpha3/clusterdashboard/clusterdashboard_test.go
...ources/v1alpha3/clusterdashboard/clusterdashboard_test.go
+126
-0
pkg/models/resources/v1alpha3/dashboard/dashboard.go
pkg/models/resources/v1alpha3/dashboard/dashboard.go
+88
-0
pkg/models/resources/v1alpha3/dashboard/dashboard_test.go
pkg/models/resources/v1alpha3/dashboard/dashboard_test.go
+143
-0
pkg/models/resources/v1alpha3/resource/resource.go
pkg/models/resources/v1alpha3/resource/resource.go
+4
-0
未找到文件。
config/crds/monitoring.kubesphere.io_clusterdashboards.yaml
0 → 100644
浏览文件 @
4f5c1378
---
apiVersion
:
apiextensions.k8s.io/v1beta1
kind
:
CustomResourceDefinition
metadata
:
annotations
:
controller-gen.kubebuilder.io/version
:
v0.2.4
creationTimestamp
:
null
name
:
clusterdashboards.monitoring.kubesphere.io
spec
:
group
:
monitoring.kubesphere.io
names
:
kind
:
ClusterDashboard
listKind
:
ClusterDashboardList
plural
:
clusterdashboards
singular
:
clusterdashboard
scope
:
Cluster
validation
:
openAPIV3Schema
:
description
:
ClusterDashboard is the Schema for the culsterdashboards API
properties
:
apiVersion
:
description
:
'
APIVersion
defines
the
versioned
schema
of
this
representation
of
an
object.
Servers
should
convert
recognized
schemas
to
the
latest
internal
value,
and
may
reject
unrecognized
values.
More
info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type
:
string
kind
:
description
:
'
Kind
is
a
string
value
representing
the
REST
resource
this
object
represents.
Servers
may
infer
this
from
the
endpoint
the
client
submits
requests
to.
Cannot
be
updated.
In
CamelCase.
More
info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type
:
string
metadata
:
type
:
object
spec
:
description
:
DashboardSpec defines the desired state of Dashboard
properties
:
datasource
:
description
:
Dashboard datasource
type
:
string
description
:
description
:
Dashboard description
type
:
string
panels
:
description
:
Collection of panels. Panel is one of [Row](row.md), [Singlestat](#singlestat.md)
or [Graph](graph.md)
items
:
description
:
Supported panel type
properties
:
bars
:
description
:
Display as a bar chart
type
:
boolean
colors
:
description
:
Set series color
items
:
type
:
string
type
:
array
decimals
:
description
:
Limit the decimal numbers
format
:
int64
type
:
integer
description
:
description
:
Panel description
type
:
string
format
:
description
:
Display unit
type
:
string
id
:
description
:
Panel ID
format
:
int64
type
:
integer
lines
:
description
:
Display as a line chart
type
:
boolean
stack
:
description
:
Display as a stacked chart
type
:
boolean
targets
:
allOf
:
-
items
:
description
:
Query editor options
properties
:
expr
:
description
:
Input for fetching metrics.
type
:
string
legendFormat
:
description
:
Legend format for outputs. You can make a
dynamic legend with templating variables.
type
:
string
refId
:
description
:
Reference ID
format
:
int64
type
:
integer
step
:
description
:
Set series time interval
type
:
string
type
:
object
-
items
:
description
:
Query editor options
properties
:
expr
:
description
:
Input for fetching metrics.
type
:
string
legendFormat
:
description
:
Legend format for outputs. You can make a
dynamic legend with templating variables.
type
:
string
refId
:
description
:
Reference ID
format
:
int64
type
:
integer
step
:
description
:
Set series time interval
type
:
string
type
:
object
description
:
A collection of queries
type
:
array
title
:
description
:
Name of the row panel
type
:
string
type
:
description
:
Must be `row`
type
:
string
yaxes
:
description
:
Y-axis options
items
:
properties
:
decimals
:
description
:
Limit the decimal numbers
format
:
int64
type
:
integer
format
:
description
:
Display unit
type
:
string
type
:
object
type
:
array
required
:
-
type
type
:
object
type
:
array
templating
:
description
:
Templating variables
items
:
description
:
Templating defines a variable, which can be used as a
placeholder in query
properties
:
name
:
description
:
Variable name
type
:
string
query
:
description
:
Set variable values to be the return result of the
query
type
:
string
type
:
object
type
:
array
time
:
description
:
Time range for display
properties
:
from
:
description
:
Start time in the format of `^now([+-][0-9]+[smhdwMy])?$`,
eg. `now-1M`. It denotes the end time is set to the last month
since now.
type
:
string
to
:
description
:
End time in the format of `^now([+-][0-9]+[smhdwMy])?$`,
eg. `now-1M`. It denotes the start time is set to the last month
since now.
type
:
string
type
:
object
title
:
description
:
Dashboard title
type
:
string
type
:
object
type
:
object
version
:
v1alpha1
versions
:
-
name
:
v1alpha1
served
:
true
storage
:
true
status
:
acceptedNames
:
kind
:
"
"
plural
:
"
"
conditions
:
[]
storedVersions
:
[]
config/crds/monitoring.kubesphere.io_dashboards.yaml
0 → 100644
浏览文件 @
4f5c1378
---
apiVersion
:
apiextensions.k8s.io/v1beta1
kind
:
CustomResourceDefinition
metadata
:
annotations
:
controller-gen.kubebuilder.io/version
:
v0.2.4
creationTimestamp
:
null
name
:
dashboards.monitoring.kubesphere.io
spec
:
group
:
monitoring.kubesphere.io
names
:
kind
:
Dashboard
listKind
:
DashboardList
plural
:
dashboards
singular
:
dashboard
scope
:
Namespaced
validation
:
openAPIV3Schema
:
description
:
Dashboard is the Schema for the dashboards API
properties
:
apiVersion
:
description
:
'
APIVersion
defines
the
versioned
schema
of
this
representation
of
an
object.
Servers
should
convert
recognized
schemas
to
the
latest
internal
value,
and
may
reject
unrecognized
values.
More
info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type
:
string
kind
:
description
:
'
Kind
is
a
string
value
representing
the
REST
resource
this
object
represents.
Servers
may
infer
this
from
the
endpoint
the
client
submits
requests
to.
Cannot
be
updated.
In
CamelCase.
More
info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type
:
string
metadata
:
type
:
object
spec
:
description
:
DashboardSpec defines the desired state of Dashboard
properties
:
datasource
:
description
:
Dashboard datasource
type
:
string
description
:
description
:
Dashboard description
type
:
string
panels
:
description
:
Collection of panels. Panel is one of [Row](row.md), [Singlestat](#singlestat.md)
or [Graph](graph.md)
items
:
description
:
Supported panel type
properties
:
bars
:
description
:
Display as a bar chart
type
:
boolean
colors
:
description
:
Set series color
items
:
type
:
string
type
:
array
decimals
:
description
:
Limit the decimal numbers
format
:
int64
type
:
integer
description
:
description
:
Panel description
type
:
string
format
:
description
:
Display unit
type
:
string
id
:
description
:
Panel ID
format
:
int64
type
:
integer
lines
:
description
:
Display as a line chart
type
:
boolean
stack
:
description
:
Display as a stacked chart
type
:
boolean
targets
:
allOf
:
-
items
:
description
:
Query editor options
properties
:
expr
:
description
:
Input for fetching metrics.
type
:
string
legendFormat
:
description
:
Legend format for outputs. You can make a
dynamic legend with templating variables.
type
:
string
refId
:
description
:
Reference ID
format
:
int64
type
:
integer
step
:
description
:
Set series time interval
type
:
string
type
:
object
-
items
:
description
:
Query editor options
properties
:
expr
:
description
:
Input for fetching metrics.
type
:
string
legendFormat
:
description
:
Legend format for outputs. You can make a
dynamic legend with templating variables.
type
:
string
refId
:
description
:
Reference ID
format
:
int64
type
:
integer
step
:
description
:
Set series time interval
type
:
string
type
:
object
description
:
A collection of queries
type
:
array
title
:
description
:
Name of the row panel
type
:
string
type
:
description
:
Must be `row`
type
:
string
yaxes
:
description
:
Y-axis options
items
:
properties
:
decimals
:
description
:
Limit the decimal numbers
format
:
int64
type
:
integer
format
:
description
:
Display unit
type
:
string
type
:
object
type
:
array
required
:
-
type
type
:
object
type
:
array
templating
:
description
:
Templating variables
items
:
description
:
Templating defines a variable, which can be used as a
placeholder in query
properties
:
name
:
description
:
Variable name
type
:
string
query
:
description
:
Set variable values to be the return result of the
query
type
:
string
type
:
object
type
:
array
time
:
description
:
Time range for display
properties
:
from
:
description
:
Start time in the format of `^now([+-][0-9]+[smhdwMy])?$`,
eg. `now-1M`. It denotes the end time is set to the last month
since now.
type
:
string
to
:
description
:
End time in the format of `^now([+-][0-9]+[smhdwMy])?$`,
eg. `now-1M`. It denotes the start time is set to the last month
since now.
type
:
string
type
:
object
title
:
description
:
Dashboard title
type
:
string
type
:
object
type
:
object
version
:
v1alpha1
versions
:
-
name
:
v1alpha1
served
:
true
storage
:
true
status
:
acceptedNames
:
kind
:
"
"
plural
:
"
"
conditions
:
[]
storedVersions
:
[]
go.mod
浏览文件 @
4f5c1378
...
...
@@ -17,12 +17,14 @@ require (
github.com/containernetworking/cni
v0.8.0
github.com/coreos/go-oidc
v2.1.0+incompatible
github.com/davecgh/go-spew
v1.1.1
github.com/deckarep/golang-set
v1.7.1 // indirect
github.com/dgrijalva/jwt-go
v3.2.0+incompatible
github.com/docker/distribution
v2.7.1+incompatible
github.com/docker/docker
v1.4.2-0.20200203170920-46ec8731fbce
github.com/elastic/go-elasticsearch/v5
v5.6.1
github.com/elastic/go-elasticsearch/v6
v6.8.2
github.com/elastic/go-elasticsearch/v7
v7.3.0
github.com/elazarl/goproxy
v0.0.0-20200315184450-1f3cb6622dad // indirect
github.com/emicklei/go-restful
v2.14.3+incompatible
github.com/emicklei/go-restful-openapi
v1.4.1
github.com/emirpasic/gods
v1.12.0 // indirect
...
...
@@ -39,8 +41,6 @@ require (
github.com/go-sql-driver/mysql
v1.5.0
github.com/gocraft/dbr
v0.0.0-20180507214907-a0fd650918f6
github.com/golang/example
v0.0.0-20170904185048-46695d81d1fa
github.com/golang/mock
v1.4.3
github.com/golang/protobuf
v1.4.2
github.com/google/go-cmp
v0.5.0
github.com/google/uuid
v1.1.1
github.com/gorilla/websocket
v1.4.1
...
...
@@ -67,12 +67,12 @@ require (
github.com/prometheus/prometheus
v1.8.2-0.20200907175821-8219b442c864
github.com/sony/sonyflake
v1.0.0
github.com/speps/go-hashids
v2.0.0+incompatible
github.com/spf13/afero
v1.2.2
github.com/spf13/cobra
v1.0.0
github.com/spf13/pflag
v1.0.5
github.com/spf13/viper
v1.4.0
github.com/stretchr/testify
v1.6.1
github.com/xanzy/ssh-agent
v0.2.1 // indirect
go.etcd.io/etcd
v3.3.17+incompatible // indirect
golang.org/x/crypto
v0.0.0-20200728195943-123391ffb6de
golang.org/x/net
v0.0.0-20200707034311-ab3426394381
golang.org/x/oauth2
v0.0.0-20200107190931-bf48bf16ab8d
...
...
@@ -104,7 +104,7 @@ require (
k8s.io/metrics
v0.18.6
k8s.io/utils
v0.0.0-20200603063816-c1c6865ac451
kubesphere.io/client-go
v0.0.0
openpitrix.io/openpitrix
v0.4.9-0.20200611125425-ae07f141e797
kubesphere.io/monitoring-dashboard
v0.1.1
sigs.k8s.io/application
v0.8.4-0.20201016185654-c8e2959e57a0
sigs.k8s.io/controller-runtime
v0.6.4
sigs.k8s.io/controller-tools
v0.4.0
...
...
go.sum
浏览文件 @
4f5c1378
此差异已折叠。
点击以展开。
pkg/apis/addtoscheme_dashboard_v1alpha1.go
0 → 100644
浏览文件 @
4f5c1378
package
apis
import
monitoringdashboardv1alpha1
"kubesphere.io/monitoring-dashboard/api/v1alpha1"
func
init
()
{
AddToSchemes
=
append
(
AddToSchemes
,
monitoringdashboardv1alpha1
.
SchemeBuilder
.
AddToScheme
)
}
pkg/models/resources/v1alpha3/clusterdashboard/clusterdashboard.go
0 → 100644
浏览文件 @
4f5c1378
/*
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
)
}
pkg/models/resources/v1alpha3/clusterdashboard/clusterdashboard_test.go
0 → 100644
浏览文件 @
4f5c1378
/*
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
)
}
}
pkg/models/resources/v1alpha3/dashboard/dashboard.go
0 → 100644
浏览文件 @
4f5c1378
/*
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
)
}
pkg/models/resources/v1alpha3/dashboard/dashboard_test.go
0 → 100644
浏览文件 @
4f5c1378
/*
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
)
}
}
pkg/models/resources/v1alpha3/resource/resource.go
浏览文件 @
4f5c1378
...
...
@@ -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"
...
...
@@ -130,6 +132,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
[
schema
.
GroupVersionResource
{
Group
:
"monitoring.kubesphere.io"
,
Version
:
"v1alpha1"
,
Resource
:
"clusterdashboards"
}]
=
clusterdashboard
.
New
(
cache
)
// federated resources
namespacedResourceGetters
[
typesv1beta1
.
SchemeGroupVersion
.
WithResource
(
typesv1beta1
.
ResourcePluralFederatedNamespace
)]
=
federatednamespace
.
New
(
factory
.
KubeSphereSharedInformerFactory
())
...
...
@@ -141,6 +144,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
[
schema
.
GroupVersionResource
{
Group
:
"monitoring.kubesphere.io"
,
Version
:
"v1alpha1"
,
Resource
:
"dashboards"
}]
=
dashboard
.
New
(
cache
)
return
&
ResourceGetter
{
namespacedResourceGetters
:
namespacedResourceGetters
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录