Skip to content

Commit d0bab6c

Browse files
committed
Add support for API groups with multiple slashes
1 parent 5d5330f commit d0bab6c

9 files changed

Lines changed: 71 additions & 68 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
apiVersion: constraints.gatekeeper.sh/v1beta1
2+
kind: VirtualServiceNaming
3+
metadata:
4+
name: virtualservicenaming
5+
spec:
6+
match:
7+
kinds:
8+
- apiGroups:
9+
- networking.istio.io/v1alpha3
10+
kinds:
11+
- VirtualService
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package policy
2+
3+
import data.lib.k8s
4+
5+
# VirtualServices must not be named virtual-service
6+
# @Kinds networking.istio.io/v1alpha3/VirtualService
7+
violation[msg] {
8+
not virtualservice_name_allowed
9+
10+
msg := k8s.format(sprintf("(%s) %s: VirtualServices must not be named virtual-service", [k8s.kind, k8s.name]))
11+
}
12+
13+
virtualservice_name_allowed {
14+
k8s.kind == "VirtualService"
15+
not k8s.name == "virtual-service"
16+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package policy
2+
3+
test_input_as_invalid_name {
4+
input := {
5+
"kind": "VirtualService",
6+
"metadata": {
7+
"name": "virtual-service"
8+
}
9+
}
10+
11+
not virtualservice_name_allowed with input as input
12+
}
13+
14+
test_input_as_valid_name {
15+
input := {
16+
"kind": "VirtualService",
17+
"metadata": {
18+
"name": "valid-name"
19+
}
20+
}
21+
22+
virtualservice_name_allowed with input as input
23+
}

examples/volumes-emptydir-limits-required/template.yaml renamed to examples/virtual-service-naming/template.yaml

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ apiVersion: templates.gatekeeper.sh/v1beta1
22
kind: ConstraintTemplate
33
metadata:
44
creationTimestamp: null
5-
name: volumesemptydirlimitsrequired
5+
name: virtualservicenaming
66
spec:
77
crd:
88
spec:
99
names:
10-
kind: VolumesEmptydirLimitsRequired
10+
kind: VirtualServiceNaming
1111
targets:
1212
- libs:
1313
- |
@@ -297,16 +297,13 @@ spec:
297297
298298
import data.lib.k8s
299299
300-
# EmptyDir volume mounts must specify a size limit.
301-
# @Kinds apps/DaemonSet apps/Deployment apps/StatefulSet core/Pod
300+
# VirtualServices must not be named virtual-service
301+
# @Kinds networking.istio.io/v1alpha3/VirtualService
302302
violation[msg] {
303-
volumes_emptydir_size_limit_required
303+
k8s.kind == "VirtualService"
304+
k8s.name == "virtual-service"
304305
305-
msg := k8s.format(sprintf("(%s) %s: Volume mounts of type emptyDir must set a size limit", [k8s.kind, k8s.name]))
306-
}
307-
308-
volumes_emptydir_size_limit_required {
309-
k8s.missing_field(k8s.volumes[_].emptyDir, "sizeLimit")
306+
msg := k8s.format(sprintf("(%s) %s: VirtualServices must not be named virtual-service", [k8s.kind, k8s.name]))
310307
}
311308
target: admission.k8s.gatekeeper.sh
312309
status: {}

examples/volumes-emptydir-limits-required/constraint.yaml

Lines changed: 0 additions & 15 deletions
This file was deleted.

examples/volumes-emptydir-limits-required/src.rego

Lines changed: 0 additions & 15 deletions
This file was deleted.

examples/volumes-emptydir-limits-required/src_test.rego

Lines changed: 0 additions & 23 deletions
This file was deleted.

internal/commands/document.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,13 @@ func getPolicyCommentBlocks(policy []byte) ([]PolicyCommentBlock, error) {
116116
var kinds []string
117117
for _, kindGroup := range kindGroups {
118118
kindTokens := strings.Split(kindGroup, "/")
119+
apiGroup := strings.Join(kindTokens[:len(kindTokens)-1], "/")
119120

120-
if !contains(apiGroups, kindTokens[0]) {
121-
apiGroups = append(apiGroups, kindTokens[0])
121+
if !contains(apiGroups, apiGroup) {
122+
apiGroups = append(apiGroups, apiGroup)
122123
}
123124

124-
kinds = append(kinds, kindTokens[1])
125+
kinds = append(kinds, kindTokens[len(kindTokens)-1])
125126
}
126127

127128
policyCommentBlock := PolicyCommentBlock{

internal/commands/document_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ violation[msg] {
2323
func TestGetPolicyCommentBlocks(t *testing.T) {
2424
policy := `
2525
# First description
26-
# @Kinds core/Pods apps/Deployments apps/DaemonSet
26+
# @Kinds core/Pods apps/Deployments apps/DaemonSet networking.istio.io/v1alpha3/VirtualService
2727
violation[msg] {
2828
false
2929
}`
@@ -38,7 +38,7 @@ violation[msg] {
3838
t.Errorf("expected policy block to exist, but one did not.")
3939
}
4040

41-
expectedAPIGroupCount := 2
41+
expectedAPIGroupCount := 3
4242
if len(actual[0].APIGroups) != expectedAPIGroupCount {
4343
t.Errorf("expected %v APIGroups to exists but %v were found", expectedAPIGroupCount, len(actual[0].APIGroups))
4444
}
@@ -51,11 +51,19 @@ violation[msg] {
5151
t.Errorf("expected policy block to contain 'apps' APIGroup, but was not found.")
5252
}
5353

54+
if !contains(actual[0].APIGroups, "networking.istio.io/v1alpha3") {
55+
t.Errorf("expected policy block to contain 'apps' APIGroup, but was not found.")
56+
}
57+
5458
if !contains(actual[0].Kinds, "Pods") {
5559
t.Errorf("expected policy block to contain 'Pods' Kind, but was not found.")
5660
}
5761

5862
if !contains(actual[0].Kinds, "Deployments") {
5963
t.Errorf("expected policy block to contain 'Deployments' Kind, but was not found.")
6064
}
65+
66+
if !contains(actual[0].Kinds, "VirtualService") {
67+
t.Errorf("expected policy block to contain 'VirtualService' Kind, but was not found.")
68+
}
6169
}

0 commit comments

Comments
 (0)