Skip to content

Commit

Permalink
fix validation for zero value in PDB
Browse files Browse the repository at this point in the history
  • Loading branch information
sircthulhu committed Apr 2, 2024
1 parent 248b31f commit 2cb3ae9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 3 additions & 2 deletions api/v1alpha1/etcdcluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
Expand Down Expand Up @@ -146,7 +147,7 @@ func (r *EtcdCluster) validatePdb() (admission.Warnings, field.ErrorList) {
minQuorumSize := r.CalculateQuorumSize()
if pdb.Spec.MinAvailable != nil {
minAvailable := pdb.Spec.MinAvailable.IntValue()
if pdb.Spec.MinAvailable.IntVal == 0 && pdb.Spec.MinAvailable.IntValue() == 0 {
if pdb.Spec.MinAvailable.Type == intstr.String && minAvailable == 0 && pdb.Spec.MinAvailable.StrVal != "0" {
var percentage int
_, err := fmt.Sscanf(pdb.Spec.MinAvailable.StrVal, "%d%%", &percentage)
if err != nil {
Expand Down Expand Up @@ -180,7 +181,7 @@ func (r *EtcdCluster) validatePdb() (admission.Warnings, field.ErrorList) {
}
if pdb.Spec.MaxUnavailable != nil {
maxUnavailable := pdb.Spec.MaxUnavailable.IntValue()
if pdb.Spec.MaxUnavailable.IntVal == 0 && pdb.Spec.MaxUnavailable.IntValue() == 0 {
if pdb.Spec.MaxUnavailable.Type == intstr.String && maxUnavailable == 0 && pdb.Spec.MaxUnavailable.StrVal != "0" {
var percentage int
_, err := fmt.Sscanf(pdb.Spec.MaxUnavailable.StrVal, "%d%%", &percentage)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions api/v1alpha1/etcdcluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,17 @@ var _ = Describe("EtcdCluster Webhook", func() {
}
}
})
It("should correctly use zero numeric value for maxUnavailable PDB", func() {
localCluster := etcdCluster.DeepCopy()
localCluster.Spec.PodDisruptionBudget.Spec.MaxUnavailable = ptr.To(intstr.FromInt32(int32(0)))
_, err := localCluster.validatePdb()
Expect(err).To(BeNil())
})
It("should correctly use zero string value for PDB", func() {
localCluster := etcdCluster.DeepCopy()
localCluster.Spec.PodDisruptionBudget.Spec.MaxUnavailable = ptr.To(intstr.FromString("0"))
_, err := localCluster.validatePdb()
Expect(err).To(BeNil())
})
})
})

0 comments on commit 2cb3ae9

Please sign in to comment.