Merge pull request #2758 from RainbowMango/pr_pp_priority

Introduce priority to PropagationPolicy
This commit is contained in:
karmada-bot 2022-11-11 11:57:55 +08:00 committed by GitHub
commit 5c88cba7d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 159 additions and 0 deletions

View File

@ -15361,6 +15361,11 @@
"default": {},
"$ref": "#/definitions/com.github.karmada-io.karmada.pkg.apis.policy.v1alpha1.Placement"
},
"priority": {
"description": "Priority indicates the importance of a policy(PropagationPolicy or ClusterPropagationPolicy). A policy will be applied for the matched resource templates if there is no other policies with higher priority at the point of the resource template be processed. Once a resource template has been claimed by a policy, by default it will not be preempted by following policies even with a higher priority.\n\nIn case of two policies have the same priority, the one with a more precise matching rules in ResourceSelectors wins: - matching by name(resourceSelector.name) has higher priority than\n by selector(resourceSelector.labelSelector)\n- matching by selector(resourceSelector.labelSelector) has higher priority\n than by APIVersion(resourceSelector.apiVersion) and Kind(resourceSelector.kind).\nIf there is still no winner at this point, the one with the lower alphabetic order wins, e.g. policy 'bar' has higher priority than 'foo'.\n\nThe higher the value, the higher the priority. Defaults to zero.",
"type": "integer",
"format": "int32"
},
"propagateDeps": {
"description": "PropagateDeps tells if relevant resources should be propagated automatically. Take 'Deployment' which referencing 'ConfigMap' and 'Secret' as an example, when 'propagateDeps' is 'true', the referencing resources could be omitted(for saving config effort) from 'resourceSelectors' as they will be propagated along with the Deployment. In addition to the propagating process, the referencing resources will be migrated along with the Deployment in the fail-over scenario.\n\nDefaults to false.",
"type": "boolean"

View File

@ -426,6 +426,25 @@ spec:
type: object
type: array
type: object
priority:
default: 0
description: "Priority indicates the importance of a policy(PropagationPolicy
or ClusterPropagationPolicy). A policy will be applied for the matched
resource templates if there is no other policies with higher priority
at the point of the resource template be processed. Once a resource
template has been claimed by a policy, by default it will not be
preempted by following policies even with a higher priority. \n
In case of two policies have the same priority, the one with a more
precise matching rules in ResourceSelectors wins: - matching by
name(resourceSelector.name) has higher priority than by selector(resourceSelector.labelSelector)
- matching by selector(resourceSelector.labelSelector) has higher
priority than by APIVersion(resourceSelector.apiVersion) and Kind(resourceSelector.kind).
If there is still no winner at this point, the one with the lower
alphabetic order wins, e.g. policy 'bar' has higher priority than
'foo'. \n The higher the value, the higher the priority. Defaults
to zero."
format: int32
type: integer
propagateDeps:
description: "PropagateDeps tells if relevant resources should be
propagated automatically. Take 'Deployment' which referencing 'ConfigMap'

View File

@ -422,6 +422,25 @@ spec:
type: object
type: array
type: object
priority:
default: 0
description: "Priority indicates the importance of a policy(PropagationPolicy
or ClusterPropagationPolicy). A policy will be applied for the matched
resource templates if there is no other policies with higher priority
at the point of the resource template be processed. Once a resource
template has been claimed by a policy, by default it will not be
preempted by following policies even with a higher priority. \n
In case of two policies have the same priority, the one with a more
precise matching rules in ResourceSelectors wins: - matching by
name(resourceSelector.name) has higher priority than by selector(resourceSelector.labelSelector)
- matching by selector(resourceSelector.labelSelector) has higher
priority than by APIVersion(resourceSelector.apiVersion) and Kind(resourceSelector.kind).
If there is still no winner at this point, the one with the lower
alphabetic order wins, e.g. policy 'bar' has higher priority than
'foo'. \n The higher the value, the higher the priority. Defaults
to zero."
format: int32
type: integer
propagateDeps:
description: "PropagateDeps tells if relevant resources should be
propagated automatically. Take 'Deployment' which referencing 'ConfigMap'

View File

@ -0,0 +1,20 @@
package v1alpha1
// ExplicitPriority returns the explicit priority declared
// by '.spec.Priority'.
func (p *PropagationSpec) ExplicitPriority() int32 {
if p.Priority == nil {
return 0
}
return *p.Priority
}
// ExplicitPriority returns the explicit priority of a PropagationPolicy.
func (p *PropagationPolicy) ExplicitPriority() int32 {
return p.Spec.ExplicitPriority()
}
// ExplicitPriority returns the explicit priority of a ClusterPropagationPolicy.
func (p *ClusterPropagationPolicy) ExplicitPriority() int32 {
return p.Spec.ExplicitPriority()
}

View File

