Skip to content

Commit 9f73ae4

Browse files
authored
Add ability to specify the name of an existing Placement or PlacementRule (#51)
Signed-off-by: Brian Jarvis <[email protected]>
1 parent 6960e27 commit 9f73ae4

File tree

5 files changed

+532
-34
lines changed

5 files changed

+532
-34
lines changed

docs/policygenerator-reference.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ policyDefaults:
6565
# kustomization.yaml file. If given, this placement rule will be used by all policies by
6666
# default. (See clusterSelectors to generate a new PlacementRule instead.)
6767
placementRulePath: ""
68+
# Use a placement that already exists in the cluster in the same namespace as the policy to be
69+
# generated. It is the responsibility of the administrator to ensure the placement exists. Use
70+
# of this setting will prevent a Placement from being generated, but the Placement Binding will
71+
# still be created.
72+
placementName: ""
73+
# Use a placement rule that already exists in the cluster in the same namespace as the policy to
74+
# be generated. It is the responsibility of the administrator to ensure the placement rule
75+
# exists. Use of this setting will prevent a placement rule from being generated, but the
76+
# placement binding will still be created.
77+
placementRuleName: ""
78+
79+
6880
# Optional. The remediation action ("inform" or "enforce") for each configuration policy. This
6981
# defaults to "inform".
7082
remediationAction: "inform"

internal/plugin.go

Lines changed: 94 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -473,22 +473,33 @@ func (p *Plugin) applyDefaults(unmarshaledConfig map[string]interface{}) {
473473
}
474474

475475
// Determine whether defaults are set for placement
476-
plcDefaultSet := len(p.PolicyDefaults.Placement.LabelSelector) != 0 || p.PolicyDefaults.Placement.PlacementPath != ""
477-
plrDefaultSet := len(p.PolicyDefaults.Placement.ClusterSelectors) != 0 || p.PolicyDefaults.Placement.PlacementRulePath != ""
478-
479-
// If both cluster label selectors and placement path aren't set, then use the defaults with a
480-
// priority on placement path.
481-
if len(policy.Placement.LabelSelector) == 0 && policy.Placement.PlacementPath == "" && plcDefaultSet {
476+
plcDefaultSet := len(p.PolicyDefaults.Placement.LabelSelector) != 0 ||
477+
p.PolicyDefaults.Placement.PlacementPath != "" ||
478+
p.PolicyDefaults.Placement.PlacementName != ""
479+
plrDefaultSet := len(p.PolicyDefaults.Placement.ClusterSelectors) != 0 ||
480+
p.PolicyDefaults.Placement.PlacementRulePath != "" ||
481+
p.PolicyDefaults.Placement.PlacementRuleName != ""
482+
483+
// If both cluster label selectors and placement path/name aren't set, then use the defaults with a
484+
// priority on placement path followed by placement name.
485+
if len(policy.Placement.LabelSelector) == 0 && policy.Placement.PlacementPath == "" && policy.Placement.PlacementName == "" && plcDefaultSet {
482486
if p.PolicyDefaults.Placement.PlacementPath != "" {
483487
policy.Placement.PlacementPath = p.PolicyDefaults.Placement.PlacementPath
488+
} else if p.PolicyDefaults.Placement.PlacementName != "" {
489+
policy.Placement.PlacementName = p.PolicyDefaults.Placement.PlacementName
484490
} else if len(p.PolicyDefaults.Placement.LabelSelector) > 0 {
485491
policy.Placement.LabelSelector = p.PolicyDefaults.Placement.LabelSelector
486492
}
487-
// Else if both cluster selectors and placement rule path aren't set, then use the defaults with a
488-
// priority on placement rule path.
489-
} else if len(policy.Placement.ClusterSelectors) == 0 && policy.Placement.PlacementRulePath == "" && plrDefaultSet {
493+
// Else if both cluster selectors and placement rule path/name aren't set, then use the defaults with a
494+
// priority on placement rule path followed by placement rule name.
495+
} else if len(policy.Placement.ClusterSelectors) == 0 &&
496+
policy.Placement.PlacementRulePath == "" &&
497+
policy.Placement.PlacementRuleName == "" &&
498+
plrDefaultSet {
490499
if p.PolicyDefaults.Placement.PlacementRulePath != "" {
491500
policy.Placement.PlacementRulePath = p.PolicyDefaults.Placement.PlacementRulePath
501+
} else if p.PolicyDefaults.Placement.PlacementRuleName != "" {
502+
policy.Placement.PlacementRuleName = p.PolicyDefaults.Placement.PlacementRuleName
492503
} else if len(p.PolicyDefaults.Placement.ClusterSelectors) > 0 {
493504
policy.Placement.ClusterSelectors = p.PolicyDefaults.Placement.ClusterSelectors
494505
}
@@ -598,10 +609,25 @@ func (p *Plugin) assertValidConfig() error {
598609
"policyDefaults must provide only one of placement.labelSelector or placement.clusterSelectors",
599610
)
600611
}
601-
if (len(p.PolicyDefaults.Placement.LabelSelector) != 0 || len(p.PolicyDefaults.Placement.ClusterSelectors) != 0) &&
602-
(p.PolicyDefaults.Placement.PlacementRulePath != "" || p.PolicyDefaults.Placement.PlacementPath != "") {
612+
if p.PolicyDefaults.Placement.PlacementRuleName != "" && p.PolicyDefaults.Placement.PlacementName != "" {
603613
return errors.New(
604-
"policyDefaults may not specify a placement selector and placement path together",
614+
"policyDefaults must provide only one of placement.placementName or placement.placementRuleName",
615+
)
616+
}
617+
618+
defaultPlacementOptions := 0
619+
if len(p.PolicyDefaults.Placement.LabelSelector) != 0 || len(p.PolicyDefaults.Placement.ClusterSelectors) != 0 {
620+
defaultPlacementOptions++
621+
}
622+
if p.PolicyDefaults.Placement.PlacementRulePath != "" || p.PolicyDefaults.Placement.PlacementPath != "" {
623+
defaultPlacementOptions++
624+
}
625+
if p.PolicyDefaults.Placement.PlacementRuleName != "" || p.PolicyDefaults.Placement.PlacementName != "" {
626+
defaultPlacementOptions++
627+
}
628+
if defaultPlacementOptions > 1 {
629+
return errors.New(
630+
"policyDefaults must specify only one of placement selector, placement path, or placement name",
605631
)
606632
}
607633

@@ -722,18 +748,35 @@ func (p *Plugin) assertValidConfig() error {
722748
)
723749
}
724750

751+
if policy.Placement.PlacementRuleName != "" && policy.Placement.PlacementName != "" {
752+
return fmt.Errorf(
753+
"policy %s must provide only one of placementRuleName or placementName", policy.Name,
754+
)
755+
}
756+
725757
if len(policy.Placement.ClusterSelectors) > 0 && len(policy.Placement.LabelSelector) > 0 {
726758
return fmt.Errorf(
727759
"policy %s must provide only one of placement.labelSelector or placement.clusterselectors",
728760
policy.Name,
729761
)
730762
}
731-
if (len(policy.Placement.ClusterSelectors) != 0 || len(policy.Placement.LabelSelector) != 0) &&
732-
(policy.Placement.PlacementRulePath != "" || policy.Placement.PlacementPath != "") {
763+
764+
policyPlacementOptions := 0
765+
if len(policy.Placement.LabelSelector) != 0 || len(policy.Placement.ClusterSelectors) != 0 {
766+
policyPlacementOptions++
767+
}
768+
if policy.Placement.PlacementRulePath != "" || policy.Placement.PlacementPath != "" {
769+
policyPlacementOptions++
770+
}
771+
if policy.Placement.PlacementRuleName != "" || policy.Placement.PlacementName != "" {
772+
policyPlacementOptions++
773+
}
774+
if policyPlacementOptions > 1 {
733775
return fmt.Errorf(
734-
"policy %s may not specify a placement selector and placement path together", policy.Name,
776+
"policy %s must specify only one of placement selector, placement path, or placement name", policy.Name,
735777
)
736778
}
779+
737780
if policy.Placement.PlacementRulePath != "" {
738781
_, err := os.Stat(policy.Placement.PlacementRulePath)
739782
if err != nil {
@@ -754,11 +797,11 @@ func (p *Plugin) assertValidConfig() error {
754797
}
755798

756799
foundPl := false
757-
if len(policy.Placement.LabelSelector) != 0 || policy.Placement.PlacementPath != "" {
800+
if len(policy.Placement.LabelSelector) != 0 || policy.Placement.PlacementPath != "" || policy.Placement.PlacementName != "" {
758801
plCount.plc++
759802
foundPl = true
760803
}
761-
if len(policy.Placement.ClusterSelectors) != 0 || policy.Placement.PlacementRulePath != "" {
804+
if len(policy.Placement.ClusterSelectors) != 0 || policy.Placement.PlacementRulePath != "" || policy.Placement.PlacementRuleName != "" {
762805
plCount.plr++
763806
if foundPl {
764807
return fmt.Errorf(
@@ -792,18 +835,35 @@ func (p *Plugin) assertValidConfig() error {
792835
)
793836
}
794837

838+
if plcset.Placement.PlacementRuleName != "" && plcset.Placement.PlacementName != "" {
839+
return fmt.Errorf(
840+
"policySet %s must provide only one of placementRuleName or placementName", plcset.Name,
841+
)
842+
}
843+
795844
if len(plcset.Placement.ClusterSelectors) > 0 && len(plcset.Placement.LabelSelector) > 0 {
796845
return fmt.Errorf(
797846
"policySet %s must provide only one of placement.labelSelector or placement.clusterselectors",
798847
plcset.Name,
799848
)
800849
}
801-
if (len(plcset.Placement.ClusterSelectors) != 0 || len(plcset.Placement.LabelSelector) != 0) &&
802-
(plcset.Placement.PlacementRulePath != "" || plcset.Placement.PlacementPath != "") {
850+
851+
policySetPlacementOptions := 0
852+
if len(plcset.Placement.LabelSelector) != 0 || len(plcset.Placement.ClusterSelectors) != 0 {
853+
policySetPlacementOptions++
854+
}
855+
if plcset.Placement.PlacementRulePath != "" || plcset.Placement.PlacementPath != "" {
856+
policySetPlacementOptions++
857+
}
858+
if plcset.Placement.PlacementRuleName != "" || plcset.Placement.PlacementName != "" {
859+
policySetPlacementOptions++
860+
}
861+
if policySetPlacementOptions > 1 {
803862
return fmt.Errorf(
804-
"policySet %s may not specify a placement selector and placement path together", plcset.Name,
863+
"policySet %s must specify only one of placement selector, placement path, or placement name", plcset.Name,
805864
)
806865
}
866+
807867
if plcset.Placement.PlacementRulePath != "" {
808868
_, err := os.Stat(plcset.Placement.PlacementRulePath)
809869
if err != nil {
@@ -824,11 +884,11 @@ func (p *Plugin) assertValidConfig() error {
824884
}
825885

826886
foundPl := false
827-
if len(plcset.Placement.LabelSelector) != 0 || plcset.Placement.PlacementPath != "" {
887+
if len(plcset.Placement.LabelSelector) != 0 || plcset.Placement.PlacementPath != "" || plcset.Placement.PlacementName != "" {
828888
plCount.plc++
829889
foundPl = true
830890
}
831-
if len(plcset.Placement.ClusterSelectors) != 0 || plcset.Placement.PlacementRulePath != "" {
891+
if len(plcset.Placement.ClusterSelectors) != 0 || plcset.Placement.PlacementRulePath != "" || plcset.Placement.PlacementRuleName != "" {
832892
plCount.plr++
833893
if foundPl {
834894
return fmt.Errorf(
@@ -1039,6 +1099,18 @@ func (p *Plugin) getPlcName(placementConfig *types.PlacementConfig, nameDefault
10391099
func (p *Plugin) createPlacement(placementConfig *types.PlacementConfig, nameDefault string) (
10401100
name string, err error,
10411101
) {
1102+
// If a placementName or placementRuleName is defined just return it
1103+
if placementConfig.PlacementName != "" {
1104+
name = placementConfig.PlacementName
1105+
1106+
return
1107+
}
1108+
if placementConfig.PlacementRuleName != "" {
1109+
name = placementConfig.PlacementRuleName
1110+
1111+
return
1112+
}
1113+
10421114
plrPath := placementConfig.PlacementRulePath
10431115
plcPath := placementConfig.PlacementPath
10441116
var placement map[string]interface{}

0 commit comments

Comments
 (0)