forked from kubernetes/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.go
121 lines (100 loc) · 4.21 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
// 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 cronjob
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"
"k8s.io/api/batch/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
client "k8s.io/client-go/kubernetes"
)
// CronJobList contains a list of CronJobs in the cluster.
type CronJobList struct {
ListMeta api.ListMeta `json:"listMeta"`
CumulativeMetrics []metricapi.Metric `json:"cumulativeMetrics"`
Items []CronJob `json:"items"`
// Basic information about resources status on the list.
Status common.ResourceStatus `json:"status"`
// List of non-critical errors, that occurred during resource retrieval.
Errors []error `json:"errors"`
}
// CronJob is a presentation layer view of Kubernetes Cron Job resource.
type CronJob struct {
ObjectMeta api.ObjectMeta `json:"objectMeta"`
TypeMeta api.TypeMeta `json:"typeMeta"`
Schedule string `json:"schedule"`
Suspend *bool `json:"suspend"`
Active int `json:"active"`
LastSchedule *metav1.Time `json:"lastSchedule"`
}
// GetCronJobList returns a list of all CronJobs in the cluster.
func GetCronJobList(client client.Interface, nsQuery *common.NamespaceQuery,
dsQuery *dataselect.DataSelectQuery, metricClient metricapi.MetricClient) (*CronJobList, error) {
log.Print("Getting list of all cron jobs in the cluster")
channels := &common.ResourceChannels{
CronJobList: common.GetCronJobListChannel(client, nsQuery, 1),
}
return GetCronJobListFromChannels(channels, dsQuery, metricClient)
}
// GetCronJobListFromChannels returns a list of all CronJobs in the cluster reading required resource
// list once from the channels.
func GetCronJobListFromChannels(channels *common.ResourceChannels, dsQuery *dataselect.DataSelectQuery,
metricClient metricapi.MetricClient) (*CronJobList, error) {
cronJobs := <-channels.CronJobList.List
err := <-channels.CronJobList.Error
nonCriticalErrors, criticalError := errors.HandleError(err)
if criticalError != nil {
return nil, criticalError
}
cronJobList := toCronJobList(cronJobs.Items, nonCriticalErrors, dsQuery, metricClient)
cronJobList.Status = getStatus(cronJobs)
return cronJobList, nil
}
func toCronJobList(cronJobs []v1beta1.CronJob, nonCriticalErrors []error, dsQuery *dataselect.DataSelectQuery,
metricClient metricapi.MetricClient) *CronJobList {
list := &CronJobList{
Items: make([]CronJob, 0),
ListMeta: api.ListMeta{TotalItems: len(cronJobs)},
Errors: nonCriticalErrors,
}
cachedResources := &metricapi.CachedResources{}
cronJobCells, metricPromises, filteredTotal := dataselect.GenericDataSelectWithFilterAndMetrics(ToCells(cronJobs),
dsQuery, cachedResources, metricClient)
cronJobs = FromCells(cronJobCells)
list.ListMeta = api.ListMeta{TotalItems: filteredTotal}
for _, cronJob := range cronJobs {
list.Items = append(list.Items, toCronJob(&cronJob))
}
cumulativeMetrics, err := metricPromises.GetMetrics()
if err != nil {
list.CumulativeMetrics = make([]metricapi.Metric, 0)
} else {
list.CumulativeMetrics = cumulativeMetrics
}
return list
}
func toCronJob(cj *v1beta1.CronJob) CronJob {
return CronJob{
ObjectMeta: api.NewObjectMeta(cj.ObjectMeta),
TypeMeta: api.NewTypeMeta(api.ResourceKindCronJob),
Schedule: cj.Spec.Schedule,
Suspend: cj.Spec.Suspend,
Active: len(cj.Status.Active),
LastSchedule: cj.Status.LastScheduleTime,
}
}