forked from zegl/kube-score
-
Notifications
You must be signed in to change notification settings - Fork 0
/
score.go
217 lines (189 loc) · 6.64 KB
/
score.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
package score
import (
"errors"
"github.com/zegl/kube-score/config"
ks "github.com/zegl/kube-score/domain"
"github.com/zegl/kube-score/score/apps"
"github.com/zegl/kube-score/score/checks"
"github.com/zegl/kube-score/score/container"
"github.com/zegl/kube-score/score/cronjob"
"github.com/zegl/kube-score/score/deployment"
"github.com/zegl/kube-score/score/disruptionbudget"
"github.com/zegl/kube-score/score/hpa"
"github.com/zegl/kube-score/score/ingress"
"github.com/zegl/kube-score/score/meta"
"github.com/zegl/kube-score/score/networkpolicy"
"github.com/zegl/kube-score/score/podtopologyspreadconstraints"
"github.com/zegl/kube-score/score/probes"
"github.com/zegl/kube-score/score/security"
"github.com/zegl/kube-score/score/service"
"github.com/zegl/kube-score/score/stable"
"github.com/zegl/kube-score/scorecard"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func RegisterAllChecks(allObjects ks.AllTypes, checksConfig *checks.Config, runConfig *config.RunConfiguration) *checks.Checks {
allChecks := checks.New(checksConfig)
deployment.Register(allChecks, allObjects)
ingress.Register(allChecks, allObjects)
cronjob.Register(allChecks)
container.Register(allChecks, runConfig.IgnoreContainerCpuLimitRequirement, runConfig.IgnoreContainerMemoryLimitRequirement)
disruptionbudget.Register(allChecks, allObjects)
networkpolicy.Register(allChecks, allObjects, allObjects, allObjects)
probes.Register(allChecks, allObjects)
security.Register(allChecks)
service.Register(allChecks, allObjects, allObjects)
stable.Register(runConfig.KubernetesVersion, allChecks)
apps.Register(allChecks, allObjects.HorizontalPodAutoscalers(), allObjects.Services())
meta.Register(allChecks)
hpa.Register(allChecks, allObjects.Metas())
podtopologyspreadconstraints.Register(allChecks)
return allChecks
}
type podSpeccer struct {
typeMeta metav1.TypeMeta
objectMeta metav1.ObjectMeta
spec corev1.PodTemplateSpec
}
func (p *podSpeccer) GetTypeMeta() metav1.TypeMeta {
return p.typeMeta
}
func (p *podSpeccer) GetObjectMeta() metav1.ObjectMeta {
return p.objectMeta
}
func (p *podSpeccer) GetPodTemplateSpec() corev1.PodTemplateSpec {
return p.spec
}
func (p *podSpeccer) FileLocation() ks.FileLocation {
return ks.FileLocation{}
}
// Score runs a pre-configured list of tests against the files defined in the configuration, and returns a scorecard.
// Additional configuration and tuning parameters can be provided via the config.
func Score(allObjects ks.AllTypes, allChecks *checks.Checks, cnf *config.RunConfiguration) (*scorecard.Scorecard, error) {
if cnf == nil {
cnf = &config.RunConfiguration{}
}
if allChecks == nil {
return nil, errors.New("no checks registered")
}
scoreCard := scorecard.New()
newObject := func(typeMeta metav1.TypeMeta, objectMeta metav1.ObjectMeta) *scorecard.ScoredObject {
return scoreCard.NewObject(typeMeta, objectMeta, cnf)
}
for _, ingress := range allObjects.Ingresses() {
o := newObject(ingress.GetTypeMeta(), ingress.GetObjectMeta())
for _, test := range allChecks.Ingresses() {
fn, err := test.Fn(ingress)
if err != nil {
return nil, err
}
o.Add(fn, test.Check, ingress, ingress.GetObjectMeta().Annotations)
}
}
for _, meta := range allObjects.Metas() {
o := newObject(meta.TypeMeta, meta.ObjectMeta)
for _, test := range allChecks.Metas() {
fn, err := test.Fn(meta)
if err != nil {
return nil, err
}
o.Add(fn, test.Check, meta, meta.ObjectMeta.Annotations)
}
}
for _, pod := range allObjects.Pods() {
o := newObject(pod.Pod().TypeMeta, pod.Pod().ObjectMeta)
for _, test := range allChecks.Pods() {
podTemplateSpec := corev1.PodTemplateSpec{
ObjectMeta: pod.Pod().ObjectMeta,
Spec: pod.Pod().Spec,
}
score, _ := test.Fn(&podSpeccer{
typeMeta: pod.Pod().TypeMeta,
objectMeta: pod.Pod().ObjectMeta,
spec: podTemplateSpec,
})
o.Add(score, test.Check, pod, pod.Pod().ObjectMeta.Annotations)
}
}
for _, podspecer := range allObjects.PodSpeccers() {
o := newObject(podspecer.GetTypeMeta(), podspecer.GetObjectMeta())
for _, test := range allChecks.Pods() {
score, _ := test.Fn(podspecer)
o.Add(score, test.Check, podspecer,
podspecer.GetObjectMeta().Annotations,
podspecer.GetPodTemplateSpec().Annotations,
)
}
}
for _, service := range allObjects.Services() {
o := newObject(service.Service().TypeMeta, service.Service().ObjectMeta)
for _, test := range allChecks.Services() {
fn, err := test.Fn(service.Service())
if err != nil {
return nil, err
}
o.Add(fn, test.Check, service, service.Service().Annotations)
}
}
for _, statefulset := range allObjects.StatefulSets() {
o := newObject(statefulset.StatefulSet().TypeMeta, statefulset.StatefulSet().ObjectMeta)
for _, test := range allChecks.StatefulSets() {
fn, err := test.Fn(statefulset.StatefulSet())
if err != nil {
return nil, err
}
o.Add(fn, test.Check, statefulset, statefulset.StatefulSet().ObjectMeta.Annotations)
}
}
for _, deployment := range allObjects.Deployments() {
o := newObject(deployment.Deployment().TypeMeta, deployment.Deployment().ObjectMeta)
for _, test := range allChecks.Deployments() {
res, err := test.Fn(deployment.Deployment())
if err != nil {
return nil, err
}
o.Add(res, test.Check, deployment, deployment.Deployment().ObjectMeta.Annotations)
}
}
for _, netpol := range allObjects.NetworkPolicies() {
o := newObject(netpol.NetworkPolicy().TypeMeta, netpol.NetworkPolicy().ObjectMeta)
for _, test := range allChecks.NetworkPolicies() {
fn, err := test.Fn(netpol.NetworkPolicy())
if err != nil {
return nil, err
}
o.Add(fn, test.Check, netpol, netpol.NetworkPolicy().ObjectMeta.Annotations)
}
}
for _, cjob := range allObjects.CronJobs() {
o := newObject(cjob.GetTypeMeta(), cjob.GetObjectMeta())
for _, test := range allChecks.CronJobs() {
fn, err := test.Fn(cjob)
if err != nil {
return nil, err
}
o.Add(fn, test.Check, cjob, cjob.GetObjectMeta().Annotations)
}
}
for _, hpa := range allObjects.HorizontalPodAutoscalers() {
o := newObject(hpa.GetTypeMeta(), hpa.GetObjectMeta())
for _, test := range allChecks.HorizontalPodAutoscalers() {
fn, err := test.Fn(hpa)
if err != nil {
return nil, err
}
o.Add(fn, test.Check, hpa, hpa.GetObjectMeta().Annotations)
}
}
for _, pdb := range allObjects.PodDisruptionBudgets() {
o := newObject(pdb.GetTypeMeta(), pdb.GetObjectMeta())
for _, test := range allChecks.PodDisruptionBudgets() {
fn, err := test.Fn(pdb)
if err != nil {
return nil, err
}
o.Add(fn, test.Check, pdb, pdb.GetObjectMeta().Annotations)
}
}
return &scoreCard, nil
}