[ADD] add pod status

This commit is contained in:
zhoujunhao 2018-06-26 14:11:25 +08:00
parent 64543d4c41
commit 9a195ab946
2 changed files with 121 additions and 97 deletions

View File

@ -318,29 +318,51 @@ func Instances(w http.ResponseWriter, r *http.Request) {
pod.Name = v.Name pod.Name = v.Name
pod.Id = serviceID pod.Id = serviceID
lc := v.Spec.Containers[0].Resources.Limits.Cpu().MilliValue() ConditionsStatuss := v.Status.Conditions
cpuL += lc for _,val := range ConditionsStatuss{
lm := v.Spec.Containers[0].Resources.Limits.Memory().Value() if val.Type == "Ready"{
pod.Status = model.ConditionStatus(val.Status)
}
}
memL += lm
rc := v.Spec.Containers[0].Resources.Requests.Cpu().MilliValue() //lc := v.Spec.Containers[0].Resources.Limits.Cpu().MilliValue()
cpuR += rc lc := v.Spec.Containers
rm := v.Spec.Containers[0].Resources.Requests.Memory().Value() for _,v:=range lc{
cpuL += v.Resources.Limits.Cpu().MilliValue()
}
memR += rm //lm := v.Spec.Containers[0].Resources.Limits.Memory().Value()
pod.CPURequests = strconv.FormatFloat(float64(rc)/float64(1000), 'f', 2, 64) lm := v.Spec.Containers
for _,v:=range lm{
memL += v.Resources.Limits.Memory().Value()
}
pod.CPURequestsR = strconv.FormatFloat(float64(rc/10)/float64(capCPU), 'f', 1, 64)
pod.CPULimits = strconv.FormatFloat(float64(lc)/float64(1000), 'f', 2, 64) //rc := v.Spec.Containers[0].Resources.Requests.Cpu().MilliValue()
pod.CPULimitsR = strconv.FormatFloat(float64(lc/10)/float64(capCPU), 'f', 1, 64) rc := v.Spec.Containers
for _,v:=range rc{
cpuR+=v.Resources.Requests.Cpu().MilliValue()
}
pod.MemoryRequests = strconv.Itoa(int(rm)) //rm := v.Spec.Containers[0].Resources.Requests.Memory().Value()
pod.MemoryRequestsR = strconv.FormatFloat(float64(rm*100)/float64(capMEM), 'f', 1, 64) rm := v.Spec.Containers
for _,v :=range rm{
memR += v.Resources.Requests.Memory().Value()
}
pod.CPURequests = strconv.FormatFloat(float64(cpuR)/float64(1000), 'f', 2, 64)
pod.CPURequestsR = strconv.FormatFloat(float64(cpuR/10)/float64(capCPU), 'f', 1, 64)
pod.CPULimits = strconv.FormatFloat(float64(cpuL)/float64(1000), 'f', 2, 64)
pod.CPULimitsR = strconv.FormatFloat(float64(cpuL/10)/float64(capCPU), 'f', 1, 64)
pod.MemoryRequests = strconv.Itoa(int(memR))
pod.MemoryRequestsR = strconv.FormatFloat(float64(memR*100)/float64(capMEM), 'f', 1, 64)
pod.TenantName = v.Labels["tenant_name"] pod.TenantName = v.Labels["tenant_name"]
pod.MemoryLimits = strconv.Itoa(int(lm)) pod.MemoryLimits = strconv.Itoa(int(memL))
pod.MemoryLimitsR = strconv.FormatFloat(float64(lm*100)/float64(capMEM), 'f', 1, 64) pod.MemoryLimitsR = strconv.FormatFloat(float64(memL*100)/float64(capMEM), 'f', 1, 64)
pods = append(pods, pod) pods = append(pods, pod)
} }

View File

