Skip to content

Commit fd7a535

Browse files
mprahlopenshift-merge-bot[bot]
authored andcommitted
Set the severity annotation for non-OCM policies
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]>
1 parent a8d6871 commit fd7a535

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

internal/plugin.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const (
3636
maxObjectNameLength = 63
3737
dnsReference = "https://kubernetes.io/docs/concepts/overview/working-with-objects/names/" +
3838
"#dns-subdomain-names"
39+
severityAnnotation = "policy.open-cluster-management.io/severity"
3940
)
4041

4142
// Plugin is used to store the PolicyGenerator configuration and the methods to generate the

internal/plugin_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,6 +1183,7 @@ metadata:
11831183

11841184
p.PolicyDefaults.Namespace = "gatekeeper-policies"
11851185
p.PolicyDefaults.InformGatekeeperPolicies = false
1186+
p.PolicyDefaults.Severity = "critical"
11861187
policyConf := types.PolicyConfig{
11871188
Name: "policy-gatekeeper",
11881189
Manifests: []types.Manifest{
@@ -1220,6 +1221,8 @@ spec:
12201221
apiVersion: templates.gatekeeper.sh/v1
12211222
kind: ConstraintTemplate
12221223
metadata:
1224+
annotations:
1225+
policy.open-cluster-management.io/severity: critical
12231226
name: myconstrainingtemplate
12241227
`
12251228
expected = strings.TrimPrefix(expected, "\n")
@@ -1283,6 +1286,8 @@ spec:
12831286
apiVersion: constraints.gatekeeper.sh/v1
12841287
kind: MyConstrainingTemplate
12851288
metadata:
1289+
annotations:
1290+
policy.open-cluster-management.io/severity: low
12861291
name: thisthingimconstraining
12871292
`
12881293
expected = strings.TrimPrefix(expected, "\n")

internal/utils.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,17 @@ func getPolicyTemplates(policyConf *types.PolicyConfig) ([]map[string]interface{
190190
// Only set dependency options if it's an OCM policy
191191
if isOcmPolicy {
192192
setTemplateOptions(policyTemplate, ignorePending, extraDeps)
193+
} else {
194+
policyTemplateUnstructured := unstructured.Unstructured{Object: manifest}
195+
196+
annotations := policyTemplateUnstructured.GetAnnotations()
197+
if annotations == nil {
198+
annotations = make(map[string]string, 1)
199+
}
200+
201+
annotations[severityAnnotation] = policyConf.Severity
202+
203+
policyTemplateUnstructured.SetAnnotations(annotations)
193204
}
194205

195206
policyTemplates = append(policyTemplates, policyTemplate)

internal/utils_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,8 @@ func TestIsPolicyTypeManifest(t *testing.T) {
768768
gotIsPolicy, gotIsOcmPolicy, gotErr := isPolicyTypeManifest(test.manifest, test.informGatekeeperPolicies)
769769
if gotErr != nil {
770770
assertEqual(t, gotErr.Error(), test.wantErr)
771+
} else if test.wantErr != "" {
772+
t.Fatalf("expected the error `%s` but got none", test.wantErr)
771773
}
772774
assertEqual(t, gotIsPolicy, test.wantIsPolicy)
773775
assertEqual(t, gotIsOcmPolicy, test.wantIsOcmPolicy)

0 commit comments

Comments
 (0)