Skip to content
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

Fix constraint template #591

Merged
merged 3 commits into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion .github/workflows/cron_e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ jobs:
run: |
chmod +x ./konstraint
./konstraint create -o e2e-resources examples
./konstraint create -o e2e-resources test

- name: create kind cluster
run: kind create cluster
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/pull_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ jobs:
run: |
chmod +x ./konstraint
./konstraint create -o e2e-resources examples
./konstraint create -o e2e-resources test

- name: create kind cluster
run: kind create cluster
Expand Down
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
}
19 changes: 14 additions & 5 deletions internal/commands/constraint_template.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ metadata:
{{- end }}
name: {{ .Name }}
spec:
{{- if .Matchers }}
match: {{- .GetAnnotation "matchers" | toIndentYAML 2 | nindent 4 }}
{{- end }}
{{- if ne .Enforcement "deny" }}
enforcementAction: {{ .Enforcement }}
{{- end -}}
{{- if .AnnotationParameters }}
parameters: {{- .AnnotationParameters | toIndentYAML 2 | nindent 4 }}
{{- if or .AnnotationKindMatchers .AnnotationNamespaceMatchers .AnnotationExcludedNamespaceMatchers .AnnotationLabelSelectorMatcher }}
match:
{{- if .AnnotationExcludedNamespaceMatchers }}
excludedNamespaces: {{- .AnnotationExcludedNamespaceMatchers | toIndentYAML 2 | nindent 6 }}
{{- end }}
{{- if .AnnotationKindMatchers }}
kinds: {{- .AnnotationKindMatchers | toJSON | fromJSON | toIndentYAML 2 | nindent 6 }}
{{- end }}
{{- if .AnnotationLabelSelectorMatcher }}
labelSelector: {{- .AnnotationLabelSelectorMatcher | toJSON | fromJSON | toIndentYAML 2 | nindent 6 }}
{{- end }}
{{- if .AnnotationNamespaceMatchers }}
namespaces: {{- .AnnotationNamespaceMatchers | toIndentYAML 2 | nindent 6 }}
{{- end }}
{{- end }}
10 changes: 5 additions & 5 deletions internal/commands/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestRenderConstraint(t *testing.T) {
t.Errorf("Error getting violations: %v", err)
}

expected, err := os.ReadFile("../../test/constraint_Test.yaml")
expected, err := os.ReadFile("../../test/output/standard/constraint_FullMetadata.yaml")
if err != nil {
t.Errorf("Error reading expected file: %v", err)
}
Expand Down Expand Up @@ -48,7 +48,7 @@ func TestRenderConstraintWithCustomTemplate(t *testing.T) {
t.Errorf("Error getting violations: %v", err)
}

expected, err := os.ReadFile("../../test/custom/constraint_Test.yaml")
expected, err := os.ReadFile("../../test/output/custom/constraint_FullMetadata.yaml")
if err != nil {
t.Errorf("Error reading expected file: %v", err)
}
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestRenderConstraintTemplate(t *testing.T) {
t.Errorf("Error getting violations: %v", err)
}

expected, err := os.ReadFile("../../test/template_Test.yaml")
expected, err := os.ReadFile("../../test/output/standard/template_FullMetadata.yaml")
if err != nil {
t.Errorf("Error reading expected file: %v", err)
}
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestRenderConstraintTemplateWithCustomTemplate(t *testing.T) {
t.Errorf("Error getting violations: %v", err)
}

expected, err := os.ReadFile("../../test/custom/template_Test.yaml")
expected, err := os.ReadFile("../../test/output/custom/template_FullMetadata.yaml")
if err != nil {
t.Errorf("Error reading expected file: %v", err)
}
Expand All @@ -129,7 +129,7 @@ func TestRenderConstraintTemplateWithCustomTemplate(t *testing.T) {
}

func GetViolations() ([]rego.Rego, error) {
violations, err := rego.GetViolations("../../test")
violations, err := rego.GetViolations("../../test/policies/")
if err != nil {
return nil, err
}
Expand Down
82 changes: 0 additions & 82 deletions test/custom/constraint_Test.yaml

This file was deleted.

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
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
name: test
name: fullmetadata
spec:
crd:
spec:
names:
kind: Test
kind: FullMetadata
validation:
openAPIV3Schema:
properties:
Expand All @@ -25,7 +25,7 @@ spec:
- |-
package lib.libraryB
rego: |-
package test
package test_fullmetadata

import future.keywords.if
import data.lib.libraryA
Expand Down
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_partialmetadata

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

policyID := "P123456"

violation if {
true # some comment
}
target: admission.k8s.gatekeeper.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apiVersion: constraints.gatekeeper.sh/v1beta1
kind: Test
kind: FullMetadata
metadata:
name: test
name: fullmetadata
spec:
match:
excludedNamespaces:
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
creationTimestamp: null
name: test
name: fullmetadata
spec:
crd:
spec:
names:
kind: Test
kind: FullMetadata
validation:
openAPIV3Schema:
properties:
Expand All @@ -25,7 +25,7 @@ spec:
import data.lib.libraryB
- package lib.libraryB
rego: |-
package test
package test_fullmetadata

import future.keywords.if
import data.lib.libraryA
Expand Down
Loading
Loading