提交 d37d67d8 编写于 作者: H hongming 提交者: zryfish

refactor component api

Signed-off-by: Nhongming <talonwan@yunify.com>
上级 e72b880b
......@@ -184,18 +184,18 @@ func addWebService(c *restful.Container) error {
To(components.GetComponents).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.ComponentStatusTag}).
Doc("List the system components.").
Returns(http.StatusOK, ok, map[string]models.Component{}))
Returns(http.StatusOK, ok, []models.ComponentStatus{}))
webservice.Route(webservice.GET("/components/{component}").
To(components.GetComponentStatus).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.ComponentStatusTag}).
Doc("Describe the specified system component.").
Param(webservice.PathParameter("component", "component name")).
Returns(http.StatusOK, ok, models.Component{}))
Returns(http.StatusOK, ok, models.ComponentStatus{}))
webservice.Route(webservice.GET("/componenthealth").
To(components.GetSystemHealthStatus).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.ComponentStatusTag}).
Doc("Get the health status of system components.").
Returns(http.StatusOK, ok, map[string]int{}))
Returns(http.StatusOK, ok, models.HealthStatus{}))
webservice.Route(webservice.GET("/quotas").
To(quotas.GetClusterQuotas).
......
......@@ -62,7 +62,7 @@ func GetComponentStatus(name string) (interface{}, error) {
return nil, err
}
component := models.Component{
component := models.ComponentStatus{
Name: service.Name,
Namespace: service.Namespace,
SelfLink: service.SelfLink,
......@@ -71,40 +71,41 @@ func GetComponentStatus(name string) (interface{}, error) {
HealthyBackends: 0,
TotalBackends: 0,
}
for _, v := range pods {
for _, pod := range pods {
component.TotalBackends++
component.HealthyBackends++
for _, c := range v.Status.ContainerStatuses {
if !c.Ready {
component.HealthyBackends--
break
}
if pod.Status.Phase == corev1.PodRunning && isAllContainersReady(pod) {
component.HealthyBackends++
}
}
return component, nil
}
func GetSystemHealthStatus() (map[string]interface{}, error) {
func isAllContainersReady(pod *corev1.Pod) bool {
for _, c := range pod.Status.ContainerStatuses {
if !c.Ready {
return false
}
}
return true
}
func GetSystemHealthStatus() (*models.HealthStatus, error) {
status := make(map[string]interface{})
status := &models.HealthStatus{}
componentStatuses, err := k8s.Client().CoreV1().ComponentStatuses().List(meta_v1.ListOptions{})
if err != nil {
return nil, err
}
for _, cs := range componentStatuses.Items {
status[cs.Name] = cs.Conditions[0]
}
status.KubernetesComponents = append(status.KubernetesComponents, componentStatuses.Items...)
// get kubesphere-system components
systemComponentStatus, err := GetAllComponentsStatus()
components, err := GetAllComponentsStatus()
if err != nil {
glog.Errorln(err)
}
for k, v := range systemComponentStatus {
status[k] = v
}
status.KubeSphereComponents = components
nodeLister := informers.SharedInformerFactory().Core().V1().Nodes().Lister()
// get node status
......@@ -114,7 +115,6 @@ func GetSystemHealthStatus() (map[string]interface{}, error) {
return status, nil
}
nodeStatus := make(map[string]int)
totalNodes := 0
healthyNodes := 0
for _, nodes := range nodes {
......@@ -125,26 +125,23 @@ func GetSystemHealthStatus() (map[string]interface{}, error) {
}
}
}
nodeStatus["totalNodes"] = totalNodes
nodeStatus["healthyNodes"] = healthyNodes
nodeStatus := models.NodeStatus{TotalNodes: totalNodes, HealthyNodes: healthyNodes}
status["nodes"] = nodeStatus
status.NodeStatus = nodeStatus
return status, nil
}
func GetAllComponentsStatus() (map[string]interface{}, error) {
func GetAllComponentsStatus() ([]models.ComponentStatus, error) {
serviceLister := informers.SharedInformerFactory().Core().V1().Services().Lister()
podLister := informers.SharedInformerFactory().Core().V1().Pods().Lister()
status := make(map[string]interface{})
components := make([]models.ComponentStatus, 0)
var err error
for _, ns := range constants.SystemNamespaces {
nsStatus := make(map[string]interface{})
services, err := serviceLister.Services(ns).List(labels.Everything())
if err != nil {
......@@ -159,7 +156,7 @@ func GetAllComponentsStatus() (map[string]interface{}, error) {
continue
}
component := models.Component{
component := models.ComponentStatus{
Name: service.Name,
Namespace: service.Namespace,
SelfLink: service.SelfLink,
......@@ -178,22 +175,14 @@ func GetAllComponentsStatus() (map[string]interface{}, error) {
for _, pod := range pods {
component.TotalBackends++
component.HealthyBackends++
for _, c := range pod.Status.ContainerStatuses {
if !c.Ready {
component.HealthyBackends--
break
}
if pod.Status.Phase == corev1.PodRunning && isAllContainersReady(pod) {
component.HealthyBackends++
}
}
nsStatus[service.Name] = component
}
if len(nsStatus) > 0 {
status[ns] = nsStatus
components = append(components, component)
}
}
return status, err
return components, err
}
......@@ -81,7 +81,7 @@ type Group struct {
Description string `json:"description"`
}
type Component struct {
type ComponentStatus struct {
Name string `json:"name" description:"component name"`
Namespace string `json:"namespace" description:"namespace"`
SelfLink string `json:"selfLink" description:"self link"`
......@@ -90,6 +90,16 @@ type Component struct {
TotalBackends int `json:"totalBackends" description:"total backends"`
HealthyBackends int `json:"healthyBackends" description:"healthy backends"`
}
type NodeStatus struct {
TotalNodes int `json:"totalNodes" description:"total number of nodes"`
HealthyNodes int `json:"healthyNodes" description:"number of healthy nodes"`
}
type HealthStatus struct {
KubeSphereComponents []ComponentStatus `json:"kubesphereStatus" description:"kubesphere components status"`
KubernetesComponents []corev1.ComponentStatus `json:"kubernetesStatus" description:"kubernetes components status"`
NodeStatus NodeStatus `json:"nodeStatus" description:"nodes status"`
}
type PodInfo struct {
Namespace string `json:"namespace" description:"namespace"`
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册