Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
2020fengziyang
kubesphere
提交
1b15e5e7
K
kubesphere
项目概览
2020fengziyang
/
kubesphere
与 Fork 源项目一致
Fork自
KubeSphere / kubesphere
通知
1
Star
1
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
1b15e5e7
编写于
11月 07, 2018
作者:
Z
zryfish
提交者:
GitHub
11月 07, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #200 from zryfish/component_status
component status
上级
c5b11300
a38bb378
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
96 addition
and
116 deletion
+96
-116
pkg/apis/v1alpha/components/components_handler.go
pkg/apis/v1alpha/components/components_handler.go
+17
-6
pkg/models/components.go
pkg/models/components.go
+79
-110
未找到文件。
pkg/apis/v1alpha/components/components_handler.go
浏览文件 @
1b15e5e7
...
...
@@ -30,22 +30,33 @@ func Register(ws *restful.WebService, subPath string) {
ws
.
Route
(
ws
.
GET
(
subPath
)
.
To
(
handleGetComponents
)
.
Filter
(
route
.
RouteLogging
))
.
Consumes
(
restful
.
MIME_JSON
,
restful
.
MIME_XML
)
.
Produces
(
restful
.
MIME_JSON
)
ws
.
Route
(
ws
.
GET
(
subPath
+
"/{namespace}/{componentName}"
)
.
To
(
handleGetComponentStatus
)
.
Filter
(
route
.
RouteLogging
))
.
Consumes
(
restful
.
MIME_JSON
,
restful
.
MIME_XML
)
.
Produces
(
restful
.
MIME_JSON
)
}
//get all components
// get a specific component status
func
handleGetComponentStatus
(
request
*
restful
.
Request
,
response
*
restful
.
Response
)
{
namespace
:=
request
.
PathParameter
(
"namespace"
)
componentName
:=
request
.
PathParameter
(
"componentName"
)
if
component
,
err
:=
models
.
GetComponentStatus
(
namespace
,
componentName
);
err
!=
nil
{
response
.
WriteHeaderAndEntity
(
http
.
StatusInternalServerError
,
constants
.
MessageResponse
{
Message
:
err
.
Error
()})
}
else
{
response
.
WriteAsJson
(
component
)
}
}
// get all components
func
handleGetComponents
(
request
*
restful
.
Request
,
response
*
restful
.
Response
)
{
result
,
err
:=
models
.
Get
Component
s
()
result
,
err
:=
models
.
Get
AllComponentsStatu
s
()
if
err
!=
nil
{
response
.
WriteHeaderAndEntity
(
http
.
StatusInternalServerError
,
constants
.
MessageResponse
{
Message
:
err
.
Error
()})
}
else
{
response
.
WriteAsJson
(
result
)
}
}
pkg/models/components.go
浏览文件 @
1b15e5e7
...
...
@@ -19,150 +19,119 @@ package models
import
(
"time"
"k8s.io/apimachinery/pkg/labels"
"github.com/golang/glog"
meta_v1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"kubesphere.io/kubesphere/pkg/client"
"kubesphere.io/kubesphere/pkg/constants"
)
type
ComponentsCount
struct
{
KubernetesCount
int
`json:"kubernetesCount"`
OpenpitrixCount
int
`json:"openpitrixCount"`
KubesphereCount
int
`json:"kubesphereCount"`
}
type
Components
struct
{
Name
string
`json:"name"`
Namespace
string
`json:"namespace"`
SelfLink
string
`json:"selfLink"`
Label
interface
{}
`json:"label"`
HealthStatus
string
`json:"healthStatus"`
CreateTime
time
.
Time
`json:"createTime"`
// Namespaces need to watch
var
SYSTEM_NAMESPACES
=
[
...
]
string
{
"kubesphere-system"
,
"openpitrix-system"
,
"kube-system"
}
type
Component
struct
{
Name
string
`json:"name"`
Namespace
string
`json:"namespace"`
SelfLink
string
`json:"selfLink"`
Label
interface
{}
`json:"label"`
StartedAt
time
.
Time
`json:"startedAt"`
TotalBackends
int
`json:"totalBackends"`
HealthyBackends
int
`json:"healthyBackends"`
}
/***
* get all components from k8s and kubesphere system
*
*/
func
GetComponents
()
(
map
[
string
]
interface
{},
error
)
{
result
:=
make
(
map
[
string
]
interface
{})
componentsList
:=
make
([]
Components
,
0
)
func
GetComponentStatus
(
namespace
string
,
componentName
string
)
(
interface
{},
error
)
{
k8sClient
:=
client
.
NewK8sClient
()
var
count
ComponentsCount
var
components
Components
label
:=
""
namespaces
:=
[]
string
{
constants
.
KubeSystemNamespace
,
constants
.
OpenPitrixNamespace
,
constants
.
KubeSphereNamespace
}
for
_
,
ns
:=
range
namespaces
{
if
ns
==
constants
.
KubeSystemNamespace
{
label
=
"kubernetes.io/cluster-service=true"
}
else
if
ns
==
constants
.
OpenPitrixNamespace
{
label
=
"app=openpitrix"
}
else
{
label
=
"app=kubesphere"
if
service
,
err
:=
k8sClient
.
CoreV1
()
.
Services
(
namespace
)
.
Get
(
componentName
,
meta_v1
.
GetOptions
{});
err
!=
nil
{
glog
.
Error
(
err
)
return
nil
,
err
}
else
{
set
:=
labels
.
Set
(
service
.
Spec
.
Selector
)
component
:=
Component
{
Name
:
service
.
Name
,
Namespace
:
service
.
Namespace
,
SelfLink
:
service
.
SelfLink
,
Label
:
service
.
Spec
.
Selector
,
StartedAt
:
service
.
CreationTimestamp
.
Time
,
HealthyBackends
:
0
,
TotalBackends
:
0
,
}
option
:=
meta_v1
.
ListOptions
{
LabelSelector
:
label
,
}
servicelists
,
err
:=
k8sClient
.
CoreV1
()
.
Services
(
ns
)
.
List
(
option
)
if
err
!=
nil
{
if
pods
,
err
:=
k8sClient
.
CoreV1
()
.
Pods
(
namespace
)
.
List
(
meta_v1
.
ListOptions
{
LabelSelector
:
set
.
AsSelector
()
.
String
()});
err
!=
nil
{
glog
.
Error
(
err
)
return
result
,
err
}
if
len
(
servicelists
.
Items
)
>
0
{
for
_
,
service
:=
range
servicelists
.
Items
{
switch
ns
{
case
constants
.
KubeSystemNamespace
:
count
.
KubernetesCount
++
case
constants
.
OpenPitrixNamespace
:
count
.
OpenpitrixCount
++
default
:
count
.
KubesphereCount
++
return
nil
,
err
}
else
{
for
_
,
v
:=
range
pods
.
Items
{
for
_
,
c
:=
range
v
.
Status
.
ContainerStatuses
{
component
.
TotalBackends
++
if
c
.
Ready
{
component
.
HealthyBackends
++
}
}
}
}
components
.
Name
=
service
.
Name
components
.
Namespace
=
service
.
Namespace
components
.
CreateTime
=
service
.
CreationTimestamp
.
Time
components
.
Label
=
service
.
Spec
.
Selector
components
.
SelfLink
=
service
.
SelfLink
label
:=
service
.
Spec
.
Selector
combination
:=
""
for
key
,
val
:=
range
label
{
labelstr
:=
key
+
"="
+
val
if
combination
==
""
{
return
component
,
nil
}
combination
=
labelstr
}
}
else
{
func
GetAllComponentsStatus
()
(
map
[
string
]
interface
{},
error
)
{
combination
=
combination
+
","
+
labelstr
status
:=
make
(
map
[
string
]
interface
{})
var
err
error
}
k8sClient
:=
client
.
NewK8sClient
()
}
option
:=
meta_v1
.
ListOptions
{
LabelSelector
:
combination
,
}
podsList
,
err
:=
k8sClient
.
CoreV1
()
.
Pods
(
ns
)
.
List
(
option
)
for
_
,
ns
:=
range
SYSTEM_NAMESPACES
{
if
err
!=
nil
{
nsStatus
:=
make
(
map
[
string
]
interface
{})
glog
.
Error
(
err
)
return
result
,
err
}
services
,
err
:=
k8sClient
.
CoreV1
()
.
Services
(
ns
)
.
List
(
meta_v1
.
ListOptions
{})
if
err
!=
nil
{
glog
.
Error
(
err
)
continue
}
if
len
(
podsList
.
Items
)
>
0
{
var
health
bool
for
_
,
pod
:=
range
podsList
.
Items
{
for
_
,
service
:=
range
services
.
Items
{
for
_
,
status
:=
range
pod
.
Status
.
ContainerStatuses
{
set
:=
labels
.
Set
(
service
.
Spec
.
Selector
)
if
status
.
Ready
==
false
{
health
=
status
.
Ready
break
}
else
{
health
=
status
.
Ready
}
if
len
(
set
)
==
0
{
continue
}
}
component
:=
Component
{
Name
:
service
.
Name
,
Namespace
:
service
.
Namespace
,
SelfLink
:
service
.
SelfLink
,
Label
:
service
.
Spec
.
Selector
,
StartedAt
:
service
.
CreationTimestamp
.
Time
,
HealthyBackends
:
0
,
TotalBackends
:
0
,
}
if
health
==
false
{
components
.
HealthStatus
=
"unhealth"
break
if
pods
,
err
:=
k8sClient
.
CoreV1
()
.
Pods
(
ns
)
.
List
(
meta_v1
.
ListOptions
{
LabelSelector
:
set
.
AsSelector
()
.
String
()});
err
!=
nil
{
glog
.
Error
(
err
)
continue
}
else
{
for
_
,
v
:=
range
pods
.
Items
{
for
_
,
c
:=
range
v
.
Status
.
ContainerStatuses
{
component
.
TotalBackends
++
if
c
.
Ready
{
component
.
HealthyBackends
++
}
}
if
health
==
true
{
components
.
HealthStatus
=
"health"
}
}
else
{
components
.
HealthStatus
=
"unhealth"
}
componentsList
=
append
(
componentsList
,
components
)
}
nsStatus
[
service
.
Name
]
=
component
}
status
[
ns
]
=
nsStatus
}
result
[
"count"
]
=
count
result
[
"item"
]
=
componentsList
return
result
,
nil
return
status
,
err
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录