From d37d67d82b101573144b6b0d5b23e00e23c2fd92 Mon Sep 17 00:00:00 2001 From: hongming Date: Fri, 28 Jun 2019 18:15:46 +0800 Subject: [PATCH] refactor component api Signed-off-by: hongming --- pkg/apis/resources/v1alpha2/register.go | 6 +-- pkg/models/components/components.go | 65 ++++++++++--------------- pkg/models/types.go | 12 ++++- 3 files changed, 41 insertions(+), 42 deletions(-) diff --git a/pkg/apis/resources/v1alpha2/register.go b/pkg/apis/resources/v1alpha2/register.go index a973d87c..4c320366 100644 --- a/pkg/apis/resources/v1alpha2/register.go +++ b/pkg/apis/resources/v1alpha2/register.go @@ -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). diff --git a/pkg/models/components/components.go b/pkg/models/components/components.go index e9c274a0..42f4b3f3 100644 --- a/pkg/models/components/components.go +++ b/pkg/models/components/components.go @@ -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 } diff --git a/pkg/models/types.go b/pkg/models/types.go index 378a8717..b4c0e95f 100644 --- a/pkg/models/types.go +++ b/pkg/models/types.go @@ -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"` -- GitLab