@ -0,0 +1,63 @@
package v1alpha1
import (
"testing"
"k8s.io/utils/pointer"
)
func TestPropagationPolicy_ExplicitPriority(t *testing.T) {
var tests = []struct {
name string
declaredPriority *int32
expectedPriority int32
}{
{
name: "no priority declared should defaults to zero",
expectedPriority: 0,
},
{
name: "no priority declared should defaults to zero",
declaredPriority: pointer.Int32Ptr(20),
expectedPriority: 20,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
policy := PropagationPolicy{Spec: PropagationSpec{Priority: test.declaredPriority}}
got := policy.ExplicitPriority()
if test.expectedPriority != got {
t.Fatalf("Expected%d, but got: %d", test.expectedPriority, got)
}
})
}
}
func TestClusterPropagationPolicy_ExplicitPriority(t *testing.T) {
var tests = []struct {
name string
declaredPriority *int32
expectedPriority int32
}{
{
name: "no priority declared should defaults to zero",
expectedPriority: 0,
},
{
name: "no priority declared should defaults to zero",
declaredPriority: pointer.Int32Ptr(20),
expectedPriority: 20,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
policy := ClusterPropagationPolicy{Spec: PropagationSpec{Priority: test.declaredPriority}}
got := policy.ExplicitPriority()
if test.expectedPriority != got {
t.Fatalf("Expected%d, but got: %d", test.expectedPriority, got)
}
})
}
}

View File

@ -70,6 +70,27 @@ type PropagationSpec struct {
// +optional
Placement Placement `json:"placement,omitempty"`
// Priority indicates the importance of a policy(PropagationPolicy or ClusterPropagationPolicy).
// A policy will be applied for the matched resource templates if there is
// no other policies with higher priority at the point of the resource
// template be processed.
// Once a resource template has been claimed by a policy, by default it will
// not be preempted by following policies even with a higher priority.
//
// In case of two policies have the same priority, the one with a more precise
// matching rules in ResourceSelectors wins:
// - matching by name(resourceSelector.name) has higher priority than
// by selector(resourceSelector.labelSelector)
// - matching by selector(resourceSelector.labelSelector) has higher priority
// than by APIVersion(resourceSelector.apiVersion) and Kind(resourceSelector.kind).
// If there is still no winner at this point, the one with the lower alphabetic
// order wins, e.g. policy 'bar' has higher priority than 'foo'.
//
// The higher the value, the higher the priority. Defaults to zero.
// +optional
// +kubebuilder:default=0
Priority *int32 `json:"priority,omitempty"`
// DependentOverrides represents the list of overrides(OverridePolicy)
// which must present before the current PropagationPolicy takes effect.
//

View File

@ -719,6 +719,11 @@ func (in *PropagationSpec) DeepCopyInto(out *PropagationSpec) {
}
}
in.Placement.DeepCopyInto(&out.Placement)
if in.Priority != nil {
in, out := &in.Priority, &out.Priority
*out = new(int32)
**out = **in
}
if in.DependentOverrides != nil {
in, out := &in.DependentOverrides, &out.DependentOverrides
*out = make([]string, len(*in))

View File

@ -3326,6 +3326,13 @@ func schema_pkg_apis_policy_v1alpha1_PropagationSpec(ref common.ReferenceCallbac
Ref: ref("github.com/karmada-io/karmada/pkg/apis/policy/v1alpha1.Placement"),
},
},
"priority": {
SchemaProps: spec.SchemaProps{
Description: "Priority indicates the importance of a policy(PropagationPolicy or ClusterPropagationPolicy). A policy will be applied for the matched resource templates if there is no other policies with higher priority at the point of the resource template be processed. Once a resource template has been claimed by a policy, by default it will not be preempted by following policies even with a higher priority.\n\nIn case of two policies have the same priority, the one with a more precise matching rules in ResourceSelectors wins: - matching by name(resourceSelector.name) has higher priority than\n by selector(resourceSelector.labelSelector)\n- matching by selector(resourceSelector.labelSelector) has higher priority\n than by APIVersion(resourceSelector.apiVersion) and Kind(resourceSelector.kind).\nIf there is still no winner at this point, the one with the lower alphabetic order wins, e.g. policy 'bar' has higher priority than 'foo'.\n\nThe higher the value, the higher the priority. Defaults to zero.",
Type: []string{"integer"},
Format: "int32",
},
},
"dependentOverrides": {
SchemaProps: spec.SchemaProps{
Description: "DependentOverrides represents the list of overrides(OverridePolicy) which must present before the current PropagationPolicy takes effect.\n\nIt used to explicitly specify overrides which current PropagationPolicy rely on. A typical scenario is the users create OverridePolicy(ies) and resources at the same time, they want to ensure the new-created policies would be adopted.\n\nNote: For the overrides, OverridePolicy(ies) in current namespace and ClusterOverridePolicy(ies), which not present in this list will still be applied if they matches the resources.",