forked from kubernetes/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
137 lines (108 loc) · 4.45 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// 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 pod
import (
"log"
"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/event"
"github.com/kubernetes/dashboard/src/app/backend/resource/metric"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sClient "k8s.io/client-go/kubernetes"
api "k8s.io/client-go/pkg/api/v1"
)
// ReplicationSetList contains a list of Pods in the cluster.
type PodList struct {
ListMeta common.ListMeta `json:"listMeta"`
// Unordered list of Pods.
Pods []Pod `json:"pods"`
CumulativeMetrics []metric.Metric `json:"cumulativeMetrics"`
}
type PodStatus struct {
// Status of the Pod. See Kubernetes API for reference.
Status string `json:"status"`
PodPhase api.PodPhase `json:"podPhase"`
ContainerStates []api.ContainerState `json:"containerStates"`
}
// Pod is a presentation layer view of Kubernetes Pod resource. This means
// it is Pod plus additional augmented data we can get from other sources
// (like services that target it).
type Pod struct {
ObjectMeta common.ObjectMeta `json:"objectMeta"`
TypeMeta common.TypeMeta `json:"typeMeta"`
// More info on pod status
PodStatus PodStatus `json:"podStatus"`
// Count of containers restarts.
RestartCount int32 `json:"restartCount"`
// Pod metrics.
Metrics *common.PodMetrics `json:"metrics"`
// Pod warning events
Warnings []common.Event `json:"warnings"`
}
// GetPodList returns a list of all Pods in the cluster.
func GetPodList(client k8sClient.Interface, heapsterClient client.HeapsterClient,
nsQuery *common.NamespaceQuery, dsQuery *dataselect.DataSelectQuery) (*PodList, error) {
log.Print("Getting list of all pods in the cluster")
channels := &common.ResourceChannels{
PodList: common.GetPodListChannelWithOptions(client, nsQuery, metaV1.ListOptions{}, 1),
EventList: common.GetEventListChannel(client, nsQuery, 1),
}
return GetPodListFromChannels(channels, dsQuery, heapsterClient)
}
// GetPodList returns a list of all Pods in the cluster
// reading required resource list once from the channels.
func GetPodListFromChannels(channels *common.ResourceChannels, dsQuery *dataselect.DataSelectQuery,
heapsterClient client.HeapsterClient) (*PodList, error) {
pods := <-channels.PodList.List
if err := <-channels.PodList.Error; err != nil {
return nil, err
}
eventList := <-channels.EventList.List
if err := <-channels.EventList.Error; err != nil {
return nil, err
}
podList := CreatePodList(pods.Items, eventList.Items, dsQuery, heapsterClient)
return &podList, nil
}
func CreatePodList(pods []api.Pod, events []api.Event, dsQuery *dataselect.DataSelectQuery,
heapsterClient client.HeapsterClient) PodList {
channels := &common.ResourceChannels{
PodMetrics: common.GetPodListMetricsChannel(heapsterClient, pods, 1),
}
if err := <-channels.PodMetrics.Error; err != nil {
log.Printf("Skipping Heapster metrics because of error: %s\n", err)
}
metrics := <-channels.PodMetrics.MetricsByPod
podList := PodList{
Pods: make([]Pod, 0),
}
cache := &dataselect.CachedResources{Pods: pods}
podCells, cumulativeMetricsPromises, filteredTotal := dataselect.GenericDataSelectWithFilterAndMetrics(toCells(pods), dsQuery,
cache, &heapsterClient)
pods = fromCells(podCells)
podList.ListMeta = common.ListMeta{TotalItems: filteredTotal}
for _, pod := range pods {
warnings := event.GetPodsEventWarnings(events, []api.Pod{pod})
podDetail := ToPod(&pod, metrics, warnings)
podDetail.Warnings = warnings
podList.Pods = append(podList.Pods, podDetail)
}
cumulativeMetrics, err := cumulativeMetricsPromises.GetMetrics()
podList.CumulativeMetrics = cumulativeMetrics
if err != nil {
podList.CumulativeMetrics = make([]metric.Metric, 0)
}
return podList
}