From 7a3207c8ee099df64467cbda2388870274f2904c Mon Sep 17 00:00:00 2001 From: chaunceyjiang Date: Sat, 31 Dec 2022 16:19:04 +0800 Subject: [PATCH] fix: set the default value of replicaSchedulengType Signed-off-by: chaunceyjiang --- .../policy.karmada.io_clusterpropagationpolicies.yaml | 1 + .../bases/policy.karmada.io_propagationpolicies.yaml | 1 + pkg/apis/policy/v1alpha1/propagation_types.go | 1 + pkg/util/helper/policy.go | 8 ++++++++ pkg/webhook/clusterpropagationpolicy/mutating.go | 3 +++ pkg/webhook/propagationpolicy/mutating.go | 3 +++ 6 files changed, 17 insertions(+) diff --git a/charts/karmada/_crds/bases/policy.karmada.io_clusterpropagationpolicies.yaml b/charts/karmada/_crds/bases/policy.karmada.io_clusterpropagationpolicies.yaml index d12a6a399..ffb77da02 100644 --- a/charts/karmada/_crds/bases/policy.karmada.io_clusterpropagationpolicies.yaml +++ b/charts/karmada/_crds/bases/policy.karmada.io_clusterpropagationpolicies.yaml @@ -228,6 +228,7 @@ spec: - Weighted type: string replicaSchedulingType: + default: Divided description: ReplicaSchedulingType determines how the replicas is scheduled when karmada propagating a resource. Valid options are Duplicated and Divided. "Duplicated" duplicates diff --git a/charts/karmada/_crds/bases/policy.karmada.io_propagationpolicies.yaml b/charts/karmada/_crds/bases/policy.karmada.io_propagationpolicies.yaml index 158aff102..c8b938c8c 100644 --- a/charts/karmada/_crds/bases/policy.karmada.io_propagationpolicies.yaml +++ b/charts/karmada/_crds/bases/policy.karmada.io_propagationpolicies.yaml @@ -224,6 +224,7 @@ spec: - Weighted type: string replicaSchedulingType: + default: Divided description: ReplicaSchedulingType determines how the replicas is scheduled when karmada propagating a resource. Valid options are Duplicated and Divided. "Duplicated" duplicates diff --git a/pkg/apis/policy/v1alpha1/propagation_types.go b/pkg/apis/policy/v1alpha1/propagation_types.go index 70e94328e..64deabe45 100644 --- a/pkg/apis/policy/v1alpha1/propagation_types.go +++ b/pkg/apis/policy/v1alpha1/propagation_types.go @@ -255,6 +255,7 @@ type ReplicaSchedulingStrategy struct { // "Divided" divides replicas into parts according to number of valid candidate member // clusters, and exact replicas for each cluster are determined by ReplicaDivisionPreference. // +kubebuilder:validation:Enum=Duplicated;Divided + // +kubebuilder:default=Divided // +optional ReplicaSchedulingType ReplicaSchedulingType `json:"replicaSchedulingType,omitempty"` diff --git a/pkg/util/helper/policy.go b/pkg/util/helper/policy.go index 4867f6c93..914f26f44 100644 --- a/pkg/util/helper/policy.go +++ b/pkg/util/helper/policy.go @@ -130,3 +130,11 @@ func GetAppliedPlacement(annotations map[string]string) (*policyv1alpha1.Placeme } return placement, nil } + +// SetReplicaDivisionPreferenceWeighted Set the default value of ReplicaDivisionPreference to Weighted +func SetReplicaDivisionPreferenceWeighted(strategy *policyv1alpha1.ReplicaSchedulingStrategy) { + if strategy == nil || strategy.ReplicaSchedulingType != policyv1alpha1.ReplicaSchedulingTypeDivided || strategy.ReplicaDivisionPreference != "" { + return + } + strategy.ReplicaDivisionPreference = policyv1alpha1.ReplicaDivisionPreferenceWeighted +} diff --git a/pkg/webhook/clusterpropagationpolicy/mutating.go b/pkg/webhook/clusterpropagationpolicy/mutating.go index 7d1060ff6..c66fcf932 100644 --- a/pkg/webhook/clusterpropagationpolicy/mutating.go +++ b/pkg/webhook/clusterpropagationpolicy/mutating.go @@ -56,6 +56,9 @@ func (a *MutatingAdmission) Handle(ctx context.Context, req admission.Request) a policy.Spec.ResourceSelectors = append(policy.Spec.ResourceSelectors, addedResourceSelectors...) } + // When ReplicaSchedulingType is Divided, set the default value of ReplicaDivisionPreference to Weighted. + helper.SetReplicaDivisionPreferenceWeighted(policy.Spec.Placement.ReplicaScheduling) + marshaledBytes, err := json.Marshal(policy) if err != nil { return admission.Errored(http.StatusInternalServerError, err) diff --git a/pkg/webhook/propagationpolicy/mutating.go b/pkg/webhook/propagationpolicy/mutating.go index decb4d4b6..55d82b081 100644 --- a/pkg/webhook/propagationpolicy/mutating.go +++ b/pkg/webhook/propagationpolicy/mutating.go @@ -68,6 +68,9 @@ func (a *MutatingAdmission) Handle(ctx context.Context, req admission.Request) a policy.Spec.ResourceSelectors = append(policy.Spec.ResourceSelectors, addedResourceSelectors...) } + // When ReplicaSchedulingType is Divided, set the default value of ReplicaDivisionPreference to Weighted. + helper.SetReplicaDivisionPreferenceWeighted(policy.Spec.Placement.ReplicaScheduling) + marshaledBytes, err := json.Marshal(policy) if err != nil { return admission.Errored(http.StatusInternalServerError, err)