Skip to content

Fix constraint template #591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add more acceptance tests
  • Loading branch information
mrueg committed Feb 25, 2025
commit 6c46674c5ddbd9de40642c43b6b5d087171a5be6
8 changes: 4 additions & 4 deletions acceptance.bats
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
}

@test "[CREATE] Creating constraints using --output matches expected output" {
run ./build/konstraint create test --output test
run ./build/konstraint create test/policies --output test/output/standard
[ "$status" -eq 0 ]
git diff --quiet -- test/
git diff --quiet -- test/output/standard
}

@test "[CREATE] Creating constraints using --constraint-custom-template-file, --constraint-template-custom-template-file and --output matches expected output" {
run ./build/konstraint create test --constraint-custom-template-file internal/commands/constraint_template.tpl --constraint-template-custom-template-file internal/commands/constrainttemplate_template.tpl --partial-constraints --output test/custom
run ./build/konstraint create test/policies --constraint-custom-template-file internal/commands/constraint_template.tpl --constraint-template-custom-template-file internal/commands/constrainttemplate_template.tpl --partial-constraints --output test/output/custom
[ "$status" -eq 0 ]
git diff --quiet -- test/custom
git diff --quiet -- test/output/custom
}
34 changes: 34 additions & 0 deletions test/output/custom/constraint_FullMetadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This is a custom template for constraints
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: FullMetadata
metadata:
name: fullmetadata
spec:
match:
excludedNamespaces:
- kube-system
- gatekeeper-system
kinds:
- apiGroups:
- ""
kinds:
- Pod
- apiGroups:
- apps
kinds:
- DaemonSet
- Deployment
- StatefulSet
labelSelector:
matchExpressions:
- key: foo
operator: In
values:
- bar
- baz
- key: doggos
operator: Exists
namespaces:
- dev
- stage
- prod
6 changes: 6 additions & 0 deletions test/output/custom/constraint_NoMetadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This is a custom template for constraints
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: NoMetadata
metadata:
name: nometadata
spec:
11 changes: 11 additions & 0 deletions test/output/custom/constraint_PartialMetadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This is a custom template for constraints
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: PartialMetadata
metadata:
name: partialmetadata
spec:
match:
namespaces:
- dev
- stage
- prod
34 changes: 34 additions & 0 deletions test/output/custom/constraint_Policies.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This is a custom template for constraints
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: Policies
metadata:
name: policies
spec:
match:
excludedNamespaces:
- kube-system
- gatekeeper-system
kinds:
- apiGroups:
- ""
kinds:
- Pod
- apiGroups:
- apps
kinds:
- DaemonSet
- Deployment
- StatefulSet
labelSelector:
matchExpressions:
- key: foo
operator: In
values:
- bar
- baz
- key: doggos
operator: Exists
namespaces:
- dev
- stage
- prod
38 changes: 38 additions & 0 deletions test/output/custom/template_FullMetadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# This is a custom template for a constraint template
apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
name: fullmetadata
spec:
crd:
spec:
names:
kind: FullMetadata
validation:
openAPIV3Schema:
properties:
super:
description: |-
super duper cool parameter with a description
on two lines.
type: string
targets:
- libs:
- |-
package lib.libraryA

import data.lib.libraryB
- |-
package lib.libraryB
rego: |-
package test

import future.keywords.if
import data.lib.libraryA

policyID := "P123456"

violation if {
true # some comment
}
target: admission.k8s.gatekeeper.sh
30 changes: 30 additions & 0 deletions test/output/custom/template_NoMetadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This is a custom template for a constraint template
apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
name: nometadata
spec:
crd:
spec:
names:
kind: NoMetadata
targets:
- libs:
- |-
package lib.libraryA

import data.lib.libraryB
- |-
package lib.libraryB
rego: |-
package test_nometadata

import future.keywords.if
import data.lib.libraryA

policyID := "P123456"

violation if {
true # some comment
}
target: admission.k8s.gatekeeper.sh
30 changes: 30 additions & 0 deletions test/output/custom/template_PartialMetadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# This is a custom template for a constraint template
apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
name: partialmetadata
spec:
crd:
spec:
names:
kind: PartialMetadata
targets:
- libs:
- |-
package lib.libraryA

import data.lib.libraryB
- |-
package lib.libraryB
rego: |-
package test_partial_metadata

import future.keywords.if
import data.lib.libraryA

policyID := "P123456"

violation if {
true # some comment
}
target: admission.k8s.gatekeeper.sh
38 changes: 38 additions & 0 deletions test/output/custom/template_Policies.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# This is a custom template for a constraint template
apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
name: policies
spec:
crd:
spec:
names:
kind: Policies
validation:
openAPIV3Schema:
properties:
super:
description: |-
super duper cool parameter with a description
on two lines.
type: string
targets:
- libs:
- |-
package lib.libraryA

import data.lib.libraryB
- |-
package lib.libraryB
rego: |-
package test

import future.keywords.if
import data.lib.libraryA

policyID := "P123456"

violation if {
true # some comment
}
target: admission.k8s.gatekeeper.sh
File renamed without changes.
4 changes: 4 additions & 0 deletions test/output/standard/constraint_NoMetadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: NoMetadata
metadata:
name: nometadata
10 changes: 10 additions & 0 deletions test/output/standard/constraint_PartialMetadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: PartialMetadata
metadata:
name: partialmetadata
spec:
match:
namespaces:
- dev
- stage
- prod
File renamed without changes.
38 changes: 38 additions & 0 deletions test/output/standard/template_FullMetadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
apiVersion: templates.gatekeeper.sh/v1beta1
kind: ConstraintTemplate
metadata:
creationTimestamp: null
name: fullmetadata
spec:
crd:
spec:
names:
kind: FullMetadata
validation:
openAPIV3Schema:
properties:
super:
description: |-
super duper cool parameter with a description
on two lines.
type: string
targets:
- libs:
- |-
package lib.libraryA

import data.lib.libraryB
- package lib.libraryB
rego: |-
package test

import future.keywords.if
import data.lib.libraryA

policyID := "P123456"

violation if {
true # some comment
}
target: admission.k8s.gatekeeper.sh
status: {}
30 changes: 30 additions & 0 deletions test/output/standard/template_NoMetadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apiVersion: templates.gatekeeper.sh/v1beta1
kind: ConstraintTemplate
metadata:
creationTimestamp: null
name: nometadata
spec:
crd:
spec:
names:
kind: NoMetadata
targets:
- libs:
- |-
package lib.libraryA

import data.lib.libraryB
- package lib.libraryB
rego: |-
package test_nometadata

import future.keywords.if
import data.lib.libraryA

policyID := "P123456"

violation if {
true # some comment
}
target: admission.k8s.gatekeeper.sh
status: {}
30 changes: 30 additions & 0 deletions test/output/standard/template_PartialMetadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apiVersion: templates.gatekeeper.sh/v1beta1
kind: ConstraintTemplate
metadata:
creationTimestamp: null
name: partialmetadata
spec:
crd:
spec:
names:
kind: PartialMetadata
targets:
- libs:
- |-
package lib.libraryA

import data.lib.libraryB
- package lib.libraryB
rego: |-
package test_partial_metadata

import future.keywords.if
import data.lib.libraryA

policyID := "P123456"

violation if {
true # some comment
}
target: admission.k8s.gatekeeper.sh
status: {}
Loading