-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
cronjob.go
93 lines (79 loc) 路 2.29 KB
/
cronjob.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
package view
import (
"context"
"fmt"
"github.com/derailed/k9s/internal"
"github.com/derailed/k9s/internal/client"
"github.com/derailed/k9s/internal/dao"
"github.com/derailed/k9s/internal/render"
"github.com/derailed/k9s/internal/ui"
"github.com/gdamore/tcell"
"github.com/rs/zerolog/log"
batchv1beta1 "k8s.io/api/batch/v1beta1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
)
// CronJob represents a cronjob viewer.
type CronJob struct {
ResourceViewer
}
// NewCronJob returns a new viewer.
func NewCronJob(gvr client.GVR) ResourceViewer {
c := CronJob{ResourceViewer: NewBrowser(gvr)}
c.AddBindKeysFn(c.bindKeys)
c.GetTable().SetEnterFn(c.showJobs)
c.GetTable().SetColorerFn(render.CronJob{}.ColorerFunc())
return &c
}
func (c *CronJob) showJobs(app *App, model ui.Tabular, gvr, path string) {
log.Debug().Msgf("Showing Jobs %q:%q -- %q", model.GetNamespace(), gvr, path)
o, err := app.factory.Get(gvr, path, true, labels.Everything())
if err != nil {
app.Flash().Err(err)
return
}
var cj batchv1beta1.CronJob
err = runtime.DefaultUnstructuredConverter.FromUnstructured(o.(*unstructured.Unstructured).Object, &cj)
if err != nil {
app.Flash().Err(err)
return
}
v := NewJob(client.NewGVR("batch/v1/jobs"))
v.SetContextFn(jobCtx(path, string(cj.UID)))
if err := app.inject(v); err != nil {
app.Flash().Err(err)
}
}
func jobCtx(path, uid string) ContextFunc {
return func(ctx context.Context) context.Context {
ctx = context.WithValue(ctx, internal.KeyPath, path)
return context.WithValue(ctx, internal.KeyUID, uid)
}
}
func (c *CronJob) bindKeys(aa ui.KeyActions) {
aa.Add(ui.KeyActions{
tcell.KeyCtrlT: ui.NewKeyAction("Trigger", c.trigger, true),
})
}
func (c *CronJob) trigger(evt *tcell.EventKey) *tcell.EventKey {
sel := c.GetTable().GetSelectedItem()
if sel == "" {
return evt
}
res, err := dao.AccessorFor(c.App().factory, c.GVR())
if err != nil {
return nil
}
runner, ok := res.(dao.Runnable)
if !ok {
c.App().Flash().Err(fmt.Errorf("expecting a jobrunner resource for %q", c.GVR()))
return nil
}
if err := runner.Run(sel); err != nil {
c.App().Flash().Errf("Cronjob trigger failed %v", err)
return evt
}
c.App().Flash().Infof("Triggering Job %s %s", c.GVR(), sel)
return nil
}