forked from kubernetes/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlist.go
166 lines (135 loc) · 5.36 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2017 The Kubernetes Authors.
//
// 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/api"
"github.com/kubernetes/dashboard/src/app/backend/errors"
metricapi "github.com/kubernetes/dashboard/src/app/backend/integration/metric/api"
"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"
"k8s.io/api/core/v1"
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sClient "k8s.io/client-go/kubernetes"
)
// PodList contains a list of Pods in the cluster.
type PodList struct {
ListMeta api.ListMeta `json:"listMeta"`
CumulativeMetrics []metricapi.Metric `json:"cumulativeMetrics"`
// Basic information about resources status on the list.
Status common.ResourceStatus `json:"status"`
// Unordered list of Pods.
Pods []Pod `json:"pods"`
// List of non-critical errors, that occurred during resource retrieval.
Errors []error `json:"errors"`
}
type PodStatus struct {
Status string `json:"status"`
PodPhase v1.PodPhase `json:"podPhase"`
ContainerStates []v1.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 api.ObjectMeta `json:"objectMeta"`
TypeMeta api.TypeMeta `json:"typeMeta"`
// More info on pod status
PodStatus PodStatus `json:"podStatus"`
// Count of containers restarts.
RestartCount int32 `json:"restartCount"`
// Pod metrics.
Metrics *PodMetrics `json:"metrics"`
// Pod warning events
Warnings []common.Event `json:"warnings"`
// Name of the Node this Pod runs on.
NodeName string `json:"nodeName"`
}
var EmptyPodList = &PodList{
Pods: make([]Pod, 0),
Errors: make([]error, 0),
ListMeta: api.ListMeta{
TotalItems: 0,
},
}
// GetPodList returns a list of all Pods in the cluster.
func GetPodList(client k8sClient.Interface, metricClient metricapi.MetricClient, 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, metricClient)
}
// GetPodListFromChannels 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,
metricClient metricapi.MetricClient) (*PodList, error) {
pods := <-channels.PodList.List
err := <-channels.PodList.Error
nonCriticalErrors, criticalError := errors.HandleError(err)
if criticalError != nil {
return nil, criticalError
}
eventList := <-channels.EventList.List
err = <-channels.EventList.Error
nonCriticalErrors, criticalError = errors.AppendError(err, nonCriticalErrors)
if criticalError != nil {
return nil, criticalError
}
podList := ToPodList(pods.Items, eventList.Items, nonCriticalErrors, dsQuery, metricClient)
podList.Status = getStatus(pods, eventList.Items)
return &podList, nil
}
func ToPodList(pods []v1.Pod, events []v1.Event, nonCriticalErrors []error, dsQuery *dataselect.DataSelectQuery,
metricClient metricapi.MetricClient) PodList {
podList := PodList{
Pods: make([]Pod, 0),
Errors: nonCriticalErrors,
}
podCells, cumulativeMetricsPromises, filteredTotal := dataselect.
GenericDataSelectWithFilterAndMetrics(toCells(pods), dsQuery, metricapi.NoResourceCache, metricClient)
pods = fromCells(podCells)
podList.ListMeta = api.ListMeta{TotalItems: filteredTotal}
metrics, err := getMetricsPerPod(pods, metricClient, dsQuery)
if err != nil {
log.Printf("Skipping Heapster metrics because of error: %s\n", err)
}
for _, pod := range pods {
warnings := event.GetPodsEventWarnings(events, []v1.Pod{pod})
podDetail := toPod(&pod, metrics, warnings)
podList.Pods = append(podList.Pods, podDetail)
}
cumulativeMetrics, err := cumulativeMetricsPromises.GetMetrics()
podList.CumulativeMetrics = cumulativeMetrics
if err != nil {
podList.CumulativeMetrics = make([]metricapi.Metric, 0)
}
return podList
}
func toPod(pod *v1.Pod, metrics *MetricsByPod, warnings []common.Event) Pod {
podDetail := Pod{
ObjectMeta: api.NewObjectMeta(pod.ObjectMeta),
TypeMeta: api.NewTypeMeta(api.ResourceKindPod),
Warnings: warnings,
PodStatus: getPodStatus(*pod, warnings),
RestartCount: getRestartCount(*pod),
NodeName: pod.Spec.NodeName,
}
if m, exists := metrics.MetricsMap[pod.UID]; exists {
podDetail.Metrics = &m
}
return podDetail
}