Skip to content

Commit c41dec1

Browse files
committed
score: generic pod checker functions
1 parent 07e5b94 commit c41dec1

File tree

8 files changed

+506
-503
lines changed

8 files changed

+506
-503
lines changed

score/checks/checks.go

+33-130
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ package checks
33
import (
44
"strings"
55

6-
appsv1 "k8s.io/api/apps/v1"
7-
corev1 "k8s.io/api/core/v1"
8-
networkingv1 "k8s.io/api/networking/v1"
9-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10-
116
"github.com/zegl/kube-score/config"
127
ks "github.com/zegl/kube-score/domain"
138
"github.com/zegl/kube-score/scorecard"
9+
appsv1 "k8s.io/api/apps/v1"
10+
corev1 "k8s.io/api/core/v1"
11+
networkingv1 "k8s.io/api/networking/v1"
1412
)
1513

1614
func New(cnf config.Configuration) *Checks {
@@ -19,7 +17,7 @@ func New(cnf config.Configuration) *Checks {
1917

2018
all: make([]ks.Check, 0),
2119
metas: make(map[string]GenCheck[ks.BothMeta]),
22-
pods: make(map[string]PodCheck),
20+
pods: make(map[string]GenCheck[ks.PodSpecer]),
2321
services: make(map[string]GenCheck[corev1.Service]),
2422
statefulsets: make(map[string]GenCheck[appsv1.StatefulSet]),
2523
deployments: make(map[string]GenCheck[appsv1.Deployment]),
@@ -53,12 +51,6 @@ type MetaCheck struct {
5351
Fn MetaCheckFn
5452
}
5553

56-
type PodCheckFn = func(corev1.PodTemplateSpec, metav1.TypeMeta) scorecard.TestScore
57-
type PodCheck struct {
58-
ks.Check
59-
Fn PodCheckFn
60-
}
61-
6254
type CheckFunc[T any] func(T) (scorecard.TestScore, error)
6355

6456
type GenCheck[T any] struct {
@@ -69,7 +61,7 @@ type GenCheck[T any] struct {
6961
type Checks struct {
7062
all []ks.Check
7163
metas map[string]GenCheck[ks.BothMeta]
72-
pods map[string]PodCheck
64+
pods map[string]GenCheck[ks.PodSpecer]
7365
services map[string]GenCheck[corev1.Service]
7466
statefulsets map[string]GenCheck[appsv1.StatefulSet]
7567
deployments map[string]GenCheck[appsv1.Deployment]
@@ -110,203 +102,114 @@ func (c *Checks) Metas() map[string]GenCheck[ks.BothMeta] {
110102
return c.metas
111103
}
112104

113-
func (c *Checks) RegisterPodCheck(name, comment string, fn PodCheckFn) {
114-
ch := NewCheck(name, "Pod", comment, false)
115-
c.registerPodCheck(PodCheck{ch, fn})
105+
func reg[T any](c *Checks, targetType, name, comment string, optional bool, fn CheckFunc[T], mp map[string]GenCheck[T]) {
106+
ch := NewCheck(name, targetType, comment, optional)
107+
check := GenCheck[T]{Check: ch, Fn: fn}
108+
c.all = append(c.all, check.Check)
109+
if !c.isEnabled(check.Check) {
110+
return
111+
}
112+
mp[machineFriendlyName(ch.Name)] = check
116113
}
117114

118-
func (c *Checks) RegisterOptionalPodCheck(name, comment string, fn PodCheckFn) {
119-
ch := NewCheck(name, "Pod", comment, true)
120-
c.registerPodCheck(PodCheck{ch, fn})
115+
func (c *Checks) RegisterPodCheck(name, comment string, fn CheckFunc[ks.PodSpecer]) {
116+
reg(c, "Pod", name, comment, false, fn, c.pods)
121117
}
122118

123-
func (c *Checks) registerPodCheck(ch PodCheck) {
124-
c.all = append(c.all, ch.Check)
125-
126-
if !c.isEnabled(ch.Check) {
127-
return
128-
}
129-
c.pods[machineFriendlyName(ch.Name)] = ch
119+
func (c *Checks) RegisterOptionalPodCheck(name, comment string, fn CheckFunc[ks.PodSpecer]) {
120+
reg(c, "Pod", name, comment, true, fn, c.pods)
130121
}
131122

132-
func (c *Checks) Pods() map[string]PodCheck {
123+
func (c *Checks) Pods() map[string]GenCheck[ks.PodSpecer] {
133124
return c.pods
134125
}
135126

136127
func (c *Checks) RegisterHorizontalPodAutoscalerCheck(name, comment string, fn CheckFunc[ks.HpaTargeter]) {
137-
ch := NewCheck(name, "HorizontalPodAutoscaler", comment, false)
138-
c.registerHorizontalPodAutoscalerCheck(GenCheck[ks.HpaTargeter]{ch, fn})
128+
reg(c, "HorizontalPodAutoscaler", name, comment, false, fn, c.horizontalPodAutoscalers)
139129
}
140130

141131
func (c *Checks) RegisterOptionalHorizontalPodAutoscalerCheck(name, comment string, fn CheckFunc[ks.HpaTargeter]) {
142-
ch := NewCheck(name, "HorizontalPodAutoscaler", comment, true)
143-
c.registerHorizontalPodAutoscalerCheck(GenCheck[ks.HpaTargeter]{ch, fn})
144-
}
145-
146-
func (c *Checks) registerHorizontalPodAutoscalerCheck(ch GenCheck[ks.HpaTargeter]) {
147-
c.all = append(c.all, ch.Check)
148-
149-
if !c.isEnabled(ch.Check) {
150-
return
151-
}
152-
c.horizontalPodAutoscalers[machineFriendlyName(ch.Name)] = ch
132+
reg(c, "HorizontalPodAutoscaler", name, comment, true, fn, c.horizontalPodAutoscalers)
153133
}
154134

155135
func (c *Checks) HorizontalPodAutoscalers() map[string]GenCheck[ks.HpaTargeter] {
156136
return c.horizontalPodAutoscalers
157137
}
158138

159139
func (c *Checks) RegisterCronJobCheck(name, comment string, fn CheckFunc[ks.CronJob]) {
160-
ch := NewCheck(name, "CronJob", comment, false)
161-
c.registerCronJobCheck(GenCheck[ks.CronJob]{ch, fn})
140+
reg(c, "CronJob", name, comment, false, fn, c.cronjobs)
162141
}
163142

164143
func (c *Checks) RegisterOptionalCronJobCheck(name, comment string, fn CheckFunc[ks.CronJob]) {
165-
ch := NewCheck(name, "CronJob", comment, true)
166-
c.registerCronJobCheck(GenCheck[ks.CronJob]{ch, fn})
167-
}
168-
169-
func (c *Checks) registerCronJobCheck(ch GenCheck[ks.CronJob]) {
170-
c.all = append(c.all, ch.Check)
171-
172-
if !c.isEnabled(ch.Check) {
173-
return
174-
}
175-
c.cronjobs[machineFriendlyName(ch.Name)] = ch
144+
reg(c, "CronJob", name, comment, true, fn, c.cronjobs)
176145
}
177146

178147
func (c *Checks) CronJobs() map[string]GenCheck[ks.CronJob] {
179148
return c.cronjobs
180149
}
181150

182151
func (c *Checks) RegisterStatefulSetCheck(name, comment string, fn CheckFunc[appsv1.StatefulSet]) {
183-
ch := NewCheck(name, "StatefulSet", comment, false)
184-
c.registerStatefulSetCheck(GenCheck[appsv1.StatefulSet]{ch, fn})
152+
reg(c, "StatefulSet", name, comment, false, fn, c.statefulsets)
185153
}
186154

187155
func (c *Checks) RegisterOptionalStatefulSetCheck(name, comment string, fn CheckFunc[appsv1.StatefulSet]) {
188-
ch := NewCheck(name, "StatefulSet", comment, true)
189-
c.registerStatefulSetCheck(GenCheck[appsv1.StatefulSet]{ch, fn})
190-
}
191-
192-
func (c *Checks) registerStatefulSetCheck(ch GenCheck[appsv1.StatefulSet]) {
193-
c.all = append(c.all, ch.Check)
194-
195-
if !c.isEnabled(ch.Check) {
196-
return
197-
}
198-
c.statefulsets[machineFriendlyName(ch.Name)] = ch
156+
reg(c, "StatefulSet", name, comment, true, fn, c.statefulsets)
199157
}
200158

201159
func (c *Checks) StatefulSets() map[string]GenCheck[appsv1.StatefulSet] {
202160
return c.statefulsets
203161
}
204162

205163
func (c *Checks) RegisterDeploymentCheck(name, comment string, fn CheckFunc[appsv1.Deployment]) {
206-
ch := NewCheck(name, "Deployment", comment, false)
207-
c.registerDeploymentCheck(GenCheck[appsv1.Deployment]{ch, fn})
164+
reg(c, "Deployment", name, comment, false, fn, c.deployments)
208165
}
209166

210167
func (c *Checks) RegisterOptionalDeploymentCheck(name, comment string, fn CheckFunc[appsv1.Deployment]) {
211-
ch := NewCheck(name, "Deployment", comment, true)
212-
c.registerDeploymentCheck(GenCheck[appsv1.Deployment]{ch, fn})
213-
}
214-
215-
func (c *Checks) registerDeploymentCheck(ch GenCheck[appsv1.Deployment]) {
216-
c.all = append(c.all, ch.Check)
217-
218-
if !c.isEnabled(ch.Check) {
219-
return
220-
}
221-
c.deployments[machineFriendlyName(ch.Name)] = ch
168+
reg(c, "Deployment", name, comment, true, fn, c.deployments)
222169
}
223170

224171
func (c *Checks) Deployments() map[string]GenCheck[appsv1.Deployment] {
225172
return c.deployments
226173
}
227174

228175
func (c *Checks) RegisterIngressCheck(name, comment string, fn CheckFunc[ks.Ingress]) {
229-
ch := NewCheck(name, "Ingress", comment, false)
230-
c.registerIngressCheck(GenCheck[ks.Ingress]{ch, fn})
176+
reg(c, "Ingress", name, comment, false, fn, c.ingresses)
231177
}
232178

233179
func (c *Checks) RegisterOptionalIngressCheck(name, comment string, fn CheckFunc[ks.Ingress]) {
234-
ch := NewCheck(name, "Ingress", comment, true)
235-
c.registerIngressCheck(GenCheck[ks.Ingress]{ch, fn})
236-
}
237-
238-
func (c *Checks) registerIngressCheck(ch GenCheck[ks.Ingress]) {
239-
c.all = append(c.all, ch.Check)
240-
241-
if !c.isEnabled(ch.Check) {
242-
return
243-
}
244-
c.ingresses[machineFriendlyName(ch.Name)] = ch
180+
reg(c, "Ingress", name, comment, true, fn, c.ingresses)
245181
}
246182

247183
func (c *Checks) Ingresses() map[string]GenCheck[ks.Ingress] {
248184
return c.ingresses
249185
}
250186

251187
func (c *Checks) RegisterNetworkPolicyCheck(name, comment string, fn CheckFunc[networkingv1.NetworkPolicy]) {
252-
ch := NewCheck(name, "NetworkPolicy", comment, false)
253-
c.registerNetworkPolicyCheck(GenCheck[networkingv1.NetworkPolicy]{ch, fn})
188+
reg(c, "NetworkPolicy", name, comment, false, fn, c.networkpolicies)
254189
}
255190

256191
func (c *Checks) RegisterOptionalNetworkPolicyCheck(name, comment string, fn CheckFunc[networkingv1.NetworkPolicy]) {
257-
ch := NewCheck(name, "NetworkPolicy", comment, true)
258-
c.registerNetworkPolicyCheck(GenCheck[networkingv1.NetworkPolicy]{ch, fn})
259-
}
260-
261-
func (c *Checks) registerNetworkPolicyCheck(ch GenCheck[networkingv1.NetworkPolicy]) {
262-
c.all = append(c.all, ch.Check)
263-
264-
if !c.isEnabled(ch.Check) {
265-
return
266-
}
267-
c.networkpolicies[machineFriendlyName(ch.Name)] = ch
192+
reg(c, "NetworkPolicy", name, comment, true, fn, c.networkpolicies)
268193
}
269194

270195
func (c *Checks) NetworkPolicies() map[string]GenCheck[networkingv1.NetworkPolicy] {
271196
return c.networkpolicies
272197
}
273198

274199
func (c *Checks) RegisterPodDisruptionBudgetCheck(name, comment string, fn CheckFunc[ks.PodDisruptionBudget]) {
275-
ch := NewCheck(name, "PodDisruptionBudget", comment, false)
276-
c.registerPodDisruptionBudgetCheck(GenCheck[ks.PodDisruptionBudget]{ch, fn})
277-
}
278-
279-
func (c *Checks) registerPodDisruptionBudgetCheck(ch GenCheck[ks.PodDisruptionBudget]) {
280-
c.all = append(c.all, ch.Check)
281-
282-
if !c.isEnabled(ch.Check) {
283-
return
284-
}
285-
286-
c.poddisruptionbudgets[machineFriendlyName(ch.Name)] = ch
200+
reg(c, "PodDisruptionBudget", name, comment, false, fn, c.poddisruptionbudgets)
287201
}
288202

289203
func (c *Checks) PodDisruptionBudgets() map[string]GenCheck[ks.PodDisruptionBudget] {
290204
return c.poddisruptionbudgets
291205
}
292206

293207
func (c *Checks) RegisterServiceCheck(name, comment string, fn CheckFunc[corev1.Service]) {
294-
ch := NewCheck(name, "Service", comment, false)
295-
c.registerServiceCheck(GenCheck[corev1.Service]{ch, fn})
208+
reg(c, "Service", name, comment, false, fn, c.services)
296209
}
297210

298211
func (c *Checks) RegisterOptionalServiceCheck(name, comment string, fn CheckFunc[corev1.Service]) {
299-
ch := NewCheck(name, "Service", comment, true)
300-
c.registerServiceCheck(GenCheck[corev1.Service]{ch, fn})
301-
}
302-
303-
func (c *Checks) registerServiceCheck(ch GenCheck[corev1.Service]) {
304-
c.all = append(c.all, ch.Check)
305-
306-
if !c.isEnabled(ch.Check) {
307-
return
308-
}
309-
c.services[machineFriendlyName(ch.Name)] = ch
212+
reg(c, "Service", name, comment, true, fn, c.services)
310213
}
311214

312215
func (c *Checks) Services() map[string]GenCheck[corev1.Service] {

0 commit comments

Comments
 (0)