@@ -5,11 +5,11 @@ violation[{"msg": msg}] {
55 input.review.kind.kind == " PodDisruptionBudget"
66 pdb := input.review.object
77
8- pdb.spec.maxUnavailable
8+ pdb.spec.maxUnavailable
99
1010 not_valid_pdb_max_unavailable (pdb)
1111 # TODO update messages with link to public docs
12- msg := sprintf (
12+ msg := sprintf (
1313 " PodDisruptionBudget rejected: PodDisruptionBudget <%v> has maxUnavailable of %v, only positive integers or percentages are allowed for maxUnavailable" ,
1414 [pdb.metadata.name, pdb.spec.maxUnavailable],
1515 )
@@ -20,17 +20,17 @@ violation[{"msg": msg}] {
2020 input.review.kind.kind == " PodDisruptionBudget"
2121 pdb := input.review.object
2222
23- pdb.spec.minAvailable
23+ pdb.spec.minAvailable
2424
25- objs := [controllers | controllers := data.inventory.namespace[pdb.metadata.namespace][" apps/v1" ][_]]
26- obj := objs[_][_]
25+ objs := [controllers | controllers := data.inventory.namespace[pdb.metadata.namespace][" apps/v1" ][_]]
26+ obj := objs[_][_]
2727
28- not mismatched_selector (pdb, obj)
28+ not mismatched_selector (pdb, obj)
2929
3030 not_valid_pdb_min_available (obj, pdb)
31- not replica_set_under_deployment (obj)
31+ not replica_set_under_deployment (obj)
3232
33- keys := [key | data.inventory.namespace[pdb.metadata.namespace][" apps/v1" ][key]]
33+ keys := [key | data.inventory.namespace[pdb.metadata.namespace][" apps/v1" ][key]]
3434 msg := sprintf (
3535 " PodDisruptionBudget rejected: %v <%v> has %v replica(s) but PodDisruptionBudget <%v> has minAvailable of %v, minAvailable should always be lower than replica(s), and not used when replica(s) is set to 1." ,
3636 [obj.kind, obj.metadata.name, obj.spec.replicas, pdb.metadata.name, pdb.spec.minAvailable],
@@ -39,15 +39,15 @@ violation[{"msg": msg}] {
3939
4040# Reject pod controller if connected PDBs maxUnavailable does not allow at least 1 pod disruption
4141violation[{" msg" : msg}] {
42- input.review.kind.kind == podControllerKinds[_]
42+ input.review.kind.kind == podControllerKinds[_]
4343 obj := input.review.object
44- not replica_set_under_deployment (obj)
44+ not replica_set_under_deployment (obj)
4545
4646 pdb := data.inventory.namespace[obj.metadata.namespace][" policy/v1" ].PodDisruptionBudget[_]
4747
48- pdb.spec.maxUnavailable
48+ pdb.spec.maxUnavailable
4949
50- not mismatched_selector (pdb, obj)
50+ not mismatched_selector (pdb, obj)
5151
5252 not_valid_pdb_max_unavailable (pdb)
5353 msg := sprintf (
@@ -60,13 +60,13 @@ violation[{"msg": msg}] {
6060violation[{" msg" : msg}] {
6161 input.review.kind.kind == podControllerKinds[_]
6262 obj := input.review.object
63- not replica_set_under_deployment (obj)
63+ not replica_set_under_deployment (obj)
6464
6565 pdb := data.inventory.namespace[obj.metadata.namespace][" policy/v1" ].PodDisruptionBudget[_]
6666
67- pdb.spec.minAvailable
67+ pdb.spec.minAvailable
6868
69- not mismatched_selector (pdb, obj)
69+ not mismatched_selector (pdb, obj)
7070
7171 not_valid_pdb_min_available (obj, pdb)
7272 msg := sprintf (
@@ -77,32 +77,32 @@ violation[{"msg": msg}] {
7777
7878# The type of pod controller to validate
7979podControllerKinds := [
80- " Deployment" ,
81- " StatefulSet" ,
82- " ReplicaSet" ,
83- " ReplicationController"
80+ " Deployment" ,
81+ " StatefulSet" ,
82+ " ReplicaSet" ,
83+ " ReplicationController"
8484]
8585
8686# Do not reject replicasets that are controlled by deployment, instead reject the deploymentd
8787replica_set_under_deployment (obj) {
88- obj.kind == " ReplicaSet"
88+ obj.kind == " ReplicaSet"
8989 count ([i | obj.metadata.ownerReferences[i].kind == " Deployment" ]) > 0
9090}
9191
9292# Check minAvailable if it is integer
9393not_valid_pdb_min_available (obj, pdb) {
94- not regex.match (" ^[0-9]+%$" , pdb.spec.minAvailable)
94+ not regex.match (" ^[0-9]+%$" , pdb.spec.minAvailable)
9595 obj.spec.replicas < = pdb.spec.minAvailable
9696}
9797
9898# Check minAvailable if it is percentage
9999not_valid_pdb_min_available (obj, pdb) {
100- replicas := obj.spec.replicas
101- regex.match (" ^[0-9]+%$" , pdb.spec.minAvailable)
100+ replicas := obj.spec.replicas
101+ regex.match (" ^[0-9]+%$" , pdb.spec.minAvailable)
102102 percentage_num := to_number (replace (pdb.spec.minAvailable, " %" , " " ))
103- min_available := ceil ((percentage_num/ 100 )* replicas)
103+ min_available := ceil ((percentage_num/ 100 )* replicas)
104104
105- replicas < = min_available
105+ replicas < = min_available
106106}
107107
108108not_valid_pdb_max_unavailable (pdb) {
@@ -115,7 +115,7 @@ not_valid_pdb_max_unavailable(pdb) {
115115
116116# Check one podDisruptionBudget and pod(controller), returns true if it does not match
117117mismatched_selector (pdb, obj) = res {
118- r1 := match_labels (pdb, obj)
118+ r1 := match_labels (pdb, obj)
119119 r2 := match_expressions_exists (pdb, obj)
120120 r3 := match_expressions_does_not_exist (pdb, obj)
121121 r4 := any (match_expressions_in (pdb, obj))
@@ -125,7 +125,7 @@ mismatched_selector(pdb, obj) = res {
125125}
126126
127127match_labels (pdb, obj) = res {
128- pdb_match_labels := { [label, value] | some label; value := pdb.spec.selector.matchLabels[label] }
128+ pdb_match_labels := { [label, value] | some label; value := pdb.spec.selector.matchLabels[label] }
129129 obj_match_labels := { [label, value] | some label; value := obj.spec.selector.matchLabels[label] }
130130 res := count (pdb_match_labels - obj_match_labels) != 0
131131}
0 commit comments