component status
This commit is contained in:
@@ -30,22 +30,33 @@ func Register(ws *restful.WebService, subPath string) {
|
|||||||
ws.Route(ws.GET(subPath).To(handleGetComponents).Filter(route.RouteLogging)).
|
ws.Route(ws.GET(subPath).To(handleGetComponents).Filter(route.RouteLogging)).
|
||||||
Consumes(restful.MIME_JSON, restful.MIME_XML).
|
Consumes(restful.MIME_JSON, restful.MIME_XML).
|
||||||
Produces(restful.MIME_JSON)
|
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) {
|
func handleGetComponents(request *restful.Request, response *restful.Response) {
|
||||||
|
|
||||||
result, err := models.GetComponents()
|
result, err := models.GetAllComponentsStatus()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
||||||
response.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
|
response.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
response.WriteAsJson(result)
|
response.WriteAsJson(result)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,150 +19,119 @@ package models
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"k8s.io/apimachinery/pkg/labels"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
|
||||||
"kubesphere.io/kubesphere/pkg/client"
|
"kubesphere.io/kubesphere/pkg/client"
|
||||||
"kubesphere.io/kubesphere/pkg/constants"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ComponentsCount struct {
|
// Namespaces need to watch
|
||||||
KubernetesCount int `json:"kubernetesCount"`
|
var SYSTEM_NAMESPACES = [...]string{"kubesphere-system", "openpitrix-system", "kube-system"}
|
||||||
OpenpitrixCount int `json:"openpitrixCount"`
|
|
||||||
KubesphereCount int `json:"kubesphereCount"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type Components struct {
|
type Component struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Namespace string `json:"namespace"`
|
Namespace string `json:"namespace"`
|
||||||
SelfLink string `json:"selfLink"`
|
SelfLink string `json:"selfLink"`
|
||||||
Label interface{} `json:"label"`
|
Label interface{} `json:"label"`
|
||||||
HealthStatus string `json:"healthStatus"`
|
StartedAt time.Time `json:"startedAt"`
|
||||||
CreateTime time.Time `json:"createTime"`
|
TotalBackends int `json:"totalBackends"`
|
||||||
|
HealthyBackends int `json:"healthyBackends"`
|
||||||
}
|
}
|
||||||
|
|
||||||
/***
|
func GetComponentStatus(namespace string, componentName string) (interface{}, error) {
|
||||||
* get all components from k8s and kubesphere system
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
func GetComponents() (map[string]interface{}, error) {
|
|
||||||
|
|
||||||
result := make(map[string]interface{})
|
|
||||||
componentsList := make([]Components, 0)
|
|
||||||
k8sClient := client.NewK8sClient()
|
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"
|
|
||||||
}
|
|
||||||
option := meta_v1.ListOptions{
|
|
||||||
|
|
||||||
LabelSelector: label,
|
|
||||||
}
|
|
||||||
servicelists, err := k8sClient.CoreV1().Services(ns).List(option)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
|
|
||||||
|
if service, err := k8sClient.CoreV1().Services(namespace).Get(componentName, meta_v1.GetOptions{}); err != nil {
|
||||||
glog.Error(err)
|
glog.Error(err)
|
||||||
|
return nil, 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++
|
|
||||||
}
|
|
||||||
|
|
||||||
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 == "" {
|
|
||||||
|
|
||||||
combination = labelstr
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
set := labels.Set(service.Spec.Selector)
|
||||||
|
|
||||||
combination = combination + "," + labelstr
|
component := Component{
|
||||||
|
Name: service.Name,
|
||||||
|
Namespace: service.Namespace,
|
||||||
|
SelfLink: service.SelfLink,
|
||||||
|
Label: service.Spec.Selector,
|
||||||
|
StartedAt: service.CreationTimestamp.Time,
|
||||||
|
HealthyBackends: 0,
|
||||||
|
TotalBackends: 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
if pods, err := k8sClient.CoreV1().Pods(namespace).List(meta_v1.ListOptions{LabelSelector: set.AsSelector().String()}); err != nil {
|
||||||
option := meta_v1.ListOptions{
|
|
||||||
LabelSelector: combination,
|
|
||||||
}
|
|
||||||
podsList, err := k8sClient.CoreV1().Pods(ns).List(option)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
|
|
||||||
glog.Error(err)
|
glog.Error(err)
|
||||||
return result, err
|
return nil, err
|
||||||
}
|
|
||||||
|
|
||||||
if len(podsList.Items) > 0 {
|
|
||||||
var health bool
|
|
||||||
for _, pod := range podsList.Items {
|
|
||||||
|
|
||||||
for _, status := range pod.Status.ContainerStatuses {
|
|
||||||
|
|
||||||
if status.Ready == false {
|
|
||||||
health = status.Ready
|
|
||||||
break
|
|
||||||
} else {
|
} else {
|
||||||
health = status.Ready
|
for _, v := range pods.Items {
|
||||||
|
for _, c := range v.Status.ContainerStatuses {
|
||||||
|
component.TotalBackends++
|
||||||
|
if c.Ready {
|
||||||
|
component.HealthyBackends++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return component, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if health == false {
|
}
|
||||||
components.HealthStatus = "unhealth"
|
|
||||||
break
|
func GetAllComponentsStatus() (map[string]interface{}, error) {
|
||||||
}
|
|
||||||
|
status := make(map[string]interface{})
|
||||||
}
|
var err error
|
||||||
|
|
||||||
if health == true {
|
k8sClient := client.NewK8sClient()
|
||||||
components.HealthStatus = "health"
|
|
||||||
}
|
for _, ns := range SYSTEM_NAMESPACES {
|
||||||
|
|
||||||
} else {
|
nsStatus := make(map[string]interface{})
|
||||||
components.HealthStatus = "unhealth"
|
|
||||||
}
|
services, err := k8sClient.CoreV1().Services(ns).List(meta_v1.ListOptions{})
|
||||||
|
if err != nil {
|
||||||
componentsList = append(componentsList, components)
|
glog.Error(err)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
for _, service := range services.Items {
|
||||||
|
|
||||||
}
|
set := labels.Set(service.Spec.Selector)
|
||||||
result["count"] = count
|
|
||||||
result["item"] = componentsList
|
if len(set) == 0 {
|
||||||
return result, nil
|
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 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++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nsStatus[service.Name] = component
|
||||||
|
}
|
||||||
|
|
||||||
|
status[ns] = nsStatus
|
||||||
|
}
|
||||||
|
|
||||||
|
return status, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user