Skip to content

Commit 76ba418

Browse files
committed
Add field to set labels on the Policy generated
Signed-off-by: Brian Jarvis <[email protected]>
1 parent 98488b5 commit 76ba418

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed

docs/policygenerator-reference.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ policyDefaults:
182182
# Optional. Annotations that the policy will include under its metadata.annotations. It will be applied for all
183183
# policies unless specified in the policy.
184184
policyAnnotations: {}
185+
# Optional. Labels that the policy will include under its metadata.labels. It will be applied for all
186+
# policies unless specified in the policy.
187+
policyLabels: {}
185188

186189
# Optional. Defaults for policy set generation. Any default value listed here can be overridden under an entry in the
187190
# policySets array.
@@ -315,6 +318,9 @@ policies:
315318
# Optional. Annotations that the policy will include under its metadata.annotations. It will overwrite the
316319
# policyAnnotation defined in the policyDefaults.
317320
policyAnnotations: {}
321+
# Optional. Labels that the policy will include under its metadata.labels. It will overwrite the
322+
# policyLabels defined in the policyDefaults.
323+
policyLabels: {}
318324

319325
# Optional. The list of policy sets to create. To include a policy in a policy set, use policies[*].policySets or
320326
# policyDefaults.policySets or policySets.policies.

internal/plugin.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,15 @@ func (p *Plugin) applyDefaults(unmarshaledConfig map[string]interface{}) {
522522
policy.PolicyAnnotations = annotations
523523
}
524524

525+
if policy.PolicyLabels == nil {
526+
labels := map[string]string{}
527+
for k, v := range p.PolicyDefaults.PolicyLabels {
528+
labels[k] = v
529+
}
530+
531+
policy.PolicyLabels = labels
532+
}
533+
525534
if policy.Categories == nil {
526535
policy.Categories = p.PolicyDefaults.Categories
527536
}
@@ -1296,6 +1305,10 @@ func (p *Plugin) createPolicy(policyConf *types.PolicyConfig) error {
12961305
policyConf.PolicyAnnotations = map[string]string{}
12971306
}
12981307

1308+
if policyConf.PolicyLabels == nil {
1309+
policyConf.PolicyLabels = map[string]string{}
1310+
}
1311+
12991312
policyConf.PolicyAnnotations["policy.open-cluster-management.io/categories"] = strings.Join(
13001313
policyConf.Categories, ",",
13011314
)
@@ -1344,6 +1357,10 @@ func (p *Plugin) createPolicy(policyConf *types.PolicyConfig) error {
13441357
"spec": spec,
13451358
}
13461359

1360+
if len(policyConf.PolicyLabels) != 0 {
1361+
policy["metadata"].(map[string]interface{})["labels"] = policyConf.PolicyLabels
1362+
}
1363+
13471364
// set the root policy remediation action if all the remediation actions match
13481365
if rootRemediationAction := getRootRemediationAction(policyTemplates); rootRemediationAction != "" {
13491366
policy["spec"].(map[string]interface{})["remediationAction"] = rootRemediationAction

internal/plugin_test.go

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,165 @@ spec:
11051105
assertEqual(t, output, expected)
11061106
}
11071107

