-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreplica-count-check.yaml
82 lines (81 loc) · 1.98 KB
/
replica-count-check.yaml
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
apiVersion: kubevious.io/v1alpha1
kind: ClusterRule
metadata:
name: replica-count-check
spec:
summary: |
Checks Deployments to have min/max replicas - with or without HPAs.
categories:
- k8s
- deployment
- replica-count
values:
minReplicas: 1
maxReplicas: 1000
target: |
Union(
Api('apps')
.Kind('Deployment'),
Api('argoproj.io')
.Kind('Rollout')
)
rule: |
const hpa = findHPA();
if (hpa)
{
const minReplicas = hpa.config.spec?.minReplicas ?? 1;
if (values.minReplicas)
{
if (minReplicas < values.minReplicas)
{
error(`HPA ${hpa.name} minReplicas should be at least ${values.minReplicas}`);
}
}
if (values.maxReplicas)
{
const maxReplicas = hpa.config.spec?.maxReplicas ?? minReplicas;
if (maxReplicas > values.maxReplicas)
{
error(`HPA ${hpa.name} maxReplicas should be at most ${values.maxReplicas}`);
}
}
}
else
{
const replicas = config.spec?.replicas ?? 1;
if (values.minReplicas)
{
if (replicas < values.minReplicas)
{
error(`Replica count should be at least ${values.minReplicas}`);
}
}
if (values.maxReplicas)
{
if (replicas > values.maxReplicas)
{
error(`Replica count should be at most ${values.maxReplicas}`);
}
}
}
function findHPA()
{
for(const hpa of Api('autoscaling')
.Kind('HorizontalPodAutoscaler')
.many())
{
const scaleTargetRef = hpa.config?.spec?.scaleTargetRef;
if (scaleTargetRef)
{
if (scaleTargetRef.apiVersion === item.apiVersion &&
scaleTargetRef.kind === item.kind &&
scaleTargetRef.name === item.name)
{
return hpa;
}
}
}
return null;
}
application:
useApplicator: true