Merge pull request #118 from littlebeer2100/master

alter components function
This commit is contained in:
alex.fan
2018-06-26 13:57:56 +08:00
committed by GitHub
2 changed files with 17 additions and 117 deletions

View File

@@ -30,11 +30,6 @@ 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}").To(handleGetComponentsByNamespace).Filter(route.RouteLogging)).
Consumes(restful.MIME_JSON, restful.MIME_XML).
Produces(restful.MIME_JSON)
}
//get all components
@@ -54,22 +49,3 @@ func handleGetComponents(request *restful.Request, response *restful.Response) {
}
}
//get components from ns
func handleGetComponentsByNamespace(request *restful.Request, response *restful.Response) {
ns := request.PathParameter("namespace")
result, err := models.GetComponentsByNamespace(ns)
if err != nil {
response.WriteHeaderAndEntity(http.StatusInternalServerError, constants.MessageResponse{Message: err.Error()})
} else {
response.WriteAsJson(result)
}
}

View File

@@ -129,21 +129,33 @@ func GetComponents() (map[string]interface{}, error) {
}
if len(podsList.Items) > 0 {
var health bool
for _, pod := range podsList.Items {
if pod.Status.Phase == "Running" {
for _, status := range pod.Status.ContainerStatuses {
components.HealthStatus = "health"
if status.Ready == false {
health = status.Ready
break
} else {
health = status.Ready
}
} else {
}
if health == false {
components.HealthStatus = "unhealth"
break
}
}
if health == true {
components.HealthStatus = "health"
}
} else {
components.HealthStatus = "unhealth"
}
componentsList = append(componentsList, components)
@@ -158,91 +170,3 @@ func GetComponents() (map[string]interface{}, error) {
return result, nil
}
func GetComponentsByNamespace(ns string) ([]Components, error) {
result := make([]Components, 0)
k8sClient := client.NewK8sClient()
var components Components
label := "kubernetes.io/cluster-service=true"
option := meta_v1.ListOptions{
LabelSelector: label,
}
if ns != KUBESYSTEM {
option.LabelSelector = ""
}
servicelists, err := k8sClient.CoreV1().Services(ns).List(option)
if err != nil {
glog.Error(err)
return result, err
}
if len(servicelists.Items) > 0 {
for _, service := range servicelists.Items {
components.Name = service.Name
components.Namespace = service.Namespace
components.CreateTime = service.CreationTimestamp.Time
components.SelfLink = service.SelfLink
components.Label = service.Spec.Selector
label := service.Spec.Selector
combination := ""
for key, val := range label {
labelstr := key + "=" + val
if combination == "" {
combination = labelstr
} else {
combination = combination + "," + labelstr
}
}
option := meta_v1.ListOptions{
LabelSelector: combination,
}
podsList, err := k8sClient.CoreV1().Pods(ns).List(option)
if err != nil {
glog.Error(err)
return result, err
}
if len(podsList.Items) > 0 {
for _, pod := range podsList.Items {
if pod.Status.Phase == "Running" {
components.HealthStatus = "health"
} else {
components.HealthStatus = "unhealth"
}
}
}
result = append(result, components)
}
}
return result, nil
}