refactor component api

Signed-off-by: hongming <talonwan@yunify.com>
This commit is contained in:
hongming
2019-06-28 18:15:46 +08:00
committed by zryfish
parent e72b880b18
commit d37d67d82b
3 changed files with 41 additions and 42 deletions

View File

@@ -184,18 +184,18 @@ func addWebService(c *restful.Container) error {
To(components.GetComponents). To(components.GetComponents).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.ComponentStatusTag}). Metadata(restfulspec.KeyOpenAPITags, []string{constants.ComponentStatusTag}).
Doc("List the system components."). 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}"). webservice.Route(webservice.GET("/components/{component}").
To(components.GetComponentStatus). To(components.GetComponentStatus).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.ComponentStatusTag}). Metadata(restfulspec.KeyOpenAPITags, []string{constants.ComponentStatusTag}).
Doc("Describe the specified system component."). Doc("Describe the specified system component.").
Param(webservice.PathParameter("component", "component name")). Param(webservice.PathParameter("component", "component name")).
Returns(http.StatusOK, ok, models.Component{})) Returns(http.StatusOK, ok, models.ComponentStatus{}))
webservice.Route(webservice.GET("/componenthealth"). webservice.Route(webservice.GET("/componenthealth").
To(components.GetSystemHealthStatus). To(components.GetSystemHealthStatus).
Metadata(restfulspec.KeyOpenAPITags, []string{constants.ComponentStatusTag}). Metadata(restfulspec.KeyOpenAPITags, []string{constants.ComponentStatusTag}).
Doc("Get the health status of system components."). 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"). webservice.Route(webservice.GET("/quotas").
To(quotas.GetClusterQuotas). To(quotas.GetClusterQuotas).

View File

@@ -62,7 +62,7 @@ func GetComponentStatus(name string) (interface{}, error) {
return nil, err return nil, err
} }
component := models.Component{ component := models.ComponentStatus{
Name: service.Name, Name: service.Name,
Namespace: service.Namespace, Namespace: service.Namespace,
SelfLink: service.SelfLink, SelfLink: service.SelfLink,
@@ -71,40 +71,41 @@ func GetComponentStatus(name string) (interface{}, error) {
HealthyBackends: 0, HealthyBackends: 0,
TotalBackends: 0, TotalBackends: 0,
} }
for _, v := range pods { for _, pod := range pods {
component.TotalBackends++ component.TotalBackends++
component.HealthyBackends++ if pod.Status.Phase == corev1.PodRunning && isAllContainersReady(pod) {
for _, c := range v.Status.ContainerStatuses { component.HealthyBackends++
if !c.Ready {
component.HealthyBackends--
break
}
} }
} }
return component, nil 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
}
status := make(map[string]interface{}) func GetSystemHealthStatus() (*models.HealthStatus, error) {
status := &models.HealthStatus{}
componentStatuses, err := k8s.Client().CoreV1().ComponentStatuses().List(meta_v1.ListOptions{}) componentStatuses, err := k8s.Client().CoreV1().ComponentStatuses().List(meta_v1.ListOptions{})
if err != nil { if err != nil {
return nil, err return nil, err
} }
for _, cs := range componentStatuses.Items { status.KubernetesComponents = append(status.KubernetesComponents, componentStatuses.Items...)
status[cs.Name] = cs.Conditions[0]
}
// get kubesphere-system components // get kubesphere-system components
systemComponentStatus, err := GetAllComponentsStatus() components, err := GetAllComponentsStatus()
if err != nil { if err != nil {
glog.Errorln(err) glog.Errorln(err)
} }
for k, v := range systemComponentStatus { status.KubeSphereComponents = components
status[k] = v
}
nodeLister := informers.SharedInformerFactory().Core().V1().Nodes().Lister() nodeLister := informers.SharedInformerFactory().Core().V1().Nodes().Lister()
// get node status // get node status
@@ -114,7 +115,6 @@ func GetSystemHealthStatus() (map[string]interface{}, error) {
return status, nil return status, nil
} }
nodeStatus := make(map[string]int)
totalNodes := 0 totalNodes := 0
healthyNodes := 0 healthyNodes := 0
for _, nodes := range nodes { for _, nodes := range nodes {
@@ -125,26 +125,23 @@ func GetSystemHealthStatus() (map[string]interface{}, error) {
} }
} }
} }
nodeStatus["totalNodes"] = totalNodes nodeStatus := models.NodeStatus{TotalNodes: totalNodes, HealthyNodes: healthyNodes}
nodeStatus["healthyNodes"] = healthyNodes
status["nodes"] = nodeStatus status.NodeStatus = nodeStatus
return status, nil return status, nil
} }
func GetAllComponentsStatus() (map[string]interface{}, error) { func GetAllComponentsStatus() ([]models.ComponentStatus, error) {
serviceLister := informers.SharedInformerFactory().Core().V1().Services().Lister() serviceLister := informers.SharedInformerFactory().Core().V1().Services().Lister()
podLister := informers.SharedInformerFactory().Core().V1().Pods().Lister() podLister := informers.SharedInformerFactory().Core().V1().Pods().Lister()
status := make(map[string]interface{}) components := make([]models.ComponentStatus, 0)
var err error var err error
for _, ns := range constants.SystemNamespaces { for _, ns := range constants.SystemNamespaces {
nsStatus := make(map[string]interface{})
services, err := serviceLister.Services(ns).List(labels.Everything()) services, err := serviceLister.Services(ns).List(labels.Everything())
if err != nil { if err != nil {
@@ -159,7 +156,7 @@ func GetAllComponentsStatus() (map[string]interface{}, error) {
continue continue
} }
component := models.Component{ component := models.ComponentStatus{
Name: service.Name, Name: service.Name,
Namespace: service.Namespace, Namespace: service.Namespace,
SelfLink: service.SelfLink, SelfLink: service.SelfLink,
@@ -178,22 +175,14 @@ func GetAllComponentsStatus() (map[string]interface{}, error) {
for _, pod := range pods { for _, pod := range pods {
component.TotalBackends++ component.TotalBackends++
component.HealthyBackends++ if pod.Status.Phase == corev1.PodRunning && isAllContainersReady(pod) {
for _, c := range pod.Status.ContainerStatuses { component.HealthyBackends++
if !c.Ready {
component.HealthyBackends--
break
}
} }
} }
nsStatus[service.Name] = component components = append(components, component)
}
if len(nsStatus) > 0 {
status[ns] = nsStatus
} }
} }
return status, err return components, err
} }

View File

@@ -81,7 +81,7 @@ type Group struct {
Description string `json:"description"` Description string `json:"description"`
} }
type Component struct { type ComponentStatus struct {
Name string `json:"name" description:"component name"` Name string `json:"name" description:"component name"`
Namespace string `json:"namespace" description:"namespace"` Namespace string `json:"namespace" description:"namespace"`
SelfLink string `json:"selfLink" description:"self link"` SelfLink string `json:"selfLink" description:"self link"`
@@ -90,6 +90,16 @@ type Component struct {
TotalBackends int `json:"totalBackends" description:"total backends"` TotalBackends int `json:"totalBackends" description:"total backends"`
HealthyBackends int `json:"healthyBackends" description:"healthy 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 { type PodInfo struct {
Namespace string `json:"namespace" description:"namespace"` Namespace string `json:"namespace" description:"namespace"`