@@ -3,14 +3,12 @@ package checks
3
3
import (
4
4
"strings"
5
5
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
-
11
6
"github.com/zegl/kube-score/config"
12
7
ks "github.com/zegl/kube-score/domain"
13
8
"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"
14
12
)
15
13
16
14
func New (cnf config.Configuration ) * Checks {
@@ -19,7 +17,7 @@ func New(cnf config.Configuration) *Checks {
19
17
20
18
all : make ([]ks.Check , 0 ),
21
19
metas : make (map [string ]GenCheck [ks.BothMeta ]),
22
- pods : make (map [string ]PodCheck ),
20
+ pods : make (map [string ]GenCheck [ks. PodSpecer ] ),
23
21
services : make (map [string ]GenCheck [corev1.Service ]),
24
22
statefulsets : make (map [string ]GenCheck [appsv1.StatefulSet ]),
25
23
deployments : make (map [string ]GenCheck [appsv1.Deployment ]),
@@ -53,12 +51,6 @@ type MetaCheck struct {
53
51
Fn MetaCheckFn
54
52
}
55
53
56
- type PodCheckFn = func (corev1.PodTemplateSpec , metav1.TypeMeta ) scorecard.TestScore
57
- type PodCheck struct {
58
- ks.Check
59
- Fn PodCheckFn
60
- }
61
-
62
54
type CheckFunc [T any ] func (T ) (scorecard.TestScore , error )
63
55
64
56
type GenCheck [T any ] struct {
@@ -69,7 +61,7 @@ type GenCheck[T any] struct {
69
61
type Checks struct {
70
62
all []ks.Check
71
63
metas map [string ]GenCheck [ks.BothMeta ]
72
- pods map [string ]PodCheck
64
+ pods map [string ]GenCheck [ks. PodSpecer ]
73
65
services map [string ]GenCheck [corev1.Service ]
74
66
statefulsets map [string ]GenCheck [appsv1.StatefulSet ]
75
67
deployments map [string ]GenCheck [appsv1.Deployment ]
@@ -110,203 +102,114 @@ func (c *Checks) Metas() map[string]GenCheck[ks.BothMeta] {
110
102
return c .metas
111
103
}
112
104
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
116
113
}
117
114
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 )
121
117
}
122
118
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 )
130
121
}
131
122
132
- func (c * Checks ) Pods () map [string ]PodCheck {
123
+ func (c * Checks ) Pods () map [string ]GenCheck [ks. PodSpecer ] {
133
124
return c .pods
134
125
}
135
126
136
127
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 )
139
129
}
140
130
141
131
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 )
153
133
}
154
134
155
135
func (c * Checks ) HorizontalPodAutoscalers () map [string ]GenCheck [ks.HpaTargeter ] {
156
136
return c .horizontalPodAutoscalers
157
137
}
158
138
159
139
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 )
162
141
}
163
142
164
143
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 )
176
145
}
177
146
178
147
func (c * Checks ) CronJobs () map [string ]GenCheck [ks.CronJob ] {
179
148
return c .cronjobs
180
149
}
181
150
182
151
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 )
185
153
}
186
154
187
155
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 )
199
157
}
200
158
201
159
func (c * Checks ) StatefulSets () map [string ]GenCheck [appsv1.StatefulSet ] {
202
160
return c .statefulsets
203
161
}
204
162
205
163
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 )
208
165
}
209
166
210
167
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 )
222
169
}
223
170
224
171
func (c * Checks ) Deployments () map [string ]GenCheck [appsv1.Deployment ] {
225
172
return c .deployments
226
173
}
227
174
228
175
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 )
231
177
}
232
178
233
179
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 )
245
181
}
246
182
247
183
func (c * Checks ) Ingresses () map [string ]GenCheck [ks.Ingress ] {
248
184
return c .ingresses
249
185
}
250
186
251
187
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 )
254
189
}
255
190
256
191
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 )
268
193
}
269
194
270
195
func (c * Checks ) NetworkPolicies () map [string ]GenCheck [networkingv1.NetworkPolicy ] {
271
196
return c .networkpolicies
272
197
}
273
198
274
199
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 )
287
201
}
288
202
289
203
func (c * Checks ) PodDisruptionBudgets () map [string ]GenCheck [ks.PodDisruptionBudget ] {
290
204
return c .poddisruptionbudgets
291
205
}
292
206
293
207
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 )
296
209
}
297
210
298
211
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 )
310
213
}
311
214
312
215
func (c * Checks ) Services () map [string ]GenCheck [corev1.Service ] {
0 commit comments