Skip to content

Commit e32fb03

Browse files
dhaiducekopenshift-merge-robot
authored andcommitted
Allow generatePlacement to override placement
Signed-off-by: Dale Haiducek <[email protected]>
1 parent 2b7390c commit e32fb03

File tree

3 files changed

+101
-88
lines changed

3 files changed

+101
-88
lines changed

internal/plugin.go

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -831,8 +831,7 @@ func (p *Plugin) assertValidConfig() error {
831831
}
832832

833833
// Validate default policy placement settings
834-
err := assertValidPlacement(
835-
p.PolicyDefaults.Placement, p.PolicyDefaults.GeneratePolicyPlacement, "policyDefaults", nil)
834+
err := assertValidPlacement(p.PolicyDefaults.Placement, "policyDefaults", nil)
836835
if err != nil {
837836
return err
838837
}
@@ -1062,16 +1061,14 @@ func (p *Plugin) assertValidConfig() error {
10621061
}
10631062
}
10641063

1065-
err := assertValidPlacement(
1066-
policy.Placement, policy.GeneratePolicyPlacement, fmt.Sprintf("policy %s", policy.Name), &plCount)
1064+
err := assertValidPlacement(policy.Placement, fmt.Sprintf("policy %s", policy.Name), &plCount)
10671065
if err != nil {
10681066
return err
10691067
}
10701068
}
10711069

