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

Set the severity annotation for non-OCM policies #132

Merged
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: 1 addition & 0 deletions internal/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
maxObjectNameLength = 63
dnsReference = "https://kubernetes.io/docs/concepts/overview/working-with-objects/names/" +
"#dns-subdomain-names"
severityAnnotation = "policy.open-cluster-management.io/severity"
)

// Plugin is used to store the PolicyGenerator configuration and the methods to generate the
Expand Down
5 changes: 5 additions & 0 deletions internal/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1183,6 +1183,7 @@ metadata:

p.PolicyDefaults.Namespace = "gatekeeper-policies"
p.PolicyDefaults.InformGatekeeperPolicies = false
p.PolicyDefaults.Severity = "critical"
policyConf := types.PolicyConfig{
Name: "policy-gatekeeper",
Manifests: []types.Manifest{
Expand Down Expand Up @@ -1220,6 +1221,8 @@ spec:
apiVersion: templates.gatekeeper.sh/v1
kind: ConstraintTemplate
metadata:
annotations:
policy.open-cluster-management.io/severity: critical
name: myconstrainingtemplate
`
expected = strings.TrimPrefix(expected, "\n")
Expand Down Expand Up @@ -1283,6 +1286,8 @@ spec:
apiVersion: constraints.gatekeeper.sh/v1
kind: MyConstrainingTemplate
metadata:
annotations:
policy.open-cluster-management.io/severity: low
name: thisthingimconstraining
`
expected = strings.TrimPrefix(expected, "\n")
Expand Down
11 changes: 11 additions & 0 deletions internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,17 @@ func getPolicyTemplates(policyConf *types.PolicyConfig) ([]map[string]interface{
// Only set dependency options if it's an OCM policy
if isOcmPolicy {
setTemplateOptions(policyTemplate, ignorePending, extraDeps)
} else {
policyTemplateUnstructured := unstructured.Unstructured{Object: manifest}

annotations := policyTemplateUnstructured.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string, 1)
}

annotations[severityAnnotation] = policyConf.Severity

policyTemplateUnstructured.SetAnnotations(annotations)
}

policyTemplates = append(policyTemplates, policyTemplate)
Expand Down
2 changes: 2 additions & 0 deletions internal/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,8 @@ func TestIsPolicyTypeManifest(t *testing.T) {
gotIsPolicy, gotIsOcmPolicy, gotErr := isPolicyTypeManifest(test.manifest, test.informGatekeeperPolicies)
if gotErr != nil {
assertEqual(t, gotErr.Error(), test.wantErr)
} else if test.wantErr != "" {
t.Fatalf("expected the error `%s` but got none", test.wantErr)
}
assertEqual(t, gotIsPolicy, test.wantIsPolicy)
assertEqual(t, gotIsOcmPolicy, test.wantIsOcmPolicy)
Expand Down