This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 481
/
job.go
274 lines (218 loc) · 7.21 KB
/
job.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
Copyright (c) 2019 the Octant contributors. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package printer
import (
"context"
"fmt"
"github.com/pkg/errors"
batchv1 "k8s.io/api/batch/v1"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"github.com/vmware-tanzu/octant/internal/conversion"
"github.com/vmware-tanzu/octant/internal/util/kubernetes"
"github.com/vmware-tanzu/octant/pkg/store"
"github.com/vmware-tanzu/octant/pkg/view/component"
)
var (
JobCols = component.NewTableCols("Name", "Labels", "Completions", "Successful", "Age")
)
// JobListHandler prints a job list.
func JobListHandler(ctx context.Context, list *batchv1.JobList, opts Options) (component.Component, error) {
if list == nil {
return nil, errors.New("job list is nil")
}
ot := NewObjectTable("Jobs", "We couldn't find any jobs!", JobCols, opts.DashConfig.ObjectStore())
ot.EnablePluginStatus(opts.DashConfig.PluginManager())
for _, job := range list.Items {
row := component.TableRow{}
nameLink, err := opts.Link.ForObject(&job, job.Name)
if err != nil {
return nil, err
}
row["Name"] = nameLink
row["Labels"] = component.NewLabels(job.Labels)
row["Completions"] = component.NewText(conversion.PtrInt32ToString(job.Spec.Completions))
succeeded := fmt.Sprintf("%d", job.Status.Succeeded)
row["Successful"] = component.NewText(succeeded)
row["Age"] = component.NewTimestamp(job.CreationTimestamp.Time)
if err := ot.AddRowForObject(ctx, &job, row); err != nil {
return nil, fmt.Errorf("add row for object: %w", err)
}
}
return ot.ToComponent()
}
var jobConditionColumns = [][]string{
{"Type", "type"},
{"Last Probe", "lastProbeTime"},
{"Last Transition", "lastTransitionTime"},
{"Status", "status"},
{"Message", "message"},
{"Reason", "reason"},
}
// JobHandler printers a job.
func JobHandler(ctx context.Context, job *batchv1.Job, options Options) (component.Component, error) {
o := NewObject(job)
o.ConditionsGen = conditionsGenFactory("", jobConditionColumns, nil)
o.EnableEvents()
jh, err := newJobHandler(job, o)
if err != nil {
return nil, err
}
if err := jh.Config(options); err != nil {
return nil, errors.Wrap(err, "print job configuration")
}
if err := jh.Status(options); err != nil {
return nil, errors.Wrap(err, "print job status")
}
if err := jh.Pods(ctx, job, options); err != nil {
return nil, errors.Wrap(err, "print job pods")
}
return o.ToComponent(ctx, options)
}
// JobConfiguration generates a job configuration
type JobConfiguration struct {
job *batchv1.Job
}
// NewJobConfiguration creates an instance of JobConfiguration
func NewJobConfiguration(job *batchv1.Job) *JobConfiguration {
return &JobConfiguration{
job: job,
}
}
// Create creates a job configuration summary
func (j *JobConfiguration) Create(option Options) (*component.Summary, error) {
if j == nil || j.job == nil {
return nil, errors.New("job is nil")
}
job := j.job
sections := component.SummarySections{}
sections.Add("Back Off Limit", component.NewText(conversion.PtrInt32ToString(job.Spec.BackoffLimit)))
sections.Add("Completions", component.NewText(conversion.PtrInt32ToString(job.Spec.Completions)))
sections.Add("Parallelism", component.NewText(conversion.PtrInt32ToString(job.Spec.Parallelism)))
summary := component.NewSummary("Configuration", sections...)
return summary, nil
}
func createJobStatus(job batchv1.Job) (*component.Summary, error) {
sections := component.SummarySections{}
if startTime := job.Status.StartTime; startTime != nil {
sections.Add("Started", component.NewTimestamp(startTime.Time))
}
if completionTime := job.Status.CompletionTime; completionTime != nil {
sections.Add("Completed", component.NewTimestamp(completionTime.Time))
}
sections.Add("Succeeded", component.NewText(fmt.Sprintf("%d", job.Status.Succeeded)))
summary := component.NewSummary("Status", sections...)
return summary, nil
}
func createJobListView(ctx context.Context, object runtime.Object, options Options) (component.Component, error) {
options.DisableLabels = true
jobList := &batchv1.JobList{}
objectStore := options.DashConfig.ObjectStore()
accessor := meta.NewAccessor()
namespace, err := accessor.Namespace(object)
if err != nil {
return nil, errors.Wrap(err, "get namespace for object")
}
apiVersion, err := accessor.APIVersion(object)
if err != nil {
return nil, errors.Wrap(err, "Get apiVersion for object")
}
kind, err := accessor.Kind(object)
if err != nil {
return nil, errors.Wrap(err, "get kind for object")
}
name, err := accessor.Name(object)
if err != nil {
return nil, errors.Wrap(err, "get name for object")
}
key := store.Key{
Namespace: namespace,
APIVersion: "batch/v1beta1",
Kind: "Job",
}
list, _, err := objectStore.List(ctx, key)
if err != nil {
return nil, errors.Wrapf(err, "list all objects for key %+v", key)
}
for i := range list.Items {
job := &batchv1.Job{}
err := kubernetes.FromUnstructured(&list.Items[i], job)
if err != nil {
return nil, err
}
for _, ownerReference := range job.OwnerReferences {
if ownerReference.APIVersion == apiVersion &&
ownerReference.Kind == kind &&
ownerReference.Name == name {
jobList.Items = append(jobList.Items, *job)
}
}
}
return JobListHandler(ctx, jobList, options)
}
type jobObject interface {
Config(options Options) error
Status(options Options) error
Pods(ctx context.Context, object runtime.Object, options Options) error
}
type jobHandler struct {
job *batchv1.Job
configFunc func(*batchv1.Job, Options) (*component.Summary, error)
statusFunc func(*batchv1.Job, Options) (*component.Summary, error)
podFunc func(context.Context, runtime.Object, Options) (component.Component, error)
object *Object
}
var _ jobObject = (*jobHandler)(nil)
func newJobHandler(job *batchv1.Job, object *Object) (*jobHandler, error) {
if job == nil {
return nil, errors.New("can't print a nil job")
}
if object == nil {
return nil, errors.New("can't print a job using a nil object printer")
}
jh := &jobHandler{
job: job,
configFunc: defaultJobConfig,
statusFunc: defaultJobStatus,
podFunc: defaultJobPods,
object: object,
}
return jh, nil
}
func (j *jobHandler) Config(options Options) error {
out, err := j.configFunc(j.job, options)
if err != nil {
return err
}
j.object.RegisterConfig(out)
return nil
}
func defaultJobConfig(job *batchv1.Job, options Options) (*component.Summary, error) {
return NewJobConfiguration(job).Create(options)
}
func (j *jobHandler) Status(options Options) error {
out, err := j.statusFunc(j.job, options)
if err != nil {
return err
}
j.object.RegisterSummary(out)
return nil
}
func defaultJobStatus(job *batchv1.Job, options Options) (*component.Summary, error) {
return createJobStatus(*job)
}
func (j *jobHandler) Pods(ctx context.Context, object runtime.Object, options Options) error {
j.object.EnablePodTemplate(j.job.Spec.Template)
j.object.RegisterItems(ItemDescriptor{
Width: component.WidthFull,
Func: func() (component.Component, error) {
return j.podFunc(ctx, object, options)
},
})
return nil
}
func defaultJobPods(ctx context.Context, object runtime.Object, options Options) (component.Component, error) {
return createPodListView(ctx, object, options)
}