10721070
// Validate default policy set placement settings
1073-
err = assertValidPlacement(
1074-
p.PolicySetDefaults.Placement, p.PolicySetDefaults.GeneratePolicySetPlacement, "policySetDefaults", nil)
1071+
err = assertValidPlacement(p.PolicySetDefaults.Placement, "policySetDefaults", nil)
10751072
if err != nil {
10761073
return err
10771074
}
@@ -1102,8 +1099,7 @@ func (p *Plugin) assertValidConfig() error {
11021099
seenPlcset[plcset.Name] = true
11031100

11041101
// Validate policy set Placement settings
1105-
err := assertValidPlacement(
1106-
plcset.Placement, plcset.GeneratePolicySetPlacement, fmt.Sprintf("policySet %s", plcset.Name), &plCount)
1102+
err := assertValidPlacement(plcset.Placement, fmt.Sprintf("policySet %s", plcset.Name), &plCount)
11071103
if err != nil {
11081104
return err
11091105
}
@@ -1126,7 +1122,6 @@ func (p *Plugin) assertValidConfig() error {
11261122
// assertValidPlacement is a helper for assertValidConfig to verify placement configurations
11271123
func assertValidPlacement(
11281124
placement types.PlacementConfig,
1129-
generatePlacement bool,
11301125
path string,
11311126
plCount *struct {
11321127
plc int
@@ -1151,26 +1146,20 @@ func assertValidPlacement(
11511146
)
11521147
}
11531148

1154-
defaultPlacementOptions := 0
1149+
placementOptionCount := 0
11551150
if len(placement.LabelSelector) != 0 || len(placement.ClusterSelectors) != 0 {
1156-
defaultPlacementOptions++
1151+
placementOptionCount++
11571152
}
11581153

11591154
if placement.PlacementRulePath != "" || placement.PlacementPath != "" {
1160-
defaultPlacementOptions++
1155+
placementOptionCount++
11611156
}
11621157

11631158
if placement.PlacementRuleName != "" || placement.PlacementName != "" {
1164-
defaultPlacementOptions++
1159+
placementOptionCount++
11651160
}
11661161

1167-
if defaultPlacementOptions > 0 && !generatePlacement {
1168-
return fmt.Errorf(
1169-
"%s must not specify a placement when generatePlacement is set to false", path,
1170-
)
1171-
}
1172-
1173-
if defaultPlacementOptions > 1 {
1162+
if placementOptionCount > 1 {
11741163
return fmt.Errorf(
11751164
"%s must specify only one of placement selector, placement path, or placement name", path,
11761165
)

internal/plugin_config_test.go

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -379,74 +379,6 @@ policies:
379379
assertEqual(t, err.Error(), expected)
380380
}
381381

382-
func TestConfigDefaultPlacementWithDisabledPlacement(t *testing.T) {
383-
t.Parallel()
384-
tmpDir := t.TempDir()
385-
createConfigMap(t, tmpDir, "configmap.yaml")
386-
config := fmt.Sprintf(`
387-
apiVersion: policy.open-cluster-management.io/v1
388-
kind: PolicyGenerator
389-
metadata:
390-
name: policy-generator-name
391-
policyDefaults:
392-
namespace: my-policies
393-
generatePolicyPlacement: false
394-
placement:
395-
clusterSelectors:
396-
cloud: red hat
397-
policies:
398-
- name: policy-app-config
399-
manifests:
400-
- path: %s
401-
`,
402-
path.Join(tmpDir, "configmap.yaml"),
403-
)
404-
p := Plugin{}
405-
406-
err := p.Config([]byte(config), tmpDir)
407-
if err == nil {
408-
t.Fatal("Expected an error but did not get one")
409-
}
410-
411-
expected := "policyDefaults must not specify " +
412-
"a placement when generatePlacement is set to false"
413-
assertEqual(t, err.Error(), expected)
414-
}
415-
416-
func TestConfigPlacementWithDisabledPlacement(t *testing.T) {
417-
t.Parallel()
418-
tmpDir := t.TempDir()
419-
createConfigMap(t, tmpDir, "configmap.yaml")
420-
config := fmt.Sprintf(`
421-
apiVersion: policy.open-cluster-management.io/v1
422-
kind: PolicyGenerator
423-
metadata:
424-
name: policy-generator-name
425-
policyDefaults:
426-
namespace: my-policies
427-
placement:
428-
clusterSelectors:
429-
cloud: red hat
430-
policies:
431-
- name: policy-app-config
432-
generatePolicyPlacement: false
433-
manifests:
434-
- path: %s
435-
`,
436-
path.Join(tmpDir, "configmap.yaml"),
437-
)
438-
p := Plugin{}
439-
440-
err := p.Config([]byte(config), tmpDir)
441-
if err == nil {
442-
t.Fatal("Expected an error but did not get one")
443-
}
444-
445-
expected := "policy policy-app-config must not specify " +
446-
"a placement when generatePlacement is set to false"
447-
assertEqual(t, err.Error(), expected)
448-
}
449-
450382
func TestConfigMultiplePlacementsClusterSelectorAndPlRPath(t *testing.T) {
451383
t.Parallel()
452384
tmpDir := t.TempDir()

internal/plugin_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ func TestGeneratePolicyDisablePlacement(t *testing.T) {
306306

307307
p.PolicyDefaults.Namespace = "my-policies"
308308
p.PolicyDefaults.MetadataComplianceType = "musthave"
309+
p.PolicyDefaults.Placement.PlacementName = "my-placement"
309310
policyConf := types.PolicyConfig{
310311
Name: "policy-app-config",
311312
Manifests: []types.Manifest{
@@ -373,6 +374,97 @@ spec:
373374
assertEqual(t, string(output), expected)
374375
}
375376

377+
func TestGeneratePolicyDisablePlacementOverride(t *testing.T) {
378+
t.Parallel()
379+
tmpDir := t.TempDir()
380+
createConfigMap(t, tmpDir, "configmap.yaml")
381+
382+
p := Plugin{}
383+
var err error
384+
385+
p.baseDirectory, err = filepath.EvalSymlinks(tmpDir)
386+
if err != nil {
387+
t.Fatal(err.Error())
388+
}
389+
390+
p.PolicyDefaults.Namespace = "my-policies"
391+
p.PolicyDefaults.MetadataComplianceType = "musthave"
392+
p.PolicyDefaults.Placement.PlacementName = "my-placement"
393+
policyConf := types.PolicyConfig{
394+
Name: "policy-app-config",
395+
Manifests: []types.Manifest{
396+
{
397+
Path: path.Join(tmpDir, "configmap.yaml"),
398+
},
399+
},
400+
PolicyOptions: types.PolicyOptions{
401+
GeneratePolicyPlacement: false,
402+
Placement: types.PlacementConfig{
403+
PlacementName: "my-placement",
404+
},
405+
},
406+
}
407+
p.Policies = append(p.Policies, policyConf)
408+
p.applyDefaults(map[string]interface{}{
409+
"policies": []interface{}{
410+
map[string]interface{}{
411+
"generatePolicyPlacement": false,
412+
},
413+
},
414+
})
415+
assertEqual(t, p.Policies[0].GeneratePolicyPlacement, false)
416+
// Default all policy ConsolidateManifests flags are set to true
417+
// unless explicitly set
418+
assertEqual(t, p.Policies[0].ConsolidateManifests, true)
419+
420+
if err := p.assertValidConfig(); err != nil {
421+
t.Fatal(err.Error())
422+
}
423+
424+
expected := `
425+
---
426+
apiVersion: policy.open-cluster-management.io/v1
427+
kind: Policy
428+
metadata:
429+
annotations:
430+
policy.open-cluster-management.io/categories: CM Configuration Management
431+
policy.open-cluster-management.io/controls: CM-2 Baseline Configuration
432+
policy.open-cluster-management.io/standards: NIST SP 800-53
433+
name: policy-app-config
434+
namespace: my-policies
435+
spec:
436+
disabled: false
437+
policy-templates:
438+
- objectDefinition:
439+
apiVersion: policy.open-cluster-management.io/v1
440+
kind: ConfigurationPolicy
441+
metadata:
442+
name: policy-app-config
443+
spec:
444+
object-templates:
445+
- complianceType: musthave
446+
metadataComplianceType: musthave
447+
objectDefinition:
448+
apiVersion: v1
449+
data:
450+
game.properties: enemies=potato
451+
kind: ConfigMap
452+
metadata:
453+
name: my-configmap
454+
remediationAction: inform
455+
severity: low
456+
remediationAction: inform
457+
`
458+
expected = strings.TrimPrefix(expected, "\n")
459+
460+
output, err := p.Generate()
461+
if err != nil {
462+
t.Fatal(err.Error())
463+
}
464+
465+
assertEqual(t, string(output), expected)
466+
}
467+
376468
func TestGeneratePolicyExistingPlacementRuleName(t *testing.T) {
377469
t.Parallel()
378470
tmpDir := t.TempDir()

0 commit comments

Comments
 (0)