未验证 提交 9eba26b7 编写于 作者: Z zryfish 提交者: GitHub

fix node resources requests not returned bug (#2877)

Signed-off-by: NJeff <jeffzhang@yunify.com>
上级 2aa120f8
...@@ -27,6 +27,7 @@ import ( ...@@ -27,6 +27,7 @@ import (
"kubesphere.io/kubesphere/pkg/api" "kubesphere.io/kubesphere/pkg/api"
"kubesphere.io/kubesphere/pkg/apiserver/query" "kubesphere.io/kubesphere/pkg/apiserver/query"
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3" "kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
"sort"
) )
// Those annotations were added to node only for display purposes // Those annotations were added to node only for display purposes
...@@ -69,14 +70,44 @@ func (c nodesGetter) Get(_, name string) (runtime.Object, error) { ...@@ -69,14 +70,44 @@ func (c nodesGetter) Get(_, name string) (runtime.Object, error) {
return node, nil return node, nil
} }
func (c nodesGetter) List(_ string, query *query.Query) (*api.ListResult, error) { func (c nodesGetter) List(_ string, q *query.Query) (*api.ListResult, error) {
nodes, err := c.informers.Core().V1().Nodes().Lister().List(query.Selector()) nodes, err := c.informers.Core().V1().Nodes().Lister().List(q.Selector())
if err != nil { if err != nil {
return nil, err return nil, err
} }
var filtered []*v1.Node
for _, object := range nodes {
selected := true
for field, value := range q.Filters {
if !c.filter(object, query.Filter{Field: field, Value: value}) {
selected = false
break
}
}
if selected {
filtered = append(filtered, object)
}
}
// sort by sortBy field
sort.Slice(filtered, func(i, j int) bool {
if !q.Ascending {
return c.compare(filtered[i], filtered[j], q.SortBy)
}
return !c.compare(filtered[i], filtered[j], q.SortBy)
})
total := len(filtered)
if q.Pagination == nil {
q.Pagination = query.NoPagination
}
start, end := q.Pagination.GetValidPagination(total)
selectedNodes := filtered[start:end]
// ignore the error, skip annotating process if error happened // ignore the error, skip annotating process if error happened
pods, _ := c.informers.Core().V1().Pods().Lister().Pods("").List(query.Selector()) pods, _ := c.informers.Core().V1().Pods().Lister().Pods("").List(labels.Everything())
var nonTerminatedPodsList []*v1.Pod var nonTerminatedPodsList []*v1.Pod
for _, pod := range pods { for _, pod := range pods {
if pod.Status.Phase != v1.PodSucceeded && pod.Status.Phase != v1.PodFailed { if pod.Status.Phase != v1.PodSucceeded && pod.Status.Phase != v1.PodFailed {
...@@ -84,13 +115,16 @@ func (c nodesGetter) List(_ string, query *query.Query) (*api.ListResult, error) ...@@ -84,13 +115,16 @@ func (c nodesGetter) List(_ string, query *query.Query) (*api.ListResult, error)
} }
} }
var result []runtime.Object var result []interface{}
for _, node := range nodes { for _, node := range selectedNodes {
c.annotateNode(node, nonTerminatedPodsList) c.annotateNode(node, nonTerminatedPodsList)
result = append(result, node) result = append(result, node)
} }
return v1alpha3.DefaultList(result, query, c.compare, c.filter), nil return &api.ListResult{
TotalItems: total,
Items: result,
}, nil
} }
func (c nodesGetter) compare(left runtime.Object, right runtime.Object, field query.Field) bool { func (c nodesGetter) compare(left runtime.Object, right runtime.Object, field query.Field) bool {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册