-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(general/restrict-prioriy-classes): add a policy to allow for the…
… restriction of priority classes. (#51) * feat(general/restrict-prioriy-classes): add a policy to allow for the restriction of priority classes. * fix: update names. * fix: typo. * fix: use an array. * fix: comment out to fix ci. * feat: add no priority class test. * doc: add extra comments.
- Loading branch information
Justin Bertrand
authored
Jun 28, 2023
1 parent
3748e8a
commit 6b97721
Showing
6 changed files
with
172 additions
and
24 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
general/restrict-priority-classes/examples/constraint.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
apiVersion: constraints.gatekeeper.sh/v1beta1 | ||
kind: RestrictPriorityClasses | ||
metadata: | ||
name: restrict-priority-classes-solution-critical | ||
annotations: | ||
kubernetes.io/description: | | ||
Restricts the types of priority classes that can be used by solution builders of critical priority. | ||
spec: | ||
match: | ||
kinds: | ||
- apiGroups: [""] | ||
kinds: ["Pod"] | ||
namespaceSelector: | ||
matchExpressions: | ||
- key: project.statcan.gc.ca/purpose | ||
operator: In | ||
values: ["solution"] | ||
- key: project.statcan.gc.ca/priority | ||
operator: In | ||
values: ["critical"] | ||
parameters: | ||
# The names of the priority classes that can be used. | ||
priorityClassNames: | ||
- 'business-value-medium' | ||
- 'business-value-critical' | ||
# --- | ||
# apiVersion: constraints.gatekeeper.sh/v1beta1 | ||
# kind: RestrictPriorityClasses | ||
# metadata: | ||
# name: restrict-priority-classes-solution-medium | ||
# annotations: | ||
# kubernetes.io/description: | | ||
# Restricts the types of priority classes that can be used by solution builders of medium priority. | ||
# spec: | ||
# match: | ||
# kinds: | ||
# - apiGroups: [""] | ||
# kinds: ["Pod"] | ||
# namespaceSelector: | ||
# matchExpressions: | ||
# - key: project.statcan.gc.ca/purpose | ||
# operator: In | ||
# values: ["solution"] | ||
# - key: project.statcan.gc.ca/priority | ||
# operator: In | ||
# values: ["medium"] | ||
# parameters: | ||
# # The names of the priority classes that can be used. | ||
# priorityClassNames: | ||
# - 'business-value-medium' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# ---------------------------------------------------- | ||
# apiVersion and kind of Kustomization | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
|
||
# Each entry in this list must resolve to an existing | ||
# resource definition in YAML. These are the resource | ||
# files that kustomize reads, modifies and emits as a | ||
# YAML string, with resources separated by document | ||
# markers ("---"). | ||
resources: | ||
- template.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# This policy restricts the priority classes that | ||
# can be used by Pods. | ||
package restrictpriorityclasses | ||
|
||
# Create a set of the priority class names | ||
priority_class_names := {name | name := input.parameters.priorityClassNames[_]} | ||
|
||
violation[{"msg": msg}] { | ||
priority_class_name := input.review.object.spec.priorityClassName | ||
|
||
# Check intersection of sets. | ||
# If empty, is in violation. | ||
priority_class_names & {priority_class_name} == set() | ||
|
||
msg := sprintf("pod %s is using an unapproved priority class %q. Available priority classes are %v.", [input.review.object.metadata.name, priority_class_name, priority_class_names]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package restrictpriorityclasses | ||
|
||
names := ["priority-low", "priority-high"] | ||
|
||
test_allowed_names { | ||
priority_class_name := "priority-low" | ||
pod_name := "test-pod" | ||
|
||
result := violation with input.parameters.priorityClassNames as names with input.review.object.spec.priorityClassName as priority_class_name with input.review.object.metadata.name as pod_name | ||
|
||
# No results mean there is no violation. | ||
result == set() | ||
} | ||
|
||
test_unallowed_names { | ||
priority_class_name := "priority-med" | ||
pod_name := "test-pod" | ||
|
||
result := violation with input.parameters.priorityClassNames as names with input.review.object.spec.priorityClassName as priority_class_name with input.review.object.metadata.name as pod_name | ||
|
||
# A result means there is a violation. | ||
result != set() | ||
} | ||
|
||
test_no_names { | ||
priority_class_name := "" | ||
pod_name := "test-pod" | ||
|
||
result := violation with input.parameters.priorityClassNames as names with input.review.object.spec.priorityClassName as priority_class_name with input.review.object.metadata.name as pod_name | ||
|
||
# A result means there is a violation. | ||
result != set() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
apiVersion: templates.gatekeeper.sh/v1beta1 | ||
kind: ConstraintTemplate | ||
metadata: | ||
name: restrictpriorityclasses | ||
annotations: | ||
kubernetes.io/description: Restrict which priority classes can be used by a namespace. | ||
spec: | ||
crd: | ||
spec: | ||
names: | ||
kind: RestrictPriorityClasses | ||
validation: | ||
openAPIV3Schema: | ||
type: object | ||
properties: | ||
priorityClassNames: | ||
type: array | ||
items: | ||
type: string | ||
targets: | ||
- target: admission.k8s.gatekeeper.sh | ||
rego: |- | ||
# This policy restricts the priority classes that | ||
# can be used by Pods. | ||
package restrictpriorityclasses | ||
# Create a set of the priority class names | ||
priority_class_names := {name | name := input.parameters.priorityClassNames[_]} | ||
violation[{"msg": msg}] { | ||
priority_class_name := input.review.object.spec.priorityClassName | ||
# Check intersection of sets. | ||
# If empty, is in violation. | ||
priority_class_names & {priority_class_name} == set() | ||
msg := sprintf("pod %s is using an unapproved priority class %q. Available priority classes are %v.", [input.review.object.metadata.name, priority_class_name, priority_class_names]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters