forked from kubernetes/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
118 lines (97 loc) · 3.96 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package node
import (
"log"
heapster "github.com/kubernetes/dashboard/src/app/backend/client"
"github.com/kubernetes/dashboard/src/app/backend/resource/common"
"github.com/kubernetes/dashboard/src/app/backend/resource/dataselect"
"github.com/kubernetes/dashboard/src/app/backend/resource/metric"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
client "k8s.io/client-go/kubernetes"
api "k8s.io/client-go/pkg/api/v1"
)
// NodeList contains a list of nodes in the cluster.
type NodeList struct {
ListMeta common.ListMeta `json:"listMeta"`
// Unordered list of Nodes.
Nodes []Node `json:"nodes"`
CumulativeMetrics []metric.Metric `json:"cumulativeMetrics"`
}
// Node is a presentation layer view of Kubernetes nodes. This means it is node plus additional
// augmented data we can get from other sources.
type Node struct {
ObjectMeta common.ObjectMeta `json:"objectMeta"`
TypeMeta common.TypeMeta `json:"typeMeta"`
// Ready Status of the node
Ready api.ConditionStatus `json:"ready"`
}
// GetNodeListFromChannels returns a list of all namespaces in the cluster.
func GetNodeListFromChannels(channels *common.ResourceChannels, dsQuery *dataselect.DataSelectQuery,
heapsterClient *heapster.HeapsterClient) (*NodeList, error) {
log.Print("Getting node list")
namespaces := <-channels.NodeList.List
if err := <-channels.NodeList.Error; err != nil {
return nil, err
}
return toNodeList(namespaces.Items, dsQuery, heapsterClient), nil
}
// GetNodeList returns a list of all Nodes in the cluster.
func GetNodeList(client client.Interface, dsQuery *dataselect.DataSelectQuery, heapsterClient *heapster.HeapsterClient) (*NodeList, error) {
log.Print("Getting list of all nodes in the cluster")
nodes, err := client.Core().Nodes().List(metaV1.ListOptions{
LabelSelector: labels.Everything().String(),
FieldSelector: fields.Everything().String(),
})
if err != nil {
return nil, err
}
return toNodeList(nodes.Items, dsQuery, heapsterClient), nil
}
func toNodeList(nodes []api.Node, dsQuery *dataselect.DataSelectQuery, heapsterClient *heapster.HeapsterClient) *NodeList {
nodeList := &NodeList{
Nodes: make([]Node, 0),
ListMeta: common.ListMeta{TotalItems: len(nodes)},
}
replicationControllerCells, metricPromises := dataselect.GenericDataSelectWithMetrics(toCells(nodes), dsQuery, dataselect.NoResourceCache, heapsterClient)
nodes = fromCells(replicationControllerCells)
for _, node := range nodes {
nodeList.Nodes = append(nodeList.Nodes, toNode(node))
}
// this may be slow because heapster does not support all in one download for nodes.
cumulativeMetrics, err := metricPromises.GetMetrics()
nodeList.CumulativeMetrics = cumulativeMetrics
if err != nil {
nodeList.CumulativeMetrics = make([]metric.Metric, 0)
}
return nodeList
}
func toNode(node api.Node) Node {
return Node{
ObjectMeta: common.NewObjectMeta(node.ObjectMeta),
TypeMeta: common.NewTypeMeta(common.ResourceKindNode),
Ready: getNodeConditionStatus(node, api.NodeReady),
}
}
// Returns the status (True, False, Unknown) of a particular NodeConditionType
func getNodeConditionStatus(node api.Node, conditionType api.NodeConditionType) api.ConditionStatus {
for _, condition := range node.Status.Conditions {
if condition.Type == conditionType {
return condition.Status
}
}
return api.ConditionUnknown
}