Skip to content

Commit b0245e2

Browse files
mprahlopenshift-merge-robot
authored andcommitted
Add support for the copyPolicyMetadata field
Relates: https://issues.redhat.com/browse/ACM-1690 Signed-off-by: mprahl <[email protected]>
1 parent b25ff84 commit b0245e2

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

docs/policygenerator-reference.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ policyDefaults:
3131
# Optional. This determines if a single configuration policy should be generated for all the manifests being wrapped
3232
# in the policy. If set to false, a configuration policy per manifest will be generated. This defaults to true.
3333
consolidateManifests: true
34+
# Optional. If set to true (default), all the policy's labels and annotations will be copied to the replicated policy.
35+
# If set to false, only the policy framework specific policy labels and annotations will be copied to the replicated
36+
# policy.
37+
copyPolicyMetadata: true
3438
# Optional. A list of objects that should be in specific compliance states before this policy is applied. Cannot be
3539
# specified when policyDefaults.orderPolicies is set to true.
3640
dependencies:
@@ -240,6 +244,8 @@ policies:
240244
complianceType: "musthave"
241245
# Optional. (See policyDefaults.configurationPolicyAnnotations for description.)
242246
configurationPolicyAnnotations: {}
247+
# Optional. (See policyDefaults.copyPolicyMetadata for description.)
248+
copyPolicyMetadata: true
243249
# Optional. (See policyDefaults.metadataComplianceType for description.)
244250
metadataComplianceType: ""
245251
# Optional. (See policyDefaults.controls for description.)

internal/plugin.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,13 @@ func (p *Plugin) applyDefaults(unmarshaledConfig map[string]interface{}) {
432432
p.PolicyDefaults.Controls = defaults.Controls
433433
}
434434

435+
cpmValue, setCPM := getPolicyDefaultBool(unmarshaledConfig, "copyPolicyMetadata")
436+
if setCPM {
437+
p.PolicyDefaults.CopyPolicyMetadata = cpmValue
438+
} else {
439+
p.PolicyDefaults.CopyPolicyMetadata = true
440+
}
441+
435442
// Policy expanders default to true unless explicitly set in the config.
436443
// Gatekeeper policy expander policyDefault
437444
igvValue, setIgv := getPolicyDefaultBool(unmarshaledConfig, "informGatekeeperPolicies")
@@ -523,6 +530,13 @@ func (p *Plugin) applyDefaults(unmarshaledConfig map[string]interface{}) {
523530
policy.ConfigurationPolicyAnnotations = annotations
524531
}
525532

533+
cpmValue, setCpm := getPolicyBool(unmarshaledConfig, i, "copyPolicyMetadata")
534+
if setCpm {
535+
policy.CopyPolicyMetadata = cpmValue
536+
} else {
537+
policy.CopyPolicyMetadata = p.PolicyDefaults.CopyPolicyMetadata
538+
}
539+
526540
if policy.Standards == nil {
527541
policy.Standards = p.PolicyDefaults.Standards
528542
}
@@ -1307,6 +1321,12 @@ func (p *Plugin) createPolicy(policyConf *types.PolicyConfig) error {
13071321
spec["dependencies"] = policyConf.Dependencies
13081322
}
13091323

1324+
// When copyPolicyMetadata is unset, it defaults to the behavior of true, so this leaves it out entirely when set to
1325+
// true to avoid unnecessarily including it in the Policy YAML.
1326+
if !policyConf.CopyPolicyMetadata {
1327+
spec["copyPolicyMetadata"] = false
1328+
}
1329+
13101330
policy := map[string]interface{}{
13111331
"apiVersion": policyAPIVersion,
13121332
"kind": policyKind,

internal/plugin_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3593,3 +3593,82 @@ spec:
35933593
expected = strings.TrimPrefix(expected, "\n")
35943594
assertEqual(t, output, expected)
35953595
}
3596+
3597+
func TestCreatePolicyWithCopyPolicyMetadata(t *testing.T) {
3598+
t.Parallel()
3599+
tmpDir := t.TempDir()
3600+
createConfigMap(t, tmpDir, "configmap.yaml")
3601+
3602+
bTrue := true
3603+
bFalse := false
3604+
3605+
tests := []struct {
3606+
name string
3607+
copyPolicyMetadata *bool
3608+
expected *bool
3609+
}{
3610+
{name: "unset", copyPolicyMetadata: nil, expected: nil},
3611+
{name: "true", copyPolicyMetadata: &bTrue, expected: nil},
3612+
{name: "false", copyPolicyMetadata: &bFalse, expected: &bFalse},
3613+
}
3614+
3615+
for _, mode := range []string{"policyDefault", "policy"} {
3616+
mode := mode
3617+
3618+
for _, test := range tests {
3619+
test := test
3620+
t.Run(mode+" "+test.name, func(t *testing.T) {
3621+
t.Parallel()
3622+
3623+
p := Plugin{}
3624+
p.PolicyDefaults.Namespace = "my-policies"
3625+
policyConf := types.PolicyConfig{
3626+
Name: "policy-app-config", Manifests: []types.Manifest{
3627+
{Path: path.Join(tmpDir, "configmap.yaml")},
3628+
},
3629+
}
3630+
3631+
policyDefaultsUnmarshaled := map[string]interface{}{}
3632+
policyUnmarshaled := map[string]interface{}{}
3633+
3634+
if test.copyPolicyMetadata != nil {
3635+
if mode == "policyDefault" {
3636+
policyDefaultsUnmarshaled["copyPolicyMetadata"] = *test.copyPolicyMetadata
3637+
} else if mode == "policy" {
3638+
policyUnmarshaled["copyPolicyMetadata"] = *test.copyPolicyMetadata
3639+
}
3640+
}
3641+
3642+
p.Policies = append(p.Policies, policyConf)
3643+
p.applyDefaults(
3644+
map[string]interface{}{
3645+
"policyDefaults": policyDefaultsUnmarshaled,
3646+
"policies": []interface{}{policyUnmarshaled},
3647+
},
3648+
)
3649+
3650+
err := p.createPolicy(&p.Policies[0])
3651+
if err != nil {
3652+
t.Fatal(err.Error())
3653+
}
3654+
3655+
output := p.outputBuffer.Bytes()
3656+
policyManifests, err := unmarshalManifestBytes(output)
3657+
if err != nil {
3658+
t.Fatal(err.Error())
3659+
}
3660+
3661+
// nolint: forcetypeassert
3662+
spec := policyManifests[0]["spec"].(map[string]interface{})
3663+
3664+
if test.expected == nil {
3665+
if _, set := spec["copyPolicyMetadata"]; set {
3666+
t.Fatal("Expected the policy's spec.copyPolicyMetadata to be unset")
3667+
}
3668+
} else {
3669+
assertEqual(t, spec["copyPolicyMetadata"], *test.expected)
3670+
}
3671+
})
3672+
}
3673+
}
3674+
}

internal/types/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
type PolicyOptions struct {
1111
Categories []string `json:"categories,omitempty" yaml:"categories,omitempty"`
1212
Controls []string `json:"controls,omitempty" yaml:"controls,omitempty"`
13+
CopyPolicyMetadata bool `json:"copyPolicyMetadata,omitempty" yaml:"copyPolicyMetadata,omitempty"`
1314
Dependencies []PolicyDependency `json:"dependencies,omitempty" yaml:"dependencies,omitempty"`
1415
ExtraDependencies []PolicyDependency `json:"extraDependencies,omitempty" yaml:"extraDependencies,omitempty"`
1516
Placement PlacementConfig `json:"placement,omitempty" yaml:"placement,omitempty"`

0 commit comments

Comments
 (0)