1108+
func TestCreatePolicyWithLabels(t *testing.T) {
1109+
t.Parallel()
1110+
tmpDir := t.TempDir()
1111+
createConfigMap(t, tmpDir, "configmap.yaml")
1112+
1113+
p := Plugin{}
1114+
p.PolicyDefaults.Namespace = "my-policies"
1115+
p.PolicyDefaults.PolicyLabels = map[string]string{"test-default-label": "default"}
1116+
1117+
policyConf := types.PolicyConfig{
1118+
Name: "policy-app-config",
1119+
Manifests: []types.Manifest{
1120+
{Path: path.Join(tmpDir, "configmap.yaml")},
1121+
},
1122+
}
1123+
p.Policies = append(p.Policies, policyConf)
1124+
p.applyDefaults(map[string]interface{}{})
1125+
1126+
err := p.createPolicy(&p.Policies[0])
1127+
if err != nil {
1128+
t.Fatal(err.Error())
1129+
}
1130+
1131+
output := p.outputBuffer.String()
1132+
expected := `
1133+
---
1134+
apiVersion: policy.open-cluster-management.io/v1
1135+
kind: Policy
1136+
metadata:
1137+
annotations:
1138+
policy.open-cluster-management.io/categories: CM Configuration Management
1139+
policy.open-cluster-management.io/controls: CM-2 Baseline Configuration
1140+
policy.open-cluster-management.io/standards: NIST SP 800-53
1141+
labels:
1142+
test-default-label: default
1143+
name: policy-app-config
1144+
namespace: my-policies
1145+
spec:
1146+
disabled: false
1147+
policy-templates:
1148+
- objectDefinition:
1149+
apiVersion: policy.open-cluster-management.io/v1
1150+
kind: ConfigurationPolicy
1151+
metadata:
1152+
name: policy-app-config
1153+
spec:
1154+
object-templates:
1155+
- complianceType: musthave
1156+
objectDefinition:
1157+
apiVersion: v1
1158+
data:
1159+
game.properties: enemies=potato
1160+
kind: ConfigMap
1161+
metadata:
1162+
name: my-configmap
1163+
remediationAction: inform
1164+
severity: low
1165+
remediationAction: inform
1166+
`
1167+
expected = strings.TrimPrefix(expected, "\n")
1168+
assertEqual(t, output, expected)
1169+
1170+
// Check for override default policy with empty map to skip default annotations from the policy
1171+
p.outputBuffer.Reset()
1172+
p.Policies[0].PolicyLabels = map[string]string{}
1173+
p.applyDefaults(map[string]interface{}{})
1174+
1175+
err = p.createPolicy(&p.Policies[0])
1176+
if err != nil {
1177+
t.Fatal(err.Error())
1178+
}
1179+
1180+
output = p.outputBuffer.String()
1181+
expected = `
1182+
---
1183+
apiVersion: policy.open-cluster-management.io/v1
1184+
kind: Policy
1185+
metadata:
1186+
annotations:
1187+
policy.open-cluster-management.io/categories: CM Configuration Management
1188+
policy.open-cluster-management.io/controls: CM-2 Baseline Configuration
1189+
policy.open-cluster-management.io/standards: NIST SP 800-53
1190+
name: policy-app-config
1191+
namespace: my-policies
1192+
spec:
1193+
disabled: false
1194+
policy-templates:
1195+
- objectDefinition:
1196+
apiVersion: policy.open-cluster-management.io/v1
1197+
kind: ConfigurationPolicy
1198+
metadata:
1199+
name: policy-app-config
1200+
spec:
1201+
object-templates:
1202+
- complianceType: musthave
1203+
objectDefinition:
1204+
apiVersion: v1
1205+
data:
1206+
game.properties: enemies=potato
1207+
kind: ConfigMap
1208+
metadata:
1209+
name: my-configmap
1210+
remediationAction: inform
1211+
severity: low
1212+
remediationAction: inform
1213+
`
1214+
expected = strings.TrimPrefix(expected, "\n")
1215+
assertEqual(t, output, expected)
1216+
1217+
// Check for override default policy annotation
1218+
p.outputBuffer.Reset()
1219+
p.Policies[0].PolicyLabels = map[string]string{"test-wave-label": "100"}
1220+
p.applyDefaults(map[string]interface{}{})
1221+
1222+
err = p.createPolicy(&p.Policies[0])
1223+
if err != nil {
1224+
t.Fatal(err.Error())
1225+
}
1226+
1227+
output = p.outputBuffer.String()
1228+
expected = `
1229+
---
1230+
apiVersion: policy.open-cluster-management.io/v1
1231+
kind: Policy
1232+
metadata:
1233+
annotations:
1234+
policy.open-cluster-management.io/categories: CM Configuration Management
1235+
policy.open-cluster-management.io/controls: CM-2 Baseline Configuration
1236+
policy.open-cluster-management.io/standards: NIST SP 800-53
1237+
labels:
1238+
test-wave-label: "100"
1239+
name: policy-app-config
1240+
namespace: my-policies
1241+
spec:
1242+
disabled: false
1243+
policy-templates:
1244+
- objectDefinition:
1245+
apiVersion: policy.open-cluster-management.io/v1
1246+
kind: ConfigurationPolicy
1247+
metadata:
1248+
name: policy-app-config
1249+
spec:
1250+
object-templates:
1251+
- complianceType: musthave
1252+
objectDefinition:
1253+
apiVersion: v1
1254+
data:
1255+
game.properties: enemies=potato
1256+
kind: ConfigMap
1257+
metadata:
1258+
name: my-configmap
1259+
remediationAction: inform
1260+
severity: low
1261+
remediationAction: inform
1262+
`
1263+
expected = strings.TrimPrefix(expected, "\n")
1264+
assertEqual(t, output, expected)
1265+
}
1266+
11081267
func TestCreatePolicyFromIamPolicyTypeManifest(t *testing.T) {
11091268
t.Parallel()
11101269
tmpDir := t.TempDir()

internal/types/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type PolicyOptions struct {
2525
GeneratePlacementWhenInSet bool `json:"generatePlacementWhenInSet,omitempty" yaml:"generatePlacementWhenInSet,omitempty"`
2626
PolicySets []string `json:"policySets,omitempty" yaml:"policySets,omitempty"`
2727
PolicyAnnotations map[string]string `json:"policyAnnotations,omitempty" yaml:"policyAnnotations,omitempty"`
28+
PolicyLabels map[string]string `json:"policyLabels,omitempty" yaml:"policyLabels,omitempty"`
2829
ConfigurationPolicyAnnotations map[string]string `json:"configurationPolicyAnnotations,omitempty" yaml:"configurationPolicyAnnotations,omitempty"`
2930
}
3031

0 commit comments

Comments
 (0)