Skip to content

Commit

Permalink
Set the severity annotation for non-OCM policies
Browse files Browse the repository at this point in the history
For policy-templates entries that are not OCM policies, it is supported
to set the `policy.open-cluster-management.io/severity` annotation for
other tooling to determine the severity of a noncompliant event. This
adds that automatically.

Relates:
https://issues.redhat.com/browse/ACM-6860

Signed-off-by: mprahl <[email protected]>
  • Loading branch information
mprahl authored and openshift-merge-bot[bot] committed Nov 16, 2023
1 parent a8d6871 commit fd7a535
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
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

0 comments on commit fd7a535

Please sign in to comment.