From c25a7f0a71578105fb1ecfbbbe10eb635faf5ccf Mon Sep 17 00:00:00 2001 From: Wei-Xiang Sun Date: Fri, 26 Aug 2022 13:42:31 +0800 Subject: [PATCH] optimize workloadspread when suitable subset maxReplicas is niil (#1066) Signed-off-by: mingzhou.swx Signed-off-by: mingzhou.swx Co-authored-by: mingzhou.swx --- pkg/util/workloadspread/workloadspread.go | 7 +++++++ pkg/util/workloadspread/workloadspread_test.go | 8 +++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/pkg/util/workloadspread/workloadspread.go b/pkg/util/workloadspread/workloadspread.go index 89dc3a5493..fa69f6fe49 100644 --- a/pkg/util/workloadspread/workloadspread.go +++ b/pkg/util/workloadspread/workloadspread.go @@ -458,6 +458,10 @@ func (h *Handler) updateSubsetForPod(ws *appsv1alpha1.WorkloadSpread, ws.Namespace, ws.Name, pod.Name) return false, nil, "" } + // no need to update WorkloadSpread status if MaxReplicas == nil + if suitableSubset.MissingReplicas == -1 { + return false, suitableSubset, "" + } if suitableSubset.CreatingPods == nil { suitableSubset.CreatingPods = map[string]metav1.Time{} } @@ -483,6 +487,9 @@ func (h *Handler) updateSubsetForPod(ws *appsv1alpha1.WorkloadSpread, klog.V(5).Infof("Pod (%s/%s) matched WorkloadSpread (%s) not found Subset(%s)", ws.Namespace, pod.Name, ws.Name, injectWS.Subset) return false, nil, "" } + if suitableSubset.MissingReplicas == -1 { + return false, suitableSubset, "" + } if suitableSubset.DeletingPods == nil { suitableSubset.DeletingPods = map[string]metav1.Time{} } diff --git a/pkg/util/workloadspread/workloadspread_test.go b/pkg/util/workloadspread/workloadspread_test.go index 49a711f3a9..a5e747041c 100644 --- a/pkg/util/workloadspread/workloadspread_test.go +++ b/pkg/util/workloadspread/workloadspread_test.go @@ -163,7 +163,8 @@ func TestWorkloadSpreadCreatePodWithoutFullName(t *testing.T) { ws := workloadSpreadDemo.DeepCopy() ws.Status.SubsetStatuses[0].MissingReplicas = 0 subset := appsv1alpha1.WorkloadSpreadSubset{ - Name: "subset-b", + Name: "subset-b", + MaxReplicas: &intstr.IntOrString{Type: intstr.Int, IntVal: 2}, RequiredNodeSelectorTerm: &corev1.NodeSelectorTerm{ MatchExpressions: []corev1.NodeSelectorRequirement{ { @@ -177,7 +178,7 @@ func TestWorkloadSpreadCreatePodWithoutFullName(t *testing.T) { ws.Spec.Subsets = append(ws.Spec.Subsets, subset) status := appsv1alpha1.WorkloadSpreadSubsetStatus{ Name: "subset-b", - MissingReplicas: -1, + MissingReplicas: 2, CreatingPods: map[string]metav1.Time{}, DeletingPods: map[string]metav1.Time{}, } @@ -365,7 +366,6 @@ func TestWorkloadSpreadMutatingPod(t *testing.T) { } demo.Status.SubsetStatuses = append(demo.Status.SubsetStatuses, status) demo.ResourceVersion = "1" - demo.Status.SubsetStatuses[1].CreatingPods[podDemo.Name] = metav1.Time{Time: defaultTime} return demo }, }, @@ -493,7 +493,6 @@ func TestWorkloadSpreadMutatingPod(t *testing.T) { workloadSpread := workloadSpreadDemo.DeepCopy() workloadSpread.ResourceVersion = "1" workloadSpread.Status.SubsetStatuses[0].MissingReplicas = -1 - workloadSpread.Status.SubsetStatuses[0].CreatingPods[podDemo.Name] = metav1.Time{Time: defaultTime} return workloadSpread }, }, @@ -738,7 +737,6 @@ func TestWorkloadSpreadMutatingPod(t *testing.T) { expectWorkloadSpread: func() *appsv1alpha1.WorkloadSpread { workloadSpread := workloadSpreadDemo.DeepCopy() workloadSpread.Status.SubsetStatuses[0].MissingReplicas = -1 - workloadSpread.Status.SubsetStatuses[0].DeletingPods[podDemo.Name] = metav1.Time{Time: defaultTime} return workloadSpread }, },