-
-
Notifications
You must be signed in to change notification settings - Fork 275
/
cronjob.go
138 lines (117 loc) Β· 3.37 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
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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Popeye
package lint
import (
"context"
"github.com/derailed/popeye/internal"
"github.com/derailed/popeye/internal/cache"
"github.com/derailed/popeye/internal/client"
"github.com/derailed/popeye/internal/dao"
"github.com/derailed/popeye/internal/db"
"github.com/derailed/popeye/internal/issues"
batchv1 "k8s.io/api/batch/v1"
v1 "k8s.io/api/core/v1"
)
// CronJob tracks CronJob linting.
type CronJob struct {
*issues.Collector
db *db.DB
}
// NewCronJob returns a new instance.
func NewCronJob(co *issues.Collector, db *db.DB) *CronJob {
return &CronJob{
Collector: co,
db: db,
}
}
// Lint cleanse the resource.
func (s *CronJob) Lint(ctx context.Context) error {
over := pullOverAllocs(ctx)
txn, it := s.db.MustITFor(internal.Glossary[internal.CJOB])
defer txn.Abort()
for o := it.Next(); o != nil; o = it.Next() {
cj := o.(*batchv1.CronJob)
fqn := client.FQN(cj.Namespace, cj.Name)
s.InitOutcome(fqn)
ctx = internal.WithSpec(ctx, SpecFor(fqn, cj))
s.checkCronJob(ctx, fqn, cj)
s.checkContainers(ctx, fqn, cj.Spec.JobTemplate.Spec.Template.Spec)
s.checkUtilization(ctx, over, fqn)
}
return nil
}
// CheckCronJob checks if CronJob contract is currently happy or not.
func (s *CronJob) checkCronJob(ctx context.Context, fqn string, cj *batchv1.CronJob) {
checkEvents(ctx, s.Collector, internal.CJOB, "", "CronJob", fqn)
if cj.Spec.Suspend != nil && *cj.Spec.Suspend {
s.AddCode(ctx, 1500, cj.Kind)
}
if len(cj.Status.Active) == 0 {
s.AddCode(ctx, 1501)
}
if cj.Status.LastSuccessfulTime == nil {
s.AddCode(ctx, 1502)
}
if sa := cj.Spec.JobTemplate.Spec.Template.Spec.ServiceAccountName; sa != "" {
saFQN := client.FQN(cj.Namespace, sa)
if !s.db.Exists(internal.Glossary[internal.SA], saFQN) {
s.AddCode(ctx, 307, cj.Kind, sa)
}
}
}
// CheckContainers runs thru CronJob template and checks pod configuration.
func (s *CronJob) checkContainers(ctx context.Context, fqn string, spec v1.PodSpec) {
c := NewContainer(fqn, s)
for _, co := range spec.InitContainers {
c.sanitize(ctx, co, false)
}
for _, co := range spec.Containers {
c.sanitize(ctx, co, false)
}
}
// CheckUtilization checks CronJobs requested resources vs current utilization.
func (s *CronJob) checkUtilization(ctx context.Context, over bool, fqn string) {
jj, err := s.db.FindJobs(fqn)
if err != nil {
s.AddErr(ctx, err)
return
}
mx := jobResourceUsage(s.db, jj)
if mx.RequestCPU.IsZero() && mx.RequestMEM.IsZero() {
return
}
checkCPU(ctx, s, over, mx)
checkMEM(ctx, s, over, mx)
}
// Helpers...
func checkEvents(ctx context.Context, ii *issues.Collector, r internal.R, kind, object, fqn string) {
ee, err := dao.EventsFor(ctx, internal.Glossary[r], kind, object, fqn)
if err != nil {
ii.AddErr(ctx, err)
return
}
for _, e := range ee.Issues() {
ii.AddCode(ctx, 1503, e)
}
}
func jobResourceUsage(dba *db.DB, jobs []*batchv1.Job) ConsumptionMetrics {
var mx ConsumptionMetrics
if len(jobs) == 0 {
return mx
}
for _, job := range jobs {
fqn := cache.FQN(job.Namespace, job.Name)
cpu, mem := computePodResources(job.Spec.Template.Spec)
mx.RequestCPU.Add(cpu)
mx.RequestMEM.Add(mem)
pmx, err := dba.FindPMX(fqn)
if err != nil || pmx == nil {
continue
}
for _, cx := range pmx.Containers {
mx.CurrentCPU.Add(*cx.Usage.Cpu())
mx.CurrentMEM.Add(*cx.Usage.Memory())
}
}
return mx
}