Skip to content

Commit ed4f32c

Browse files
Add field to set labels on the Policy generated
Signed-off-by: Brian Jarvis <[email protected]>
1 parent b997c92 commit ed4f32c

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed

docs/policygenerator-reference.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ policyDefaults:
184184
# Optional. Annotations that the policy will include under its metadata.annotations. It will be applied for all
185185
# policies unless specified in the policy.
186186
policyAnnotations: {}
187+
# Optional. Labels that the policy will include under its metadata.labels. It will be applied for all
188+
# policies unless specified in the policy.
189+
policyLabels: {}
187190

188191
# Optional. Defaults for policy set generation. Any default value listed here can be overridden under an entry in the
189192
# policySets array.
@@ -319,6 +322,8 @@ policies:
319322
# Optional. Annotations that the policy will include under its metadata.annotations. It will overwrite the
320323
# policyAnnotation defined in the policyDefaults.
321324
policyAnnotations: {}
325+
# Optional. (See policyDefaults.policyLabels for description.)
326+
policyLabels: {}
322327

323328
# Optional. The list of policy sets to create. To include a policy in a policy set, use policies[*].policySets or
324329
# 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
}
@@ -1300,6 +1309,10 @@ func (p *Plugin) createPolicy(policyConf *types.PolicyConfig) error {
13001309
policyConf.PolicyAnnotations = map[string]string{}
13011310
}
13021311

1312+
if policyConf.PolicyLabels == nil {
1313+
policyConf.PolicyLabels = map[string]string{}
1314+
}
1315+
13031316
policyConf.PolicyAnnotations["policy.open-cluster-management.io/categories"] = strings.Join(
13041317
policyConf.Categories, ",",
13051318
)
@@ -1349,6 +1362,10 @@ func (p *Plugin) createPolicy(policyConf *types.PolicyConfig) error {
13491362
"spec": spec,
13501363
}
13511364

1365+
if len(policyConf.PolicyLabels) != 0 {
1366+
policy["metadata"].(map[string]interface{})["labels"] = policyConf.PolicyLabels
1367+
}
1368+
13521369
// set the root policy remediation action if all the remediation actions match
13531370
if rootRemediationAction := getRootRemediationAction(policyTemplates); rootRemediationAction != "" {
13541371
policy["spec"].(map[string]interface{})["remediationAction"] = rootRemediationAction

internal/plugin_test.go

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,168 @@ spec:
11171117
assertEqual(t, output, expected)
11181118
}
11191119

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

internal/types/types.go

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

0 commit comments

Comments
 (0)