From cc6d67b259c1b166b6f587f9ebb31c4ae3292c05 Mon Sep 17 00:00:00 2001 From: Thomas Gaudillat <88889592+tgaudillat02@users.noreply.github.com> Date: Tue, 6 Dec 2022 06:37:50 -0500 Subject: [PATCH] Add round for killNum (#240) * Add round for killNum To kill at least one pod if the replicaSet is above 2 replicas. It correct this issue: https://github.com/asobti/kube-monkey/issues/227 --- internal/pkg/victims/victims.go | 4 ++-- internal/pkg/victims/victims_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/pkg/victims/victims.go b/internal/pkg/victims/victims.go index 977e3b20..ef2ae406 100644 --- a/internal/pkg/victims/victims.go +++ b/internal/pkg/victims/victims.go @@ -281,7 +281,7 @@ func (v *VictimBase) KillNumberForFixedPercentage(clientset kube.Interface, kill } numberOfPodsToKill := float64(numRunningPods) * float64(killPercentage) / 100 - killNum := int(math.Floor(numberOfPodsToKill)) + killNum := int(math.Round(numberOfPodsToKill)) return killNum, nil } @@ -305,7 +305,7 @@ func (v *VictimBase) KillNumberForMaxPercentage(clientset kube.Interface, maxPer r := rand.New(rand.NewSource(time.Now().UnixNano())) killPercentage := r.Intn(maxPercentage + 1) // + 1 because Intn works with half open interval [0,n) and we want [0,n] numberOfPodsToKill := float64(numRunningPods) * float64(killPercentage) / 100 - killNum := int(math.Floor(numberOfPodsToKill)) + killNum := int(math.Round(numberOfPodsToKill)) return killNum, nil } diff --git a/internal/pkg/victims/victims_test.go b/internal/pkg/victims/victims_test.go index 4d2548d2..f54ecafa 100644 --- a/internal/pkg/victims/victims_test.go +++ b/internal/pkg/victims/victims_test.go @@ -258,7 +258,7 @@ func TestDeletePodsFixedPercentage(t *testing.T) { pods: append( generateNPods("running", 1, corev1.PodRunning), generateNPods("pending", 1, corev1.PodPending)...), - expectedNum: 0, + expectedNum: 1, expectedErr: false, }, }