-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Paul Krizak <paul.krizak@gmail.com>
- Loading branch information
Showing
15 changed files
with
442 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
artifacthub/library/general/replicalimits/1.1.0/artifacthub-pkg.yml
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,22 @@ | ||
version: 1.1.0 | ||
name: k8sreplicalimits | ||
displayName: Replica Limits | ||
createdAt: "2023-10-27T22:42:28Z" | ||
description: Requires that objects with the field `spec.replicas` (Deployments, ReplicaSets, etc.) specify a number of replicas within defined ranges. | ||
digest: 30c15576b26d9b879d5c2486f72478a36e39404510117734cb11f8570a2285a7 | ||
license: Apache-2.0 | ||
homeURL: https://open-policy-agent.github.io/gatekeeper-library/website/replicalimits | ||
keywords: | ||
- gatekeeper | ||
- open-policy-agent | ||
- policies | ||
readme: |- | ||
# Replica Limits | ||
Requires that objects with the field `spec.replicas` (Deployments, ReplicaSets, etc.) specify a number of replicas within defined ranges. | ||
install: |- | ||
### Usage | ||
```shell | ||
kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper-library/master/artifacthub/library/general/replicalimits/1.1.0/template.yaml | ||
``` | ||
provider: | ||
name: Gatekeeper Library |
2 changes: 2 additions & 0 deletions
2
artifacthub/library/general/replicalimits/1.1.0/kustomization.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,2 @@ | ||
resources: | ||
- template.yaml |
15 changes: 15 additions & 0 deletions
15
artifacthub/library/general/replicalimits/1.1.0/samples/replicalimits/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,15 @@ | ||
apiVersion: constraints.gatekeeper.sh/v1beta1 | ||
kind: K8sReplicaLimits | ||
metadata: | ||
name: replica-limits | ||
spec: | ||
match: | ||
kinds: | ||
- apiGroups: ["apps"] | ||
kinds: ["Deployment"] | ||
- apiGroups: ["autoscaling"] | ||
kinds: ["Scale"] | ||
parameters: | ||
ranges: | ||
- min_replicas: 3 | ||
max_replicas: 50 |
19 changes: 19 additions & 0 deletions
19
artifacthub/library/general/replicalimits/1.1.0/samples/replicalimits/example_allowed.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,19 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: allowed-deployment | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: nginx | ||
replicas: 3 | ||
template: | ||
metadata: | ||
labels: | ||
app: nginx | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx:1.14.2 | ||
ports: | ||
- containerPort: 80 |
19 changes: 19 additions & 0 deletions
19
...facthub/library/general/replicalimits/1.1.0/samples/replicalimits/example_disallowed.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,19 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: disallowed-deployment | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: nginx | ||
replicas: 100 | ||
template: | ||
metadata: | ||
labels: | ||
app: nginx | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx:1.14.2 | ||
ports: | ||
- containerPort: 80 |
6 changes: 6 additions & 0 deletions
6
...thub/library/general/replicalimits/1.1.0/samples/replicalimits/example_scale_allowed.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,6 @@ | ||
apiVersion: autoscaling/v1 | ||
kind: Scale | ||
metadata: | ||
name: allowed-deployment | ||
spec: | ||
replicas: 3 |
6 changes: 6 additions & 0 deletions
6
...b/library/general/replicalimits/1.1.0/samples/replicalimits/example_scale_disallowed.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,6 @@ | ||
apiVersion: autoscaling/v1 | ||
kind: Scale | ||
metadata: | ||
name: allowed-deployment | ||
spec: | ||
replicas: 100 |
15 changes: 15 additions & 0 deletions
15
artifacthub/library/general/replicalimits/1.1.0/samples/replicalimits_zero/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,15 @@ | ||
apiVersion: constraints.gatekeeper.sh/v1beta1 | ||
kind: K8sReplicaLimits | ||
metadata: | ||
name: replica-limits | ||
spec: | ||
match: | ||
kinds: | ||
- apiGroups: ["apps"] | ||
kinds: ["Deployment"] | ||
- apiGroups: ["autoscaling"] | ||
kinds: ["Scale"] | ||
parameters: | ||
ranges: | ||
- min_replicas: 0 | ||
max_replicas: 50 |
19 changes: 19 additions & 0 deletions
19
...cthub/library/general/replicalimits/1.1.0/samples/replicalimits_zero/example_allowed.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,19 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: allowed-deployment | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: nginx | ||
replicas: 0 | ||
template: | ||
metadata: | ||
labels: | ||
app: nginx | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx:1.14.2 | ||
ports: | ||
- containerPort: 80 |
19 changes: 19 additions & 0 deletions
19
...ub/library/general/replicalimits/1.1.0/samples/replicalimits_zero/example_disallowed.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,19 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: disallowed-deployment | ||
spec: | ||
selector: | ||
matchLabels: | ||
app: nginx | ||
replicas: 100 | ||
template: | ||
metadata: | ||
labels: | ||
app: nginx | ||
spec: | ||
containers: | ||
- name: nginx | ||
image: nginx:1.14.2 | ||
ports: | ||
- containerPort: 80 |
7 changes: 7 additions & 0 deletions
7
...library/general/replicalimits/1.1.0/samples/replicalimits_zero/example_scale_allowed.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,7 @@ | ||
apiVersion: autoscaling/v1 | ||
kind: Scale | ||
metadata: | ||
name: allowed-deployment | ||
# kubectl scale deploy <name> --replicas=0 creates a Scale | ||
# resource with an empty spec, not replicas:0 | ||
spec: {} |
6 changes: 6 additions & 0 deletions
6
...rary/general/replicalimits/1.1.0/samples/replicalimits_zero/example_scale_disallowed.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,6 @@ | ||
apiVersion: autoscaling/v1 | ||
kind: Scale | ||
metadata: | ||
name: allowed-deployment | ||
spec: | ||
replicas: 100 |
45 changes: 45 additions & 0 deletions
45
artifacthub/library/general/replicalimits/1.1.0/suite.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,45 @@ | ||
kind: Suite | ||
apiVersion: test.gatekeeper.sh/v1alpha1 | ||
metadata: | ||
name: replicalimits | ||
tests: | ||
- name: replica-limit | ||
template: template.yaml | ||
constraint: samples/replicalimits/constraint.yaml | ||
cases: | ||
- name: example-allowed | ||
object: samples/replicalimits/example_allowed.yaml | ||
assertions: | ||
- violations: no | ||
- name: example-scale-allowed | ||
object: samples/replicalimits/example_scale_allowed.yaml | ||
assertions: | ||
- violations: no | ||
- name: example-disallowed | ||
object: samples/replicalimits/example_disallowed.yaml | ||
assertions: | ||
- violations: yes | ||
- name: example-scale-disallowed | ||
object: samples/replicalimits/example_scale_disallowed.yaml | ||
assertions: | ||
- violations: yes | ||
- name: replica-limit-zero | ||
template: template.yaml | ||
constraint: samples/replicalimits_zero/constraint.yaml | ||
cases: | ||
- name: example-allowed | ||
object: samples/replicalimits_zero/example_allowed.yaml | ||
assertions: | ||
- violations: no | ||
- name: example-scale-allowed | ||
object: samples/replicalimits_zero/example_scale_allowed.yaml | ||
assertions: | ||
- violations: no | ||
- name: example-disallowed | ||
object: samples/replicalimits_zero/example_disallowed.yaml | ||
assertions: | ||
- violations: yes | ||
- name: example-scale-disallowed | ||
object: samples/replicalimits_zero/example_scale_disallowed.yaml | ||
assertions: | ||
- violations: yes |
58 changes: 58 additions & 0 deletions
58
artifacthub/library/general/replicalimits/1.1.0/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,58 @@ | ||
apiVersion: templates.gatekeeper.sh/v1 | ||
kind: ConstraintTemplate | ||
metadata: | ||
name: k8sreplicalimits | ||
annotations: | ||
metadata.gatekeeper.sh/title: "Replica Limits" | ||
metadata.gatekeeper.sh/version: 1.1.0 | ||
description: >- | ||
Requires that objects with the field `spec.replicas` (Deployments, | ||
ReplicaSets, etc.) specify a number of replicas within defined ranges. | ||
spec: | ||
crd: | ||
spec: | ||
names: | ||
kind: K8sReplicaLimits | ||
validation: | ||
# Schema for the `parameters` field | ||
openAPIV3Schema: | ||
type: object | ||
properties: | ||
ranges: | ||
type: array | ||
description: Allowed ranges for numbers of replicas. Values are inclusive. | ||
items: | ||
type: object | ||
description: A range of allowed replicas. Values are inclusive. | ||
properties: | ||
min_replicas: | ||
description: The minimum number of replicas allowed, inclusive. | ||
type: integer | ||
max_replicas: | ||
description: The maximum number of replicas allowed, inclusive. | ||
type: integer | ||
targets: | ||
- target: admission.k8s.gatekeeper.sh | ||
rego: | | ||
package k8sreplicalimits | ||
object_name = input.review.object.metadata.name | ||
object_kind = input.review.kind.kind | ||
violation[{"msg": msg}] { | ||
spec := input.review.object.spec | ||
not input_replica_limit(spec) | ||
msg := sprintf("The provided number of replicas is not allowed for %v: %v. Allowed ranges: %v", [object_kind, object_name, input.parameters]) | ||
} | ||
input_replica_limit(spec) { | ||
provided := object.get(spec, "replicas", 0) | ||
count(input.parameters.ranges) > 0 | ||
range := input.parameters.ranges[_] | ||
value_within_range(range, provided) | ||
} | ||
value_within_range(range, value) { | ||
range.min_replicas <= value | ||
range.max_replicas >= value | ||
} |
Oops, something went wrong.