Skip to content

Commit a3107c9

Browse files
committed
fix cluster quorum size calculation and tests
1 parent 67a6970 commit a3107c9

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

api/v1alpha1/etcdcluster_types.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ limitations under the License.
1717
package v1alpha1
1818

1919
import (
20-
"math"
21-
2220
corev1 "k8s.io/api/core/v1"
2321
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2422
"k8s.io/apimachinery/pkg/util/intstr"
@@ -84,12 +82,7 @@ type EtcdCluster struct {
8482

8583
// CalculateQuorumSize returns minimum quorum size for current number of replicas
8684
func (r *EtcdCluster) CalculateQuorumSize() int {
87-
replicas := *r.Spec.Replicas
88-
if replicas%2 == 0 {
89-
replicas = replicas + 1
90-
}
91-
92-
return int(math.Ceil(float64(replicas) / 2.))
85+
return int(*r.Spec.Replicas)/2 + 1
9386
}
9487

9588
// +kubebuilder:object:root=true
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package v1alpha1
2+
3+
import (
4+
. "github.com/onsi/ginkgo/v2"
5+
. "github.com/onsi/gomega"
6+
"k8s.io/utils/ptr"
7+
)
8+
9+
var _ = Context("CalculateQuorumSize", func() {
10+
It("should return correct result for odd number of replicas", func() {
11+
etcdCluster := EtcdCluster{
12+
Spec: EtcdClusterSpec{Replicas: ptr.To(int32(3))},
13+
}
14+
Expect(etcdCluster.CalculateQuorumSize()).To(Equal(2))
15+
})
16+
It("should return correct result for even number of replicas", func() {
17+
etcdCluster := EtcdCluster{
18+
Spec: EtcdClusterSpec{Replicas: ptr.To(int32(4))},
19+
}
20+
Expect(etcdCluster.CalculateQuorumSize()).To(Equal(3))
21+
})
22+
})

0 commit comments

Comments
 (0)