@ -39,53 +39,53 @@ type Resource struct {
} }
type NodePodResource struct { type NodePodResource struct {
AllocatedResources `json:"allocatedresources"` AllocatedResources `json:"allocatedresources"`
Resource `json:"allocatable"` Resource `json:"allocatable"`
} }
type InitStatus struct { type InitStatus struct {
Status int `json:"status"` Status int `json:"status"`
StatusCN string `json:"cn"` StatusCN string `json:"cn"`
HostID string `json:"uuid"` HostID string `json:"uuid"`
} }
type InstallStatus struct { type InstallStatus struct {
Status int `json:"status"` Status int `json:"status"`
StatusCN string `json:"cn"` StatusCN string `json:"cn"`
Tasks []*ExecedTask `json:"tasks"` Tasks []*ExecedTask `json:"tasks"`
} }
type AllocatedResources struct { type AllocatedResources struct {
CPURequests int64 CPURequests int64
CPULimits int64 CPULimits int64
MemoryRequests int64 MemoryRequests int64
MemoryLimits int64 MemoryLimits int64
MemoryRequestsR string MemoryRequestsR string
MemoryLimitsR string MemoryLimitsR string
CPURequestsR string CPURequestsR string
CPULimitsR string CPULimitsR string
} }
type ExecedTask struct { type ExecedTask struct {
ID string `json:"id"` ID string `json:"id"`
Seq int `json:"seq"` Seq int `json:"seq"`
Desc string `json:"desc"` Desc string `json:"desc"`
Status string `json:"status"` Status string `json:"status"`
CompleteStatus string `json:"complete_status"` CompleteStatus string `json:"complete_status"`
ErrorMsg string `json:"err_msg"` ErrorMsg string `json:"err_msg"`
Depends []string `json:"dep"` Depends []string `json:"dep"`
Next []string `json:"next"` Next []string `json:"next"`
} }
type Prome struct { type Prome struct {
Status string `json:"status"` Status string `json:"status"`
Data PromeData `json:"data"` Data PromeData `json:"data"`
} }
type PromeData struct { type PromeData struct {
ResultType string `json:"resultType"` ResultType string `json:"resultType"`
Result []*PromeResultCore `json:"result"` Result []*PromeResultCore `json:"result"`
} }
type PromeResultCore struct { type PromeResultCore struct {
Metric map[string]string `json:"metric"` Metric map[string]string `json:"metric"`
Value []interface{} `json:"value"` Value []interface{} `json:"value"`
Values []interface{} `json:"values"` Values []interface{} `json:"values"`
} }
//swagger:parameters createToken //swagger:parameters createToken
type Expr struct { type Expr struct {
Body struct { Body struct {
@ -98,7 +98,7 @@ type Expr struct {
type PrometheusInterface interface { type PrometheusInterface interface {
Query(query string) *Prome Query(query string) *Prome
QueryRange(query string,start,end,step string) *Prome QueryRange(query string, start, end, step string) *Prome
} }
type PrometheusAPI struct { type PrometheusAPI struct {
@ -106,60 +106,61 @@ type PrometheusAPI struct {
} }
//Get Get //Get Get
func (s *PrometheusAPI) Query(query string) (*Prome,*utils.APIHandleError) { func (s *PrometheusAPI) Query(query string) (*Prome, *utils.APIHandleError) {
resp, code, err := DoRequest(s.API,query,"query", "GET", nil) resp, code, err := DoRequest(s.API, query, "query", "GET", nil)
if err != nil { if err != nil {
return nil,utils.CreateAPIHandleError(400,err) return nil, utils.CreateAPIHandleError(400, err)
} }
if code==422 { if code == 422 {
return nil,utils.CreateAPIHandleError(422,fmt.Errorf("unprocessable entity,expression %s can't be executed",query)) return nil, utils.CreateAPIHandleError(422, fmt.Errorf("unprocessable entity,expression %s can't be executed", query))
} }
if code==400 { if code == 400 {
return nil,utils.CreateAPIHandleError(400,fmt.Errorf("bad request,error to request query %s",query)) return nil, utils.CreateAPIHandleError(400, fmt.Errorf("bad request,error to request query %s", query))
} }
if code==503 { if code == 503 {
return nil,utils.CreateAPIHandleError(503,fmt.Errorf("service unavailable")) return nil, utils.CreateAPIHandleError(503, fmt.Errorf("service unavailable"))
} }
var prome Prome var prome Prome
err=ffjson.Unmarshal(resp,&prome) err = ffjson.Unmarshal(resp, &prome)
if err != nil { if err != nil {
return nil,utils.CreateAPIHandleError(500,err) return nil, utils.CreateAPIHandleError(500, err)
} }
return &prome,nil return &prome, nil
} }
//Get Get //Get Get
func (s *PrometheusAPI) QueryRange(query string,start,end,step string) (*Prome,*utils.APIHandleError) { func (s *PrometheusAPI) QueryRange(query string, start, end, step string) (*Prome, *utils.APIHandleError) {
//logrus.Infof("prometheus api is %s",s.API) //logrus.Infof("prometheus api is %s",s.API)
uri:=fmt.Sprintf("%v&start=%v&end=%v&step=%v",query,start,end,step) uri := fmt.Sprintf("%v&start=%v&end=%v&step=%v", query, start, end, step)
resp, code, err := DoRequest(s.API,uri,"query_range", "GET", nil) resp, code, err := DoRequest(s.API, uri, "query_range", "GET", nil)
if err != nil { if err != nil {
return nil,utils.CreateAPIHandleError(400,err) return nil, utils.CreateAPIHandleError(400, err)
} }
if code==422 { if code == 422 {
return nil,utils.CreateAPIHandleError(422,fmt.Errorf("unprocessable entity,expression %s can't be executed",query)) return nil, utils.CreateAPIHandleError(422, fmt.Errorf("unprocessable entity,expression %s can't be executed", query))
} }
if code==400 { if code == 400 {
return nil,utils.CreateAPIHandleError(400,fmt.Errorf("bad request,error to request query %s",query)) return nil, utils.CreateAPIHandleError(400, fmt.Errorf("bad request,error to request query %s", query))
} }
if code==503 { if code == 503 {
return nil,utils.CreateAPIHandleError(503,fmt.Errorf("service unavailable")) return nil, utils.CreateAPIHandleError(503, fmt.Errorf("service unavailable"))
} }
var prome Prome var prome Prome
err=ffjson.Unmarshal(resp,&prome) err = ffjson.Unmarshal(resp, &prome)
if err != nil { if err != nil {
return nil,utils.CreateAPIHandleError(500,err) return nil, utils.CreateAPIHandleError(500, err)
} }
return &prome,nil return &prome, nil
} }
func DoRequest(baseAPI,query,queryType, method string, body []byte) ([]byte, int, error) { func DoRequest(baseAPI, query, queryType, method string, body []byte) ([]byte, int, error) {
api:=baseAPI+"/api/v1/"+queryType+"?" api := baseAPI + "/api/v1/" + queryType + "?"
query="query="+query query = "query=" + query
query=strings.Replace(query,"+","%2B",-1) query = strings.Replace(query, "+", "%2B", -1)
val,err:=url2.ParseQuery(query) val, err := url2.ParseQuery(query)
if err != nil { if err != nil {
return nil,0,err return nil, 0, err
} }
encoded:=val.Encode() encoded := val.Encode()
//logrus.Infof("uri is %s",api+encoded) //logrus.Infof("uri is %s",api+encoded)
request, err := http.NewRequest(method, api+encoded, nil) request, err := http.NewRequest(method, api+encoded, nil)
if err != nil { if err != nil {
@ -180,14 +181,14 @@ func DoRequest(baseAPI,query,queryType, method string, body []byte) ([]byte, int
//Resource 资源 //Resource 资源
type ClusterResource struct { type ClusterResource struct {
Node int `json:"node"` Node int `json:"node"`
Tenant int `json:"tenant"` Tenant int `json:"tenant"`
CapCpu int `json:"cap_cpu"` CapCpu int `json:"cap_cpu"`
CapMem int `json:"cap_mem"` CapMem int `json:"cap_mem"`
ReqCpu float32 `json:"req_cpu"` ReqCpu float32 `json:"req_cpu"`
ReqMem int `json:"req_mem"` ReqMem int `json:"req_mem"`
CapDisk uint64 `json:"cap_disk"` CapDisk uint64 `json:"cap_disk"`
ReqDisk uint64 `json:"req_disk"` ReqDisk uint64 `json:"req_disk"`
} }
type FirstConfig struct { type FirstConfig struct {
@ -402,18 +403,19 @@ type ResponseBody struct {
Body Body `json:"body,omitempty"` Body Body `json:"body,omitempty"`
} }
type Pods struct { type Pods struct {
Namespace string `json:"namespace"` Namespace string `json:"namespace"`
Id string `json:"id"` Id string `json:"id"`
Name string `json:"name"` Name string `json:"name"`
TenantName string `json:"tenant_name"` TenantName string `json:"tenant_name"`
CPURequests string `json:"cpurequest"` CPURequests string `json:"cpurequest"`
CPURequestsR string `json:"cpurequestr"` CPURequestsR string `json:"cpurequestr"`
CPULimits string `json:"cpulimits"` CPULimits string `json:"cpulimits"`
CPULimitsR string `json:"cpulimitsr"` CPULimitsR string `json:"cpulimitsr"`
MemoryRequests string `json:"memoryrequests"` MemoryRequests string `json:"memoryrequests"`
MemoryRequestsR string `json:"memoryrequestsr"` MemoryRequestsR string `json:"memoryrequestsr"`
MemoryLimits string `json:"memorylimits"` MemoryLimits string `json:"memorylimits"`
MemoryLimitsR string `json:"memorylimitsr"` MemoryLimitsR string `json:"memorylimitsr"`
Status ConditionStatus `json:"status"`
} }
//NodeDetails NodeDetails //NodeDetails NodeDetails